Skip to content

Commit

Permalink
Better fix for Unix Mutex usage
Browse files Browse the repository at this point in the history
(cherry picked from commit cbed7b3)
(cherry picked from commit f857b48)
  • Loading branch information
mshinwell committed Sep 20, 2024
1 parent 994e20d commit 3e75052
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions ocaml/otherlibs/unix/unix_unix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -939,8 +939,29 @@ type popen_process =

let popen_processes = (Hashtbl.create 7 : (popen_process, int) Hashtbl.t)

(* CR ocaml 5 all-runtime5: remove lazy, which is here to stop:
"Must initialize systhreads library before using Mutex" *)
(* CR ocaml 5 all-runtime5: go back to the normal [Mutex]. *)

module Mutex : sig
type t
val create : unit -> t
val protect : t -> (unit -> 'a) -> 'a
end = struct
type t = Mutex.t option

external runtime5 : unit -> bool = "%runtime5"

let create () =
(* On runtime4, systhreads must be linked to use [Mutex], which is
error-prone to ensure. The use of [Mutex] here is new in 5.2.0, so
we just omit it for runtime4, which doesn't have parallelism. *)
if runtime5 () then Some (Mutex.create ()) else None

let protect t f =
match t with
| None -> f ()
| Some mutex -> Mutex.protect mutex f
end

let popen_mutex = lazy (Mutex.create ())

let open_proc prog args envopt proc input output error =
Expand Down

0 comments on commit 3e75052

Please sign in to comment.