123456789101112131415161718192021222324252627282930313233343536373839404142434445moduleKey=structtypet=Optint.Int63.tletcompare=Optint.Int63.compareendmoduleJob=structtypet={time:Mtime.t;thread:unitSuspended.t;}letcompareab=Mtime.comparea.timeb.timeendmoduleQ=Psq.Make(Key)(Job)typet={mutablesleep_queue:Q.t;mutablenext_id:Optint.Int63.t;}letcreate()={sleep_queue=Q.empty;next_id=Optint.Int63.zero}letaddttimethread=letid=t.next_idint.next_id<-Optint.Int63.succt.next_id;letsleeper={Job.time;thread}int.sleep_queue<-Q.addidsleepert.sleep_queue;idletremovetid=t.sleep_queue<-Q.removeidt.sleep_queueletpopt~now=matchQ.mint.sleep_queuewith|Some(_,{Job.time;thread})whentime<=now->ifEio.Private.Fiber_context.clear_cancel_fnthread.fiberthen(t.sleep_queue<-Option.get(Q.restt.sleep_queue);`Duethread)else((* This shouldn't happen, since any cancellation will happen in the same domain as the [pop]. *)assertfalse)|Some(_,{Job.time;_})->`Wait_untiltime|None->`Nothing