123456789101112131415161718192021222324252627282930313233343536373839moduletypeS=sigexceptionLockedvalexclusively:(unit->'a)->'avalrun:(unit->'a)->'avalregister_printer:([`Exclusively]->stringoption)->unitendmoduleMake()=structexceptionLockedlet()=Printexc.register_printer@@function|Locked->Some"Mutex already locked"|_->NonemoduleS=State.Make(Bool)letexclusivelyf=ifS.get()thenraiseLockedelsebeginS.settrue;(* Favonia: I learn from the developers of eio that Fun.protect is not good at
preserving the backtraces. See https://github.com/ocaml-multicore/eio/pull/209. *)matchf()with|ans->S.setfalse;ans|exceptione->S.setfalse;raiseeendletrunf=S.run~init:falsefletregister_printerf=S.register_printer@@fun_->f`Exclusivelylet()=register_printer@@fun_->Some"Unhandled algaeff effect; use Algaeff.Mutex.run"end