This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH v2] nptl: optimize cancelstate and canceltype changing
- From: Alexander Fyodorov <halcy at yandex dot ru>
- To: Richard Henderson <rth at twiddle dot net>, "libc-alpha at sourceware dot org" <libc-alpha at sourceware dot org>, Carlos O'Donell <carlos at redhat dot com>, David Miller <davem at davemloft dot net>
- Date: Mon, 16 Jun 2014 10:56:04 +0400
- Subject: Re: [PATCH v2] nptl: optimize cancelstate and canceltype changing
- Authentication-results: sourceware.org; auth=none
- References: <180951402831608 at web26g dot yandex dot ru> <539DF667 dot 4070101 at twiddle dot net>
15.06.2014, 23:39, "Richard Henderson" <rth@twiddle.net>:
> On 06/15/2014 04:26 AM, Alexander Fyodorov wrote:
>> Changes since v1:
>> 1) Added a benchtest for pthread_setcanceltype().
>> 2) Defined atomic_write() macro for old SPARC processors.
>>
>> atomic_write() macro is needed because on some architectures there is no hardware support for atomic compare-and-swap, and it is implemented via spinlock. And writing to variables that are accessed with such compare-and-swap must be done under the same spinlock to avoid the race leading to the lost write.
>>
>> Dave, could you check my implementation of atomic_write()? I don't have SPARC...
>
> What has lack of compare-and-swap got to do with write?
Consider following scenario:
1) a = 0 initially
2) CPU0 writes 2 to a, and CPU1 executes Compare-and-Swap(&a, 0, 1)
If CAS is atomic, then "a" will equal 2. But if CAS is implemented via spinlock, the write from CPU0 could happen in between the load and the store that comprise CAS, and "a" will equal 1 which is wrong.