From 40aac94801f86355bd86cf5b340481ee2f501d3c Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Fri, 27 Aug 2004 22:33:54 -0400 Subject: re PR c++/13684 (local static object variable constructed once but ctors and dtors called multiple times on same memory when called in multiple threads) 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. From-SVN: r86687 --- libstdc++-v3/libsupc++/guard.cc | 136 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 132 insertions(+), 4 deletions(-) (limited to 'libstdc++-v3/libsupc++') 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, #include +#include +#include +#include // 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 } } -- cgit v1.1