1 // Copyright (C) 2002 Free Software Foundation, Inc.
3 // This file is part of GCC.
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)
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>
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.
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.
49 static __gthread_recursive_mutex_t mutex
;
51 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
60 __gthread_recursive_mutex_t
static_mutex::mutex
61 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
62 = __GTHREAD_RECURSIVE_MUTEX_INIT
66 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
67 void static_mutex::init()
69 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION (&mutex
);
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
);
79 __gthread_recursive_mutex_lock (&mutex
);
82 void static_mutex::unlock ()
84 __gthread_recursive_mutex_unlock (&mutex
);
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
101 recursive_init() throw() { }
102 virtual ~recursive_init() throw ();
105 recursive_init::~recursive_init() throw() { }
111 acquire_1 (__guard
*g
)
113 if (_GLIBCXX_GUARD_ACQUIRE (g
))
115 if (((char *)g
)[1]++)
118 throw __gnu_cxx::recursive_init();
129 int __cxa_guard_acquire (__guard
*g
)
132 if (__gthread_active_p ())
134 // Simple wrapper for exception safety.
138 mutex_wrapper (): unlock(true)
140 static_mutex::lock ();
145 static_mutex::unlock ();
159 return acquire_1 (g
);
163 void __cxa_guard_abort (__guard
*g
)
167 if (__gthread_active_p ())
168 static_mutex::unlock ();
173 void __cxa_guard_release (__guard
*g
)
176 _GLIBCXX_GUARD_RELEASE (g
);
178 if (__gthread_active_p ())
179 static_mutex::unlock ();