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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 FernTech
//! Waking a window from another thread.
//!
//! Some content changes off the UI thread: a terminal's child prints, a video
//! decoder finishes a frame, a background task updates a status. The thread
//! that changed it cannot touch the widget tree, so it stores the change where
//! the UI thread will read it and then *wakes* the window, which runs a frame
//! that reads it. [`RedrawWaker`] is that wake. A window gives one to the
//! framework, which hands it to the off-thread sources attached to the
//! window's widgets.
//!
//! # The protocol
//!
//! A wake carries no data, and a burst of changes must cost one frame, not one
//! per change. A [`WakeFlag`] per consumer does both:
//!
//! - **Producer:** store the change, release any lock it holds, then
//! `if flag.raise() { waker.wake(kind) }`. Only the clear-to-set edge wakes;
//! every later raise until the consumer takes the flag merges into it.
//! - **Consumer (UI thread):** `flag.take()`, *then* read the change. Or
//! `flag.park()` to leave it for later on purpose, without being woken
//! again meanwhile.
//!
//! No wake is lost. `raise` and `take` are read-modify-writes of one atomic,
//! so they are totally ordered. If the raise comes first, the take reads it
//! (or a later raise in its release sequence) and synchronises with it, so
//! the change is visible to the read that follows. If the take comes first,
//! the raise reads `false` and wakes. No `SeqCst` is needed, which matters
//! twice: loom models `SeqCst` as acquire-release, so a proof that leaned on
//! it would be one loom cannot check, and the argument holds on weakly
//! ordered hardware as written.
//!
//! `take` is `AcqRel` rather than `Acquire` for the second case: there it
//! makes the take synchronise with the raise that follows, which orders the
//! platform's own redraw flag (Wayland's is `Relaxed`) behind it, so a wake
//! the platform merged into a redraw it had already queued is served by a
//! frame that sees the change. `park` is an RMW, not a store, for the same
//! reason as `raise`: a plain store would end the release sequence, and a
//! take after it would not synchronise with a producer whose raise it
//! follows.
//!
//! # Deciding before taking
//!
//! A consumer may look at a flag to decide whether to serve the change at all
//! in this frame, and take it only if it does (a widget whose paint is cached
//! is marked for paint only when it has a repaint pending). That look is
//! [`WakeFlag::peek`], an RMW like the rest, not a load. Suppose it reads the
//! flag clear, and a producer raises it right after. The raise must see what
//! the consumer did before looking, the platform clearing its own redraw flag
//! as it delivered the frame, so that the producer's wake queues another
//! frame instead of merging into the one being delivered. With an RMW the
//! raise reads what the peek wrote and synchronises with it; a load releases
//! nothing, and on weakly ordered hardware the frame would see no request
//! while the wake merged into a redraw already delivered.
//!
//! # Hidden windows
//!
//! A window nobody can see may drop a [`WakeKind::Draw`] wake, provided it
//! owes one when it is shown again: drawing it then shows the latest state,
//! and drawing it while hidden would show nothing. A [`WakeGate`] carries the
//! window's hidden state and whether a wake was dropped in one atomic, so that
//! exactly one of "the producer routes its wake" and "showing the window
//! re-issues one" happens. A [`WakeKind::Layout`] wake is never dropped: what
//! layout and accessibility read must stay current on a window that draws
//! nothing.
//!
//! # Rules for a waker
//!
//! [`RedrawWaker::wake`] is cheap, never waits for the UI thread, is
//! idempotent (repeated wakes merge), must not panic (it can run in a `Drop`
//! while unwinding), and is never called with one of the framework's locks
//! held. [`RedrawWaker::is_hidden`] is a lock-free load: it is read on any
//! thread, possibly while a producer holds a lock of its own.
//!
//! The windowed implementation lives in teksilo-platform and is installed by
//! teksilo-app; a headless tree takes a [`CountingWaker`] or none.
use crate;
/// Wakes one window from any thread. See the [module docs](self) for the
/// protocol around it and the rules an implementation follows.
/// What a wake asks of a window, ordered by how much of a frame it needs: a
/// change that asks for both wakes once, with the larger.
/// A clear-to-set wake flag, one per consumer. Every write is an
/// acquire-release read-modify-write, so none can break a release sequence.
/// See the [module docs](self).
const HIDDEN: u8 = 1;
const MISSED: u8 = 2;
/// A window's hidden state and whether a droppable wake was dropped while it
/// was hidden, in one atomic: one modification order, so exactly one of "the
/// producer routes its wake" ([`pass`](Self::pass) returns `true`) and "showing
/// the window re-issues one" ([`show`](Self::show) returns `true`) happens.
/// A [`RedrawWaker`] that counts the wakes it receives, for tests: a headless
/// tree has no window to wake. A test can wait for a wake made on another
/// thread with [`wait_for`](Self::wait_for).
///
/// It uses the standard library's lock whatever the build, so it is not for
/// use inside a loom model.
///
/// ```
/// use std::sync::Arc;
/// use std::time::Duration;
/// use teksilo_canvas::wake::{CountingWaker, RedrawWaker, WakeKind};
///
/// let waker = Arc::new(CountingWaker::new());
/// let remote = waker.clone();
/// std::thread::spawn(move || remote.wake(WakeKind::Draw));
/// assert!(waker.wait_for(1, Duration::from_secs(5)));
/// assert_eq!(waker.count_of(WakeKind::Draw), 1);
/// ```