12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970(*
* Copyright (c) 2012 Balraj Singh <bs375@cl.cam.ac.uk>
*
* 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.Infixletsrc=Logs.Src.create"tcp.tcptimer"~doc:"Mirage TCP Tcptimer module"moduleLog=(valLogs.src_logsrc:Logs.LOG)typetime=int64typetr=|Stoptimer|ContinueofSequence.t|ContinueSetPeriodof(time*Sequence.t)typet={expire:(Sequence.t->trLwt.t);mutableperiod_ns:time;mutablerunning:bool;}lett~period_ns~expire=letrunning=falsein{period_ns;expire;running}lettimerloopts=Log.debug(funf->f"timerloop");Stats.incr_timer();letrecauxts=Log.debug(funf->f"timerloop: sleeping for %Lu ns"t.period_ns);Mirage_sleep.nst.period_ns>>=fun()->t.expires>>=function|Stoptimer->Stats.decr_timer();t.running<-false;Log.debug(funf->f"timerloop: stoptimer");Lwt.return_unit|Continued->Log.debug(funf->f"timerloop: continuer");auxtd|ContinueSetPeriod(p,d)->Log.debug(funf->f"timerloop: continuesetperiod (new period: %Lu ns)"p);t.period_ns<-p;auxtdinauxtsletperiod_nst=t.period_nsletstartt?(p=(period_nst))s=ifnott.runningthenbegint.period_ns<-p;t.running<-true;Lwt.async(fun()->timerloopts);Lwt.return_unitendelseLwt.return_unit