std1.0.0-beta-7 SafeC docs

Source sync/spinlock.h

View in the repository

// SafeC Standard Library — Spinlock
// Atomic test-and-set spinlock with platform-specific pause hints.
#pragma once

namespace std {

struct Spinlock {
    int locked;    // 0 = unlocked, 1 = locked (accessed atomically)
    unsafe impl Send + Sync;   // every access is atomic

    // An unlocked spinlock.
    static struct Spinlock new();

    // Acquire the lock. Spins until successful.
    void lock();

    // Try to acquire the lock. Returns 1 on success, 0 if already held.
    int  trylock();

    // Release the lock.
    void unlock();

    // Check if the lock is currently held (non-atomic peek).
    int  is_locked() const;
};

} // namespace std