123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139(*
* Copyright (c) 2023 Christiano Haesbaert <haesbaert@haesbaert.org>
* SPDX-License-Identifier: ISC
* Copyright (c) 2025 Romain Calascibetta <romain.calascibetta@gmail.com>
*
* 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.
*)typebuffer=(char,Bigarray.int8_unsigned_elt,Bigarray.c_layout)Bigarray.Array1.tmoduleConfig=Miou_poll_configmoduleC=structexternalpoll:buffer->int->int->int="miou_unix_poll"externalppoll:buffer->int->int64->intlist->int="miou_unix_ppoll"externalset_index:buffer->int->Unix.file_descr->int->unit="miou_unix_poll_set_index"[@@noalloc]externalinit:buffer->int->unit="miou_unix_poll_init"externalget_revents:buffer->int->int="miou_unix_poll_get_revents"[@@noalloc]externalget_fd:buffer->int->Unix.file_descr="miou_unix_poll_get_fd"[@@noalloc]externalmax_open_files:unit->int="miou_unix_poll_max_open_files"[@@noalloc]endmoduleFlags=structtypet=intletpollin=Config.pollinletpollpri=Config.pollpriletpollout=Config.polloutletpollerr=Config.pollerrletpollhup=Config.pollhupletpollnval=Config.pollnvalletempty=0let(+)=(lor)letmemab=alandb!=0letto_int=Fun.idletof_int=Fun.idendlethas_ppoll=Config.has_ppollletinvalid_fd:Unix.file_descr=Obj.magic(-1)typet={buffer:buffer;maxfds:int}typepoll_timeout=Infinite|No_wait|Millisecondsofintletpolltusedtimeout=lettimeout=matchtimeoutwithInfinite->-1|No_wait->0|Millisecondsms->msinC.pollt.bufferusedtimeouttypeppoll_timeout=Infinite|No_wait|Nanosecondsofint64letppolltusedtimeoutsigmask=lettimeout=matchtimeoutwith|Infinite->Int64.minus_one|No_wait->Int64.zero|Nanosecondstimo->timoinC.ppollt.bufferusedtimeoutsigmaskletppoll_or_polltused(timeout:ppoll_timeout)=ifhas_ppollthenppolltusedtimeout[]elselettimeout:poll_timeout=matchtimeoutwith|Infinite->Infinite|No_wait->No_wait|Nanosecondstimo_ns->MillisecondsInt64.(to_int(div(addtimo_ns999_999L)1_000_000L))inpolltusedtimeoutletguard_indextindex=ifindex>=t.maxfds||index<0theninvalid_arg"Miou_poll: index out of bounds"letset_indextindexfdevents=guard_indextindex;C.set_indext.bufferindexfdeventsletinvalidate_indextindex=guard_indextindex;C.set_indext.bufferindexinvalid_fd0letget_reventstindex=guard_indextindex;C.get_reventst.bufferindexletget_fdtindex=guard_indextindex;C.get_fdt.bufferindexletmax_open_files()=matchC.max_open_files()with|-1->failwith"Miou_poll.max_open_files"|nwhenn>524288->524288|n->nletcreate?(maxfds=max_open_files())()=letlen=maxfds*Config.sizeof_pollfdinletbuffer=Bigarray.(Array1.createcharc_layoutlen)inlett={buffer;maxfds}inC.initbuffermaxfds;tletmaxfds{maxfds;_}=maxfdsletitertnready(fn:int->Unix.file_descr->Flags.t->unit)=letrecgoindexnready=ifnready>0thenbeginletfd=get_fdtindexinletrevents=get_reventstindexiniffd<>invalid_fd&&revents!=0thenbeginfnindexfdrevents;go(index+1)(nready-1)endelsego(index+1)nreadyendingo0nready