This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
RFC: PATCH for thread-safe C++ static local initialization
- From: Jason Merrill <jason at redhat dot com>
- To: gcc-patches at gcc dot gnu dot org
- Cc: Benjamin Kosnik <bkoz at redhat dot com>
- Date: Fri, 20 Aug 2004 16:03:31 -0400
- Subject: RFC: PATCH for thread-safe C++ static local initialization
This is a patch to implement/use the C++ ABI interface for thread-safe
initialization of local statics. The use generates code something like
static <type> guard;
if (!guard.first_byte) {
if (__cxa_guard_acquire (&guard)) {
bool flag = false;
try {
// Do initialization.
flag = true; __cxa_guard_release (&guard);
// Register variable for destruction at end of program.
} catch {
if (!flag) __cxa_guard_abort (&guard);
}
}
}
which is more complex than the old code which just checked the first byte
of the guard function. Seems like this should probably be controlled by a
flag, but which?
The implementation of the API uses a recursive mutex so that only one
thread at a time is able to initialize statics. This strategy is simpler
than trying to allocate a mutex for each static, and shouldn't cause
noticeable performance issues.
However, the current gthr interface doesn't actually support recursive
mutexes; I don't think appropriate APIs exist on all targets. It's
possible to emulate a recursive mutex, as I do in this patch, but my
emulation relies on being able to request a unique identifier for the
current thread, which is also not part of the gthr interface--though it is
part of the objc thread interface.
How do people feel about extending the gthr interface to either support
recursive mutexes (emulating in the gthr-* file if necessary) or thread_id,
or both?
2004-08-20 Jason Merrill <jason@redhat.com>
* decl.c (expand_static_init): Use thread-safety API.
(register_dtor_fn): Return the call, don't expand it.
* tree.c (add_stmt_to_compound): New fn.
(stabilize_call): Use it.
* libsupc++/guard.cc (static_mutex): Internal class implementing a
recursive mutex which controls initialization of local statics.
(__cxa_recursive_init): New exception class.
(__cxa_guard_acquire): Deal with locking and recursion detection.
(acquire_1, __cxa_guard_abort, __cxa_guard_release): Likewise.
* include/Makefile.am (${host_builddir}/gthr.h): Don't add
_GLIBCXX_ to #pragma lines.
* include/Makefile.in: Update.
* gthr-posix.h (__gthread_thread_t): New typedef.
(__gthread_thread_id): New fn.
* gthr-solaris.h: Likewise.
*** ./gcc/cp/decl.c.~1~ 2004-08-20 15:18:10.460641154 -0400
--- ./gcc/cp/decl.c 2004-08-20 15:17:09.046922030 -0400
*************** end_cleanup_fn (void)
*** 5084,5090 ****
/* Generate code to handle the destruction of DECL, an object with
static storage duration. */
! void
register_dtor_fn (tree decl)
{
tree cleanup;
--- 5084,5090 ----
/* Generate code to handle the destruction of DECL, an object with
static storage duration. */
! tree
register_dtor_fn (tree decl)
{
tree cleanup;
*************** register_dtor_fn (tree decl)
*** 5093,5099 ****
tree fcall;
if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
! return;
/* Call build_cleanup before we enter the anonymous function so that
any access checks will be done relative to the current scope,
--- 5093,5099 ----
tree fcall;
if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
! return void_zero_node;
/* Call build_cleanup before we enter the anonymous function so that
any access checks will be done relative to the current scope,
*************** register_dtor_fn (tree decl)
*** 5132,5138 ****
}
else
args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
! finish_expr_stmt (build_function_call (get_atexit_node (), args));
}
/* DECL is a VAR_DECL with static storage duration. INIT, if present,
--- 5132,5138 ----
}
else
args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
! return build_function_call (get_atexit_node (), args);
}
/* DECL is a VAR_DECL with static storage duration. INIT, if present,
*************** expand_static_init (tree decl, tree init
*** 5154,5189 ****
if (! toplevel_bindings_p ())
{
/* Emit code to perform this initialization but once. */
! tree if_stmt;
! tree then_clause;
! tree assignment;
! tree guard;
! tree guard_init;
/* Emit code to perform this initialization but once. This code
looks like:
! static int guard = 0;
! if (!guard) {
! // Do initialization.
! guard = 1;
! // Register variable for destruction at end of program.
}
! Note that the `temp' variable is only set to 1 *after* the
initialization is complete. This ensures that an exception,
thrown during the construction, will cause the variable to
reinitialized when we pass through this code again, as per:
[stmt.dcl]
! If the initialization exits by throwing an exception, the
initialization is not complete, so it will be tried again
the next time control enters the declaration.
! In theory, this process should be thread-safe, too; multiple
! threads should not be able to initialize the variable more
! than once. We don't yet attempt to ensure thread-safety. */
/* Create the guard variable. */
guard = get_guard (decl);
--- 5154,5195 ----
if (! toplevel_bindings_p ())
{
/* Emit code to perform this initialization but once. */
! tree if_stmt, inner_if_stmt;
! tree then_clause, inner_then_clause;
! tree guard, guard_addr, guard_addr_list;
! tree acquire_fn, release_fn, abort_fn;
! tree flag, begin;
/* Emit code to perform this initialization but once. This code
looks like:
! static <type> guard;
! if (!guard.first_byte) {
! if (__cxa_guard_acquire (&guard)) {
! bool flag = false;
! try {
! // Do initialization.
! flag = true; __cxa_guard_release (&guard);
! // Register variable for destruction at end of program.
! } catch {
! if (!flag) __cxa_guard_abort (&guard);
! }
}
! Note that the `flag' variable is only set to 1 *after* the
initialization is complete. This ensures that an exception,
thrown during the construction, will cause the variable to
reinitialized when we pass through this code again, as per:
[stmt.dcl]
! If the initialization exits by throwing an exception, the
initialization is not complete, so it will be tried again
the next time control enters the declaration.
! This process should be thread-safe, too; multiple threads
! should not be able to initialize the variable more than
! once. */
/* Create the guard variable. */
guard = get_guard (decl);
*************** expand_static_init (tree decl, tree init
*** 5191,5219 ****
/* Begin the conditional initialization. */
if_stmt = begin_if_stmt ();
finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
! then_clause = begin_compound_stmt (0);
! /* Do the initialization itself. */
! assignment = init ? init : NULL_TREE;
! /* Once the assignment is complete, set TEMP to 1. Since the
! construction of the static object is complete at this point,
! we want to make sure TEMP is set to 1 even if a temporary
! constructed during the initialization throws an exception
! when it is destroyed. So, we combine the initialization and
! the assignment to TEMP into a single expression, ensuring
! that when we call finish_expr_stmt the cleanups will not be
! run until after TEMP is set to 1. */
! guard_init = set_guard (guard);
! if (assignment)
! assignment = build_compound_expr (assignment, guard_init);
else
! assignment = guard_init;
! finish_expr_stmt (assignment);
/* Use atexit to register a function for destroying this static
variable. */
! register_dtor_fn (decl);
finish_compound_stmt (then_clause);
finish_then_clause (if_stmt);
--- 5197,5254 ----
/* Begin the conditional initialization. */
if_stmt = begin_if_stmt ();
finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
! then_clause = begin_compound_stmt (BCS_NO_SCOPE);
! guard_addr = build_address (guard);
! guard_addr_list = build_tree_list (NULL_TREE, guard_addr);
! acquire_fn = get_identifier ("__cxa_guard_acquire");
! release_fn = get_identifier ("__cxa_guard_release");
! abort_fn = get_identifier ("__cxa_guard_abort");
! if (!get_global_value_if_present (acquire_fn, &acquire_fn))
! {
! tree argtypes = tree_cons (NULL_TREE, TREE_TYPE (guard_addr),
! void_list_node);
! tree vfntype = build_function_type (void_type_node, argtypes);
! acquire_fn = push_library_fn
! (acquire_fn, build_function_type (integer_type_node, argtypes));
! release_fn = push_library_fn (release_fn, vfntype);
! abort_fn = push_library_fn (abort_fn, vfntype);
! }
else
! {
! release_fn = identifier_global_value (release_fn);
! abort_fn = identifier_global_value (abort_fn);
! }
!
! inner_if_stmt = begin_if_stmt ();
! finish_if_stmt_cond (build_call (acquire_fn, guard_addr_list),
! inner_if_stmt);
+ inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
+ begin = get_target_expr (boolean_false_node);
+ flag = TARGET_EXPR_SLOT (begin);
+
+ /* Do the initialization itself. */
+ init = add_stmt_to_compound (begin, init);
+ init = add_stmt_to_compound
+ (init, build (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
+ init = add_stmt_to_compound
+ (init, build_call (release_fn, guard_addr_list));
/* Use atexit to register a function for destroying this static
variable. */
! init = add_stmt_to_compound (init, register_dtor_fn (decl));
!
! TARGET_EXPR_CLEANUP (begin)
! = build (COND_EXPR, void_type_node, flag,
! void_zero_node,
! build_call (abort_fn, guard_addr_list));
!
! finish_expr_stmt (init);
!
! finish_compound_stmt (inner_then_clause);
! finish_then_clause (inner_if_stmt);
! finish_if_stmt (inner_if_stmt);
finish_compound_stmt (then_clause);
finish_then_clause (if_stmt);
*** ./gcc/cp/cp-tree.h.~1~ 2004-08-20 15:18:10.443642892 -0400
--- ./gcc/cp/cp-tree.h 2004-08-20 15:23:21.988752441 -0400
*************** extern tree build_target_expr_with_type
*** 3795,3801 ****
extern int local_variable_p (tree);
extern int nonstatic_local_decl_p (tree);
extern tree declare_global_var (tree, tree);
! extern void register_dtor_fn (tree);
extern tmpl_spec_kind current_tmpl_spec_kind (int);
extern tree cp_fname_init (const char *, tree *);
extern tree builtin_function (const char *name, tree type,
--- 3795,3801 ----
extern int local_variable_p (tree);
extern int nonstatic_local_decl_p (tree);
extern tree declare_global_var (tree, tree);
! extern tree register_dtor_fn (tree);
extern tmpl_spec_kind current_tmpl_spec_kind (int);
extern tree cp_fname_init (const char *, tree *);
extern tree builtin_function (const char *name, tree type,
*************** extern void lang_check_failed (const c
*** 4192,4197 ****
--- 4192,4198 ----
extern tree stabilize_expr (tree, tree *);
extern void stabilize_call (tree, tree *);
extern bool stabilize_init (tree, tree *);
+ extern tree add_stmt_to_compound (tree, tree);
extern tree cxx_maybe_build_cleanup (tree);
extern void init_tree (void);
extern int pod_type_p (tree);
*** ./gcc/cp/decl2.c.~1~ 2004-08-20 15:18:10.467640438 -0400
--- ./gcc/cp/decl2.c 2004-08-17 12:32:48.000000000 -0400
*************** do_static_initialization (tree decl, tre
*** 2452,2458 ****
/* If we're using __cxa_atexit, register a a function that calls the
destructor for the object. */
if (flag_use_cxa_atexit)
! register_dtor_fn (decl);
/* Finish up. */
finish_static_initialization_or_destruction (guard_if_stmt);
--- 2452,2458 ----
/* If we're using __cxa_atexit, register a a function that calls the
destructor for the object. */
if (flag_use_cxa_atexit)
! finish_expr_stmt (register_dtor_fn (decl));
/* Finish up. */
finish_static_initialization_or_destruction (guard_if_stmt);
*** ./gcc/cp/tree.c.~1~ 2004-08-20 15:18:10.471640029 -0400
--- ./gcc/cp/tree.c 2004-08-20 15:17:10.779744838 -0400
*************** stabilize_expr (tree exp, tree* initp)
*** 2254,2259 ****
--- 2254,2272 ----
return exp;
}
+ /* Add NEW, an expression whose value we don't care about, after the
+ similar expression ORIG. */
+
+ tree
+ add_stmt_to_compound (tree orig, tree new)
+ {
+ if (!new || !TREE_SIDE_EFFECTS (new))
+ return orig;
+ if (!orig || !TREE_SIDE_EFFECTS (orig))
+ return new;
+ return build2 (COMPOUND_EXPR, void_type_node, orig, new);
+ }
+
/* Like stabilize_expr, but for a call whose args we want to
pre-evaluate. */
*************** stabilize_call (tree call, tree *initp)
*** 2275,2286 ****
{
tree init;
TREE_VALUE (t) = stabilize_expr (TREE_VALUE (t), &init);
! if (!init)
! /* Nothing. */;
! else if (inits)
! inits = build2 (COMPOUND_EXPR, void_type_node, inits, init);
! else
! inits = init;
}
*initp = inits;
--- 2288,2294 ----
{
tree init;
TREE_VALUE (t) = stabilize_expr (TREE_VALUE (t), &init);
! inits = add_stmt_to_compound (inits, init);
}
*initp = inits;
*** ./gcc/gthr-posix.h.~1~ 2004-08-20 15:18:10.418645449 -0400
--- ./gcc/gthr-posix.h 2004-08-18 13:31:52.000000000 -0400
*************** Software Foundation, 59 Temple Place - S
*** 46,51 ****
--- 46,52 ----
typedef pthread_key_t __gthread_key_t;
typedef pthread_once_t __gthread_once_t;
typedef pthread_mutex_t __gthread_mutex_t;
+ typedef pthread_t __gthread_thread_t;
#define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
#define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
*************** __gthread_mutex_unlock (__gthread_mutex_
*** 516,521 ****
--- 517,532 ----
return 0;
}
+ /* Returns a non-zero value which uniquely describes a thread. */
+ static inline __gthread_thread_t
+ __gthread_thread_id (void)
+ {
+ if (__gthread_active_p ())
+ return (__gthread_thread_t) pthread_self ();
+ else
+ return (__gthread_thread_t) 1;
+ }
+
#endif /* _LIBOBJC */
#endif /* ! GCC_GTHR_POSIX_H */
*** ./gcc/gthr-solaris.h.~1~ 2004-08-20 15:18:10.421645142 -0400
--- ./gcc/gthr-solaris.h 2004-08-19 18:33:32.000000000 -0400
*************** typedef struct {
*** 44,49 ****
--- 44,50 ----
int once;
} __gthread_once_t;
typedef mutex_t __gthread_mutex_t;
+ typedef thread_t __gthread_thread_t;
#define __GTHREAD_ONCE_INIT { DEFAULTMUTEX, 0 }
#define __GTHREAD_MUTEX_INIT DEFAULTMUTEX
*************** __gthread_mutex_unlock (__gthread_mutex_
*** 466,471 ****
--- 467,482 ----
return 0;
}
+ /* Returns a non-zero value which uniquely describes a thread. */
+ static inline __gthread_thread_t
+ __gthread_thread_id (void)
+ {
+ if (__gthread_active_p ())
+ return (__gthread_thread_t) thr_self ();
+ else
+ return (__gthread_thread_t) 1;
+ }
+
#endif /* _LIBOBJC */
#endif /* ! GCC_GTHR_SOLARIS_H */
*** ./libstdc++-v3/include/Makefile.am.~1~ 2004-08-20 15:18:10.484638699 -0400
--- ./libstdc++-v3/include/Makefile.am 2004-08-17 17:15:46.000000000 -0400
*************** ${host_builddir}/c++config.h: ${top_buil
*** 507,513 ****
uppercase = [ABCDEFGHIJKLMNOPQRSTUVWXYZ_]
${host_builddir}/gthr.h: ${toplevel_srcdir}/gcc/gthr.h stamp-${host_alias}
! sed -e '/^#/s/\(${uppercase}${uppercase}*\)/_GLIBCXX_\1/g' \
-e 's/_GLIBCXX_SUPPORTS_WEAK/__GXX_WEAK__/g' \
-e 's,^#include "\(.*\)",#include <bits/\1>,g' \
< ${toplevel_srcdir}/gcc/gthr.h > $@
--- 507,514 ----
uppercase = [ABCDEFGHIJKLMNOPQRSTUVWXYZ_]
${host_builddir}/gthr.h: ${toplevel_srcdir}/gcc/gthr.h stamp-${host_alias}
! sed -e '/^#pragma/b' \
! -e '/^#/s/\(${uppercase}${uppercase}*\)/_GLIBCXX_\1/g' \
-e 's/_GLIBCXX_SUPPORTS_WEAK/__GXX_WEAK__/g' \
-e 's,^#include "\(.*\)",#include <bits/\1>,g' \
< ${toplevel_srcdir}/gcc/gthr.h > $@
*** ./libstdc++-v3/include/Makefile.in.~1~ 2004-08-20 15:18:10.486638494 -0400
--- ./libstdc++-v3/include/Makefile.in 2004-08-17 17:23:46.000000000 -0400
*************** ${host_builddir}/c++config.h: ${top_buil
*** 882,888 ****
echo "#endif // _CXXCONFIG_" >>$@
${host_builddir}/gthr.h: ${toplevel_srcdir}/gcc/gthr.h stamp-${host_alias}
! sed -e '/^#/s/\(${uppercase}${uppercase}*\)/_GLIBCXX_\1/g' \
-e 's/_GLIBCXX_SUPPORTS_WEAK/__GXX_WEAK__/g' \
-e 's,^#include "\(.*\)",#include <bits/\1>,g' \
< ${toplevel_srcdir}/gcc/gthr.h > $@
--- 882,889 ----
echo "#endif // _CXXCONFIG_" >>$@
${host_builddir}/gthr.h: ${toplevel_srcdir}/gcc/gthr.h stamp-${host_alias}
! sed -e '/^#pragma/b' \
! -e '/^#/s/\(${uppercase}${uppercase}*\)/_GLIBCXX_\1/g' \
-e 's/_GLIBCXX_SUPPORTS_WEAK/__GXX_WEAK__/g' \
-e 's,^#include "\(.*\)",#include <bits/\1>,g' \
< ${toplevel_srcdir}/gcc/gthr.h > $@
*** ./libstdc++-v3/libsupc++/guard.cc.~1~ 2004-08-20 15:18:10.489638188 -0400
--- ./libstdc++-v3/libsupc++/guard.cc 2004-08-18 13:32:40.000000000 -0400
***************
*** 29,54 ****
// Written by Mark Mitchell, CodeSourcery LLC, <mark@codesourcery.com>
#include <cxxabi.h>
// The IA64/generic ABI uses the first byte of the guard variable.
// The ARM EABI uses the least significant bit.
namespace __cxxabiv1
{
extern "C"
int __cxa_guard_acquire (__guard *g)
{
! return _GLIBCXX_GUARD_ACQUIRE (g);
}
extern "C"
! void __cxa_guard_release (__guard *g)
{
! _GLIBCXX_GUARD_RELEASE (g);
}
extern "C"
! void __cxa_guard_abort (__guard *)
{
}
}
--- 29,196 ----
// 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
+ {
+ // 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_mutex_t mutex;
+ static __gthread_thread_t thread_id;
+ static int depth;
+
+ #ifdef __GTHREAD_MUTEX_INIT_FUNCTION
+ static void init();
+ #endif
+
+ public:
+ static void lock();
+ static void unlock();
+ };
+
+ #ifdef __GTHREAD_MUTEX_INIT
+ __gthread_mutex_t static_mutex::mutex = __GTHREAD_MUTEX_INIT;
+ #else
+ __gthread_mutex_t static_mutex::mutex;
+ #endif
+ int static_mutex::depth;
+ __gthread_thread_t static_mutex::thread_id;
+
+ #ifdef __GTHREAD_MUTEX_INIT_FUNCTION
+ void static_mutex::init()
+ {
+ __GTHREAD_MUTEX_INIT_FUNCTION (&mutex);
+ }
+ #endif
+
+ void static_mutex::lock()
+ {
+ __gthread_thread_t me;
+ #ifdef __GTHREAD_MUTEX_INIT_FUNCTION
+ static __gthread_once_t once = __GTHREAD_ONCE_INIT;
+ __gthread_once (&once, init);
+ #endif
+ me = __gthread_thread_id ();
+ if (thread_id != me)
+ {
+ __gthread_mutex_lock (&mutex);
+ if (depth != 0)
+ throw;
+ thread_id = me;
+ }
+ ++depth;
+ }
+
+ void static_mutex::unlock ()
+ {
+ --depth;
+ if (depth == 0)
+ {
+ thread_id = 0;
+ __gthread_mutex_unlock (&mutex);
+ }
+ }
+ }
+ #endif
+
namespace __cxxabiv1
{
+ // 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 __cxa_recursive_init: public std::exception
+ {
+ public:
+ __cxa_recursive_init() throw() { }
+ virtual ~__cxa_recursive_init() throw ();
+ };
+
+ __cxa_recursive_init::~__cxa_recursive_init() throw() { }
+
+ static int
+ acquire_1 (__guard *g)
+ {
+ if (_GLIBCXX_GUARD_ACQUIRE (g))
+ {
+ if (((char *)g)[1]++)
+ {
+ #ifdef __EXCEPTIONS
+ throw __cxa_recursive_init();
+ #else
+ abort ();
+ #endif
+ }
+ return 1;
+ }
+ return 0;
+ }
+
extern "C"
int __cxa_guard_acquire (__guard *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_abort (__guard *g)
{
! ((char *)g)[1]--;
! #ifdef __GTHREADS
! if (__gthread_active_p ())
! static_mutex::unlock ();
! #endif
}
extern "C"
! void __cxa_guard_release (__guard *g)
{
+ ((char *)g)[1]--;
+ _GLIBCXX_GUARD_RELEASE (g);
+ #ifdef __GTHREADS
+ if (__gthread_active_p ())
+ static_mutex::unlock ();
+ #endif
}
}