Module Picos_mpscq

Lock-free multi-producer, single-consumer queue.

🏎ī¸ This data structure is optimized for use as a non-work-stealing scheduler's ready queue.

API

type !'a t

A multi-producer, single-consumer queue.

val create : ?padded:bool -> unit -> 'a t

create () returns a new empty multi-producer, single-consumer queue.

Interface for producers

ℹī¸ The operations in this section can be called by both any number of producers and the single owner / consumer of the queue.

val push : 'a t -> 'a -> unit

push queue value adds the value to the tail of the queue.

val push_head : 'a t -> 'a -> unit

push_head queue value adds the value to the head of the queue.

Interface for the owner / consumer

⚠ī¸ The operations in this section should only be called by the single owner / consumer of the queue.

exception Empty

Raised by pop_exn in case it finds the queue empty.

val pop_exn : 'a t -> 'a

pop_exn queue tries to remove the value at the head of the queue. Returns the removed value or raises Empty in case the queue was empty.

  • raises Empty

    in case the queue was empty.

val pop_all : 'a t -> 'a Stdlib.Seq.t

pop_all queue removes all values from the queue and returns them as a sequence.

Examples

An example top-level session:

# let q : int Picos_mpscq.t =
    Picos_mpscq.create ()
val q : int Picos_mpscq.t = <abstr>

# Picos_mpscq.push q 42
- : unit = ()

# Picos_mpscq.push_head q 76
- : unit = ()

# Picos_mpscq.push q 101
- : unit = ()

# Picos_mpscq.pop_exn q
- : int = 76

# Picos_mpscq.pop_all q |> List.of_seq
- : int list = [42; 101]