semaphore

Generated by tinox doc

tinox.core.semaphore, bundled with the compiler and also available as a standalone tinox-central package.

Mutex

import tinox.core.semaphore
class Main
{
    fnc main() -> Int32
    {
        const mutex: Mutex = Mutex::new()
        Mutex::lock(mutex)
        println("critical section")
        Mutex::unlock(mutex)
        if (Mutex::tryLock(mutex))
        {
            println("acquired again")
            Mutex::unlock(mutex)
        }

        return 0
    }
}

Semaphore

import tinox.core.semaphore
class Main
{
    fnc main() -> Int32
    {
        // At most 2 concurrent permits.
        const sem: Semaphore = Semaphore::new(2)
        Semaphore::acquire(sem)
        Semaphore::acquire(sem)
        println("both permits acquired")
        if (Semaphore::tryAcquire(sem) == false)
        {
            println("no permits available")
        }

        Semaphore::release(sem)
        println("permit available: " + Semaphore::tryAcquire(sem).toString())
        return 0
    }
}

class Mutex

* A real mutual-exclusion lock backed by pthread_mutex_t (runtime.c). * * lock() BLOCKS until the mutex is available -- this is a real, atomic * OS-level primitive, not the "if not locked { locked = true }" * check-then-act pattern this class used to be implemented with (a * genuine, previously-undiscovered bug: not actually thread-safe under * concurrent access despite the name/purpose, since there is no way * for pure Tinox code, which has no atomic compare-and-swap builtin, * to implement real mutual exclusion). tryLock()'s non-blocking, * boolean-result contract is unchanged, now backed by a real * pthread_mutex_trylock instead of the same broken check-then-act.

Fields

handleInt64

Methods

fn new() → Mutex
fn lock(mutex: Mutex) → Nothing
fn unlock(mutex: Mutex) → Nothing
fn tryLock(mutex: Mutex) → Bool

class RWLock

* A real reader/writer lock backed by pthread_rwlock_t (runtime.c). * * All four operations BLOCK as a real RWLock should (multiple readers * concurrently, a writer excludes everyone). See Mutex.tnx's doc * comment for the general issue with this module's previous * implementations: this one was, if anything, more broken than * Mutex/Semaphore -- writeLock() didn't even check for active readers * before "acquiring", so it provided no real write exclusion at all, * on top of the same check-then-act non-atomicity.

Fields

handleInt64

Methods

fn new() → RWLock
fn readLock(lock: RWLock) → Nothing
fn readUnlock(lock: RWLock) → Nothing
fn writeLock(lock: RWLock) → Nothing
fn writeUnlock(lock: RWLock) → Nothing

class Semaphore

* A real counting semaphore backed by POSIX sem_t (runtime.c). * * acquire() BLOCKS until a permit is available -- see Mutex.tnx's doc * comment for why the previous "if count > 0 { count-- } else throw" * implementation was a real, previously-undiscovered bug (not * actually thread-safe, and "throw on contention" was an accident of * that broken check-then-act code, not a deliberate semaphore * semantic). tryAcquire()'s non-blocking, boolean-result contract is * unchanged, now backed by a real sem_trywait.

Fields

handleInt64

Methods

fn new(count: Int64) → Semaphore
fn acquire(sem: Semaphore) → Nothing
fn release(sem: Semaphore) → Nothing
fn tryAcquire(sem: Semaphore) → Bool