OSDN Git Service

* include/backward/binders.h: Fix examples in doxygen comments and
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / compatibility-atomic-c++0x.cc
1 // <atomic> compatibility -*- C++ -*-
2
3 // Copyright (C) 2008, 2009, 2010, 2011
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
9 // Free Software Foundation; either version 3, or (at your option)
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
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.
20
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/>.
25
26 #include "gstdint.h"
27 #include <atomic>
28 #include <mutex>
29
30 // XXX GLIBCXX_ABI Deprecated
31 // gcc-4.7.0
32
33 #define LOGSIZE 4
34
35 namespace
36 {
37 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
38   std::mutex&
39   get_atomic_mutex()
40   {
41     static std::mutex atomic_mutex;
42     return atomic_mutex;
43   }
44 #endif
45
46   std::__atomic_flag_base flag_table[ 1 << LOGSIZE ] =
47     {
48       ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT,
49       ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT,
50       ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT,
51       ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT,
52     };
53 } // anonymous namespace
54
55 namespace std _GLIBCXX_VISIBILITY(default)
56 {
57 _GLIBCXX_BEGIN_NAMESPACE_VERSION
58
59   namespace __atomic0
60   {
61
62     struct atomic_flag : public __atomic_flag_base
63     {
64      bool
65      test_and_set(memory_order) noexcept;
66      
67      void
68      clear(memory_order) noexcept;
69     };
70
71     bool
72     atomic_flag::test_and_set(memory_order) noexcept
73     {
74 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
75       lock_guard<mutex> __lock(get_atomic_mutex());
76 #endif
77       bool result = _M_i;
78       _M_i = true;
79       return result;
80     }
81
82     void
83     atomic_flag::clear(memory_order) noexcept
84     {
85 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
86       lock_guard<mutex> __lock(get_atomic_mutex());
87 #endif
88       _M_i = false;
89     }
90   } // namespace __atomic0
91
92   _GLIBCXX_BEGIN_EXTERN_C
93
94   bool
95   atomic_flag_test_and_set_explicit(__atomic_flag_base* __a,
96                                     memory_order __m) _GLIBCXX_NOTHROW
97   {
98     atomic_flag* d = static_cast<atomic_flag*>(__a);
99     return d->test_and_set(__m);
100   }
101
102   void
103   atomic_flag_clear_explicit(__atomic_flag_base* __a,
104                              memory_order __m) _GLIBCXX_NOTHROW
105   {
106     atomic_flag* d = static_cast<atomic_flag*>(__a);
107     return d->clear(__m);
108   }
109
110   void
111   __atomic_flag_wait_explicit(__atomic_flag_base* __a,
112                               memory_order __x) _GLIBCXX_NOTHROW
113   {
114     while (atomic_flag_test_and_set_explicit(__a, __x))
115       { };
116   }
117
118   _GLIBCXX_CONST __atomic_flag_base*
119   __atomic_flag_for_address(const volatile void* __z) _GLIBCXX_NOTHROW
120   {
121     uintptr_t __u = reinterpret_cast<uintptr_t>(__z);
122     __u += (__u >> 2) + (__u << 4);
123     __u += (__u >> 7) + (__u << 5);
124     __u += (__u >> 17) + (__u << 13);
125     if (sizeof(uintptr_t) > 4)
126       __u += (__u >> 31);
127     __u &= ~((~uintptr_t(0)) << LOGSIZE);
128     return flag_table + __u;
129   }
130
131   _GLIBCXX_END_EXTERN_C
132
133 _GLIBCXX_END_NAMESPACE_VERSION
134 } // namespace std
135
136
137 // XXX GLIBCXX_ABI Deprecated
138 // gcc-4.5.0
139 // <atomic> signature changes
140
141 // The rename syntax for default exported names is
142 //   asm (".symver name1,exportedname@GLIBCXX_3.4")
143 //   asm (".symver name2,exportedname@@GLIBCXX_3.4.5")
144 // In the future, GLIBCXX_ABI > 6 should remove all uses of
145 // _GLIBCXX_*_SYMVER macros in this file.
146
147 #if defined(_GLIBCXX_SYMVER_GNU) && defined(PIC) \
148     && defined(_GLIBCXX_HAVE_AS_SYMVER_DIRECTIVE) \
149     && defined(_GLIBCXX_HAVE_SYMVER_SYMBOL_RENAMING_RUNTIME_SUPPORT)
150
151 #define _GLIBCXX_ASM_SYMVER(cur, old, version) \
152    asm (".symver " #cur "," #old "@@" #version);
153
154 _GLIBCXX_ASM_SYMVER(_ZNSt9__atomic011atomic_flag5clearESt12memory_order, _ZNVSt9__atomic011atomic_flag5clearESt12memory_order, GLIBCXX_3.4.11)
155
156 _GLIBCXX_ASM_SYMVER(_ZNSt9__atomic011atomic_flag12test_and_setESt12memory_order, _ZNVSt9__atomic011atomic_flag12test_and_setESt12memory_order, GLIBCXX_3.4.11)
157
158 #endif