123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* Copyright (c) 2019 Nomadic Labs <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)openLwt.Infixtypestate=|Waiting|Canceling|Canceled|Canceled_with_exception(* A canceler is made of the following elements:
- One [state] field
- Two [Lwt_condition]s to resolve promises waiting on some state changes
- One [callbacks] field to hold multiple callbacks
*)typet={mutablestate:state;canceling:unitLwt_condition.t;canceled:(unit,exnlist)resultLwt_condition.t;mutablecallbacks:(unit->unitLwt.t)list;}(* preallocated values *)letlwt_ok_unit=Lwt.return(Ok())letlwt_error_nil=Lwt.return(Error[])letcreate()={state=Waiting;canceled=Lwt_condition.create();canceling=Lwt_condition.create();callbacks=[];}letcancelt=matcht.statewith|Canceled_with_exception->lwt_error_nil|Canceled->lwt_ok_unit|Canceling->Lwt_condition.waitt.canceled|Waiting->t.state<-Canceling;Lwt_condition.broadcastt.canceling();letcallbacks=t.callbacksin(* Reset the [callbacks] field's content can be collected. *)t.callbacks<-[];Lwt_list.fold_right_s(funcbexcs->Lwt.catch(fun()->cb()>>=fun()->Lwt.returnexcs)(funexc->Lwt.return(exc::excs)))callbacks[]>>=function|[]->(* no exceptions raised by any of the callbacks *)assert(t.state=Canceling);t.state<-Canceled;Lwt_condition.broadcastt.canceled(Ok());lwt_ok_unit|(_::_)asexcs->(* Some exceptions *)assert(t.state=Canceling);t.state<-Canceled_with_exception;(* NOTE: we return the exception to the first caller (the caller
that triggered the state change to [Canceling]). But we return an
empty list of exception to the other callers (the callers that
attached via [t.canceled]). This is intended to distribute the
responsibility to deal with exception to one single caller. More
information is available in the interface. *)Lwt_condition.broadcastt.canceled(Error[]);Lwt.return(Errorexcs)leton_cancelstcb=matchst.statewith|Canceled_with_exception|Canceled|Canceling->()|Waiting->letcbs=st.callbacksinst.callbacks<-cb::cbsletcancelingst=matchst.statewith|Waiting->false|Canceled_with_exception|Canceled|Canceling->trueletwhen_cancelingst=matchst.statewith|Waiting->Lwt_condition.waitst.canceling|Canceling|Canceled|Canceled_with_exception->Lwt.return_unitletcanceledst=matchst.statewith|Waiting|Canceling->false|Canceled_with_exception|Canceled->trueletwhen_canceledst=matchst.statewith|Waiting|Canceling->Lwt_condition.waitst.canceled|Canceled->lwt_ok_unit|Canceled_with_exception->lwt_error_nilletstatet=t.state