atomicity.h

Go to the documentation of this file.
00001 // Support for atomic operations -*- C++ -*-
00002 
00003 // Copyright (C) 2004, 2005, 2006 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 2, 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 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 /** @file atomicity.h
00031  *  This is an internal header file, included by other library headers.
00032  *  You should not attempt to use it directly.
00033  */
00034 
00035 #ifndef _GLIBCXX_ATOMICITY_H
00036 #define _GLIBCXX_ATOMICITY_H    1
00037 
00038 #include <bits/c++config.h>
00039 #include <bits/gthr.h>
00040 #include <bits/atomic_word.h>
00041 
00042 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
00043 
00044   // Functions for portable atomic access.
00045   // To abstract locking primitives across all thread policies, use:
00046   // __exchange_and_add_dispatch
00047   // __atomic_add_dispatch
00048 #ifdef _GLIBCXX_ATOMIC_BUILTINS
00049   static inline _Atomic_word 
00050   __exchange_and_add(volatile _Atomic_word* __mem, int __val)
00051   { return __sync_fetch_and_add(__mem, __val); }
00052 
00053   static inline void
00054   __atomic_add(volatile _Atomic_word* __mem, int __val)
00055   { __sync_fetch_and_add(__mem, __val); }
00056 #else
00057   _Atomic_word
00058   __attribute__ ((__unused__))
00059   __exchange_and_add(volatile _Atomic_word*, int);
00060 
00061   void
00062   __attribute__ ((__unused__))
00063   __atomic_add(volatile _Atomic_word*, int);
00064 #endif
00065 
00066   static inline _Atomic_word
00067   __exchange_and_add_single(_Atomic_word* __mem, int __val)
00068   {
00069     _Atomic_word __result = *__mem;
00070     *__mem += __val;
00071     return __result;
00072   }
00073 
00074   static inline void
00075   __atomic_add_single(_Atomic_word* __mem, int __val)
00076   { *__mem += __val; }
00077 
00078   static inline _Atomic_word
00079   __attribute__ ((__unused__))
00080   __exchange_and_add_dispatch(_Atomic_word* __mem, int __val)
00081   {
00082 #ifdef __GTHREADS
00083     if (__gthread_active_p())
00084       return __exchange_and_add(__mem, __val);
00085     else
00086       return __exchange_and_add_single(__mem, __val);
00087 #else
00088     return __exchange_and_add_single(__mem, __val);
00089 #endif
00090   }
00091 
00092   static inline void
00093   __attribute__ ((__unused__))
00094   __atomic_add_dispatch(_Atomic_word* __mem, int __val)
00095   {
00096 #ifdef __GTHREADS
00097     if (__gthread_active_p())
00098       __atomic_add(__mem, __val);
00099     else
00100       __atomic_add_single(__mem, __val);
00101 #else
00102     __atomic_add_single(__mem, __val);
00103 #endif
00104   }
00105 
00106 _GLIBCXX_END_NAMESPACE
00107 
00108 // Even if the CPU doesn't need a memory barrier, we need to ensure
00109 // that the compiler doesn't reorder memory accesses across the
00110 // barriers.
00111 #ifndef _GLIBCXX_READ_MEM_BARRIER
00112 #define _GLIBCXX_READ_MEM_BARRIER __asm __volatile ("":::"memory")
00113 #endif
00114 #ifndef _GLIBCXX_WRITE_MEM_BARRIER
00115 #define _GLIBCXX_WRITE_MEM_BARRIER __asm __volatile ("":::"memory")
00116 #endif
00117 
00118 #endif 

Generated on Wed Mar 26 00:42:54 2008 for libstdc++ by  doxygen 1.5.1