11
11
# # This module implements cross-platform system timer with
12
12
# # milliseconds resolution.
13
13
# #
14
- # # Timer supports two types of clocks:
14
+ # # Timer supports three types of clocks:
15
15
# # ``system`` uses the most fast OS primitive to obtain wall clock time.
16
16
# # ``mono`` uses monotonic clock time (default).
17
+ # # ``virtual`` is for event-based simulations only.
17
18
# #
18
19
# # ``system`` clock is affected by discontinuous jumps in the system time. This
19
20
# # clock is significantly faster then ``mono`` clock in most of the cases.
20
21
# #
21
22
# # ``mono`` clock is not affected by discontinuous jumps in the system time.
22
23
# # This clock is slower then ``system`` clock.
23
24
# #
24
- # # You can specify which timer you want to use ``-d:asyncTimer=<system/mono>``.
25
+ # # ``virtual`` clock is not related to wall clock or system time. It simply
26
+ # # jumps through the event queue in time order, counting execution time.
27
+ # #
28
+ # # You can specify which timer you want to use ``-d:asyncTimer=<system/mono/virtual>``.
25
29
import stew/ base10
26
30
27
31
const asyncTimer* {.strdefine.} = " mono"
@@ -31,7 +35,18 @@ when (NimMajor, NimMinor) < (1, 4):
31
35
else :
32
36
{.push raises: [].}
33
37
34
- when defined(windows):
38
+ when asyncTimer == " virtual" :
39
+ var curTime: uint64 = 0
40
+ proc fastEpochTime* (): uint64 {.
41
+ inline, deprecated: " Use Moment.now()" .} =
42
+ # # Procedure's resolution is millisecond.
43
+ curTime div 1_000_000
44
+
45
+ proc fastEpochTimeNano(): uint64 {.inline.} =
46
+ # # Procedure's resolution is nanosecond.
47
+ curTime
48
+
49
+ elif defined(windows):
35
50
when asyncTimer == " system" :
36
51
from winlean import getSystemTimeAsFileTime, FILETIME
37
52
@@ -503,3 +518,8 @@ when defined(posix):
503
518
tv_sec: Time(a.value div Second.value),
504
519
tv_nsec: int (a.value mod Second.value)
505
520
)
521
+
522
+ when asyncTimer == " virtual" :
523
+ proc advance* (t: typedesc [Moment], a: Duration) =
524
+ # # # Advance virtual time by given duration
525
+ curTime += a.nanoseconds.uint64
0 commit comments