This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: Make generic atomicity.h use gthr.h mutexes
In article <20021110012452.A20951@disaster.jaj.com>,
Phil Edwards<phil@jaj.com> writes:
>> This patch has a slight problem in that you can't assume that a gthr.h
>> mutex may be statically init'd. You need to handle systems (esp. in
>> the so-called generic code) that only support dynamic init. Once you
>> see how complex this is to get perfectly correct, you will understand
>> why I have not volunteered to produce this patch... ;-)
> Good point.
> Something like this? (I haven't tried to test it on a dynamic-init system,
> since I haven't figure out how to fake one yet.)
[...]
> static inline _Atomic_word
> __attribute__ ((__unused__))
> __exchange_and_add (volatile _Atomic_word* __mem, int __val)
> {
> - _Atomic_word __result;
> +#ifndef __GTHREAD_MUTEX_INIT
> + static bool __initialized = false;
> + if (!__initialized)
> + {
> + __GTHREAD_MUTEX_INIT_FUNCTION (_Atomic_add_mutex);
> + __initialized = true;
> + }
> +#endif
> +
> + _Atomic_word __result;
Hi Phil,
Unfortunately, no. Unless you can logically guarantee that
__exchange_and_add will be called at least once before multi-threading
starts, this code is fragile. The only robust solutions for systems
that provide no static mutex is to place the code somewhere that must
run before threading starts OR wrap it in a __gthread_once_t construct.
Regards,
Loren