I am reinventing the wheel again (yes, I like doing that).
Can you see anything wrong with these SpinLock functions? (for Win32 only)
///<summary>
/// simple spin lock function, Lk must have been initialized with 0 before first use </summary>
procedure doLock(var _Lk: Integer);
asm
mov edx, eax
mov ecx, 1
@Loop:
mov eax, 0
lock cmpxchg dword ptr [edx], ecx
jnz @Loop
end;
///<summary>
/// simple spin unlock function, Lk must have been initialized with 0 before first use </summary>
procedure doUnLock(var _Lk: Integer);
asm
lock dec dword ptr[eax];
end;
///<summary>
/// simple spin trylock function, Lk must have been initialized with 0 before first use
/// @returns True if the lock could be acquired, False otherwise </summary>
function doTryLock(var _Lk: Integer): Boolean;
asm
mov edx, eax
mov ecx, 1
mov eax, 0
lock cmpxchg dword ptr [edx], ecx
setz al
end;
doLock and doUnlock are taken from https://vitaliburkov.wordpress.com/2011/10/28/parallel-programming-with-delphi-part-ii-resolving-race-conditions/
doTryLock is based on doLock and uses the setz opcode to return a boolean rather than looping.
I have done tests and they seem to work, but it's always difficult to actually test multi threaded code.