diff options
Diffstat (limited to 'libstdc++-v3')
-rw-r--r-- | libstdc++-v3/ChangeLog | 9 | ||||
-rw-r--r-- | libstdc++-v3/libsupc++/guard.cc | 136 |
2 files changed, 141 insertions, 4 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index d96a583..c77647b 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,12 @@ +2004-08-27 Jason Merrill <jason@redhat.com> + + PR c++/13684 + * libsupc++/guard.cc (static_mutex): Internal class implementing a + recursive mutex which controls initialization of local statics. + (__gnu_cxx::recursive_init): New exception class. + (__cxa_guard_acquire): Deal with locking and recursion detection. + (acquire_1, __cxa_guard_abort, __cxa_guard_release): Likewise. + 2004-08-27 Matthias Klose <doko@debian.org> * configure.host: For mips*-*-linux* update cpu_include_dir diff --git a/libstdc++-v3/libsupc++/guard.cc b/libstdc++-v3/libsupc++/guard.cc index 255108f..a9280bc 100644 --- a/libstdc++-v3/libsupc++/guard.cc +++ b/libstdc++-v3/libsupc++/guard.cc @@ -29,26 +29,154 @@ // Written by Mark Mitchell, CodeSourcery LLC, <mark@codesourcery.com> #include <cxxabi.h> +#include <exception> +#include <bits/c++config.h> +#include <bits/gthr.h> // The IA64/generic ABI uses the first byte of the guard variable. // The ARM EABI uses the least significant bit. +// Thread-safe static local initialization support. +#ifdef __GTHREADS +namespace +{ + // static_mutex is a single mutex controlling all static initializations. + // This is a static class--the need for a static initialization function + // to pass to __gthread_once precludes creating multiple instances, though + // I suppose you could achieve the same effect with a template. + class static_mutex + { + static __gthread_recursive_mutex_t mutex; + +#ifdef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION + static void init(); +#endif + + public: + static void lock(); + static void unlock(); + }; + + __gthread_recursive_mutex_t static_mutex::mutex +#ifdef __GTHREAD_RECURSIVE_MUTEX_INIT + = __GTHREAD_RECURSIVE_MUTEX_INIT +#endif + ; + +#ifdef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION + void static_mutex::init() + { + __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION (&mutex); + } +#endif + + void static_mutex::lock() + { +#ifdef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION + static __gthread_once_t once = __GTHREAD_ONCE_INIT; + __gthread_once (&once, init); +#endif + __gthread_recursive_mutex_lock (&mutex); + } + + void static_mutex::unlock () + { + __gthread_recursive_mutex_unlock (&mutex); + } +} +#endif + +namespace __gnu_cxx +{ + // 6.7[stmt.dcl]/4: If control re-enters the declaration (recursively) + // while the object is being initialized, the behavior is undefined. + + // Since we already have a library function to handle locking, we might + // as well check for this situation and throw an exception. + // We use the second byte of the guard variable to remember that we're + // in the middle of an initialization. + class recursive_init: public std::exception + { + public: + recursive_init() throw() { } + virtual ~recursive_init() throw (); + }; + + recursive_init::~recursive_init() throw() { } +} + namespace __cxxabiv1 { + static int + acquire_1 (__guard *g) + { + if (_GLIBCXX_GUARD_ACQUIRE (g)) + { + if (((char *)g)[1]++) + { +#ifdef __EXCEPTIONS + throw __gnu_cxx::recursive_init(); +#else + abort (); +#endif + } + return 1; + } + return 0; + } + extern "C" int __cxa_guard_acquire (__guard *g) { - return _GLIBCXX_GUARD_ACQUIRE (g); +#ifdef __GTHREADS + if (__gthread_active_p ()) + { + // Simple wrapper for exception safety. + struct mutex_wrapper + { + bool unlock; + mutex_wrapper (): unlock(true) + { + static_mutex::lock (); + } + ~mutex_wrapper () + { + if (unlock) + static_mutex::unlock (); + } + } mw; + + if (acquire_1 (g)) + { + mw.unlock = false; + return 1; + } + + return 0; + } +#endif + + return acquire_1 (g); } extern "C" - void __cxa_guard_release (__guard *g) + void __cxa_guard_abort (__guard *g) { - _GLIBCXX_GUARD_RELEASE (g); + ((char *)g)[1]--; +#ifdef __GTHREADS + if (__gthread_active_p ()) + static_mutex::unlock (); +#endif } extern "C" - void __cxa_guard_abort (__guard *) + void __cxa_guard_release (__guard *g) { + ((char *)g)[1]--; + _GLIBCXX_GUARD_RELEASE (g); +#ifdef __GTHREADS + if (__gthread_active_p ()) + static_mutex::unlock (); +#endif } } |