Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-prism

Source of release 0.6.1

0.6.1 / src / wal / writers.lucb

## Bounded FIFO writer admission. No disk I/O or user code runs under this lock.
## Waiters sleep between checks; this is not a lock-free or real-time scheduler.
import sync
import thread
import time
from wal.bytes import invalid, busy, timed_out

pub let capacity: usize = 32
pub let maximum_wait_ms: i64 = 60000

struct Slot:
    var queued: bool
    var previous: usize
    var next: usize

pub struct Statistics:
    pub let waiting: i64
    pub let peak: i64
    pub let enqueued: i64
    pub let busy_returns: i64
    pub let timeouts: i64

## Zero initialization is ready to use. Slot zero is a sentinel, never a waiter.
pub struct Queue:
    var mutex: sync.Mutex
    var held: bool
    var slots: Slot[33]
    var head: usize
    var tail: usize
    var count: usize
    var peak: usize
    var enqueued: i64
    var busy_returns: i64
    var timeouts: i64

    func remove(index: usize):
        let previous = self.slots[index].previous
        let next = self.slots[index].next
        if previous == 0: self.head = next
        else: self.slots[previous].next = next
        if next == 0: self.tail = previous
        else: self.slots[next].previous = previous
        self.slots[index] = Slot()
        self.count -= 1

    pub func enter(wait_ms: i64 = 0) -> !:
        if wait_ms < 0 or wait_ms > maximum_wait_ms:
            error(invalid, "writer wait must be in 0-60000 milliseconds")
        let started = time.now()
        self.mutex.lock()
        if not self.held and self.count == 0:
            self.held = true
            self.mutex.unlock()
            return
        if wait_ms == 0 or self.count == capacity:
            self.busy_returns += 1
            self.mutex.unlock()
            error(busy, "writer admission busy or queue full")
        var index: usize = 1
        while self.slots[index].queued: index += 1
        self.slots[index] = Slot(queued = true, previous = self.tail)
        if self.tail == 0: self.head = index
        else: self.slots[self.tail].next = index
        self.tail = index
        self.count += 1
        if self.count > self.peak: self.peak = self.count
        self.enqueued += 1
        self.mutex.unlock()
        let budget = (u64)wait_ms * 1000000
        while true:
            self.mutex.lock()
            ## An expired waiter never starts I/O, even if it has become head.
            if time.since(started) >= budget:
                self.remove(index)
                self.timeouts += 1
                self.mutex.unlock()
                error(timed_out, "writer admission timed out; no commit I/O started")
            if not self.held and self.head == index:
                self.remove(index)
                self.held = true
                self.mutex.unlock()
                return
            self.mutex.unlock()
            thread.sleep(1)

    pub func leave():
        self.mutex.lock()
        assert(self.held)
        self.held = false
        self.mutex.unlock()

    pub func statistics() -> Statistics:
        self.mutex.lock()
        defer self.mutex.unlock()
        return Statistics((i64)self.count, (i64)self.peak, self.enqueued, self.busy_returns, self.timeouts)