This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Applied: 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>, joel at oarcorp dot com
- Date: Fri, 27 Aug 2004 22:31:07 -0400
- Subject: Applied: PATCH for thread-safe C++ static local initialization
- References: <xyp3c2hr2zw.fsf@miranda.boston.redhat.com>
Here's the patch I'm applying. The major change in this version is that
instead of emulating a recursive mutex in the libsupc++ code, I added
recursive mutex support to gthr. I implemented it for most of the thread
targets; I didn't implement it for rtems, though, which relies on OS hooks.
I suppose I could implement it there in terms of the current hooks, but
thought I'd leave that decision to the rtems folks (hi, Joel!). I tried to
test on solaris and dce threads, but failed for various reasons; I would
appreciate testing and feedback on all non-posix thread targets.
This version also adds an option -fno-threadsafe-statics to disable the
extra code emitted to use the libsupc++ routines.
I also added support for CLEANUP_EH_ONLY to the gimplifier.
I added a #define _GNU_SOURCE to tsystem.h so that the right bits of
pthread.h would be available when compiling unwind-dw2.c. I'm open to
suggestions for where else to put it, but this seems appropriate to me.
Benjamin, feel free to rename acquire_1 if you don't like that name :)
Tested x86_64-pc-linux-gnu, applied to trunk.
2004-08-27 Jason Merrill <jason@redhat.com>
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.
*** ./gcc/c-opts.c.~1~ 2004-08-27 22:28:35.296424031 -0400
--- ./gcc/c-opts.c 2004-08-25 12:33:01.000000000 -0400
*************** c_common_handle_option (size_t scode, co
*** 773,778 ****
--- 773,782 ----
flag_weak = value;
break;
+ case OPT_fthreadsafe_statics:
+ flag_threadsafe_statics = value;
+ break;
+
case OPT_fzero_link:
flag_zero_link = value;
break;
*** ./gcc/config/i386/gthr-win32.c.~1~ 2004-08-27 22:28:35.325421042 -0400
--- ./gcc/config/i386/gthr-win32.c 2004-08-26 12:23:44.000000000 -0400
*************** __gthr_win32_mutex_unlock (__gthread_mut
*** 182,184 ****
--- 182,254 ----
else
return 0;
}
+
+ void
+ __gthr_win32_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
+ {
+ mutex->counter = -1;
+ mutex->depth = 0;
+ mutex->owner = 0;
+ mutex->sema = CreateSemaphore (NULL, 0, 65535, NULL);
+ }
+
+ int
+ __gthr_win32_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
+ {
+ DWORD me = GetCurrentThreadId();
+ if (InterlockedIncrement (&mutex->counter) == 0)
+ {
+ mutex->depth = 1;
+ mutex->owner = me;
+ }
+ else if (mutex->owner == me)
+ {
+ InterlockedDecrement (&mx->lock_idx);
+ ++(mutex->depth);
+ }
+ else if (WaitForSingleObject (mutex->sema, INFINITE) == WAIT_OBJECT_0)
+ {
+ mutex->depth = 1;
+ mutex->owner = me;
+ }
+ else
+ {
+ /* WaitForSingleObject returns WAIT_FAILED, and we can only do
+ some best-effort cleanup here. */
+ InterlockedDecrement (&mutex->counter);
+ return 1;
+ }
+ return 0;
+ }
+
+ int
+ __gthr_win32_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
+ {
+ DWORD me = GetCurrentThreadId();
+ if (__GTHR_W32_InterlockedCompareExchange (&mutex->counter, 0, -1) < 0)
+ {
+ mutex->depth = 1;
+ mutex->owner = me;
+ }
+ else if (mutex->owner == me)
+ ++(mutex->depth);
+ else
+ return 1;
+
+ return 0;
+ }
+
+ int
+ __gthr_win32_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
+ {
+ --(mutex->depth);
+ if (mutex->depth == 0)
+ {
+ mutex->owner = 0;
+
+ if (InterlockedDecrement (&mutex->counter) >= 0)
+ return ReleaseSemaphore (mutex->sema, 1, NULL) ? 0 : 1;
+ }
+
+ return 0;
+ }
*** ./gcc/cp/decl.c.~1~ 2004-08-27 22:28:35.385414857 -0400
--- ./gcc/cp/decl.c 2004-08-27 14:47:12.090723653 -0400
*************** end_cleanup_fn (void)
*** 5081,5087 ****
/* Generate code to handle the destruction of DECL, an object with
static storage duration. */
! void
register_dtor_fn (tree decl)
{
tree cleanup;
--- 5081,5087 ----
/* 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)
*** 5090,5096 ****
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,
--- 5090,5096 ----
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)
*** 5129,5135 ****
}
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,
--- 5129,5135 ----
}
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
*** 5151,5186 ****
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;
/* 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);
--- 5151,5192 ----
if (DECL_FUNCTION_SCOPE_P (decl))
{
/* 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
*** 5188,5216 ****
/* 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);
--- 5194,5261 ----
/* 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);
! 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
! init = add_stmt_to_compound (init, set_guard (guard));
/* Use atexit to register a function for destroying this static
variable. */
! 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);
*** ./gcc/cp/cp-tree.h.~1~ 2004-08-27 22:28:35.342419290 -0400
--- ./gcc/cp/cp-tree.h 2004-08-26 19:32:09.000000000 -0400
*************** extern tree build_target_expr_with_type
*** 3801,3807 ****
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,
--- 3801,3807 ----
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
*** 4198,4203 ****
--- 4198,4204 ----
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-27 22:28:35.401413208 -0400
--- ./gcc/cp/decl2.c 2004-08-27 14:47:12.172715256 -0400
*************** do_static_initialization (tree decl, tre
*** 2446,2452 ****
/* 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);
--- 2446,2452 ----
/* 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-27 22:28:35.419411353 -0400
--- ./gcc/cp/tree.c 2004-08-27 14:47:13.655563400 -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/doc/invoke.texi.~1~ 2004-08-27 22:28:35.449408260 -0400
--- ./gcc/doc/invoke.texi 2004-08-26 19:32:14.000000000 -0400
*************** in the following sections.
*** 182,188 ****
-fno-nonansi-builtins -fno-operator-names @gol
-fno-optional-diags -fpermissive @gol
-frepo -fno-rtti -fstats -ftemplate-depth-@var{n} @gol
! -fuse-cxa-atexit -fno-weak -nostdinc++ @gol
-fno-default-inline -fvisibility-inlines-hidden @gol
-Wabi -Wctor-dtor-privacy @gol
-Wnon-virtual-dtor -Wreorder @gol
--- 182,188 ----
-fno-nonansi-builtins -fno-operator-names @gol
-fno-optional-diags -fpermissive @gol
-frepo -fno-rtti -fstats -ftemplate-depth-@var{n} @gol
! -fno-threadsafe-statics -fuse-cxa-atexit -fno-weak -nostdinc++ @gol
-fno-default-inline -fvisibility-inlines-hidden @gol
-Wabi -Wctor-dtor-privacy @gol
-Wnon-virtual-dtor -Wreorder @gol
*************** A limit on the template instantiation de
*** 1470,1475 ****
--- 1470,1482 ----
endless recursions during template class instantiation. ANSI/ISO C++
conforming programs must not rely on a maximum depth greater than 17.
+ @item -fno-threadsafe-statics
+ @opindex fno-threadsafe-statics
+ Do not emit the extra code to use the routines specified in the C++
+ ABI for thread-safe initialization of local statics. You can use this
+ option to reduce code size slightly in code that doesn't need to be
+ thread-safe.
+
@item -fuse-cxa-atexit
@opindex fuse-cxa-atexit
Register destructors for objects with static storage duration with the
*** ./gcc/c-common.c.~1~ 2004-08-27 22:28:35.290424650 -0400
--- ./gcc/c-common.c 2004-08-27 14:47:06.221324732 -0400
*************** int flag_permissive;
*** 459,464 ****
--- 459,469 ----
int flag_enforce_eh_specs = 1;
+ /* Nonzero means to generate thread-safe code for initializing local
+ statics. */
+
+ int flag_threadsafe_statics = 1;
+
/* Nonzero means warn about implicit declarations. */
int warn_implicit = 1;
*** ./gcc/c-common.h.~1~ 2004-08-27 22:28:35.293424341 -0400
--- ./gcc/c-common.h 2004-08-27 14:47:06.321314492 -0400
*************** extern int flag_permissive;
*** 571,576 ****
--- 571,581 ----
extern int flag_enforce_eh_specs;
+ /* Nonzero (the default) means to generate thread-safe code for
+ initializing local statics. */
+
+ extern int flag_threadsafe_statics;
+
/* Nonzero means warn about implicit declarations. */
extern int warn_implicit;
*** ./gcc/c.opt.~1~ 2004-08-27 22:28:35.297423928 -0400
--- ./gcc/c.opt 2004-08-25 12:29:35.000000000 -0400
*************** C++ ObjC++ Joined RejectNegative UIntege
*** 675,680 ****
--- 675,684 ----
fthis-is-variable
C++ ObjC++
+ fthreadsafe-statics
+ C++ ObjC++
+ -fno-threadsafe-statics Do not generate thread-safe code for initializing local statics.
+
funsigned-bitfields
C ObjC C++ ObjC++
When \"signed\" or \"unsigned\" is not given make the bitfield unsigned
*** ./gcc/gimplify.c.~1~ 2004-08-27 22:28:35.305423104 -0400
--- ./gcc/gimplify.c 2004-08-26 19:31:32.000000000 -0400
*************** gimplify_cleanup_point_expr (tree *expr_
*** 3271,3279 ****
else
{
tree sl, tfe;
sl = tsi_split_statement_list_after (&iter);
! tfe = build (TRY_FINALLY_EXPR, void_type_node, sl, NULL_TREE);
append_to_statement_list (TREE_OPERAND (wce, 0),
&TREE_OPERAND (tfe, 1));
*wce_p = tfe;
--- 3271,3285 ----
else
{
tree sl, tfe;
+ enum tree_code code;
+
+ if (CLEANUP_EH_ONLY (wce))
+ code = TRY_CATCH_EXPR;
+ else
+ code = TRY_FINALLY_EXPR;
sl = tsi_split_statement_list_after (&iter);
! tfe = build (code, void_type_node, sl, NULL_TREE);
append_to_statement_list (TREE_OPERAND (wce, 0),
&TREE_OPERAND (tfe, 1));
*wce_p = tfe;
*************** gimplify_cleanup_point_expr (tree *expr_
*** 3301,3307 ****
is the cleanup action required. */
static void
! gimple_push_cleanup (tree var, tree cleanup, tree *pre_p)
{
tree wce;
--- 3307,3313 ----
is the cleanup action required. */
static void
! gimple_push_cleanup (tree var, tree cleanup, bool eh_only, tree *pre_p)
{
tree wce;
*************** gimple_push_cleanup (tree var, tree clea
*** 3352,3357 ****
--- 3358,3364 ----
else
{
wce = build (WITH_CLEANUP_EXPR, void_type_node, cleanup);
+ CLEANUP_EH_ONLY (wce) = eh_only;
append_to_statement_list (wce, pre_p);
}
*************** gimplify_target_expr (tree *expr_p, tree
*** 3399,3405 ****
if (TARGET_EXPR_CLEANUP (targ))
{
gimplify_stmt (&TARGET_EXPR_CLEANUP (targ));
! gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ), pre_p);
}
/* Only expand this once. */
--- 3406,3413 ----
if (TARGET_EXPR_CLEANUP (targ))
{
gimplify_stmt (&TARGET_EXPR_CLEANUP (targ));
! gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
! CLEANUP_EH_ONLY (targ), pre_p);
}
/* Only expand this once. */
*** ./gcc/gthr-dce.h.~1~ 2004-08-27 22:28:35.311422485 -0400
--- ./gcc/gthr-dce.h 2004-08-26 00:29:31.000000000 -0400
***************
*** 1,4 ****
!
/* Compile this one with gcc. */
/* Copyright (C) 1997, 1999, 2000, 2001 Free Software Foundation, Inc.
--- 1,4 ----
! /* Threads compatibility routines for libgcc2 and libobjc. */
/* Compile this one with gcc. */
/* Copyright (C) 1997, 1999, 2000, 2001 Free Software Foundation, Inc.
*************** Software Foundation, 59 Temple Place - S
*** 52,61 ****
--- 52,63 ----
typedef pthread_key_t __gthread_key_t;
typedef pthread_once_t __gthread_once_t;
typedef pthread_mutex_t __gthread_mutex_t;
+ typedef pthread_mutex_t __gthread_recursive_mutex_t;
#define __GTHREAD_ONCE_INIT pthread_once_init
#define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
+ #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
#define __GTHREAD_MUTEX_INIT_DEFAULT pthread_once_init
*************** __gthread_mutex_unlock (__gthread_mutex_
*** 481,486 ****
--- 483,525 ----
return 0;
}
+ static inline int
+ __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
+ {
+ if (__gthread_active_p ())
+ {
+ pthread_mutexattr_t attr;
+ int r;
+
+ r = pthread_mutexattr_create (&attr);
+ if (!r)
+ r = pthread_mutexattr_setkind_np (&attr, MUTEX_RECURSIVE_NP);
+ if (!r)
+ r = pthread_mutex_init (mutex, attr);
+ if (!r)
+ r = pthread_mutexattr_delete (&attr);
+ return r;
+ }
+ }
+
+ static inline int
+ __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
+ {
+ return __gthread_mutex_lock (mutex);
+ }
+
+ static inline int
+ __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
+ {
+ return __gthread_mutex_trylock (mutex);
+ }
+
+ static inline int
+ __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
+ {
+ return __gthread_mutex_unlock (mutex);
+ }
+
#endif /* _LIBOBJC */
#undef UNUSED
*** ./gcc/gthr-posix.h.~1~ 2004-08-27 22:28:35.314422176 -0400
--- ./gcc/gthr-posix.h 2004-08-27 10:42:27.078781548 -0400
*************** Software Foundation, 59 Temple Place - S
*** 46,54 ****
--- 46,62 ----
typedef pthread_key_t __gthread_key_t;
typedef pthread_once_t __gthread_once_t;
typedef pthread_mutex_t __gthread_mutex_t;
+ typedef pthread_mutex_t __gthread_recursive_mutex_t;
#define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
#define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
+ #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
+ #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER
+ #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
+ #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
+ #else
+ #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
+ #endif
#if SUPPORTS_WEAK && GTHREAD_USE_WEAK
*************** __gthread_mutex_unlock (__gthread_mutex_
*** 516,521 ****
--- 524,568 ----
return 0;
}
+ #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
+ static inline int
+ __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
+ {
+ if (__gthread_active_p ())
+ {
+ pthread_mutexattr_t attr;
+ int r;
+
+ r = pthread_mutexattr_init (&attr);
+ if (!r)
+ r = pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
+ if (!r)
+ r = pthread_mutex_init (mutex, &attr);
+ if (!r)
+ r = pthread_mutexattr_destroy (&attr);
+ return r;
+ }
+ }
+ #endif
+
+ static inline int
+ __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
+ {
+ return __gthread_mutex_lock (mutex);
+ }
+
+ static inline int
+ __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
+ {
+ return __gthread_mutex_trylock (mutex);
+ }
+
+ static inline int
+ __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
+ {
+ return __gthread_mutex_unlock (mutex);
+ }
+
#endif /* _LIBOBJC */
#endif /* ! GCC_GTHR_POSIX_H */
*** ./gcc/gthr-single.h.~1~ 2004-08-27 22:28:35.315422073 -0400
--- ./gcc/gthr-single.h 2004-08-26 00:30:59.000000000 -0400
*************** Software Foundation, 59 Temple Place - S
*** 32,37 ****
--- 32,38 ----
/* Just provide compatibility for mutex handling. */
typedef int __gthread_mutex_t;
+ typedef int __gthread_recursive_mutex_t;
#define __GTHREAD_MUTEX_INIT 0
*************** __gthread_mutex_unlock (__gthread_mutex_
*** 232,237 ****
--- 233,256 ----
return 0;
}
+ static inline int
+ __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
+ {
+ return __gthread_mutex_lock (mutex);
+ }
+
+ static inline int
+ __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
+ {
+ return __gthread_mutex_trylock (mutex);
+ }
+
+ static inline int
+ __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
+ {
+ return __gthread_mutex_unlock (mutex);
+ }
+
#endif /* _LIBOBJC */
#undef UNUSED
*** ./gcc/gthr.h.~1~ 2004-08-27 22:28:35.323421248 -0400
--- ./gcc/gthr.h 2004-08-20 19:55:31.000000000 -0400
*************** Software Foundation, 59 Temple Place - S
*** 42,47 ****
--- 42,48 ----
__gthread_key_t
__gthread_once_t
__gthread_mutex_t
+ __gthread_recursive_mutex_t
The threads interface must define the following macros:
*************** Software Foundation, 59 Temple Place - S
*** 56,61 ****
--- 57,65 ----
function which looks like this:
void __GTHREAD_MUTEX_INIT_FUNCTION (__gthread_mutex_t *)
Don't define __GTHREAD_MUTEX_INIT in this case
+ __GTHREAD_RECURSIVE_MUTEX_INIT
+ __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
+ as above, but for a recursive mutex.
The threads interface must define the following static functions:
*************** Software Foundation, 59 Temple Place - S
*** 71,76 ****
--- 75,84 ----
int __gthread_mutex_trylock (__gthread_mutex_t *mutex);
int __gthread_mutex_unlock (__gthread_mutex_t *mutex);
+ int __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex);
+ int __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex);
+ int __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex);
+
All functions returning int should return zero on success or the error
number. If the operation is not supported, -1 is returned.
*** ./gcc/gthr-solaris.h.~1~ 2004-08-27 22:28:35.317421867 -0400
--- ./gcc/gthr-solaris.h 2004-08-26 00:29:56.000000000 -0400
*************** typedef struct {
*** 44,52 ****
--- 44,54 ----
int once;
} __gthread_once_t;
typedef mutex_t __gthread_mutex_t;
+ typedef mutex_t __gthread_recursive_mutex_t;
#define __GTHREAD_ONCE_INIT { DEFAULTMUTEX, 0 }
#define __GTHREAD_MUTEX_INIT DEFAULTMUTEX
+ #define __GTHREAD_RECURSIVE_MUTEX_INIT RECURSIVE_ERRORCHECKMUTEX
#if SUPPORTS_WEAK && GTHREAD_USE_WEAK
*************** __gthread_mutex_unlock (__gthread_mutex_
*** 466,471 ****
--- 468,491 ----
return 0;
}
+ static inline int
+ __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
+ {
+ return __gthread_mutex_lock (mutex);
+ }
+
+ static inline int
+ __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
+ {
+ return __gthread_mutex_trylock (mutex);
+ }
+
+ static inline int
+ __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
+ {
+ return __gthread_mutex_unlock (mutex);
+ }
+
#endif /* _LIBOBJC */
#endif /* ! GCC_GTHR_SOLARIS_H */
*** ./gcc/gthr-vxworks.h.~1~ 2004-08-27 22:28:35.318421764 -0400
--- ./gcc/gthr-vxworks.h 2004-08-26 12:34:33.000000000 -0400
*************** Software Foundation, 59 Temple Place - S
*** 45,51 ****
--- 45,54 ----
#include <semLib.h>
typedef SEM_ID __gthread_mutex_t;
+ /* All VxWorks mutexes are recursive. */
+ typedef SEM_ID __gthread_recursive_mutex_t;
#define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
+ #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
static inline void
__gthread_mutex_init_function (__gthread_mutex_t *mutex)
*************** __gthread_mutex_unlock (__gthread_mutex_
*** 71,76 ****
--- 74,103 ----
return semGive (*mutex);
}
+ static inline void
+ __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
+ {
+ __gthread_mutex_init_function (mutex);
+ }
+
+ static inline int
+ __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
+ {
+ return __gthread_mutex_lock (mutex);
+ }
+
+ static inline int
+ __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
+ {
+ return __gthread_mutex_trylock (mutex);
+ }
+
+ static inline int
+ __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
+ {
+ return __gthread_mutex_unlock (mutex);
+ }
+
/* pthread_once is complicated enough that it's implemented
out-of-line. See config/vxlib.c. */
*** ./gcc/gthr-win32.h.~1~ 2004-08-27 22:28:35.322421351 -0400
--- ./gcc/gthr-win32.h 2004-08-27 22:28:44.623462526 -0400
*************** typedef struct {
*** 343,351 ****
--- 343,360 ----
void *sema;
} __gthread_mutex_t;
+ typedef struct {
+ long counter;
+ long depth;
+ DWORD owner;
+ void *sema;
+ } __gthread_recursive_mutex_t;
+
#define __GTHREAD_ONCE_INIT {0, -1}
#define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
#define __GTHREAD_MUTEX_INIT_DEFAULT {-1, 0}
+ #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
+ #define __GTHREAD_RECURSIVE_MUTEX_INIT_DEFAULT {-1, 0}
#if __MINGW32_MAJOR_VERSION >= 1 || \
(__MINGW32_MAJOR_VERSION == 0 && __MINGW32_MINOR_VERSION > 2)
*************** __gthread_mutex_unlock (__gthread_mutex_
*** 472,477 ****
--- 481,513 ----
return 0;
}
+ static inline int
+ __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
+ {
+ if (__gthread_active_p ())
+ return __gthr_win32_recursive_mutex_lock (mutex);
+ else
+ return 0;
+ }
+
+ static inline int
+ __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
+ {
+ if (__gthread_active_p ())
+ return __gthr_win32_recursive_mutex_trylock (mutex);
+ else
+ return 0;
+ }
+
+ static inline int
+ __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
+ {
+ if (__gthread_active_p ())
+ return __gthr_win32_recursive_mutex_unlock (mutex);
+ else
+ return 0;
+ }
+
#else /* ! __GTHREAD_HIDE_WIN32API */
#include <windows.h>
*************** __gthread_mutex_unlock (__gthread_mutex_
*** 610,615 ****
--- 646,728 ----
return 0;
}
+ static inline void
+ __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
+ {
+ mutex->counter = -1;
+ mutex->depth = 0;
+ mutex->owner = 0;
+ mutex->sema = CreateSemaphore (NULL, 0, 65535, NULL);
+ }
+
+ static inline int
+ __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
+ {
+ if (__gthread_active_p ())
+ {
+ DWORD me = GetCurrentThreadId();
+ if (InterlockedIncrement (&mutex->counter) == 0)
+ {
+ mutex->depth = 1;
+ mutex->owner = me;
+ }
+ else if (mutex->owner == me)
+ {
+ InterlockedDecrement (&mx->lock_idx);
+ ++(mutex->depth);
+ }
+ else if (WaitForSingleObject (mutex->sema, INFINITE) == WAIT_OBJECT_0)
+ {
+ mutex->depth = 1;
+ mutex->owner = me;
+ }
+ else
+ {
+ /* WaitForSingleObject returns WAIT_FAILED, and we can only do
+ some best-effort cleanup here. */
+ InterlockedDecrement (&mutex->counter);
+ return 1;
+ }
+ }
+ return 0;
+ }
+
+ static inline int
+ __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
+ {
+ if (__gthread_active_p ())
+ {
+ DWORD me = GetCurrentThreadId();
+ if (__GTHR_W32_InterlockedCompareExchange (&mutex->counter, 0, -1) < 0)
+ {
+ mutex->depth = 1;
+ mutex->owner = me;
+ }
+ else if (mutex->owner == me)
+ ++(mutex->depth);
+ else
+ return 1;
+ }
+ return 0;
+ }
+
+ static inline int
+ __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
+ {
+ if (__gthread_active_p ())
+ {
+ --(mutex->depth);
+ if (mutex->depth == 0)
+ {
+ mutex->owner = 0;
+
+ if (InterlockedDecrement (&mutex->counter) >= 0)
+ return ReleaseSemaphore (mutex->sema, 1, NULL) ? 0 : 1;
+ }
+ }
+ return 0;
+ }
+
#endif /* __GTHREAD_HIDE_WIN32API */
#ifdef __cplusplus
*** ./gcc/tsystem.h.~1~ 2004-08-27 22:28:35.323421248 -0400
--- ./gcc/tsystem.h 2004-08-27 10:54:41.188382687 -0400
*************** Software Foundation, 59 Temple Place - S
*** 40,45 ****
--- 40,48 ----
#define HAVE_DECL_GETOPT 1
#endif
+ /* We want everything from the glibc headers. */
+ #define _GNU_SOURCE 1
+
/* GCC supplies these headers. */
#include <stddef.h>
#include <float.h>
*** ./libstdc++-v3/libsupc++/guard.cc.~1~ 2004-08-27 22:28:35.483404756 -0400
--- ./libstdc++-v3/libsupc++/guard.cc 2004-08-26 19:22:28.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,182 ----
// 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)
{
! #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
}
}