Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce HOFs that wrap functions to run with a locked mutex #716

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
16 changes: 16 additions & 0 deletions lib_eio/eio_mutex.ml
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,19 @@ let use_rw ~protect t fn =
(* {mutex t R * locked t} *)
poison t ex;
raise ex

let wrap_ro t f x =
lock t;
match f x with
| y -> unlock t; y
| exception ex -> unlock t; raise ex

let wrap_rw ~protect t f x =
lock t;
let f () = f x in
match if protect then Cancel.protect f else f () with
| y -> unlock t; y
| exception ex ->
poison t ex;
raise ex

3 changes: 3 additions & 0 deletions lib_eio/eio_mutex.mli
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ val use_ro : t -> (unit -> 'a) -> 'a
Note: a mutex still only allows one fiber to have the mutex locked at a time,
even if all operations are "read-only". *)

val wrap_rw : protect:bool -> t -> ('a -> 'b) -> 'a -> 'b
val wrap_ro : t -> ('a -> 'b) -> 'a -> 'b

(** {2 Low-level API}

Care must be taken when locking a mutex manually. It is easy to forget to unlock it in some cases,
Expand Down