123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960(* This file is part of Lwt, released under the MIT license. See LICENSE.md for
details, or visit https://github.com/ocsigen/lwt/blob/master/LICENSE.md. *)exceptionOfftypeon_switch={mutablehooks:(unit->unitLwt.t)list;}typestate=|St_onofon_switch|St_offtypet={mutablestate:state}letcreate()={state=St_on{hooks=[]}}letis_onswitch=matchswitch.statewith|St_on_->true|St_off->falseletcheck=function|Some{state=St_off}->raiseOff|Some{state=St_on_}|None->()letadd_hookswitchhook=matchswitchwith|Some{state=St_onos}->os.hooks<-hook::os.hooks|Some{state=St_off}->raiseOff|None->()letadd_hook_or_execswitchhook=matchswitchwith|Some{state=St_onos}->os.hooks<-hook::os.hooks;Lwt.return_unit|Some{state=St_off}->hook()|None->Lwt.return_unitletturn_offswitch=matchswitch.statewith|St_on{hooks=hooks}->switch.state<-St_off;Lwt.join(List.map(funhook->Lwt.applyhook())hooks)|St_off->Lwt.return_unitletwith_switchfn=letswitch=create()inLwt.finalize(fun()->fnswitch)(fun()->turn_offswitch)