# atomicity.h

Go to the documentation of this file.

    00001 // Support for atomic operations -*- C++ -*-
    00002
    00003 // Copyright (C) 2004, 2005, 2006, 2008, 2009 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 atomicity.h
    00026  *  This is an internal header file, included by other library headers.
    00027  *  You should not attempt to use it directly.
    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 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
    00038
    00039   // Functions for portable atomic access.
    00040   // To abstract locking primitives across all thread policies, use:
    00041   // __exchange_and_add_dispatch
    00042   // __atomic_add_dispatch
    00043 #ifdef _GLIBCXX_ATOMIC_BUILTINS_4
    00044   static inline _Atomic_word
    00045   __exchange_and_add(volatile _Atomic_word* __mem, int __val)
    00046   { return __sync_fetch_and_add(__mem, __val); }
    00047
    00048   static inline void
    00049   __atomic_add(volatile _Atomic_word* __mem, int __val)
    00050   { __sync_fetch_and_add(__mem, __val); }
    00051 #else
    00052   _Atomic_word
    00053   __attribute__ ((__unused__))
    00054   __exchange_and_add(volatile _Atomic_word*, int) throw ();
    00055
    00056   void
    00057   __attribute__ ((__unused__))
    00058   __atomic_add(volatile _Atomic_word*, int) throw ();
    00059 #endif
    00060
    00061   static inline _Atomic_word
    00062   __exchange_and_add_single(_Atomic_word* __mem, int __val)
    00063   {
    00064     _Atomic_word __result = *__mem;
    00065     *__mem += __val;
    00066     return __result;
    00067   }
    00068
    00069   static inline void
    00070   __atomic_add_single(_Atomic_word* __mem, int __val)
    00071   { *__mem += __val; }
    00072
    00073   static inline _Atomic_word
    00074   __attribute__ ((__unused__))
    00075   __exchange_and_add_dispatch(_Atomic_word* __mem, int __val)
    00076   {
    00077 #ifdef __GTHREADS
    00078     if (__gthread_active_p())
    00079       return __exchange_and_add(__mem, __val);
    00080     else
    00081       return __exchange_and_add_single(__mem, __val);
    00082 #else
    00083     return __exchange_and_add_single(__mem, __val);
    00084 #endif
    00085   }
    00086
    00087   static inline void
    00088   __attribute__ ((__unused__))
    00089   __atomic_add_dispatch(_Atomic_word* __mem, int __val)
    00090   {
    00091 #ifdef __GTHREADS
    00092     if (__gthread_active_p())
    00093       __atomic_add(__mem, __val);
    00094     else
    00095       __atomic_add_single(__mem, __val);
    00096 #else
    00097     __atomic_add_single(__mem, __val);
    00098 #endif
    00099   }
    00100
    00101 _GLIBCXX_END_NAMESPACE
    00102
    00103 // Even if the CPU doesn't need a memory barrier, we need to ensure
    00104 // that the compiler doesn't reorder memory accesses across the
    00105 // barriers.
    00106 #ifndef _GLIBCXX_READ_MEM_BARRIER
    00107 #define _GLIBCXX_READ_MEM_BARRIER __asm __volatile ("":::"memory")
    00108 #endif
    00109 #ifndef _GLIBCXX_WRITE_MEM_BARRIER
    00110 #define _GLIBCXX_WRITE_MEM_BARRIER __asm __volatile ("":::"memory")
    00111 #endif
    00112
    00113 #endif