1 // Support for atomic operations -*- C++ -*-
3 // Copyright (C) 2004, 2005, 2006, 2008, 2009 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
26 * This is an internal header file, included by other library headers.
27 * You should not attempt to use it directly.
30 #ifndef _GLIBCXX_ATOMICITY_H
31 #define _GLIBCXX_ATOMICITY_H 1
33 #include <bits/c++config.h>
34 #include <bits/gthr.h>
35 #include <bits/atomic_word.h>
37 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
39 // Functions for portable atomic access.
40 // To abstract locking primitives across all thread policies, use:
41 // __exchange_and_add_dispatch
42 // __atomic_add_dispatch
43 #ifdef _GLIBCXX_ATOMIC_BUILTINS_4
44 static inline _Atomic_word
45 __exchange_and_add(volatile _Atomic_word* __mem, int __val)
46 { return __sync_fetch_and_add(__mem, __val); }
49 __atomic_add(volatile _Atomic_word* __mem, int __val)
50 { __sync_fetch_and_add(__mem, __val); }
53 __attribute__ ((__unused__))
54 __exchange_and_add(volatile _Atomic_word*, int);
57 __attribute__ ((__unused__))
58 __atomic_add(volatile _Atomic_word*, int);
61 static inline _Atomic_word
62 __exchange_and_add_single(_Atomic_word* __mem, int __val)
64 _Atomic_word __result = *__mem;
70 __atomic_add_single(_Atomic_word* __mem, int __val)
73 static inline _Atomic_word
74 __attribute__ ((__unused__))
75 __exchange_and_add_dispatch(_Atomic_word* __mem, int __val)
78 if (__gthread_active_p())
79 return __exchange_and_add(__mem, __val);
81 return __exchange_and_add_single(__mem, __val);
83 return __exchange_and_add_single(__mem, __val);
88 __attribute__ ((__unused__))
89 __atomic_add_dispatch(_Atomic_word* __mem, int __val)
92 if (__gthread_active_p())
93 __atomic_add(__mem, __val);
95 __atomic_add_single(__mem, __val);
97 __atomic_add_single(__mem, __val);
101 _GLIBCXX_END_NAMESPACE
103 // Even if the CPU doesn't need a memory barrier, we need to ensure
104 // that the compiler doesn't reorder memory accesses across the
106 #ifndef _GLIBCXX_READ_MEM_BARRIER
107 #define _GLIBCXX_READ_MEM_BARRIER __asm __volatile ("":::"memory")
109 #ifndef _GLIBCXX_WRITE_MEM_BARRIER
110 #define _GLIBCXX_WRITE_MEM_BARRIER __asm __volatile ("":::"memory")