* config/frv/frv.h (ASM_OUTPUT_CASE_LABEL): Delete.
[official-gcc.git] / libstdc++-v3 / libsupc++ / guard.cc
blobeb6421689b3ab9076e6b651033ba08dddc9fbc4a
1 // Copyright (C) 2002, 2004, 2006 Free Software Foundation, Inc.
2 //
3 // This file is part of GCC.
4 //
5 // GCC is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2, or (at your option)
8 // any later version.
10 // GCC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING. If not, write to
17 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
18 // Boston, MA 02110-1301, USA.
20 // As a special exception, you may use this file as part of a free software
21 // library without restriction. Specifically, if other files instantiate
22 // templates or use macros or inline functions from this file, or you compile
23 // this file and link it with other files to produce an executable, this
24 // file does not by itself cause the resulting executable to be covered by
25 // the GNU General Public License. This exception does not however
26 // invalidate any other reasons why the executable file might be covered by
27 // the GNU General Public License.
29 // Written by Mark Mitchell, CodeSourcery LLC, <mark@codesourcery.com>
30 // Thread support written by Jason Merrill, Red Hat Inc. <jason@redhat.com>
32 #include <bits/c++config.h>
33 #include <cxxabi.h>
34 #include <exception>
35 #include <new>
36 #include <ext/atomicity.h>
37 #include <ext/concurrence.h>
39 // The IA64/generic ABI uses the first byte of the guard variable.
40 // The ARM EABI uses the least significant bit.
42 // Thread-safe static local initialization support.
43 #ifdef __GTHREADS
44 namespace
46 // A single mutex controlling all static initializations.
47 static __gnu_cxx::__recursive_mutex* static_mutex;
49 typedef char fake_recursive_mutex[sizeof(__gnu_cxx::__recursive_mutex)]
50 __attribute__ ((aligned(__alignof__(__gnu_cxx::__recursive_mutex))));
51 fake_recursive_mutex fake_mutex;
53 static void init()
54 { static_mutex = new (&fake_mutex) __gnu_cxx::__recursive_mutex(); }
56 __gnu_cxx::__recursive_mutex&
57 get_static_mutex()
59 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
60 __gthread_once(&once, init);
61 return *static_mutex;
65 #ifndef _GLIBCXX_GUARD_TEST_AND_ACQUIRE
66 inline bool
67 __test_and_acquire (__cxxabiv1::__guard *g)
69 bool b = _GLIBCXX_GUARD_TEST (g);
70 _GLIBCXX_READ_MEM_BARRIER;
71 return b;
73 #define _GLIBCXX_GUARD_TEST_AND_ACQUIRE(G) __test_and_acquire (G)
74 #endif
76 #ifndef _GLIBCXX_GUARD_SET_AND_RELEASE
77 inline void
78 __set_and_release (__cxxabiv1::__guard *g)
80 _GLIBCXX_WRITE_MEM_BARRIER;
81 _GLIBCXX_GUARD_SET (g);
83 #define _GLIBCXX_GUARD_SET_AND_RELEASE(G) __set_and_release (G)
84 #endif
86 #else /* !__GTHREADS */
88 #undef _GLIBCXX_GUARD_TEST_AND_ACQUIRE
89 #undef _GLIBCXX_GUARD_SET_AND_RELEASE
90 #define _GLIBCXX_GUARD_SET_AND_RELEASE(G) _GLIBCXX_GUARD_SET (G)
92 #endif /* __GTHREADS */
94 namespace __gnu_cxx
96 // 6.7[stmt.dcl]/4: If control re-enters the declaration (recursively)
97 // while the object is being initialized, the behavior is undefined.
99 // Since we already have a library function to handle locking, we might
100 // as well check for this situation and throw an exception.
101 // We use the second byte of the guard variable to remember that we're
102 // in the middle of an initialization.
103 class recursive_init_error: public std::exception
105 public:
106 recursive_init_error() throw() { }
107 virtual ~recursive_init_error() throw ();
110 recursive_init_error::~recursive_init_error() throw() { }
113 namespace __cxxabiv1
115 static inline int
116 recursion_push (__guard* g)
117 { return ((char *)g)[1]++; }
119 static inline void
120 recursion_pop (__guard* g)
121 { --((char *)g)[1]; }
123 static int
124 acquire (__guard *g)
126 if (_GLIBCXX_GUARD_TEST (g))
127 return 0;
129 if (recursion_push (g))
131 #ifdef __EXCEPTIONS
132 throw __gnu_cxx::recursive_init_error();
133 #else
134 // Use __builtin_trap so we don't require abort().
135 __builtin_trap ();
136 #endif
138 return 1;
141 extern "C"
142 int __cxa_guard_acquire (__guard *g)
144 #ifdef __GTHREADS
145 // If the target can reorder loads, we need to insert a read memory
146 // barrier so that accesses to the guarded variable happen after the
147 // guard test.
148 if (_GLIBCXX_GUARD_TEST_AND_ACQUIRE (g))
149 return 0;
151 if (__gthread_active_p ())
153 // Simple wrapper for exception safety.
154 struct mutex_wrapper
156 bool unlock;
157 mutex_wrapper() : unlock(true)
158 { get_static_mutex().lock(); }
160 ~mutex_wrapper()
162 if (unlock)
163 static_mutex->unlock();
167 mutex_wrapper mw;
168 if (acquire (g))
170 mw.unlock = false;
171 return 1;
174 return 0;
176 #endif
178 return acquire (g);
181 extern "C"
182 void __cxa_guard_abort (__guard *g)
184 recursion_pop (g);
185 #ifdef __GTHREADS
186 if (__gthread_active_p ())
187 static_mutex->unlock();
188 #endif
191 extern "C"
192 void __cxa_guard_release (__guard *g)
194 recursion_pop (g);
195 _GLIBCXX_GUARD_SET_AND_RELEASE (g);
196 #ifdef __GTHREADS
197 if (__gthread_active_p ())
198 static_mutex->unlock();
199 #endif