]>
Commit | Line | Data |
---|---|---|
72de368c | 1 | // Support for atomic operations -*- C++ -*- |
94540c6b | 2 | |
6bc9506f | 3 | // Copyright (C) 2004, 2005, 2006, 2008, 2009 Free Software Foundation, Inc. |
94540c6b | 4 | // |
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 | |
6bc9506f | 8 | // Free Software Foundation; either version 3, or (at your option) |
94540c6b | 9 | // any later version. |
10 | ||
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. | |
15 | ||
6bc9506f | 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. | |
94540c6b | 19 | |
6bc9506f | 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/>. | |
94540c6b | 24 | |
b2a66747 | 25 | /** @file atomicity.h |
26 | * This is an internal header file, included by other library headers. | |
27 | * You should not attempt to use it directly. | |
28 | */ | |
29 | ||
94540c6b | 30 | #ifndef _GLIBCXX_ATOMICITY_H |
31 | #define _GLIBCXX_ATOMICITY_H 1 | |
32 | ||
1069247d | 33 | #include <bits/c++config.h> |
c360b175 | 34 | #include <bits/gthr.h> |
94540c6b | 35 | #include <bits/atomic_word.h> |
1069247d | 36 | |
37 | _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) | |
38 | ||
72de368c | 39 | // Functions for portable atomic access. |
fa826926 | 40 | // To abstract locking primitives across all thread policies, use: |
72de368c | 41 | // __exchange_and_add_dispatch |
42 | // __atomic_add_dispatch | |
367b2e9f | 43 | #ifdef _GLIBCXX_ATOMIC_BUILTINS_4 |
6b1464a3 | 44 | static inline _Atomic_word |
45 | __exchange_and_add(volatile _Atomic_word* __mem, int __val) | |
46 | { return __sync_fetch_and_add(__mem, __val); } | |
47 | ||
48 | static inline void | |
49 | __atomic_add(volatile _Atomic_word* __mem, int __val) | |
50 | { __sync_fetch_and_add(__mem, __val); } | |
51 | #else | |
c360b175 | 52 | _Atomic_word |
94540c6b | 53 | __attribute__ ((__unused__)) |
72de368c | 54 | __exchange_and_add(volatile _Atomic_word*, int); |
94540c6b | 55 | |
56 | void | |
57 | __attribute__ ((__unused__)) | |
72de368c | 58 | __atomic_add(volatile _Atomic_word*, int); |
6b1464a3 | 59 | #endif |
aa475990 | 60 | |
c360b175 | 61 | static inline _Atomic_word |
cce6f477 | 62 | __exchange_and_add_single(_Atomic_word* __mem, int __val) |
c360b175 | 63 | { |
64 | _Atomic_word __result = *__mem; | |
65 | *__mem += __val; | |
66 | return __result; | |
67 | } | |
68 | ||
69 | static inline void | |
cce6f477 | 70 | __atomic_add_single(_Atomic_word* __mem, int __val) |
c360b175 | 71 | { *__mem += __val; } |
72 | ||
73 | static inline _Atomic_word | |
74 | __attribute__ ((__unused__)) | |
cce6f477 | 75 | __exchange_and_add_dispatch(_Atomic_word* __mem, int __val) |
c360b175 | 76 | { |
77 | #ifdef __GTHREADS | |
c360b175 | 78 | if (__gthread_active_p()) |
3ba82d60 | 79 | return __exchange_and_add(__mem, __val); |
c360b175 | 80 | else |
81 | return __exchange_and_add_single(__mem, __val); | |
c360b175 | 82 | #else |
c360b175 | 83 | return __exchange_and_add_single(__mem, __val); |
c360b175 | 84 | #endif |
85 | } | |
86 | ||
87 | static inline void | |
88 | __attribute__ ((__unused__)) | |
cce6f477 | 89 | __atomic_add_dispatch(_Atomic_word* __mem, int __val) |
c360b175 | 90 | { |
91 | #ifdef __GTHREADS | |
c360b175 | 92 | if (__gthread_active_p()) |
3ba82d60 | 93 | __atomic_add(__mem, __val); |
c360b175 | 94 | else |
95 | __atomic_add_single(__mem, __val); | |
c360b175 | 96 | #else |
c360b175 | 97 | __atomic_add_single(__mem, __val); |
c360b175 | 98 | #endif |
99 | } | |
100 | ||
1069247d | 101 | _GLIBCXX_END_NAMESPACE |
94540c6b | 102 | |
cce6f477 | 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 | |
105 | // barriers. | |
2ca3d426 | 106 | #ifndef _GLIBCXX_READ_MEM_BARRIER |
107 | #define _GLIBCXX_READ_MEM_BARRIER __asm __volatile ("":::"memory") | |
108 | #endif | |
109 | #ifndef _GLIBCXX_WRITE_MEM_BARRIER | |
110 | #define _GLIBCXX_WRITE_MEM_BARRIER __asm __volatile ("":::"memory") | |
111 | #endif | |
112 | ||
94540c6b | 113 | #endif |