Use monotonic time for the timer driver

Use the arbitrary monotonic timer which supports higher resolution.
This avoids Epochalypse problems (even if we are going to be mostly
dead by 2038).
This commit is contained in:
Emery Hemingway 2021-07-09 17:45:44 +02:00
parent 6920c26183
commit 9b97f1135e
3 changed files with 12 additions and 11 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
tests/test_box_and_client
tests/test_dsl
tests/test_timer

View File

@ -1,25 +1,25 @@
# SPDX-License-Identifier: ISC
import std/[asyncdispatch, times]
import std/[asyncdispatch, monotimes, times]
import preserves, preserves/records
import syndicate
const TimeLaterThan* = RecordClass(label: symbol"TimeLaterThan", arity: 1)
proc toPreserveHook*(time: Time): Preserve =
%time.toUnixFloat
proc toPreserveHook*(time: Monotime): Preserve =
%time.ticks
proc fromPreserveHook*(result: var Time; p: Preserve) =
if p.kind != pkDouble:
proc fromPreserveHook*(result: var Monotime; p: Preserve) =
if p.kind != pkSignedInteger:
raise newException(ValueError, "not a preserved time: " & $p)
result = fromUnixFloat(p.double)
result = cast[MonoTime]((p.int.int64,))
syndicate timerDriver:
spawn "timer":
during(Observe % (TimeLaterThan % `?*`)) do (deadline: Time):
during(Observe % (TimeLaterThan % `?*`)) do (deadline: MonoTime):
let
now = getTime()
now = getMonoTime()
period = inMilliseconds(deadline - now)
if period > 0:
getCurrentFacet().beginExternalTask()

View File

@ -1,6 +1,6 @@
# SPDX-License-Identifier: ISC
import std/[asyncdispatch, times]
import std/[asyncdispatch, monotimes, times]
import preserves, preserves/records
import syndicate
@ -10,14 +10,14 @@ syndicate plainTimerDemo:
boot timerDriver
spawn "laterThanDemo":
field(deadline, Time, getTime())
field(deadline, MonoTime, getMonoTime())
field(count, int, 0)
onAsserted(TimeLaterThan % deadline.get) do ():
echo "TimeLaterThan ticked for deadline ", deadline.get
count.set(count.get.succ)
if count.get < 5:
deadline.set(deadline.get + 500.milliseconds)
deadline.set(getMonoTime() + initDuration(milliseconds = 500))
onStop:
echo "dataspace stopped"