Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/sync/lock.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ let get l =
Mutex.unlock l.mutex;
x

let try_get l =
if Mutex.try_lock l.mutex then (
let x = l.content in
Mutex.unlock l.mutex;
Some x
) else
None

let set l x =
Mutex.lock l.mutex;
l.content <- x;
Expand Down
4 changes: 4 additions & 0 deletions src/sync/lock.mli
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ val get : 'a t -> 'a
(** Atomically get the value in the lock. The value that is returned isn't
protected! *)

val try_get : 'a t -> 'a option
(** Atomically get the value in the lock, but only if we get the lock
immediately. *)

val set : 'a t -> 'a -> unit
(** Atomically set the value. *)

Expand Down
6 changes: 6 additions & 0 deletions src/thread/rvar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ type 'a st = {
type 'a t = { st: 'a st Lock.t } [@@unboxed]

let[@inline] get (self : 'a t) : 'a = (Lock.get self.st).v

let[@inline] try_get (self : 'a t) : 'a option =
match Lock.try_get self.st with
| Some v -> Some v.v
| None -> None

let[@inline] pp ppx out self : unit = ppx out (get self)

let[@inline] return x =
Expand Down
3 changes: 3 additions & 0 deletions src/thread/rvar.mli
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ val return : 'a -> 'a t
val get : 'a t -> 'a
(** Get the current value. *)

val try_get : 'a t -> 'a option
(** Get the current value, but only if we get the lock immediately. *)

val pp : 'a Fmt.printer -> 'a t Fmt.printer

exception Frozen
Expand Down
Loading