Friday 27 October 2017

linux - Can atomic ops based spin lock's Unlock directly set the lock flag to zero?

Say for example, I have an exclusive atomic-ops-based spin
lock implementation as below:



bool
TryLock(volatile TInt32 * pFlag)
{
return
!(AtomicOps::Exchange32(pFlag, 1) == 1);
}

void Lock
(volatile TInt32 * pFlag)
{

while
(AtomicOps::Exchange32(pFlag, 1) == 1) {
AtomicOps::ThreadYield();

}
}

void Unlock (volatile TInt32 *
pFlag)
{
*pFlag = 0; // is this ok? or here as well a atomicity is
needed for load and store

}



Where
AtomicOps::Exchange32 is implemented on windows using
InterlockedExchange and on linux using
__atomic_exchange_n.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...