123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149[%%import"config.h"][%%ifdefinedPOLL_CONF_WIN32]letavailable=truemoduleFfi=structexternalepoll_create1:unit->Unix.file_descr="poll_stub_wepoll_create1"externalepoll_in:unit->int="poll_stub_epollin"externalepoll_rdhup:unit->int="poll_stub_epollrdhup"externalepoll_hup:unit->int="poll_stub_epollhup"externalepoll_err:unit->int="poll_stub_epollerr"externalepoll_pri:unit->int="poll_stub_epollpri"externalepoll_out:unit->int="poll_stub_epollout"externalepoll_oneshot:unit->int="poll_stub_epolloneshot"externalepoll_event_sizeof:unit->int="poll_stub_epoll_event_sizeout"externalepoll_ctl_add:Unix.file_descr->Unix.file_descr->int->unit="poll_stub_epoll_ctl_add"externalepoll_ctl_mod:Unix.file_descr->Unix.file_descr->int->unit="poll_stub_epoll_ctl_mod"externalepoll_ctl_del:Unix.file_descr->Unix.file_descr->unit="poll_stub_epoll_ctl_del"externalepoll_wait:Unix.file_descr->Bigstring.t->int->int="poll_stub_epoll_wait"externalepoll_iter_ready:Bigstring.t->int->(Unix.file_descr->int->unit)->unit="poll_stub_epoll_iter_ready"letepoll_event_sizeof=epoll_event_sizeof()letepoll_in=epoll_in()letepoll_rdhup=epoll_rdhup()letepoll_hup=epoll_hup()letepoll_err=epoll_err()letepoll_pri=epoll_pri()letepoll_out=epoll_out()letepoll_oneshot=epoll_oneshot()letflag_read=epoll_inlorepoll_rdhuplorepoll_huplorepoll_errlorepoll_priletflag_write=epoll_outlorepoll_huplorepoll_errendtypet={epoll_fd:Unix.file_descr;mutableready_events:int;events:Bigstring.t;mutableclosed:bool;flags:(Unix.file_descr,int)Hashtbl.t}letensure_opent=ift.closedthenfailwith"Attempting to use a closed epoll fd"letbackend=Backend.Wepollletcreate()={epoll_fd=Ffi.epoll_create1();ready_events=0;events=Bigstring.create(256*Ffi.epoll_event_sizeof);closed=false;flags=Hashtbl.create65536};;letcleart=ensure_opent;t.ready_events<-0;;letcloset=ifnott.closedthen(t.closed<-true;Unix.closet.epoll_fd);;letsettfdevent=ensure_opent;letcurrent_flags=Hashtbl.find_optt.flagsfdinletnew_flags=matchevent.Event.readable,event.Event.writablewith|false,false->None|true,false->Some(Ffi.epoll_oneshotlorFfi.flag_read)|false,true->Some(Ffi.epoll_oneshotlorFfi.flag_write)|true,true->SomeFfi.(epoll_oneshotlorflag_readlorflag_write)inmatchcurrent_flags,new_flagswith|None,None->()|None,Somef->Ffi.epoll_ctl_addt.epoll_fdfdf;Hashtbl.replacet.flagsfdf|Some_,None->Ffi.epoll_ctl_delt.epoll_fdfd;Hashtbl.removet.flagsfd|Some_,Someb->Ffi.epoll_ctl_modt.epoll_fdfdb;Hashtbl.replacet.flagsfdb;;letwaitttimeout=lettimeout=matchtimeoutwith|Timeout.Immediate->0|Never->-1|Afterx->Int64.to_int(Int64.divx1_000_000L)inensure_opent;t.ready_events<-0;t.ready_events<-Ffi.epoll_waitt.epoll_fdt.eventstimeout;ift.ready_events=0then`Timeoutelse`Ok;;letiter_callbackffdflags=letreadable=flagslandFfi.flag_read<>0inletwritable=flagslandFfi.flag_write<>0inffd{Event.readable;writable};;letiter_readyt~f=ensure_opent;letcallback=iter_callbackfinFfi.epoll_iter_readyt.eventst.ready_eventscallback;;[%%else]includeEmpty_pollletavailable=false[%%endif]