diff options
author | jason <jason@138bc75d-0d04-0410-961f-82ee72b054a4> | 2004-08-28 02:33:54 +0000 |
---|---|---|
committer | jason <jason@138bc75d-0d04-0410-961f-82ee72b054a4> | 2004-08-28 02:33:54 +0000 |
commit | 4813f5af50dada66ece22c495d18a5cd6393f53b (patch) | |
tree | 1f0fd6ce16170d90ee5ebfcb5972ed7af5874607 /libstdc++-v3 | |
parent | 0a53b20c990bb9b0afd292d0c9fa2eb205dd1549 (diff) | |
download | gcc-4813f5af50dada66ece22c495d18a5cd6393f53b.tar.gz |
PR c++/13684
* cp/decl.c (expand_static_init): Use thread-safety API.
(register_dtor_fn): Return the call, don't expand it.
* cp/tree.c (add_stmt_to_compound): New fn.
(stabilize_call): Use it.
* gimplify.c (gimplify_cleanup_point_expr): Handle CLEANUP_EH_ONLY.
(gimple_push_cleanup): Add eh_only parm.
(gimplify_target_expr): Pass it.
* c.opt (-fno-threadsafe-statics): New option.
* c-opts.c (c_common_handle_option): Handle it.
* c-common.h (flag_threadsafe_statics): Declare it.
* c-common.c (flag_threadsafe_statics): Record it.
* doc/invoke.texi: Document it.
* tsystem.h (_GNU_SOURCE): Define.
* gthr-posix.h (__gthread_recursive_mutex_t): New typedef.
(__GTHREAD_RECURSIVE_MUTEX_INIT): New macro.
(__GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION): New macro.
(__gthread_recursive_mutex_init_function): New fn.
(__gthread_recursive_mutex_lock): New fn.
(__gthread_recursive_mutex_trylock): New fn.
(__gthread_recursive_mutex_unlock): New fn.
* gthr-solaris.h, gthr-single.h, gthr-dce.h: Likewise.
* gthr-win32.h, gthr-vxworks.h: Likewise.
* gthr.h: Document.
* 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.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@86687 138bc75d-0d04-0410-961f-82ee72b054a4
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 d96a583e89a..c77647b7e6a 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 255108f5f7e..a9280bccdc5 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 } } |