diff options
author | Jason Merrill <jason@redhat.com> | 2004-08-27 22:33:54 -0400 |
---|---|---|
committer | Jason Merrill <jason@gcc.gnu.org> | 2004-08-27 22:33:54 -0400 |
commit | 40aac94801f86355bd86cf5b340481ee2f501d3c (patch) | |
tree | 1f0fd6ce16170d90ee5ebfcb5972ed7af5874607 /gcc/cp | |
parent | ed3479983db246f3126c12c441659ef6b8ed027e (diff) | |
download | gcc-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 'gcc/cp')
-rw-r--r-- | gcc/cp/ChangeLog | 8 | ||||
-rw-r--r-- | gcc/cp/cp-tree.h | 3 | ||||
-rw-r--r-- | gcc/cp/decl.c | 119 | ||||
-rw-r--r-- | gcc/cp/decl2.c | 2 | ||||
-rw-r--r-- | gcc/cp/tree.c | 20 |
5 files changed, 107 insertions, 45 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index afd820f..f1cac07 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,11 @@ +2004-08-27 Jason Merrill <jason@redhat.com> + + PR c++/13684 + * 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. + 2004-08-27 Richard Henderson <rth@redhat.com> * cp-tree.def (OFFSETOF_EXPR): New. diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 4597b45..2eba7a3 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -3801,7 +3801,7 @@ extern tree build_target_expr_with_type (tree, tree); 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 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, @@ -4198,6 +4198,7 @@ extern void lang_check_failed (const char *, int, 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); diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 95cf0f5..c746e33 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -5081,7 +5081,7 @@ end_cleanup_fn (void) /* Generate code to handle the destruction of DECL, an object with static storage duration. */ -void +tree register_dtor_fn (tree decl) { tree cleanup; @@ -5090,7 +5090,7 @@ register_dtor_fn (tree decl) tree fcall; if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) - return; + 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, @@ -5129,7 +5129,7 @@ register_dtor_fn (tree decl) } else args = tree_cons (NULL_TREE, cleanup, NULL_TREE); - finish_expr_stmt (build_function_call (get_atexit_node (), args)); + return build_function_call (get_atexit_node (), args); } /* DECL is a VAR_DECL with static storage duration. INIT, if present, @@ -5151,36 +5151,42 @@ expand_static_init (tree decl, tree init) if (DECL_FUNCTION_SCOPE_P (decl)) { /* Emit code to perform this initialization but once. */ - tree if_stmt; - tree then_clause; - tree assignment; - tree guard; - tree guard_init; + 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 int guard = 0; - if (!guard) { - // Do initialization. - guard = 1; - // Register variable for destruction at end of program. + 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 `temp' variable is only set to 1 *after* the + 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 + 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. */ + 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); @@ -5188,29 +5194,68 @@ expand_static_init (tree decl, tree init) /* 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); + then_clause = begin_compound_stmt (BCS_NO_SCOPE); + + if (flag_threadsafe_statics) + { + 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); + + TARGET_EXPR_CLEANUP (begin) + = build (COND_EXPR, void_type_node, flag, + void_zero_node, + build_call (abort_fn, guard_addr_list)); + CLEANUP_EH_ONLY (begin) = 1; + + /* 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)); + } else - assignment = guard_init; - finish_expr_stmt (assignment); + init = add_stmt_to_compound (init, set_guard (guard)); /* Use atexit to register a function for destroying this static variable. */ - register_dtor_fn (decl); + init = add_stmt_to_compound (init, register_dtor_fn (decl)); + + finish_expr_stmt (init); + + if (flag_threadsafe_statics) + { + 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); diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c index 5da240f..2d852f7 100644 --- a/gcc/cp/decl2.c +++ b/gcc/cp/decl2.c @@ -2446,7 +2446,7 @@ do_static_initialization (tree decl, tree init) /* 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_expr_stmt (register_dtor_fn (decl)); /* Finish up. */ finish_static_initialization_or_destruction (guard_if_stmt); diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c index 4bdadad..9933467 100644 --- a/gcc/cp/tree.c +++ b/gcc/cp/tree.c @@ -2254,6 +2254,19 @@ stabilize_expr (tree exp, tree* initp) 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. */ @@ -2275,12 +2288,7 @@ stabilize_call (tree call, tree *initp) { 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; + inits = add_stmt_to_compound (inits, init); } *initp = inits; |