gold patch committed: Avoid some race conditions

Ian Lance Taylor
Tue Mar 24 04:54:00 GMT 2009

When using threads, gold has had a long-standing race condition,
originally analyzed by Kris Van Hees.  A target may be allocated
simultaneously in two different threads, leading to an "incompatible
target" warning as the two instances of the same target appear to be
different.  While looking into that, I realized that there are two other
similar race conditions: allocating the Descriptors lock field and
allocating the Errors lock field.  This was unfortunately rather sloppy
coding on my part.  I committed this patch to use pthread_once to avoid
these problems.  It is used in a somewhat indirect manner to retain
efficiency when not threaded.


2009-03-23  Ian Lance Taylor  <>

	* gold-threads.h (class Initialize_lock): Define.
	* (class Initialize_lock_once): Define.
	(initialize_lock_control): New static variable.
	(initialize_lock_pointer): New static variable.
	(initialize_lock_once): New static function.
	(Initialize_lock::Initialize_lock): Define.
	(Initialize_lock::initialize): Define.
	* target-select.h: Include "gold-threads.h".
	(class Target_selector): Add lock_ and initialize_lock_ fields.
	Don't define instantiate_target, just declare it.
	* (Target_selector::Target_selector): Initialize
	new fields.
	(Target_selector::instantiate_target): Define.
	* descriptors.h: Include "gold-threads.h".
	(class Descriptors): Add initialize_lock_ field.
	* (Descriptors::Descriptors): Initialize new
	(Descriptors::open): Use initialize_lock_ field
	* errors.h (class Errors): Add initialize_lock_ field.
	* (Errors::Errors): Initialize new field.
	(Errors::initialize_lock): Use initialize_lock_ field.
	* (class Target_selector_powerpc): Remove
	instantiated_target_ field.  In do_recognize call
	instantiate_target rather than do_instantiate_target.  In
	do_instantiate_target just allocate a new target.
	* (class Target_selector_sparc): Likewise.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: foo.patch
Type: text/x-patch
Size: 12781 bytes
Desc: Avoid some race conditions
URL: <>

More information about the Binutils mailing list