12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667(*
* Copyright (C) 2016 Docker Inc
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*)openLwt.InfixopenResultmoduletypeMAIN=sigvalrun_in_main:(unit->'aLwt.t)->'aendtype('request,'response)fn={request:'request;response:'responseLwt.u;}moduleMake(Main:MAIN)=structtype('request,'response)t={call:('request,'response)fnoption->unit;}letrechandle_requestsblocking_opcalls=matchMain.run_in_main(fun()->Lwt.catch(fun()->Lwt_stream.nextcalls>>=funx->Lwt.return(Somex))(fun_->Lwt.returnNone))with|None->()|Somer->letresponse=tryOk(blocking_opr.request)with|e->ErroreinMain.run_in_main(fun()->matchresponsewith|Okx->Lwt.wakeup_laterr.responsex;Lwt.return_unit|Errore->Lwt.wakeup_later_exnr.responsee;Lwt.return_unit);handle_requestsblocking_opcallsletcreateblocking_op=letcalls,call=Lwt_stream.create()inlet_th=Thread.create(handle_requestsblocking_op)callsin{call}letfntrequest=letthread,response=Lwt.task()inletcall={request;response}int.call(Somecall);threadletdestroyt=t.callNoneend