1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! Injectable time and tick abstraction — the seam for frame-locked propagation.
//!
//! hyphae's default timing is *decentralized*: every time-based operator
//! (`delay`, `debounce`, `interval`, …) spawns its own timer off
//! [`crate::platform`], and monotonic time is read directly from
//! `platform::Instant`. That is fine for eager per-`set` propagation, but it
//! gives no place to (a) drive propagation from a single frame boundary or
//! (b) discipline the timebase to an external reference (PTP / IEEE-1588).
//!
//! This module introduces two small, pluggable traits:
//!
//! - [`Clock`] — a source of monotonic (and optionally *absolute*) nanoseconds.
//! The default [`MonotonicClock`] wraps `platform::now_nanos()` and is a pure
//! pass-through with no behavior change. A future `DisciplinedClock` will add
//! a smoothed PTP offset via [`Clock::to_absolute_nanos`].
//! - [`TickSource`] — a driver that fires a callback once per frame/tick, each
//! carrying a [`Tick`] (`index` + frozen frame time). The default
//! [`IntervalTickSource`] wraps `platform::spawn_interval`. A future
//! `PllTickSource` will slew this grid to phase-lock to PTP.
//!
//! Nothing here changes hyphae's synchronous default. The traits are consumed
//! only by the opt-in [`crate::scheduler`] (both gated behind the `scheduler`
//! feature); the clock is intended to be **handle-scoped**, not a process
//! global, so multiple engines can run on independent timebases.
use ;
/// A source of monotonic — and optionally absolute — time in nanoseconds.
///
/// Implementations must guarantee [`now_nanos`](Clock::now_nanos) is
/// non-decreasing on a given thread of observation. The absolute value returned
/// by `now_nanos` is meaningless across clocks; only *differences* are.
/// One propagation tick — a single frame boundary.
///
/// Carries the frame `index` and the frame time *frozen* at the boundary, so
/// every pure computation within the tick reads one consistent timestamp
/// (a prerequisite for deterministic, replicable frames).
/// Cancels tick delivery when dropped.
///
/// Holding the guard keeps the underlying timer firing; dropping it stops the
/// callback registered by [`TickSource::on_tick`] (the timer thread/task exits
/// on its next wake). Use [`TickGuard::leak`] to keep ticking for the process
/// lifetime without holding the guard.
/// A driver that fires a callback once per tick (frame).
///
/// The callback runs on the tick source's own thread/task; the returned
/// [`TickGuard`] stops delivery on drop.
/// Default [`Clock`]: monotonic nanoseconds from `platform::now_nanos()`.
///
/// A pure pass-through — no discipline, no absolute timebase. This is what the
/// scheduler uses until an application injects a PTP-disciplined clock.
/// Default [`TickSource`]: a fixed-period interval over `platform::spawn_interval`.
///
/// Each `on_tick` registration spawns one interval timer (native: a thread with
/// `spin_sleep` when `precise`; wasm: a `setTimeout` loop, `precise` ignored).
/// Ticks are stamped with the source's [`Clock`] — [`MonotonicClock`] by
/// default, or an injected disciplined clock via [`with_clock`](Self::with_clock).