Different cps for await timer
This commit is contained in:
parent
3d4b6e0bbc
commit
3ea91ecd24
|
@ -16,11 +16,11 @@
|
|||
"packages": [
|
||||
"cps"
|
||||
],
|
||||
"path": "/nix/store/6z2yi8vcc2jc4j813dbfsbklx8dpybcn-source",
|
||||
"rev": "d870c8ba485bf7f81e3941d52afffe74fc926215",
|
||||
"sha256": "0kspz3ansl0ibf2rb2hjnjg076v96kbwr196vjxc76yk4bjp4zfa",
|
||||
"path": "/nix/store/m9vpcf3dq6z2h1xpi1vlw0ycxp91s5p7-source",
|
||||
"rev": "2a4d771a715ba45cfba3a82fa625ae7ad6591c8b",
|
||||
"sha256": "0c62k5wpq9z9mn8cd4rm8jjc4z0xmnak4piyj5dsfbyj6sbdw2bf",
|
||||
"srcDir": "",
|
||||
"url": "https://github.com/nim-works/cps/archive/d870c8ba485bf7f81e3941d52afffe74fc926215.tar.gz"
|
||||
"url": "https://github.com/nim-works/cps/archive/2a4d771a715ba45cfba3a82fa625ae7ad6591c8b.tar.gz"
|
||||
},
|
||||
{
|
||||
"method": "fetchzip",
|
||||
|
|
|
@ -59,6 +59,7 @@ when defined(linux):
|
|||
let driver = TimerDriver(facet: facet, target: cap)
|
||||
facet.onStop do ():
|
||||
for fd in driver.timers:
|
||||
unregister(FD fd)
|
||||
discard close(fd)
|
||||
driver
|
||||
|
||||
|
@ -69,28 +70,22 @@ when defined(linux):
|
|||
if deadline < result:
|
||||
result = deadline
|
||||
|
||||
proc await(driver: TimerDriver; deadline: float) {.turnWork.} =
|
||||
proc await(driver: TimerDriver; deadline: float) {.asyncio.} =
|
||||
let fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK or TFD_CLOEXEC)
|
||||
if fd < 0:
|
||||
raiseOSError(osLastError(), "failed to acquire timer descriptor")
|
||||
var
|
||||
now = clock_realtime()
|
||||
old: Itimerspec
|
||||
its = Itimerspec(it_value: deadline.toTimeSpec)
|
||||
if timerfd_settime(fd, TFD_TIMER_ABSTIME, its, old) < 0:
|
||||
raiseOSError(osLastError(), "failed to set timeout")
|
||||
driver.timers.incl(fd)
|
||||
if its.it_value < now:
|
||||
echo its.it_value.toFloat.fromUnixFloat, " is before ", now.toFloat.fromUnixFloat
|
||||
echo "await waits for ", now, " < ", its.it_value
|
||||
while now.toFloat < deadline:
|
||||
while clock_realtime().toFloat() < deadline:
|
||||
echo "waiting on timer descriptor"
|
||||
wait(FD fd, Read)
|
||||
now = clock_realtime()
|
||||
echo "wait returned on timer descriptor"
|
||||
if deadline in driver.deadlines:
|
||||
{.warning: "TODO: need to start a turn at driver.facet".}
|
||||
echo "driver publishes ", LaterThan(seconds: deadline)
|
||||
discard publish(driver.target, LaterThan(seconds: deadline))
|
||||
discard close(fd)
|
||||
driver.timers.excl(fd)
|
||||
|
@ -103,7 +98,7 @@ when defined(linux):
|
|||
let pat = inject(grab Observe(pattern: dropType LaterThan), {0: grabLit()})
|
||||
during(ds, pat) do (deadline: float):
|
||||
if change(driver.deadlines, deadline, +1) == cdAbsentToPresent:
|
||||
facet.queueWork(whelp await(driver, deadline))
|
||||
discard trampoline(whelp await(driver, deadline))
|
||||
do:
|
||||
discard change(driver.deadlines, deadline, -1, clamp = true)
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ import pkg/cps
|
|||
import ./[actors, patterns]
|
||||
|
||||
type
|
||||
DuringProc* = proc (f: Facet; a: Value; h: Handle): FacetProc {.gcsafe.}
|
||||
DuringProc* = proc (f: Facet; a: Value; h: Handle): FacetProc {.closure.}
|
||||
DuringActionKind = enum null, dead, act
|
||||
DuringAction = object
|
||||
case kind: DuringActionKind
|
||||
|
|
|
@ -22,18 +22,15 @@ let actor = bootActor("timer-test") do (facet: Facet):
|
|||
timers = facet.newDataspace()
|
||||
spawnTimerActor(timers)
|
||||
|
||||
timers.after(initDuration(seconds = 3)) do ():
|
||||
echo "slept three seconds"
|
||||
stopActor(facet)
|
||||
|
||||
timers.after(initDuration(seconds = 1)) do ():
|
||||
echo "slept one second"
|
||||
|
||||
timers.after(initDuration(seconds = 2)) do ():
|
||||
echo "slept 3 seconds"
|
||||
|
||||
timers.after(initDuration(seconds = 3)) do ():
|
||||
echo "slept six seconds"
|
||||
stopActor(facet)
|
||||
|
||||
timers.after(initDuration(seconds = 20)) do ():
|
||||
echo "slept twenty seconds"
|
||||
timers.after(initDuration(seconds = 2)) do ():
|
||||
echo "slept two seconds"
|
||||
|
||||
var progress = true
|
||||
while not actor.stopped:
|
||||
|
|
Loading…
Reference in New Issue