123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609openBaseopenImportmoduleCapsule=Capsule.Primmodule[@inline]Make(Prim:Mutex_intf.Prim)(C:Lock_common_intf.Capability_with_of_await)=structtype'kt=Prim.tletcreate?padded_key=Prim.create?padded()moduleGuard=structtype'kmutex='kttype'kinner={mutex:'kmutex@@many;mutableshould_poison:bool[@atomic]}type'kt={inner:'kinner@@aliasedcontendedportable}[@@unboxed]letpoison_if_locked({mutex;_}asinner)=ifAtomic.Loc.get[%atomic.locinner.should_poison]thenPrim.poisonmutex;;letcreate:'kmutex->'kt@unique=funmutex->lett={mutex;should_poison=true}inStdlib.Gc.Safe.finalisepoison_if_lockedt;{inner=t};;letwith_key:'kt@unique->f:('kCapsule.Key.t@unique->#('a*'kCapsule.Key.t)@onceunique)@localonce->'a*'kt@onceunique=funt~f->matchf(Capsule.Key.unsafe_mk())with|#(res,_key)->res,t|exceptionexn->Prim.poison_and_reraiseexnt.inner.mutex;;letwith_password:'kt@unique->f:('kCapsule.Password.t@local->'a@unique)@localonce->'a*'kt@unique=funt~f->let{many=a},t=with_keyt~f:(Capsule.Key.with_password~f:(funpassword->{many=fpassword})[@nontail])ina,t;;letaccess:'kt@unique->f:('kCapsule.Access.t->'a@contendedonceportableunique)@localonceportable->'a*'kt@contendedonceportableunique=funt~f->let{contended={portable=a}},t=with_keyt~f:(funkey->let#(a,key)=Capsule.Key.accesskey~f:(funaccess->{portable=faccess})in#({contended=a},key))ina,t;;letrelease{inner={mutex;_}asinner}=Atomic.Loc.set[%atomic.locinner.should_poison]false;(* Make sure the stack root for [inner] stays alive at least this long, to make sure
that if its finalizer runs, it sees [should_poison] set to [false]. *)let_:_=(Sys.opaque_identity[@modecontended])innerinPrim.releasemutex;;letpoison:'kt@unique->'kCapsule.Key.t@unique=fun{inner={mutex;_}asinner}->(* Not technically releasing, but setting this value to [false] prevents calling
[poison] again in the finalizer, which is moderately more efficient. *)Atomic.Loc.set[%atomic.locinner.should_poison]false;Prim.poisonmutex;Capsule.Key.unsafe_mk();;letis_poisoning{inner}=Atomic.Loc.get[%atomic.locinner.should_poison]endletacquirewt=Prim.acquirewt;Guard.createt;;lettry_acquiret:_Or_would_block.t=matchPrim.try_acquiretwith|true->Acquired(Guard.createt)|false->Would_block;;letacquire_or_cancel:C.t@local->Cancellation.t@local->'kt->'kGuard.tOr_canceled.t@unique=funwct->matchPrim.acquire_or_cancel(C.unsafe_to_awaitw)ctwith|Canceled->Canceled|Completed()->Completed(Guard.createt);;moduleUnsafe=struct(* SAFETY:
Since these functions unlock the mutex on uncaught exceptions rather than
poisoning, they are unsound: it's possible to stash the provided key in a raised
exception, which leaks the key. We define them internally to implement functions
like [with_access] and [with_password], but intentionally don't expose them in the
interface.
*)[%%template[@@@mode.defaultl=(local,global)]let[@inline]with_key:type(a:value_or_null)k.C.t@local->kt@local->f:(kCapsule.Key.t@unique->#(a*kCapsule.Key.t)@lonceunique)@localonce->a@lonceunique=funwt~f->Prim.acquire(C.unsafe_to_awaitw)t;match[@exclave_if_locall~reasons:[May_return_local]]f(Capsule.Key.unsafe_mk())with|#(res,_key)->Prim.releaset;res|exceptionexn->Prim.release_and_reraiseexnt;;let[@inline]try_with_key:type(a:value_or_null)k.kt@local->f:(kCapsule.Key.t@unique->#(a*kCapsule.Key.t)@lonceunique)@localonce->aOr_would_block.t@lonceunique=funt~f->ifPrim.try_acquiretthen(match[@exclave_if_locall~reasons:[May_return_local]]f(Capsule.Key.unsafe_mk())with|#(res,_key)->Prim.releaset;Acquiredres|exceptionexn->Prim.release_and_reraiseexnt)elseWould_block;;let[@inline]with_key_or_cancel:C.t@local->Cancellation.t@local->'kt@local->f:('kCapsule.Key.t@unique->#('a*'kCapsule.Key.t)@lonceunique)@localonce->'aOr_canceled.t@lonceunique=funwct~f->matchPrim.acquire_or_cancel(C.unsafe_to_awaitw)ctwith|Canceled->Canceled|Completed()->(match[@exclave_if_locall~reasons:[May_return_local]]f(Capsule.Key.unsafe_mk())with|#(res,_key)->Prim.releaset;Completedres|exceptionexn->Prim.release_and_reraiseexnt);;]end[%%template[@@@mode.defaultl=(local,global)]let[@inline]with_key_poisoning:C.t@local->'kt@local->f:('kCapsule.Key.t@unique->#('a*'kCapsule.Key.t)@lonceunique)@localonce->'a@lonceunique=funwt~f->(Prim.acquire(C.unsafe_to_awaitw)t;matchf(Capsule.Key.unsafe_mk())with|#(res,_key)->Prim.releaset;res|exceptionexn->Prim.poison_and_reraiseexnt)[@exclave_if_locall~reasons:[May_return_local]];;let[@inline]try_with_key_poisoning:'kt@local->f:('kCapsule.Key.t@unique->#('a*'kCapsule.Key.t)@lonceunique)@localonce->'aOr_would_block.t@lonceunique=funt~f->(ifPrim.try_acquiretthen(matchf(Capsule.Key.unsafe_mk())with|#(res,_key)->Prim.releaset;Acquiredres|exceptionexn->Prim.poison_and_reraiseexnt)elseWould_block)[@exclave_if_locall~reasons:[May_return_local]];;let[@inline]with_key_or_cancel_poisoning:C.t@local->Cancellation.t@local->'kt@local->f:('kCapsule.Key.t@unique->#('a*'kCapsule.Key.t)@lonceunique)@localonce->'aOr_canceled.t@lonceunique=funwct~f->match[@exclave_if_locall~reasons:[May_return_local]]Prim.acquire_or_cancel(C.unsafe_to_awaitw)ctwith|Canceled->Canceled|Completed()->(matchf(Capsule.Key.unsafe_mk())with|#(res,_key)->Prim.releaset;Completedres|exceptionexn->Prim.poison_and_reraiseexnt);;letwith_access_poisoningwt~f=((with_key_poisoning[@model])wt~f:(funkey->(let#(a,key)=(Capsule.Key.access[@model])key~f:(funaccess->{portable=faccess}[@exclave_if_locall~reasons:[May_return_local]])in#({contended=a},key))[@exclave_if_locall~reasons:[May_return_local]])).contended.portable[@exclave_if_locall~reasons:[May_return_local]];;lettry_with_access_poisoningt~f:_Or_would_block.t@l=match[@exclave_if_locall~reasons:[May_return_local]](try_with_key_poisoning[@model])t~f:(funkey->(let#(a,key)=(Capsule.Key.access[@model])key~f:(funaccess->{portable=faccess}[@exclave_if_locall~reasons:[May_return_local]])in#({contended=a},key))[@exclave_if_locall~reasons:[May_return_local]])with|Would_block->Would_block|Acquired{contended={portable=a}}->Acquireda;;letwith_accesswt~f=((Unsafe.with_key[@model])wt~f:(funkey->(let#(a,key)=(Capsule.Key.access[@model])key~f:(funaccess->{portable=faccess}[@exclave_if_locall~reasons:[May_return_local]])in#({contended=a},key))[@exclave_if_locall~reasons:[May_return_local]])).contended.portable[@exclave_if_locall~reasons:[May_return_local]];;lettry_with_accesst~f:_Or_would_block.t@l=match[@exclave_if_locall~reasons:[May_return_local]](Unsafe.try_with_key[@model])t~f:(funkey->(let#(a,key)=(Capsule.Key.access[@model])key~f:(funaccess->{portable=faccess}[@exclave_if_locall~reasons:[May_return_local]])in#({contended=a},key))[@exclave_if_locall~reasons:[May_return_local]])with|Would_block->Would_block|Acquired{contended={portable=a}}->Acquireda;;letwith_access_or_cancel_poisoningwct~f:_Or_canceled.t=match[@exclave_if_locall~reasons:[May_return_local]](with_key_or_cancel_poisoning[@model])wct~f:(funkey->(let#(a,key)=(Capsule.Key.access[@model])key~f:(funaccess->{portable=faccess}[@exclave_if_locall~reasons:[May_return_local]])in#({contended=a},key))[@exclave_if_locall~reasons:[May_return_local]])with|Canceled->Canceled|Completed{contended={portable=a}}->Completeda;;letwith_access_or_cancelwct~f:_Or_canceled.t=match[@exclave_if_locall~reasons:[May_return_local]](Unsafe.with_key_or_cancel[@model])wct~f:(funkey->(let#(a,key)=(Capsule.Key.access[@model])key~f:(funaccess->{portable=faccess}[@exclave_if_locall~reasons:[May_return_local]])in#({contended=a},key))[@exclave_if_locall~reasons:[May_return_local]])with|Canceled->Canceled|Completed{contended={portable=a}}->Completeda;;]letwith_password_poisoningwt~f=(with_key_poisoningwt~f:(funkey->Capsule.Key.with_passwordkey~f:(funpassword->{many=fpassword})[@nontail])).many;;lettry_with_password_poisoningt~f:_Or_would_block.t=matchtry_with_key_poisoningt~f:(funkey->Capsule.Key.with_passwordkey~f:(funpassword->{many=fpassword})[@nontail])with|Would_block->Would_block|Acquired{many=a}->Acquireda;;letwith_passwordwt~f=(Unsafe.with_keywt~f:(funkey->Capsule.Key.with_passwordkey~f:(funpassword->{many=fpassword})[@nontail])).many;;lettry_with_passwordt~f:_Or_would_block.t=matchUnsafe.try_with_keyt~f:(funkey->Capsule.Key.with_passwordkey~f:(funpassword->{many=fpassword})[@nontail])with|Would_block->Would_block|Acquired{many=a}->Acquireda;;letwith_password_or_cancel_poisoningwct~f:_Or_canceled.t=matchwith_key_or_cancel_poisoningwct~f:(funkey->Capsule.Key.with_passwordkey~f:(funpassword->{many=fpassword})[@nontail])with|Canceled->Canceled|Completed{many=a}->Completeda;;letwith_password_or_cancelwct~f:_Or_canceled.t=matchUnsafe.with_key_or_cancelwct~f:(funkey->Capsule.Key.with_passwordkey~f:(funpassword->{many=fpassword})[@nontail])with|Canceled->Canceled|Completed{many=a}->Completeda;;letrelease_temporarily:C.t@local->'kt@local->'kCapsule.Key.t@unique->f:(unit->'a@unique)@localonce->#('a*'kCapsule.Key.t)@unique=funwtk~f->Prim.releaset;letres=f()inPrim.acquire(C.unsafe_to_awaitw)t;#(res,k);;letrelease_temporarily_or_cancel:('a:value_or_null).C.t@local->Cancellation.t@local->'kt@local->'kCapsule.Key.t@unique->f:(unit->'a@unique)@localonce->(#('a*'kCapsule.Key.t)Or_canceled.t[@kindvalue_or_null&void])@unique=funwctk~f->Prim.releaset;letres=f()inmatchPrim.acquire_or_cancel(C.unsafe_to_awaitw)ctwith|Canceled->Canceled|Completed()->Completed#(res,k);;letacquire_and_poison:C.t@local->'kt@local->'kCapsule.Key.t@unique=funwt->Prim.acquire(C.unsafe_to_awaitw)t;Prim.poisont;Capsule.Key.unsafe_mk();;letacquire_and_poison_or_cancel:C.t@local->Cancellation.t@local->'kt@local->('kCapsule.Key.tOr_canceled.t[@kindvoid])@unique=funwct->matchPrim.acquire_or_cancel(C.unsafe_to_awaitw)ctwith|Canceled->Canceled|Completed()->Prim.poisont;Completed(Capsule.Key.unsafe_mk());;letpoison_unacquired:'kt@local->unit=funt->Prim.poisontletis_poisoned=Prim.is_poisonedletis_locked=Prim.is_lockedletpoisontkey=Prim.poisont;key;;moduleCondition=Condition.Make(structtypenonrec'kt='ktletunsafe_acquirewt=Prim.acquirewtletunsafe_releaset=Prim.releasetend)[%%template[@@@mode.defaultl=(global,local)]let[@inline]with_key_and_condition_wait_poisoning:type(a:value_or_null)k.Await.t@local->kt@local->f:(kCondition.Wait.t@local->kCapsule.Key.t@unique->#(a*kCapsule.Key.t)@lonceunique)@localonce->a@lonceunique=funwt~f->(Prim.acquirewt;(Condition.with_wait[@model])wt(Capsule.Key.unsafe_mk():kCapsule.Key.t)(funcwkey->match[@exclave_if_locall~reasons:[May_return_local]]fcwkeywith|#(res,_key)->(* If the caller has been able to give a key back to us, the lock must be held *)assert%debug(Condition.lock_is_heldcw);Prim.releaset;res|exceptionexn->ifCondition.lock_is_heldcwthenPrim.poison_and_reraiseexntelse(letbt=Backtrace.Exn.most_recent()inExn.raise_with_original_backtraceexnbt))[@nontail])[@exclave_if_locall~reasons:[May_return_local]];;letwith_key_and_condition_wait_or_cancel_poisoning:type(a:value_or_null)k.Await.t@local->Cancellation.t@local->kt@local->f:(kCondition.Wait.t@local->kCapsule.Key.t@unique->#(a*kCapsule.Key.t)@lonceunique)@localonce->aOr_canceled.t@lonceunique=funwct~f->match[@exclave_if_locall~reasons:[May_return_local]]Prim.acquire_or_cancelwctwith|Canceled->Canceled|Completed()->(Condition.with_wait[@model])wt(Capsule.Key.unsafe_mk():kCapsule.Key.t)(funcwkey->match[@exclave_if_locall~reasons:[May_return_local]]fcwkeywith|#(res,_key)->(* If the caller has been able to give a key back to us, the lock must be held *)assert%debug(Condition.lock_is_heldcw);Prim.releaset;Or_canceled.Completedres|exceptionexn->ifCondition.lock_is_heldcwthenPrim.poison_and_reraiseexntelse(letbt=Backtrace.Exn.most_recent()inExn.raise_with_original_backtraceexnbt))[@nontail];;]moduleFor_testing=Prim.For_testingendmodule[@inline]Make_sync(Prim:Mutex_intf.Prim):Mutex_intf.Syncwithtype'kt=Prim.t=structincludeMake(Prim)(Lock_common.Sync)(* The following definitions shadow the ones defined by Make, but also pass the Sync.t
to the callback. We can do this here because the actual implementation doesn't
require an unyielding callback, but the signature exposes these functions as
requiring unyielding callbacks, which is an important safety property. *)[%%template[@@@mode.defaultl=(local,global)]letwith_accessst~f=(with_access[@model])st~f:(funaccess->fsaccess[@nontail][@exclave_if_locall~reasons:[May_return_local]])[@nontail][@exclave_if_locall~reasons:[May_return_local]];;let[@inline]with_access_poisoningst~f=(with_access_poisoning[@model])st~f:(funaccess->fsaccess[@nontail][@exclave_if_locall~reasons:[May_return_local]])[@exclave_if_locall~reasons:[May_return_local]][@nontail];;let[@inline]with_access_or_cancelsct~f=(with_access_or_cancel[@model])sct~f:(funaccess->fsaccess[@nontail][@exclave_if_locall~reasons:[May_return_local]])[@exclave_if_locall~reasons:[May_return_local]][@nontail];;let[@inline]with_access_or_cancel_poisoningsct~f=(with_access_or_cancel_poisoning[@model])sct~f:(funaccess->fsaccess[@nontail][@exclave_if_locall~reasons:[May_return_local]])[@exclave_if_locall~reasons:[May_return_local]][@nontail];;]let[@inline]with_passwordst~f=with_passwordst~f:(funpassword->fspassword[@nontail])[@nontail];;let[@inline]with_password_poisoningst~f=with_password_poisoningst~f:(funpassword->fspassword[@nontail])[@nontail];;let[@inline]with_password_or_cancelsct~f=with_password_or_cancelsct~f:(funpassword->fspassword[@nontail])[@nontail];;let[@inline]with_password_or_cancel_poisoningsct~f=with_password_or_cancel_poisoningsct~f:(funpassword->fspassword[@nontail])[@nontail];;[%%template[@@@mode.defaultl=(global,local)]let[@inline]with_key_poisoningst~f=(with_key_poisoning[@model])st~f:(funkey->fskey[@nontail][@exclave_if_locall~reasons:[May_return_local]])[@nontail][@exclave_if_locall~reasons:[May_return_local]];;let[@inline]with_key_or_cancel_poisoningsct~f=(with_key_or_cancel_poisoning[@model])sct~f:(funkey->fskey[@nontail][@exclave_if_locall~reasons:[May_return_local]])[@nontail][@exclave_if_locall~reasons:[May_return_local]];;let[@inline]with_key_and_condition_wait_poisoningwt~f=(with_key_and_condition_wait_poisoning[@model])wt~f:(funcwkey->f(Await.syncw)cwkey[@nontail][@exclave_if_locall~reasons:[May_return_local]])[@nontail][@exclave_if_locall~reasons:[May_return_local]];;let[@inline]with_key_and_condition_wait_or_cancel_poisoningwct~f=(with_key_and_condition_wait_or_cancel_poisoning[@model])wct~f:(funcwkey->f(Await.syncw)cwkey[@nontail][@exclave_if_locall~reasons:[May_return_local]])[@nontail][@exclave_if_locall~reasons:[May_return_local]];;]endmodule[@inline]Make_await(Prim:Mutex_intf.Prim):Mutex_intf.Awaitwithtype'kt=Prim.t=Make(Prim)(Lock_common.Await)