]>
Commit | Line | Data |
---|---|---|
2e362c74 | 1 | // Support for atomic operations -*- C++ -*- |
2c5d0ae8 | 2 | |
748086b7 | 3 | // Copyright (C) 2004, 2005, 2006, 2008, 2009 Free Software Foundation, Inc. |
2c5d0ae8 BK |
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 | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
2c5d0ae8 BK |
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 | ||
748086b7 JJ |
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. | |
2c5d0ae8 | 19 | |
748086b7 JJ |
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/>. | |
2c5d0ae8 | 24 | |
f910786b BK |
25 | /** @file ext/atomicity.h |
26 | * This file is a GNU extension to the Standard C++ Library. | |
0aa06b18 BK |
27 | */ |
28 | ||
2c5d0ae8 BK |
29 | #ifndef _GLIBCXX_ATOMICITY_H |
30 | #define _GLIBCXX_ATOMICITY_H 1 | |
31 | ||
3cbc7af0 | 32 | #include <bits/c++config.h> |
b7ee72de | 33 | #include <bits/gthr.h> |
2c5d0ae8 | 34 | #include <bits/atomic_word.h> |
3cbc7af0 BK |
35 | |
36 | _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) | |
37 | ||
2e362c74 | 38 | // Functions for portable atomic access. |
ee5ca789 | 39 | // To abstract locking primitives across all thread policies, use: |
2e362c74 BK |
40 | // __exchange_and_add_dispatch |
41 | // __atomic_add_dispatch | |
35648b45 | 42 | #ifdef _GLIBCXX_ATOMIC_BUILTINS_4 |
1b98c24e BK |
43 | static inline _Atomic_word |
44 | __exchange_and_add(volatile _Atomic_word* __mem, int __val) | |
45 | { return __sync_fetch_and_add(__mem, __val); } | |
46 | ||
47 | static inline void | |
48 | __atomic_add(volatile _Atomic_word* __mem, int __val) | |
49 | { __sync_fetch_and_add(__mem, __val); } | |
50 | #else | |
b7ee72de | 51 | _Atomic_word |
2c5d0ae8 | 52 | __attribute__ ((__unused__)) |
09f2a1e4 | 53 | __exchange_and_add(volatile _Atomic_word*, int) throw (); |
2c5d0ae8 BK |
54 | |
55 | void | |
56 | __attribute__ ((__unused__)) | |
09f2a1e4 | 57 | __atomic_add(volatile _Atomic_word*, int) throw (); |
1b98c24e | 58 | #endif |
9268b7cb | 59 | |
b7ee72de | 60 | static inline _Atomic_word |
b93d5ca9 | 61 | __exchange_and_add_single(_Atomic_word* __mem, int __val) |
b7ee72de PC |
62 | { |
63 | _Atomic_word __result = *__mem; | |
64 | *__mem += __val; | |
65 | return __result; | |
66 | } | |
67 | ||
68 | static inline void | |
b93d5ca9 | 69 | __atomic_add_single(_Atomic_word* __mem, int __val) |
b7ee72de PC |
70 | { *__mem += __val; } |
71 | ||
72 | static inline _Atomic_word | |
73 | __attribute__ ((__unused__)) | |
b93d5ca9 | 74 | __exchange_and_add_dispatch(_Atomic_word* __mem, int __val) |
b7ee72de PC |
75 | { |
76 | #ifdef __GTHREADS | |
b7ee72de | 77 | if (__gthread_active_p()) |
701a3eee | 78 | return __exchange_and_add(__mem, __val); |
b7ee72de PC |
79 | else |
80 | return __exchange_and_add_single(__mem, __val); | |
b7ee72de | 81 | #else |
b7ee72de | 82 | return __exchange_and_add_single(__mem, __val); |
b7ee72de PC |
83 | #endif |
84 | } | |
85 | ||
86 | static inline void | |
87 | __attribute__ ((__unused__)) | |
b93d5ca9 | 88 | __atomic_add_dispatch(_Atomic_word* __mem, int __val) |
b7ee72de PC |
89 | { |
90 | #ifdef __GTHREADS | |
b7ee72de | 91 | if (__gthread_active_p()) |
701a3eee | 92 | __atomic_add(__mem, __val); |
b7ee72de PC |
93 | else |
94 | __atomic_add_single(__mem, __val); | |
b7ee72de | 95 | #else |
b7ee72de | 96 | __atomic_add_single(__mem, __val); |
b7ee72de PC |
97 | #endif |
98 | } | |
99 | ||
3cbc7af0 | 100 | _GLIBCXX_END_NAMESPACE |
2c5d0ae8 | 101 | |
b93d5ca9 BK |
102 | // Even if the CPU doesn't need a memory barrier, we need to ensure |
103 | // that the compiler doesn't reorder memory accesses across the | |
104 | // barriers. | |
445cf5eb JM |
105 | #ifndef _GLIBCXX_READ_MEM_BARRIER |
106 | #define _GLIBCXX_READ_MEM_BARRIER __asm __volatile ("":::"memory") | |
107 | #endif | |
108 | #ifndef _GLIBCXX_WRITE_MEM_BARRIER | |
109 | #define _GLIBCXX_WRITE_MEM_BARRIER __asm __volatile ("":::"memory") | |
110 | #endif | |
111 | ||
2c5d0ae8 | 112 | #endif |