]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.1.0/libsupc++/guard.cc
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.1.0 / libsupc++ / guard.cc
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.
9
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.
14
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. 
19
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.
28
29 // Written by Mark Mitchell, CodeSourcery LLC, <mark@codesourcery.com>
30 // Thread support written by Jason Merrill, Red Hat Inc. <jason@redhat.com>
31
32 #include <cxxabi.h>
33 #include <exception>
34 #include <bits/c++config.h>
35 #include <bits/gthr.h>
36 #include <bits/atomicity.h>
37
38 // The IA64/generic ABI uses the first byte of the guard variable.
39 // The ARM EABI uses the least significant bit.
40
41 // Thread-safe static local initialization support.
42 #ifdef __GTHREADS
43 namespace
44 {
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
50   {
51     static __gthread_recursive_mutex_t mutex;
52
53 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
54     static void init();
55 #endif
56
57   public:
58     static void lock();
59     static void unlock();
60   };
61
62   __gthread_recursive_mutex_t static_mutex::mutex
63 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
64   = __GTHREAD_RECURSIVE_MUTEX_INIT
65 #endif
66   ;
67
68 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
69   void static_mutex::init()
70   {
71     __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION (&mutex);
72   }
73 #endif
74
75   void static_mutex::lock()
76   {
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);
82   }
83
84   void static_mutex::unlock ()
85   {
86     __gthread_recursive_mutex_unlock (&mutex);
87   }
88 }
89
90 #ifndef _GLIBCXX_GUARD_TEST_AND_ACQUIRE
91 inline bool
92 __test_and_acquire (__cxxabiv1::__guard *g)
93 {
94   bool b = _GLIBCXX_GUARD_TEST (g);
95   _GLIBCXX_READ_MEM_BARRIER;
96   return b;
97 }
98 #define _GLIBCXX_GUARD_TEST_AND_ACQUIRE(G) __test_and_acquire (G)
99 #endif
100
101 #ifndef _GLIBCXX_GUARD_SET_AND_RELEASE
102 inline void
103 __set_and_release (__cxxabiv1::__guard *g)
104 {
105   _GLIBCXX_WRITE_MEM_BARRIER;
106   _GLIBCXX_GUARD_SET (g);
107 }
108 #define _GLIBCXX_GUARD_SET_AND_RELEASE(G) __set_and_release (G)
109 #endif
110
111 #else /* !__GTHREADS */
112
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)
116
117 #endif /* __GTHREADS */
118
119 namespace __gnu_cxx
120 {
121   // 6.7[stmt.dcl]/4: If control re-enters the declaration (recursively)
122   // while the object is being initialized, the behavior is undefined.
123
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
129   {
130   public:
131     recursive_init() throw() { }
132     virtual ~recursive_init() throw ();
133   };
134
135   recursive_init::~recursive_init() throw() { }
136 }
137
138 namespace __cxxabiv1 
139 {
140   static inline int
141   recursion_push (__guard* g)
142   {
143     return ((char *)g)[1]++;
144   }
145
146   static inline void
147   recursion_pop (__guard* g)
148   {
149     --((char *)g)[1];
150   }
151
152   static int
153   acquire_1 (__guard *g)
154   {
155     if (_GLIBCXX_GUARD_TEST (g))
156       return 0;
157
158     if (recursion_push (g))
159       {
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
166       }
167     return 1;
168   }
169
170   extern "C"
171   int __cxa_guard_acquire (__guard *g) 
172   {
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;
179
180     if (__gthread_active_p ())
181       {
182         // Simple wrapper for exception safety.
183         struct mutex_wrapper
184         {
185           bool unlock;
186           mutex_wrapper (): unlock(true)
187           {
188             static_mutex::lock ();
189           }
190           ~mutex_wrapper ()
191           {
192             if (unlock)
193               static_mutex::unlock ();
194           }
195         } mw;
196
197         if (acquire_1 (g))
198           {
199             mw.unlock = false;
200             return 1;
201           }
202
203         return 0;
204       }
205 #endif
206
207     return acquire_1 (g);
208   }
209
210   extern "C"
211   void __cxa_guard_abort (__guard *g)
212   {
213     recursion_pop (g);
214 #ifdef __GTHREADS
215     if (__gthread_active_p ())
216       static_mutex::unlock ();
217 #endif
218   }
219
220   extern "C"
221   void __cxa_guard_release (__guard *g)
222   {
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
229   }
230 }