Island RTL has a very interesting class - SpinLockClassEnter
What is the point of “spinning”, then return a False???
Can someone share some insights how to use it properly? What does the return value False mean? Should I normally just use SpinLockEnter, instead of SpinLockClassEnter?
class method SpinLockClassEnter(var x: NativeInt): Boolean;
begin
{$IFDEF NOTHREADS}
exit InternalCalls.Exchange(var x, 1) = 0;
{$ELSE}
var cid := Thread.CurrentThreadID;
var lValue := InternalCalls.CompareExchange(var x, cid, NativeInt(0)); // returns old
if lValue = NativeInt(0) then exit true; // value was zero, we should run.
if lValue = cid then exit false; // same thread, but already running, don't go again.
if lValue = NativeInt(Int64(-1)) then exit false; // it's done in the mean time, we should exit.
// At this point it's NOT the same thread, and not done loading yet;
repeat
if not Thread.&Yield() then
Thread.Sleep(1);
lValue := InternalCalls.VolatileRead(var x); // returns old
until lValue = NativeInt(Int64(-1));
exit false;
{$ENDIF}
end;