package fiber

  1. Overview
  2. Docs

Source file mutex.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
open Stdune
open Core
open Core.O

type t =
  { mutable locked : bool
  ; mutable waiters : unit k Queue.t
  }

let lock t k =
  if t.locked then suspend (fun k -> Queue.push t.waiters k) k
  else (
    t.locked <- true;
    k ())

let unlock t k =
  assert t.locked;
  match Queue.pop t.waiters with
  | None ->
    t.locked <- false;
    k ()
  | Some next -> resume next () k

let with_lock t ~f =
  let* () = lock t in
  finalize f ~finally:(fun () -> unlock t)

let create () = { locked = false; waiters = Queue.create () }
OCaml

Innovation. Community. Security.