Exclusive spin-based lock wrapper with explicit initialization, locking, and unlocking operations. The wrapper has no shared-lock mode or try operation.

The lock is built on atomic; link programs that use it with atomic.ll from the library root (see the atomic page).


Spinlock model


Spinlock

Methods

INIT

(--) Resets the lock state to UNLOCKED.

lock

(--) Acquires the lock in exclusive mode by repeatedly attempting the transition from UNLOCKED to LOCKED until one attempt succeeds.

unlock

(--) Releases one exclusive lock acquisition.

Preconditions

  • The current state is LOCKED.

Examples

Runtime example: direct and guarded locking

"Spinlock"  use
"String"    use
"control"   use
"lockGuard" use

{} Int32 {} [
  spinlock: Spinlock;
  ("initial=" spinlock.state LF) printList

  @spinlock.lock
  ("locked=" spinlock.state LF) printList
  @spinlock.unlock
  ("unlocked=" spinlock.state LF) printList

  [
    guard: @spinlock lockGuard;
    ("guarded=" spinlock.state LF) printList
  ] call
  ("after-guard=" spinlock.state LF) printList
  0
] "main" exportFunction

Expected Output

initial=0
locked=1
unlocked=0
guarded=1
after-guard=0

See also