libstdc++
|
00001 // Support for atomic operations -*- C++ -*- 00002 00003 // Copyright (C) 2004-2015 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file ext/atomicity.h 00026 * This file is a GNU extension to the Standard C++ Library. 00027 */ 00028 00029 #ifndef _GLIBCXX_ATOMICITY_H 00030 #define _GLIBCXX_ATOMICITY_H 1 00031 00032 #pragma GCC system_header 00033 00034 #include <bits/c++config.h> 00035 #include <bits/gthr.h> 00036 #include <bits/atomic_word.h> 00037 00038 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 00039 { 00040 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00041 00042 // Functions for portable atomic access. 00043 // To abstract locking primitives across all thread policies, use: 00044 // __exchange_and_add_dispatch 00045 // __atomic_add_dispatch 00046 #ifdef _GLIBCXX_ATOMIC_BUILTINS 00047 static inline _Atomic_word 00048 __exchange_and_add(volatile _Atomic_word* __mem, int __val) 00049 { return __atomic_fetch_add(__mem, __val, __ATOMIC_ACQ_REL); } 00050 00051 static inline void 00052 __atomic_add(volatile _Atomic_word* __mem, int __val) 00053 { __atomic_fetch_add(__mem, __val, __ATOMIC_ACQ_REL); } 00054 #else 00055 _Atomic_word 00056 __attribute__ ((__unused__)) 00057 __exchange_and_add(volatile _Atomic_word*, int) throw (); 00058 00059 void 00060 __attribute__ ((__unused__)) 00061 __atomic_add(volatile _Atomic_word*, int) throw (); 00062 #endif 00063 00064 static inline _Atomic_word 00065 __exchange_and_add_single(_Atomic_word* __mem, int __val) 00066 { 00067 _Atomic_word __result = *__mem; 00068 *__mem += __val; 00069 return __result; 00070 } 00071 00072 static inline void 00073 __atomic_add_single(_Atomic_word* __mem, int __val) 00074 { *__mem += __val; } 00075 00076 static inline _Atomic_word 00077 __attribute__ ((__unused__)) 00078 __exchange_and_add_dispatch(_Atomic_word* __mem, int __val) 00079 { 00080 #ifdef __GTHREADS 00081 if (__gthread_active_p()) 00082 return __exchange_and_add(__mem, __val); 00083 else 00084 return __exchange_and_add_single(__mem, __val); 00085 #else 00086 return __exchange_and_add_single(__mem, __val); 00087 #endif 00088 } 00089 00090 static inline void 00091 __attribute__ ((__unused__)) 00092 __atomic_add_dispatch(_Atomic_word* __mem, int __val) 00093 { 00094 #ifdef __GTHREADS 00095 if (__gthread_active_p()) 00096 __atomic_add(__mem, __val); 00097 else 00098 __atomic_add_single(__mem, __val); 00099 #else 00100 __atomic_add_single(__mem, __val); 00101 #endif 00102 } 00103 00104 _GLIBCXX_END_NAMESPACE_VERSION 00105 } // namespace 00106 00107 // Even if the CPU doesn't need a memory barrier, we need to ensure 00108 // that the compiler doesn't reorder memory accesses across the 00109 // barriers. 00110 #ifndef _GLIBCXX_READ_MEM_BARRIER 00111 #define _GLIBCXX_READ_MEM_BARRIER __asm __volatile ("":::"memory") 00112 #endif 00113 #ifndef _GLIBCXX_WRITE_MEM_BARRIER 00114 #define _GLIBCXX_WRITE_MEM_BARRIER __asm __volatile ("":::"memory") 00115 #endif 00116 00117 #endif