37 lines
1.1 KiB
Nim
37 lines
1.1 KiB
Nim
# SPDX-FileCopyrightText: 2021 ☭ Emery Hemingway
|
|
# SPDX-License-Identifier: Unlicense
|
|
|
|
import std/[asyncdispatch, monotimes, times]
|
|
import preserves, preserves/records
|
|
import syndicate, syndicate/assertions
|
|
|
|
type TimeLaterThan* {.record: "TimeLaterThan".} = object
|
|
`deadline`*: Monotime
|
|
|
|
proc prsTimeLaterThan*(deadline: Preserve | Monotime): Preserve =
|
|
initRecord(symbol("TimeLaterThan"), deadline)
|
|
|
|
proc toPreserveHook*(time: Monotime): Preserve =
|
|
time.ticks.toPreserve
|
|
|
|
proc fromPreserveHook*(mt: var Monotime; p: Preserve): bool =
|
|
if p.kind == pkSignedInteger:
|
|
mt = cast[MonoTime]((p.int.int64,))
|
|
result = true
|
|
|
|
syndicate timerDriver:
|
|
|
|
spawn "timer":
|
|
during(observe(prsTimeLaterThan(?deadline))) do (deadline: MonoTime):
|
|
let
|
|
now = getMonoTime()
|
|
period = inMilliseconds(deadline - now)
|
|
if period > 0:
|
|
getCurrentFacet().beginExternalTask()
|
|
addTimer(period.int, oneshot = true) do (fd: AsyncFD) -> bool:
|
|
react: asserting: prsTimeLaterThan(deadline)
|
|
getCurrentFacet().endExternalTask()
|
|
true
|
|
else:
|
|
react: asserting: prsTimeLaterThan(deadline)
|