123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869# 1 "clib/memprof_coq.std.ml"letis_interrupted_=false[@@inline]moduleResource_bind=structlet(let&)fscope=f~scopeend(* We do our own Mutex_aux for OCaml 5.x *)moduleMutex_aux=Mutex_auxmoduleThread_map_core=structopenResource_bindmoduleIMap=Map.Make(structtypet=intletcompare=Stdlib.compareend)type'at={mutex:Mutex.t;mutablemap:'aIMap.t}letcreate()={mutex=Mutex.create();map=IMap.empty}letcurrent_thread()=Thread.id(Thread.self())letgets=(* Concurrent threads do not alter the value for the current
thread, so we do not need a lock. *)IMap.find_opt(current_thread())s.map(* For set and clear we need a lock *)letsetsv=let&()=Mutex_aux.with_locks.mutexinletnew_map=matchvwith|None->IMap.remove(current_thread())s.map|Somev->IMap.add(current_thread())vs.mapins.map<-new_maplet_clears=let&()=Mutex_aux.with_locks.mutexins.map<-IMap.emptyendmoduleMasking=struct(* There's no mechanism to block OCaml's async exceptions,
so without memprof there is nothing interesting to do. *)letwith_resource~acquirearg~scope~(release:_->unit)=letr=acquirearginFun.protect~finally:(fun()->releaser)(fun()->scoper)endmoduleThread_map=structincludeThread_map_coreletwith_valuetls~value~scope=letold_value=gettlsin(* FIXME: needs proper masking here as there is a race between
resources and asynchronous exceptions. For now, it is
exception-safe only for exceptions arising from Memprof_callbacks. *)Masking.with_resource~acquire:(fun()->settls(Somevalue))()~scope~release:(fun()->settlsold_value)end