1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071openOrder_indirtypeorder=ttypet={t:order;(* This strange dance with protect is to prevent the GC from collecting
values in the middle of an operation. *)lock:gc_lock;}andgc_lock={mutablelocks:int;mutableforgotten:orderlist;}letlocklock=lock.locks<-lock.locks+1letunlocklock=lock.locks<-lock.locks-1;iflock.locks=0thenmatchlock.forgottenwith|[]->()|forgotten->lock.forgotten<-[];List.iterforgetforgottenletforget{lock;t}=iflock.locks>0thenlock.forgotten<-t::lock.forgottenelseforgettletis_validt=lockt.lock;letresult=is_validt.tinunlockt.lock;resultletroot()=lett={t=root();lock={locks=0;forgotten=[]}}inGc.finaliseforgett;tletaftert=lockt.lock;lett'={t=aftert.t;lock=t.lock}inGc.finaliseforgett';unlockt.lock;t'letbeforet=lockt.lock;lett'={t=beforet.t;lock=t.lock}inGc.finaliseforgett';unlockt.lock;t'letsame_ordert1t2=same_ordert1.tt2.tletcomparet1t2=comparet1.tt2.tletcardinalt=cardinalt.tletunsafe_checktmsg=lockt.lock;unsafe_checkt.t("(Order_managed) "^msg);unlockt.lock