[PATCH v1] nptl: Replace CAS with cheaper atomics for pthread_cancel logic
Wilco Dijkstra
Wilco.Dijkstra@arm.com
Thu Jul 14 14:03:02 GMT 2022
Hi Noah,
In general I agree with replacing any compare_exchange loops which can be done
using simpler atomics to make code clearer and possibly faster. However we have
to be careful, consider this:
if ((cancelhandling & CANCELTYPE_BITMASK) == 0)
{
- int newval;
- do
- {
- newval = cancelhandling | CANCELTYPE_BITMASK;
- }
- while (!atomic_compare_exchange_weak_acquire (&self->cancelhandling,
- &cancelhandling, newval));
-
+ atomic_fetch_or_acquire (&self->cancelhandling, CANCELTYPE_BITMASK);
if (cancel_enabled_and_canceled (cancelhandling))
This now uses an stale read of cancelhandling - this might be bad if it was modified
by another thread. It seems you want something like:
cancelhandling = atomic_fetch_or_acquire (&self->cancelhandling, CANCELTYPE_BITMASK);
if (cancel_enabled_and_canceled (cancelhandling)
Using or_fetch may also be feasible. Also assuming we only need atomicity and no
synchronization of global data then these can become relaxed atomics which should
be a lot faster on most targets. We should consider removing some of the pre-reads
before atomics as well since this is a premature optimization and counterproductive
on modern cores.
Cheers,
Wilco
More information about the Libc-alpha
mailing list