#! @file locks.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains what guards what threads share: a mutex owning a value only one #! thread reaches at a time, the guard lending it, a condition threads wait on #! until another says something changed, and something run once however many #! threads ask. Each is a word threads change atomically and wait on with #! `system::futex_wait`, so none makes a system call but where a thread waits, or #! one waits to be woken. import system; import std::time; module std::thread; #! A value only one thread reaches at a time: `lock` waits until no other thread #! holds it, and gives a guard lending it, which lets go of it where it closes. #! #! Its word is 0 where no thread holds it, 1 where one does, and 2 where one does #! and others may be waiting, as Drepper's "Futexes Are Tricky" has it, so #! letting go makes a system call only where another thread may wait. #! @tparam T What it guards, which it owns, and closes where it closes. pub struct mutex : owning { var _word: atomic; var _value: T; } #! Makes a mutex owning a value. #! @param value What it guards. #! @returns The mutex, which no thread holds. pub def mutex::new(value: T): mutex { return mutex:: { _word = atomic::::new(0), _value = move(value) }; } #! Waits until no other thread holds a mutex, and holds it. #! @param this The mutex. #! @returns The guard lending what it guards, which lets go of it where it #! closes. pub def mutex::lock(&mut this): guard { this.hold(); return guard:: { _mutex = this as *mut mutex }; } #! Holds a mutex where no other thread does, without waiting. #! @param this The mutex. #! @returns The guard where it held it, and nothing where another thread does. pub def mutex::try_lock(&mut this): maybe> { if this._word.compare_exchange(0, 1, order::acquire, order::relaxed) is value { return guard:: { _mutex = this as *mut mutex }; } return nothing {}; } #! Closes what a mutex guards. No thread holds it, nor will. #! @param this The mutex. pub def mutex::close(&mut this): void { close(&mut this._value); } # Waits until no other thread holds the mutex, and holds it. Where it was held, the # word is made 2 before waiting, so whoever lets go wakes a waiter. def mutex::hold(&mut this): void { if this._word.compare_exchange(0, 1, order::acquire, order::relaxed) is value { return; } while this._word.exchange(2, order::acquire) != 0 { _ = system::futex_wait(this.word(), 2, -1); } } # Lets go of the mutex, waking a thread waiting for it where one may be. def mutex::let_go(&mut this): void { if this._word.exchange(0, order::release) == 2 { _ = system::futex_wake(this.word(), 1); } } # The word the mutex's threads wait on. def mutex::word(&mut this): *mut u32 { return ((&mut this._word) as *mut atomic) as *mut u32; } #! Lends the value a mutex guards, to the one thread holding it, and lets go of #! the mutex where it closes. A guard dropped as soon as it is made would let go #! at once, so one must be kept. #! @tparam T What the mutex guards. @nodiscard pub struct guard : owning { var _mutex: *mut mutex; } #! What the mutex guards, which no other thread reaches while the guard is held. #! @param this The guard. #! @returns The value. pub def guard::value(&mut this): &mut T { return &mut this._mutex[0]._value; } #! Lets go of the mutex. #! @param this The guard, which holds nothing after. pub def guard::close(&mut this): void { if this._mutex != null { this._mutex[0].let_go(); this._mutex = null; } } #! A place threads wait until another says something changed, which they look #! at holding a mutex: a thread holding it finds what it waits for missing, and #! waits, letting go of the mutex as it does, and holding it again once woken. #! A thread may wake without being notified, so it looks again. #! #! Its word counts notifications. A thread reads it before it lets go of the #! mutex, and waits only while it holds that, so a notification between the two #! is never lost. pub struct condition { var _count: atomic; } #! Makes a condition. #! @returns The condition. pub def condition::new(): condition { return condition { _count = atomic::::new(0) }; } #! Lets go of a mutex and waits until notified, holding it again after. #! @param this The condition. #! @param held The guard of the mutex, which holds it again after. pub def condition::wait(&mut this, held: &mut guard): void { val seen := this._count.load(order::relaxed); held._mutex[0].let_go(); _ = system::futex_wait(this.word(), seen, -1); held._mutex[0].hold(); } #! Lets go of a mutex and waits until notified, or until a length of time has #! passed, holding it again after. #! @param this The condition. #! @param held The guard of the mutex, which holds it again after. #! @param most How long to wait at most. #! @returns Whether it was woken before the time ran out. pub def condition::wait_for(&mut this, held: &mut guard, most: time::duration): bool { val seen := this._count.load(order::relaxed); held._mutex[0].let_go(); val result := system::futex_wait(this.word(), seen, most.in_nanoseconds); held._mutex[0].hold(); return result != -110; } #! Wakes one thread waiting on a condition. #! @param this The condition. pub def condition::notify_one(&mut this): void { _ = this._count.fetch_add(1, order::release); _ = system::futex_wake(this.word(), 1); } #! Wakes every thread waiting on a condition. #! @param this The condition. pub def condition::notify_all(&mut this): void { _ = this._count.fetch_add(1, order::release); _ = system::futex_wake(this.word(), 0x7FFFFFFF); } # The word the condition's threads wait on. def condition::word(&mut this): *mut u32 { return ((&mut this._count) as *mut atomic) as *mut u32; } #! Runs a function once, however many threads ask: the first runs it, and the #! rest wait until it has run. #! #! Its word is 0 before it runs, 1 while it runs, 2 while it runs and others #! wait, and 3 once it has run. pub struct once { var _state: atomic; } #! Makes a `once` that has run nothing. #! @returns It. pub def once::new(): once { return once { _state = atomic::::new(0) }; } #! Runs a function where none has run, or waits until the one running has. #! @param this The `once`. #! @param body The function. pub def once::run(&mut this, body: def(): void): void { return if this._state.load(order::acquire) == 3; if this._state.compare_exchange(0, 1, order::acquire, order::acquire) is value { body(); if this._state.exchange(3, order::release) == 2 { _ = system::futex_wake(this.word(), 0x7FFFFFFF); } return; } while true { val state := this._state.load(order::acquire); return if state == 3; if state == 1 && this._state.compare_exchange(1, 2, order::relaxed, order::relaxed) is error { continue; } _ = system::futex_wait(this.word(), 2, -1); } } #! Whether a `once` has run its function. #! @param this The `once`. #! @returns Whether it has. pub def once::done(&mut this): bool { return this._state.load(order::acquire) == 3; } # The word the threads waiting for it wait on. def once::word(&mut this): *mut u32 { return ((&mut this._state) as *mut atomic) as *mut u32; }