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
- Scope helpers that rely only on exclusive locking are compatible.
- Condition-variable waiting is unsupported.
LOCKEDis1n8andUNLOCKEDis0n8.- The interface exposes no sleep-based waiting path, no waiter queue, no timeout result, and no fairness guarantee.
- In
DEBUGmode, more thanMAXIMUM_SPIN_COUNTfailed spin attempts are rejected through assertion. - A
Spinlockowns one lock state. No separate lock token or guard object is produced by the wrapper itself.
Spinlock
Methods
lock
(--) Acquires the lock in exclusive mode by repeatedly attempting the transition from UNLOCKED to LOCKED until one attempt succeeds.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
- lockGuard: Scope-based locking and unlocking helpers.
- windows/Mutex: SRW lock wrapper with exclusive and shared locking.
- windows/ConditionVariable: SRW-lock-based condition-variable wrapper.