aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/libsupc++
diff options
context:
space:
mode:
authorJason Merrill <jason@redhat.com>2004-08-27 22:33:54 -0400
committerJason Merrill <jason@gcc.gnu.org>2004-08-27 22:33:54 -0400
commit40aac94801f86355bd86cf5b340481ee2f501d3c (patch)
tree1f0fd6ce16170d90ee5ebfcb5972ed7af5874607 /libstdc++-v3/libsupc++
parented3479983db246f3126c12c441659ef6b8ed027e (diff)
downloadgcc-40aac94801f86355bd86cf5b340481ee2f501d3c.zip
gcc-40aac94801f86355bd86cf5b340481ee2f501d3c.tar.gz
gcc-40aac94801f86355bd86cf5b340481ee2f501d3c.tar.bz2
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
Diffstat (limited to 'libstdc++-v3/libsupc++')
-rw-r--r--libstdc++-v3/libsupc++/guard.cc136
1 files changed, 132 insertions, 4 deletions
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
}
}