Import gcc-4.4.1
[dragonfly.git] / contrib / gcc-4.4 / libstdc++-v3 / src / atomic.cc
1 // Support for atomic operations -*- C++ -*-
2
3 // Copyright (C) 2008, 2009
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 <cstdatomic>
28 #include <mutex>
29
30 #define LOGSIZE 4
31
32 namespace
33 {
34 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
35   std::mutex&
36   get_atomic_mutex()
37   {
38     static std::mutex atomic_mutex;
39     return atomic_mutex;
40   }
41 #endif
42
43   std::__atomic_flag_base volatile flag_table[ 1 << LOGSIZE ] =
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
52 namespace std
53 {
54   namespace __atomic0
55   {
56     bool
57     atomic_flag::test_and_set(memory_order) volatile
58     {
59 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
60       lock_guard<mutex> __lock(get_atomic_mutex());
61 #endif
62       bool result = _M_i;
63       _M_i = true;
64       return result;
65     }
66
67     void
68     atomic_flag::clear(memory_order) volatile
69     {
70 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
71       lock_guard<mutex> __lock(get_atomic_mutex());
72 #endif
73       _M_i = false;
74     }
75   }
76
77   extern "C"
78   {
79     bool
80     atomic_flag_test_and_set_explicit(volatile __atomic_flag_base* __a,
81                                       memory_order __m)
82     {
83       volatile atomic_flag d(__a->_M_i);
84       return d.test_and_set(__m);
85     }
86
87     void
88     atomic_flag_clear_explicit(volatile __atomic_flag_base* __a,
89                                memory_order __m)
90     {
91       volatile atomic_flag d(__a->_M_i);
92       return d.clear(__m);
93     }
94
95     void
96     __atomic_flag_wait_explicit(volatile __atomic_flag_base* __a,
97                                 memory_order __x)
98     {
99       while (atomic_flag_test_and_set_explicit(__a, __x))
100         { };
101     }
102
103     volatile __atomic_flag_base*
104     __atomic_flag_for_address(const volatile void* __z)
105     {
106       uintptr_t __u = reinterpret_cast<uintptr_t>(__z);
107       __u += (__u >> 2) + (__u << 4);
108       __u += (__u >> 7) + (__u << 5);
109       __u += (__u >> 17) + (__u << 13);
110       if (sizeof(uintptr_t) > 4)
111         __u += (__u >> 31);
112       __u &= ~((~uintptr_t(0)) << LOGSIZE);
113       return flag_table + __u;
114     }
115   } // extern "C"
116 } // namespace std