libstdc++
atomicity.h
Go to the documentation of this file.
00001 // Support for atomic operations -*- C++ -*-
00002 
00003 // Copyright (C) 2004-2013 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 #include <bits/c++config.h>
00033 #include <bits/gthr.h>
00034 #include <bits/atomic_word.h>
00035 
00036 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00037 {
00038 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00039 
00040   // Functions for portable atomic access.
00041   // To abstract locking primitives across all thread policies, use:
00042   // __exchange_and_add_dispatch
00043   // __atomic_add_dispatch
00044 #ifdef _GLIBCXX_ATOMIC_BUILTINS
00045   static inline _Atomic_word 
00046   __exchange_and_add(volatile _Atomic_word* __mem, int __val)
00047   { return __atomic_fetch_add(__mem, __val, __ATOMIC_ACQ_REL); }
00048 
00049   static inline void
00050   __atomic_add(volatile _Atomic_word* __mem, int __val)
00051   { __atomic_fetch_add(__mem, __val, __ATOMIC_ACQ_REL); }
00052 #else
00053   _Atomic_word
00054   __attribute__ ((__unused__))
00055   __exchange_and_add(volatile _Atomic_word*, int) throw ();
00056 
00057   void
00058   __attribute__ ((__unused__))
00059   __atomic_add(volatile _Atomic_word*, int) throw ();
00060 #endif
00061 
00062   static inline _Atomic_word
00063   __exchange_and_add_single(_Atomic_word* __mem, int __val)
00064   {
00065     _Atomic_word __result = *__mem;
00066     *__mem += __val;
00067     return __result;
00068   }
00069 
00070   static inline void
00071   __atomic_add_single(_Atomic_word* __mem, int __val)
00072   { *__mem += __val; }
00073 
00074   static inline _Atomic_word
00075   __attribute__ ((__unused__))
00076   __exchange_and_add_dispatch(_Atomic_word* __mem, int __val)
00077   {
00078 #ifdef __GTHREADS
00079     if (__gthread_active_p())
00080       return __exchange_and_add(__mem, __val);
00081     else
00082       return __exchange_and_add_single(__mem, __val);
00083 #else
00084     return __exchange_and_add_single(__mem, __val);
00085 #endif
00086   }
00087 
00088   static inline void
00089   __attribute__ ((__unused__))
00090   __atomic_add_dispatch(_Atomic_word* __mem, int __val)
00091   {
00092 #ifdef __GTHREADS
00093     if (__gthread_active_p())
00094       __atomic_add(__mem, __val);
00095     else
00096       __atomic_add_single(__mem, __val);
00097 #else
00098     __atomic_add_single(__mem, __val);
00099 #endif
00100   }
00101 
00102 _GLIBCXX_END_NAMESPACE_VERSION
00103 } // namespace
00104 
00105 // Even if the CPU doesn't need a memory barrier, we need to ensure
00106 // that the compiler doesn't reorder memory accesses across the
00107 // barriers.
00108 #ifndef _GLIBCXX_READ_MEM_BARRIER
00109 #define _GLIBCXX_READ_MEM_BARRIER __asm __volatile ("":::"memory")
00110 #endif
00111 #ifndef _GLIBCXX_WRITE_MEM_BARRIER
00112 #define _GLIBCXX_WRITE_MEM_BARRIER __asm __volatile ("":::"memory")
00113 #endif
00114 
00115 #endif