This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH v2] nptl: optimize cancelstate and canceltype changing


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]