]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/src/atomic.cc
syntax_option_type.cc: Avoid -Wall warnings.
[thirdparty/gcc.git] / libstdc++-v3 / src / atomic.cc
CommitLineData
d466a7e2
BK
1// Support for atomic operations -*- C++ -*-
2
c18dc5cc 3// Copyright (C) 2008, 2009, 2010
d466a7e2
BK
4// Free Software Foundation, Inc.
5//
6// This file is part of the GNU ISO C++ Library. This library is free
7// software; you can redistribute it and/or modify it under the
8// terms of the GNU General Public License as published by the
748086b7 9// Free Software Foundation; either version 3, or (at your option)
d466a7e2
BK
10// any later version.
11
12// This library is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16
748086b7
JJ
17// Under Section 7 of GPL version 3, you are granted additional
18// permissions described in the GCC Runtime Library Exception, version
19// 3.1, as published by the Free Software Foundation.
d466a7e2 20
748086b7
JJ
21// You should have received a copy of the GNU General Public License and
22// a copy of the GCC Runtime Library Exception along with this program;
23// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24// <http://www.gnu.org/licenses/>.
d466a7e2 25
ed0840ba 26#include "gstdint.h"
afd88205 27#include <atomic>
1a6e6753 28#include <mutex>
d466a7e2
BK
29
30#define LOGSIZE 4
31
32namespace
33{
b0c2c850 34#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
99827523
BK
35 std::mutex&
36 get_atomic_mutex()
37 {
38 static std::mutex atomic_mutex;
39 return atomic_mutex;
40 }
50ce8d3d
BK
41#endif
42
afd88205 43 std::__atomic_flag_base flag_table[ 1 << LOGSIZE ] =
d466a7e2
BK
44 {
45 ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT,
46 ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT,
47 ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT,
48 ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT,
49 };
50} // anonymous namespace
51
52namespace std
53{
50ce8d3d 54 namespace __atomic0
d466a7e2 55 {
50ce8d3d 56 bool
afd88205 57 atomic_flag::test_and_set(memory_order)
50ce8d3d 58 {
b0c2c850 59#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
99827523 60 lock_guard<mutex> __lock(get_atomic_mutex());
d466a7e2 61#endif
50ce8d3d
BK
62 bool result = _M_i;
63 _M_i = true;
64 return result;
65 }
d466a7e2 66
50ce8d3d 67 void
afd88205 68 atomic_flag::clear(memory_order)
50ce8d3d 69 {
b0c2c850 70#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
99827523 71 lock_guard<mutex> __lock(get_atomic_mutex());
d466a7e2 72#endif
50ce8d3d
BK
73 _M_i = false;
74 }
94a86be0
BK
75
76 _GLIBCXX_BEGIN_EXTERN_C
77
78 bool
79 atomic_flag_test_and_set_explicit(__atomic_flag_base* __a,
80 memory_order __m) _GLIBCXX_NOTHROW
81 {
82 atomic_flag* d = static_cast<atomic_flag*>(__a);
83 return d->test_and_set(__m);
50ce8d3d 84 }
d466a7e2 85
94a86be0
BK
86 void
87 atomic_flag_clear_explicit(__atomic_flag_base* __a,
88 memory_order __m) _GLIBCXX_NOTHROW
d466a7e2 89 {
94a86be0
BK
90 atomic_flag* d = static_cast<atomic_flag*>(__a);
91 return d->clear(__m);
92 }
d466a7e2 93
94a86be0
BK
94 void
95 __atomic_flag_wait_explicit(__atomic_flag_base* __a,
96 memory_order __x) _GLIBCXX_NOTHROW
97 {
98 while (atomic_flag_test_and_set_explicit(__a, __x))
99 { };
100 }
d466a7e2 101
94a86be0
BK
102 _GLIBCXX_CONST __atomic_flag_base*
103 __atomic_flag_for_address(const volatile void* __z) _GLIBCXX_NOTHROW
104 {
105 uintptr_t __u = reinterpret_cast<uintptr_t>(__z);
106 __u += (__u >> 2) + (__u << 4);
107 __u += (__u >> 7) + (__u << 5);
108 __u += (__u >> 17) + (__u << 13);
109 if (sizeof(uintptr_t) > 4)
110 __u += (__u >> 31);
111 __u &= ~((~uintptr_t(0)) << LOGSIZE);
112 return flag_table + __u;
113 }
d466a7e2 114
94a86be0
BK
115 _GLIBCXX_END_EXTERN_C
116
117 } // namespace __atomic0
d466a7e2 118} // namespace std
afd88205
BK
119
120
121// XXX GLIBCXX_ABI Deprecated
122// gcc-4.5.0
123// <atomic> signature changes
124
125// The rename syntax for default exported names is
126// asm (".symver name1,exportedname@GLIBCXX_3.4")
127// asm (".symver name2,exportedname@@GLIBCXX_3.4.5")
128// In the future, GLIBCXX_ABI > 6 should remove all uses of
129// _GLIBCXX_*_SYMVER macros in this file.
130
ac2e5c89 131#if defined(_GLIBCXX_SYMVER_GNU) && defined(PIC) \
c18dc5cc
RO
132 && defined(_GLIBCXX_HAVE_AS_SYMVER_DIRECTIVE) \
133 && defined(_GLIBCXX_HAVE_SYMVER_SYMBOL_RENAMING_RUNTIME_SUPPORT)
ac2e5c89 134
afd88205
BK
135#define _GLIBCXX_ASM_SYMVER(cur, old, version) \
136 asm (".symver " #cur "," #old "@@" #version);
137
afd88205
BK
138_GLIBCXX_ASM_SYMVER(_ZNSt9__atomic011atomic_flag5clearESt12memory_order, _ZNVSt9__atomic011atomic_flag5clearESt12memory_order, GLIBCXX_3.4.11)
139
140_GLIBCXX_ASM_SYMVER(_ZNSt9__atomic011atomic_flag12test_and_setESt12memory_order, _ZNVSt9__atomic011atomic_flag12test_and_setESt12memory_order, GLIBCXX_3.4.11)
ac2e5c89 141
afd88205 142#endif