Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-base

Source of release 0.42.0

0.42.0 / src / std / interop / worker_queue.lucb

#==============================================================================================
#
#   worker_queue - A worker's message queue
#
#   DESCRIPTION:
#       Queue storage protected by the worker's mutex; removing a value transfers ownership
#       before waking a producer, and disposal runs outside the lock.
#
#==============================================================================================

## Queue storage is protected by WorkerState's mutex. Removing a value transfers
## ownership before waking a producer; disposal runs outside the queue lock.
struct WorkerQueue[T]:
    var slots: (T?)[]
    var head: usize = 0
    var count: usize = 0

    func full() -> bool:
        return self.count == self.slots.length

    func push(value: T):
        assert(not self.full())
        self.slots[(self.head + self.count) % self.slots.length] = value
        self.count += 1

    func pop() -> T:
        assert(self.count > 0)
        let value = self.slots[self.head] else trap("an occupied worker slot is empty")
        self.slots[self.head] = none
        self.head = (self.head + 1) % self.slots.length
        self.count -= 1
        return value