aboutsummaryrefslogtreecommitdiff
path: root/clib/cThread.ml
diff options
context:
space:
mode:
authorcoqbot-app[bot]2021-04-12 12:34:16 +0000
committerGitHub2021-04-12 12:34:16 +0000
commit271445decd0fc1a37da3009f148f2e68c7168fe1 (patch)
tree631d9976d17d6573b0f348cfd6fdbd83bb779ddb /clib/cThread.ml
parent7ce1c4844b077adb25d14cf1bbd2d22548b1e935 (diff)
parent520ac61dfe5a6e865cb7b10f4a822c0d72f3ded9 (diff)
Merge PR #14046: make critical sections safe in the presence of exceptions
Reviewed-by: ejgallego Ack-by: gares Ack-by: SkySkimmer Ack-by: gadmm
Diffstat (limited to 'clib/cThread.ml')
-rw-r--r--clib/cThread.ml19
1 files changed, 19 insertions, 0 deletions
diff --git a/clib/cThread.ml b/clib/cThread.ml
index 89ca2f7d83..3796fdf788 100644
--- a/clib/cThread.ml
+++ b/clib/cThread.ml
@@ -107,3 +107,22 @@ let mask_sigalrm f x =
let create f x =
Thread.create (mask_sigalrm f) x
+
+(*
+ Atomic mutex lock taken from https://gitlab.com/gadmm/memprof-limits/-/blob/master/src/thread_map.ml#L23-34
+ Critical sections :
+ - Mutex.lock does not poll on leaving the blocking section
+ since 4.12.
+ - Never inline, to avoid theoretically-possible reorderings with
+ flambda.
+ (workaround to the lack of masking)
+*)
+
+(* We inline the call to Mutex.unlock to avoid polling in bytecode mode *)
+external unlock: Mutex.t -> unit = "caml_mutex_unlock"
+
+let[@inline never] with_lock m ~scope =
+ let () = Mutex.lock m (* BEGIN ATOMIC *) in
+ match (* END ATOMIC *) scope () with
+ | (* BEGIN ATOMIC *) x -> unlock m ; (* END ATOMIC *) x
+ | (* BEGIN ATOMIC *) exception e -> unlock m ; (* END ATOMIC *) raise e