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