| 1 | (* backport of Mutex.protect from OCaml 5.1 *) |
| 2 | external unlock: Mutex.t -> unit = "caml_ml_mutex_unlock" |
| 3 | |
| 4 | (* Critical sections : |
| 5 | - Mutex.lock does not poll on leaving the blocking section |
| 6 | since 4.12. |
| 7 | - Never inline, to avoid theoretically-possible reorderings with |
| 8 | flambda. |
| 9 | - Inline the call to Mutex.unlock to avoid polling in bytecode. |
| 10 | (workaround to the lack of masking) *) |
| 11 | let[@inline never] with_lock m ~scope = |
| 12 | let () = Mutex.lock m (* BEGIN ATOMIC *) in |
| 13 | match (* END ATOMIC *) scope () with |
| 14 | | (* BEGIN ATOMIC *) x -> unlock m ; (* END ATOMIC *) x |
| 15 | | (* BEGIN ATOMIC *) exception e -> unlock m ; (* END ATOMIC *) raise e |