12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061(* Copyright (C) 2023--2024 Petter A. Urkedal <paurkedal@gmail.com>
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version, with the LGPL-3.0 Linking Exception.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* and the LGPL-3.0 Linking Exception along with this library. If not, see
* <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively.
*)moduleUnix=structtypefile_descr=Miou_unix.file_descrletwrap_fdffd=f(Miou_unix.of_file_descr~non_blocking:truefd)exceptionTimeoutletor_raise=functionOkv->v|Errorexn->raiseexnletpoll~stdenv:()?(read=false)?(write=false)?timeoutfd=letfn()=matchread,writewith|false,false->(false,false,false)|true,true->letreader=Miou.async@@fun()->Miou_unix.(blocking_read(to_file_descrfd));(true,false,false)inletwriter=Miou.async@@fun()->Miou_unix.(blocking_write(to_file_descrfd));(false,true,false)inMiou.await_first[reader;writer]|>or_raise|true,false->Miou_unix.(blocking_read(to_file_descrfd));(true,false,false)|false,true->Miou_unix.(blocking_write(to_file_descrfd));(false,true,false)inmatchtimeoutwith|None->fn()|Somet->letsleep=Miou.async@@fun()->Miou_unix.sleept;raiseTimeoutinmatchMiou.await_first[sleep;Miou.asyncfn]with|Okv->v|ErrorTimeout->(false,false,true)|Errorexn->raiseexnendmodulePreemptive=structletdetachfx=letfn()=fxinletprm=ifMiou.Domain.available()>0thenMiou.callfnelseMiou.asyncfninMiou.await_exnprmletrun_in_mainfn=fn()end