]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/libsupc++/guard.cc
re PR c++/13684 (local static object variable constructed once but ctors and dtors...
[thirdparty/gcc.git] / libstdc++-v3 / 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, 59 Temple Place - Suite 330,
18 // Boston, MA 02111-1307, 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
31 #include <cxxabi.h>
32 #include <exception>
33 #include <bits/c++config.h>
34 #include <bits/gthr.h>
35
36 // The IA64/generic ABI uses the first byte of the guard variable.
37 // The ARM EABI uses the least significant bit.
38
39 // Thread-safe static local initialization support.
40 #ifdef __GTHREADS
41 namespace
42 {
43 // static_mutex is a single mutex controlling all static initializations.
44 // This is a static class--the need for a static initialization function
45 // to pass to __gthread_once precludes creating multiple instances, though
46 // I suppose you could achieve the same effect with a template.
47 class static_mutex
48 {
49 static __gthread_recursive_mutex_t mutex;
50
51 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
52 static void init();
53 #endif
54
55 public:
56 static void lock();
57 static void unlock();
58 };
59
60 __gthread_recursive_mutex_t static_mutex::mutex
61 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
62 = __GTHREAD_RECURSIVE_MUTEX_INIT
63 #endif
64 ;
65
66 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
67 void static_mutex::init()
68 {
69 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION (&mutex);
70 }
71 #endif
72
73 void static_mutex::lock()
74 {
75 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
76 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
77 __gthread_once (&once, init);
78 #endif
79 __gthread_recursive_mutex_lock (&mutex);
80 }
81
82 void static_mutex::unlock ()
83 {
84 __gthread_recursive_mutex_unlock (&mutex);
85 }
86 }
87 #endif
88
89 namespace __gnu_cxx
90 {
91 // 6.7[stmt.dcl]/4: If control re-enters the declaration (recursively)
92 // while the object is being initialized, the behavior is undefined.
93
94 // Since we already have a library function to handle locking, we might
95 // as well check for this situation and throw an exception.
96 // We use the second byte of the guard variable to remember that we're
97 // in the middle of an initialization.
98 class recursive_init: public std::exception
99 {
100 public:
101 recursive_init() throw() { }
102 virtual ~recursive_init() throw ();
103 };
104
105 recursive_init::~recursive_init() throw() { }
106 }
107
108 namespace __cxxabiv1
109 {
110 static int
111 acquire_1 (__guard *g)
112 {
113 if (_GLIBCXX_GUARD_ACQUIRE (g))
114 {
115 if (((char *)g)[1]++)
116 {
117 #ifdef __EXCEPTIONS
118 throw __gnu_cxx::recursive_init();
119 #else
120 abort ();
121 #endif
122 }
123 return 1;
124 }
125 return 0;
126 }
127
128 extern "C"
129 int __cxa_guard_acquire (__guard *g)
130 {
131 #ifdef __GTHREADS
132 if (__gthread_active_p ())
133 {
134 // Simple wrapper for exception safety.
135 struct mutex_wrapper
136 {
137 bool unlock;
138 mutex_wrapper (): unlock(true)
139 {
140 static_mutex::lock ();
141 }
142 ~mutex_wrapper ()
143 {
144 if (unlock)
145 static_mutex::unlock ();
146 }
147 } mw;
148
149 if (acquire_1 (g))
150 {
151 mw.unlock = false;
152 return 1;
153 }
154
155 return 0;
156 }
157 #endif
158
159 return acquire_1 (g);
160 }
161
162 extern "C"
163 void __cxa_guard_abort (__guard *g)
164 {
165 ((char *)g)[1]--;
166 #ifdef __GTHREADS
167 if (__gthread_active_p ())
168 static_mutex::unlock ();
169 #endif
170 }
171
172 extern "C"
173 void __cxa_guard_release (__guard *g)
174 {
175 ((char *)g)[1]--;
176 _GLIBCXX_GUARD_RELEASE (g);
177 #ifdef __GTHREADS
178 if (__gthread_active_p ())
179 static_mutex::unlock ();
180 #endif
181 }
182 }