Skip to content

Commit

Permalink
use Mutex.protect directly
Browse files Browse the repository at this point in the history
  • Loading branch information
just-max committed Jul 15, 2024
1 parent 4e5ba47 commit 464b03e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
2 changes: 0 additions & 2 deletions src/common/ctx_util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ let timed : _ t = fun k ->
| Error _ as e, _ -> e
| Ok r, t -> Ok (r, t)

let lock_mutex m : _ t = Mutex.protect m

let capture_exceptions ?(filter = Fun.const true) () : _ t = fun k ->
match k () with
| Ok x -> Ok (Ok x)
Expand Down
12 changes: 6 additions & 6 deletions src/stdlib-variants/thread-counter/thread_counter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module Counter = struct
open Ctx_util
open Ctx_util.Syntax

let lock_if b m = if b then lock_mutex m else empty_context' ()
let lock_if b m = if b then Mutex.protect m else empty_context' ()

(** Note: we enforce that spawned threads don't raise uncaught exceptions,
which in theory changes the semantics of threads. The value of being
Expand Down Expand Up @@ -61,7 +61,7 @@ module Counter = struct
Condition.broadcast group.finished

let try_finish group fin =
let< _ = lock_mutex group.owner.mut in
let< _ = Mutex.protect group.owner.mut in
if group.state = Running then
finish ~lock:false group fin

Expand All @@ -81,7 +81,7 @@ module Counter = struct
Util.try_to_result f x
|> Result.iter_error (fun e -> try_finish group (Uncaught e)))
~finally:(fun () ->
let< _ = lock_mutex cnt.mut in
let< _ = Mutex.protect cnt.mut in
let tid = Thread.self () in
group.thread_count <- group.thread_count - 1;
ThreadH.remove cnt.groups tid;
Expand Down Expand Up @@ -121,15 +121,15 @@ module Counter = struct
}

let get_thread_count group =
let< _ = lock_mutex group.owner.mut in group.thread_count
let< _ = Mutex.protect group.owner.mut in group.thread_count

(** Wait for threads in a group to complete. Group must be finished first. *)
let join_group ~leftover_thread_limit ~timeout group =
(* busy waits to implement timeout *)
d_ "join_group";

let _ =
let< _ = lock_mutex group.owner.mut in
let< _ = Mutex.protect group.owner.mut in
if group.state = Running then failwith "join_group: still running"
in

Expand Down Expand Up @@ -164,7 +164,7 @@ module Counter = struct
| Running -> d_ "still running"; Condition.wait group.finished cnt.mut; loop ()
| Finished fin -> fin
in
let< _ = lock_mutex cnt.mut in loop ()
let< _ = Mutex.protect cnt.mut in loop ()
in

let leftover_count =
Expand Down

0 comments on commit 464b03e

Please sign in to comment.