Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libstdc++-v3 / libsupc++ / guard.cc
blobe9e147037704bcadddc7c17bb4b13f970e505f85
1 // Copyright (C) 2002 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, 59 Temple Place - Suite 330,
18 // Boston, MA 02111-1307, 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 <cxxabi.h>
33 #include <exception>
34 #include <bits/c++config.h>
35 #include <bits/gthr.h>
36 #include <bits/atomicity.h>
38 // The IA64/generic ABI uses the first byte of the guard variable.
39 // The ARM EABI uses the least significant bit.
41 // Thread-safe static local initialization support.
42 #ifdef __GTHREADS
43 namespace
45 // static_mutex is a single mutex controlling all static initializations.
46 // This is a static class--the need for a static initialization function
47 // to pass to __gthread_once precludes creating multiple instances, though
48 // I suppose you could achieve the same effect with a template.
49 class static_mutex
51 static __gthread_recursive_mutex_t mutex;
53 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
54 static void init();
55 #endif
57 public:
58 static void lock();
59 static void unlock();
62 __gthread_recursive_mutex_t static_mutex::mutex
63 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
64 = __GTHREAD_RECURSIVE_MUTEX_INIT
65 #endif
68 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
69 void static_mutex::init()
71 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION (&mutex);
73 #endif
75 void static_mutex::lock()
77 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
78 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
79 __gthread_once (&once, init);
80 #endif
81 __gthread_recursive_mutex_lock (&mutex);
84 void static_mutex::unlock ()
86 __gthread_recursive_mutex_unlock (&mutex);
90 #ifndef _GLIBCXX_GUARD_TEST_AND_ACQUIRE
91 inline bool
92 __test_and_acquire (__cxxabiv1::__guard *g)
94 bool b = _GLIBCXX_GUARD_TEST (g);
95 _GLIBCXX_READ_MEM_BARRIER;
96 return b;
98 #define _GLIBCXX_GUARD_TEST_AND_ACQUIRE(G) __test_and_acquire (G)
99 #endif
101 #ifndef _GLIBCXX_GUARD_SET_AND_RELEASE
102 inline void
103 __set_and_release (__cxxabiv1::__guard *g)
105 _GLIBCXX_WRITE_MEM_BARRIER;
106 _GLIBCXX_GUARD_SET (g);
108 #define _GLIBCXX_GUARD_SET_AND_RELEASE(G) __set_and_release (G)
109 #endif
111 #else /* !__GTHREADS */
113 #undef _GLIBCXX_GUARD_TEST_AND_ACQUIRE
114 #undef _GLIBCXX_GUARD_SET_AND_RELEASE
115 #define _GLIBCXX_GUARD_SET_AND_RELEASE(G) _GLIBCXX_GUARD_SET (G)
117 #endif /* __GTHREADS */
119 namespace __gnu_cxx
121 // 6.7[stmt.dcl]/4: If control re-enters the declaration (recursively)
122 // while the object is being initialized, the behavior is undefined.
124 // Since we already have a library function to handle locking, we might
125 // as well check for this situation and throw an exception.
126 // We use the second byte of the guard variable to remember that we're
127 // in the middle of an initialization.
128 class recursive_init: public std::exception
130 public:
131 recursive_init() throw() { }
132 virtual ~recursive_init() throw ();
135 recursive_init::~recursive_init() throw() { }
138 namespace __cxxabiv1
140 static inline int
141 recursion_push (__guard* g)
143 return ((char *)g)[1]++;
146 static inline void
147 recursion_pop (__guard* g)
149 --((char *)g)[1];
152 static int
153 acquire_1 (__guard *g)
155 if (_GLIBCXX_GUARD_TEST (g))
156 return 0;
158 if (recursion_push (g))
160 #ifdef __EXCEPTIONS
161 throw __gnu_cxx::recursive_init();
162 #else
163 // Use __builtin_trap so we don't require abort().
164 __builtin_trap ();
165 #endif
167 return 1;
170 extern "C"
171 int __cxa_guard_acquire (__guard *g)
173 #ifdef __GTHREADS
174 // If the target can reorder loads, we need to insert a read memory
175 // barrier so that accesses to the guarded variable happen after the
176 // guard test.
177 if (_GLIBCXX_GUARD_TEST_AND_ACQUIRE (g))
178 return 0;
180 if (__gthread_active_p ())
182 // Simple wrapper for exception safety.
183 struct mutex_wrapper
185 bool unlock;
186 mutex_wrapper (): unlock(true)
188 static_mutex::lock ();
190 ~mutex_wrapper ()
192 if (unlock)
193 static_mutex::unlock ();
195 } mw;
197 if (acquire_1 (g))
199 mw.unlock = false;
200 return 1;
203 return 0;
205 #endif
207 return acquire_1 (g);
210 extern "C"
211 void __cxa_guard_abort (__guard *g)
213 recursion_pop (g);
214 #ifdef __GTHREADS
215 if (__gthread_active_p ())
216 static_mutex::unlock ();
217 #endif
220 extern "C"
221 void __cxa_guard_release (__guard *g)
223 recursion_pop (g);
224 _GLIBCXX_GUARD_SET_AND_RELEASE (g);
225 #ifdef __GTHREADS
226 if (__gthread_active_p ())
227 static_mutex::unlock ();
228 #endif