Utilitites.SpinLockClassEnter, why spinning then return a false?

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;

It’s used for class initialization. It has three cases.

It should run the class ctor, in which it returns true right away.
It should block, because it’s already running in another thread. Returns false after waiting.
Its already running in this thread or already finished. Then returns false too, right away.

@ck
Thank you. Now I see.

If
1)I need a “low-lock” thread-safe access to a shared variable without the cost of context switching, while
2) I know before-hand that the locking-time will be very short,
then, I guess I can use SpinLockEnter(integer)/SpinLockExit(integer), instead of using Monitor?

Have you had a look at https://docs.elementscompiler.com/API/SystemFunctions/Interlocked/?

Thank you @mh
I did check the link. Actually the protected variable is a bounded queue (with fixed capacity) with writers and readers from different threads; hence I am really looking for a SpinLock

You’ll want SpinLockEnter/Exit then yes. The Class version is very specific to class initialization.