Fix typo.
[official-gcc.git] / libstdc++-v3 / libsupc++ / guard.cc
bloba9280bccdc54c548ef915b2aefe7e8f11346b6c7
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>
31 #include <cxxabi.h>
32 #include <exception>
33 #include <bits/c++config.h>
34 #include <bits/gthr.h>
36 // The IA64/generic ABI uses the first byte of the guard variable.
37 // The ARM EABI uses the least significant bit.
39 // Thread-safe static local initialization support.
40 #ifdef __GTHREADS
41 namespace
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
49 static __gthread_recursive_mutex_t mutex;
51 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
52 static void init();
53 #endif
55 public:
56 static void lock();
57 static void unlock();
60 __gthread_recursive_mutex_t static_mutex::mutex
61 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
62 = __GTHREAD_RECURSIVE_MUTEX_INIT
63 #endif
66 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
67 void static_mutex::init()
69 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION (&mutex);
71 #endif
73 void static_mutex::lock()
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);
82 void static_mutex::unlock ()
84 __gthread_recursive_mutex_unlock (&mutex);
87 #endif
89 namespace __gnu_cxx
91 // 6.7[stmt.dcl]/4: If control re-enters the declaration (recursively)
92 // while the object is being initialized, the behavior is undefined.
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
100 public:
101 recursive_init() throw() { }
102 virtual ~recursive_init() throw ();
105 recursive_init::~recursive_init() throw() { }
108 namespace __cxxabiv1
110 static int
111 acquire_1 (__guard *g)
113 if (_GLIBCXX_GUARD_ACQUIRE (g))
115 if (((char *)g)[1]++)
117 #ifdef __EXCEPTIONS
118 throw __gnu_cxx::recursive_init();
119 #else
120 abort ();
121 #endif
123 return 1;
125 return 0;
128 extern "C"
129 int __cxa_guard_acquire (__guard *g)
131 #ifdef __GTHREADS
132 if (__gthread_active_p ())
134 // Simple wrapper for exception safety.
135 struct mutex_wrapper
137 bool unlock;
138 mutex_wrapper (): unlock(true)
140 static_mutex::lock ();
142 ~mutex_wrapper ()
144 if (unlock)
145 static_mutex::unlock ();
147 } mw;
149 if (acquire_1 (g))
151 mw.unlock = false;
152 return 1;
155 return 0;
157 #endif
159 return acquire_1 (g);
162 extern "C"
163 void __cxa_guard_abort (__guard *g)
165 ((char *)g)[1]--;
166 #ifdef __GTHREADS
167 if (__gthread_active_p ())
168 static_mutex::unlock ();
169 #endif
172 extern "C"
173 void __cxa_guard_release (__guard *g)
175 ((char *)g)[1]--;
176 _GLIBCXX_GUARD_RELEASE (g);
177 #ifdef __GTHREADS
178 if (__gthread_active_p ())
179 static_mutex::unlock ();
180 #endif