2021-09-01 11:47:21 +00:00
|
|
|
# SPDX-FileCopyrightText: 2021 ☭ Emery Hemingway
|
|
|
|
# SPDX-License-Identifier: Unlicense
|
2021-07-08 21:46:21 +00:00
|
|
|
|
2021-07-09 15:45:44 +00:00
|
|
|
import std/[asyncdispatch, monotimes, times]
|
2021-07-08 21:46:21 +00:00
|
|
|
import preserves, preserves/records
|
2021-09-01 11:44:28 +00:00
|
|
|
import syndicate, syndicate/assertions
|
2021-07-08 21:46:21 +00:00
|
|
|
|
2021-08-28 08:36:30 +00:00
|
|
|
type TimeLaterThan* {.record: "TimeLaterThan".} = object
|
|
|
|
`deadline`*: Monotime
|
|
|
|
|
|
|
|
proc prsTimeLaterThan*(deadline: Preserve | Monotime): Preserve =
|
|
|
|
initRecord(symbol("TimeLaterThan"), deadline)
|
2021-07-08 21:46:21 +00:00
|
|
|
|
2021-07-09 15:45:44 +00:00
|
|
|
proc toPreserveHook*(time: Monotime): Preserve =
|
2021-08-27 20:03:02 +00:00
|
|
|
time.ticks.toPreserve
|
2021-07-08 21:46:21 +00:00
|
|
|
|
2021-08-27 20:03:02 +00:00
|
|
|
proc fromPreserveHook*(mt: var Monotime; p: Preserve): bool =
|
|
|
|
if p.kind == pkSignedInteger:
|
|
|
|
mt = cast[MonoTime]((p.int.int64,))
|
|
|
|
result = true
|
2021-07-08 21:46:21 +00:00
|
|
|
|
|
|
|
syndicate timerDriver:
|
|
|
|
|
|
|
|
spawn "timer":
|
2021-09-01 11:44:28 +00:00
|
|
|
during(observe(prsTimeLaterThan(?deadline))) do (deadline: MonoTime):
|
2021-07-08 21:46:21 +00:00
|
|
|
let
|
2021-07-09 15:45:44 +00:00
|
|
|
now = getMonoTime()
|
2021-07-08 21:46:21 +00:00
|
|
|
period = inMilliseconds(deadline - now)
|
|
|
|
if period > 0:
|
|
|
|
getCurrentFacet().beginExternalTask()
|
|
|
|
addTimer(period.int, oneshot = true) do (fd: AsyncFD) -> bool:
|
2021-08-28 08:36:30 +00:00
|
|
|
react: asserting: prsTimeLaterThan(deadline)
|
2021-07-08 21:46:21 +00:00
|
|
|
getCurrentFacet().endExternalTask()
|
|
|
|
true
|
|
|
|
else:
|
2021-08-28 08:36:30 +00:00
|
|
|
react: asserting: prsTimeLaterThan(deadline)
|