libstdc++

atomic_base.h

Go to the documentation of this file.
00001 // -*- C++ -*- header.
00002 
00003 // Copyright (C) 2008, 2009, 2010, 2011 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 bits/atomic_base.h 
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{atomic}
00028  */
00029 
00030 #ifndef _GLIBCXX_ATOMIC_BASE_H
00031 #define _GLIBCXX_ATOMIC_BASE_H 1
00032 
00033 #pragma GCC system_header
00034 
00035 #include <bits/c++config.h>
00036 #include <stdbool.h>
00037 #include <stdint.h>
00038 
00039 namespace std _GLIBCXX_VISIBILITY(default)
00040 {
00041 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00042 
00043   /**
00044    * @defgroup atomics Atomics
00045    *
00046    * Components for performing atomic operations.
00047    * @{
00048    */
00049 
00050   /// Enumeration for memory_order
00051   typedef enum memory_order
00052     {
00053       memory_order_relaxed,
00054       memory_order_consume,
00055       memory_order_acquire,
00056       memory_order_release,
00057       memory_order_acq_rel,
00058       memory_order_seq_cst
00059     } memory_order;
00060 
00061   inline memory_order
00062   __calculate_memory_order(memory_order __m)
00063   {
00064     const bool __cond1 = __m == memory_order_release;
00065     const bool __cond2 = __m == memory_order_acq_rel;
00066     memory_order __mo1(__cond1 ? memory_order_relaxed : __m);
00067     memory_order __mo2(__cond2 ? memory_order_acquire : __mo1);
00068     return __mo2;
00069   }
00070 
00071   /// kill_dependency
00072   template<typename _Tp>
00073     inline _Tp
00074     kill_dependency(_Tp __y)
00075     {
00076       _Tp __ret(__y);
00077       return __ret;
00078     }
00079 
00080   /**
00081    *  @brief Base type for atomic_flag. 
00082    *
00083    *  Base type is POD with data, allowing atomic_flag to derive from
00084    *  it and meet the standard layout type requirement. In addition to
00085    *  compatibilty with a C interface, this allows different
00086    *  implementations of atomic_flag to use the same atomic operation
00087    *  functions, via a standard conversion to the __atomic_flag_base
00088    *  argument.
00089   */
00090   _GLIBCXX_BEGIN_EXTERN_C
00091 
00092   struct __atomic_flag_base
00093   {
00094     bool _M_i;
00095   };
00096 
00097   _GLIBCXX_END_EXTERN_C
00098 
00099 #define ATOMIC_FLAG_INIT { false }
00100 
00101 
00102   // Base types for atomics.
00103   //
00104   // Three nested namespaces for atomic implementation details.
00105   //
00106   // The nested namespace inlined into std:: is determined by the value
00107   // of the _GLIBCXX_ATOMIC_PROPERTY macro and the resulting
00108   // ATOMIC_*_LOCK_FREE macros.
00109   //
00110   // 0 == __atomic0 == Never lock-free
00111   // 1 == __atomic1 == Best available, sometimes lock-free
00112   // 2 == __atomic2 == Always lock-free
00113 
00114   namespace __atomic0
00115   {
00116     struct atomic_flag;
00117     struct atomic_address;
00118 
00119     template<typename _IntTp>
00120       struct __atomic_base;
00121   } 
00122 
00123   namespace __atomic2
00124   {
00125     struct atomic_flag;
00126     struct atomic_address;
00127 
00128     template<typename _IntTp>
00129       struct __atomic_base;
00130   } 
00131 
00132   namespace __atomic1
00133   {
00134     using __atomic2::atomic_flag;
00135     using __atomic0::atomic_address;
00136     using __atomic0::__atomic_base;
00137   } 
00138 
00139   /// Lock-free Property
00140 #if defined(_GLIBCXX_ATOMIC_BUILTINS_1) && defined(_GLIBCXX_ATOMIC_BUILTINS_2) \
00141   && defined(_GLIBCXX_ATOMIC_BUILTINS_4) && defined(_GLIBCXX_ATOMIC_BUILTINS_8)
00142 # define _GLIBCXX_ATOMIC_PROPERTY 2
00143 # define _GLIBCXX_ATOMIC_NAMESPACE __atomic2
00144 #elif defined(_GLIBCXX_ATOMIC_BUILTINS_1)
00145 # define _GLIBCXX_ATOMIC_PROPERTY 1
00146 # define _GLIBCXX_ATOMIC_NAMESPACE __atomic1
00147 #else
00148 # define _GLIBCXX_ATOMIC_PROPERTY 0
00149 # define _GLIBCXX_ATOMIC_NAMESPACE __atomic0
00150 #endif
00151 
00152 #define ATOMIC_CHAR_LOCK_FREE _GLIBCXX_ATOMIC_PROPERTY
00153 #define ATOMIC_CHAR16_T_LOCK_FREE _GLIBCXX_ATOMIC_PROPERTY
00154 #define ATOMIC_CHAR32_T_LOCK_FREE _GLIBCXX_ATOMIC_PROPERTY
00155 #define ATOMIC_WCHAR_T_LOCK_FREE _GLIBCXX_ATOMIC_PROPERTY
00156 #define ATOMIC_SHORT_LOCK_FREE _GLIBCXX_ATOMIC_PROPERTY
00157 #define ATOMIC_INT_LOCK_FREE _GLIBCXX_ATOMIC_PROPERTY
00158 #define ATOMIC_LONG_LOCK_FREE _GLIBCXX_ATOMIC_PROPERTY
00159 #define ATOMIC_LLONG_LOCK_FREE _GLIBCXX_ATOMIC_PROPERTY
00160 #define ATOMIC_ADDRESS_LOCK_FREE _GLIBCXX_ATOMIC_PROPERTY
00161 
00162   inline namespace _GLIBCXX_ATOMIC_NAMESPACE { }
00163 
00164 
00165   /// atomic_char
00166   typedef __atomic_base<char>               atomic_char;
00167 
00168   /// atomic_schar
00169   typedef __atomic_base<signed char>            atomic_schar;
00170 
00171   /// atomic_uchar
00172   typedef __atomic_base<unsigned char>      atomic_uchar;
00173 
00174   /// atomic_short
00175   typedef __atomic_base<short>          atomic_short;
00176 
00177   /// atomic_ushort
00178   typedef __atomic_base<unsigned short>     atomic_ushort;
00179 
00180   /// atomic_int
00181   typedef __atomic_base<int>                atomic_int;
00182 
00183   /// atomic_uint
00184   typedef __atomic_base<unsigned int>           atomic_uint;
00185 
00186   /// atomic_long
00187   typedef __atomic_base<long>               atomic_long;
00188 
00189   /// atomic_ulong
00190   typedef __atomic_base<unsigned long>      atomic_ulong;
00191 
00192   /// atomic_llong
00193   typedef __atomic_base<long long>          atomic_llong;
00194 
00195   /// atomic_ullong
00196   typedef __atomic_base<unsigned long long>     atomic_ullong;
00197 
00198   /// atomic_wchar_t
00199   typedef __atomic_base<wchar_t>        atomic_wchar_t;
00200 
00201   /// atomic_char16_t
00202   typedef __atomic_base<char16_t>       atomic_char16_t;
00203 
00204   /// atomic_char32_t
00205   typedef __atomic_base<char32_t>       atomic_char32_t;
00206 
00207   /// atomic_char32_t
00208   typedef __atomic_base<char32_t>       atomic_char32_t;
00209 
00210 
00211   /// atomic_int_least8_t
00212   typedef __atomic_base<int_least8_t>       atomic_int_least8_t;
00213 
00214   /// atomic_uint_least8_t
00215   typedef __atomic_base<uint_least8_t>          atomic_uint_least8_t;
00216 
00217   /// atomic_int_least16_t
00218   typedef __atomic_base<int_least16_t>          atomic_int_least16_t;
00219 
00220   /// atomic_uint_least16_t
00221   typedef __atomic_base<uint_least16_t>         atomic_uint_least16_t;
00222 
00223   /// atomic_int_least32_t
00224   typedef __atomic_base<int_least32_t>          atomic_int_least32_t;
00225 
00226   /// atomic_uint_least32_t
00227   typedef __atomic_base<uint_least32_t>         atomic_uint_least32_t;
00228 
00229   /// atomic_int_least64_t
00230   typedef __atomic_base<int_least64_t>          atomic_int_least64_t;
00231 
00232   /// atomic_uint_least64_t
00233   typedef __atomic_base<uint_least64_t>         atomic_uint_least64_t;
00234 
00235 
00236   /// atomic_int_fast8_t
00237   typedef __atomic_base<int_fast8_t>        atomic_int_fast8_t;
00238 
00239   /// atomic_uint_fast8_t
00240   typedef __atomic_base<uint_fast8_t>           atomic_uint_fast8_t;
00241 
00242   /// atomic_int_fast16_t
00243   typedef __atomic_base<int_fast16_t>           atomic_int_fast16_t;
00244 
00245   /// atomic_uint_fast16_t
00246   typedef __atomic_base<uint_fast16_t>          atomic_uint_fast16_t;
00247 
00248   /// atomic_int_fast32_t
00249   typedef __atomic_base<int_fast32_t>           atomic_int_fast32_t;
00250 
00251   /// atomic_uint_fast32_t
00252   typedef __atomic_base<uint_fast32_t>          atomic_uint_fast32_t;
00253 
00254   /// atomic_int_fast64_t
00255   typedef __atomic_base<int_fast64_t>           atomic_int_fast64_t;
00256 
00257   /// atomic_uint_fast64_t
00258   typedef __atomic_base<uint_fast64_t>          atomic_uint_fast64_t;
00259 
00260 
00261   /// atomic_intptr_t
00262   typedef __atomic_base<intptr_t>           atomic_intptr_t;
00263 
00264   /// atomic_uintptr_t
00265   typedef __atomic_base<uintptr_t>              atomic_uintptr_t;
00266 
00267   /// atomic_size_t
00268   typedef __atomic_base<size_t>             atomic_size_t;
00269 
00270   /// atomic_intmax_t
00271   typedef __atomic_base<intmax_t>           atomic_intmax_t;
00272 
00273   /// atomic_uintmax_t
00274   typedef __atomic_base<uintmax_t>              atomic_uintmax_t;
00275 
00276   /// atomic_ptrdiff_t
00277   typedef __atomic_base<ptrdiff_t>              atomic_ptrdiff_t;
00278 
00279 
00280   struct atomic_bool;
00281 
00282 #define ATOMIC_VAR_INIT(_VI) { _VI }
00283 
00284   template<typename _Tp>
00285     struct atomic;
00286 
00287   // @} group atomics
00288 
00289 _GLIBCXX_END_NAMESPACE_VERSION
00290 } // namespace
00291 
00292 #endif