123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109(*
* Copyright (c) 2010-2011 Anil Madhavapeddy <anil@recoil.org>
*
* 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.Infixexternalevtchn_get_nr_events:unit->int="mirage_xen_evtchn_get_nr_events"[@@noalloc]externalevtchn_test_and_clear:int->bool="mirage_xen_evtchn_test_and_clear"[@@noalloc]letnr_events=evtchn_get_nr_events()letevent_cb=Array.initnr_events(fun_->Lwt_dllist.create())(* The high-level interface creates one counter per event channel port.
Every time the system receives a notification it increments the counter.
Threads which have blocked elsewhere call 'after' which blocks until
the stored counter is greater than the value they have already -- so
if an event comes in between calls then it will not be lost.
In the high-level interface it's almost impossible to miss an event.
The only way you can miss is if you block while your port's counter
wraps. Arguably if you have failed to notice 2bn (32-bit) wakeups then
you have bigger problems. *)typeevent=intletprogram_start=min_inttypeport={mutablecounter:event;c:unitLwt_condition.t}letports=Array.initnr_events(fun_port->{counter=program_start;c=Lwt_condition.create()})letdump()=Printf.printf"Number of received event channel events:\n";fori=0tonr_events-1doifports.(i).counter<>program_startthenPrintf.printf"port %d: %d\n%!"i(ports.(i).counter-program_start)doneletafterevtchncounter=letport=Eventchn.to_intevtchninletrecloop()=ifports.(port).counter<=counter&&Eventchn.is_validevtchnthenLwt_condition.waitports.(port).c>>=fun()->loop()elseLwt.return_unitinloop()>>=fun()->ifEventchn.is_validevtchnthenLwt.returnports.(port).counterelseLwt.failGeneration.Invalid(* Low-level interface *)(* Block waiting for an event to occur on a particular port. Note
if the event came in when we weren't looking then it is lost and
we will block forever. *)letwaitevtchn=ifEventchn.is_validevtchnthen(letport=Eventchn.to_intevtchninletth,u=Lwt.task()inletnode=Lwt_dllist.add_luevent_cb.(port)inLwt.on_cancelth(fun_->Lwt_dllist.removenode);th)else(Printf.printf"Activations.wait %d: Generation.Invalid\n%!"(Eventchn.to_intevtchn);Lwt.failGeneration.Invalid)(* Go through the event mask and activate any events, potentially spawning
new threads *)letrun_=forport=0tonr_events-1doifevtchn_test_and_clearportthen(Lwt_dllist.iter_node_l(funnode->letu=Lwt_dllist.getnodeinLwt_dllist.removenode;Lwt.wakeup_lateru())event_cb.(port);ports.(port).counter<-ports.(port).counter+1;Lwt_condition.broadcastports.(port).c())done(* Note, this should be run *after* Generation.resume *)letresume()=forport=0tonr_events-1doLwt_dllist.iter_node_l(funnode->letu=Lwt_dllist.getnodeinLwt_dllist.removenode;Lwt.wakeup_later_exnuGeneration.Invalid)event_cb.(port);Lwt_condition.broadcastports.(port).c()done