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
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT
//! Work any thread may hand to the loop's own thread.
//!
//! # The problem this solves
//!
//! A host that is not driving the loop still wants things done *on* it: end the session after a
//! deadline (a smoke test that must terminate itself), refresh a status line, poll something the
//! toolkit wants read on its own thread. Every toolkit has its own timer API for that
//! (`NSTimer`, `glib::timeout_add`, `SetTimer`) — and none of them may be *created* from another
//! thread, which is exactly the situation a watcher thread is in.
//!
//! # Why the frame tick is the executor
//!
//! The same seam that makes a cross-thread stop work ([`crate::platform::stop`]): every loop this
//! library owns calls [`crate::drive_frame`] on the thread that owns it, so a queue read there runs
//! its work on that thread — without the library creating a toolkit timer, and without any
//! per-backend code. On AppKit that is the per-window `NSTimer`; on GTK the `glib` timeout
//! closure; everywhere else the loop's own iteration.
//!
//! # Precision, and what a caller may rely on
//!
//! `after` is a **lower bound**. Work becomes due at its deadline and runs on the next frame that
//! follows, so the granularity is one frame interval (~16 ms on every backend here) and a still
//! window does not change it: the loops tick unconditionally. A caller that needs sub-frame
//! precision wants a toolkit timer, not this.
//!
//! # Repeating work
//!
//! There is no repeating form on purpose. A callback that wants to run again calls
//! [`schedule_in`] again at its end, which is one line and makes the *interval* the caller's
//! explicit choice rather than a second mechanism with its own drift (principles #54/#101).
// Standard-library types on purpose, and the module is `#[cfg(not(alloc_frugal))]`: the queue
// needs an allocator, and the profiles that have one are exactly the ones with a platform loop to
// hand work to. A `mini`/`embedded` build has no loop that could run it, so it does not get the
// entry point either — an honest absence rather than a function that always answers `false`.
use ;
use ;
/// A callback waiting for the loop's next frame at or after its deadline.
/// The queue, created on first use.
///
/// A `Mutex<Vec<_>>` rather than a channel: the frame tick has to *select* the due items and leave
/// the rest, which is a partition over one collection rather than a receive.
static QUEUE: = new;
/// Locks a queue, recovering a poisoned one.
///
/// A callback panics with the due items already *out* of the queue, so the contents are still
/// consistent and the lock is safe to take back.
/// Appends `work` with its deadline.
///
/// Shared by [`schedule_in`] and the tests' own queues, so the queue's rule is exercised directly
/// rather than through the process-global instance — which two tests running in parallel would
/// otherwise race each other for.
/// Runs the items in `queue` that have come due, leaving the rest.
///
/// # Why the due items are taken out before they run
///
/// A callback may schedule more work (the documented way to repeat), which takes the same lock.
/// Running callbacks while holding it would deadlock the loop on the first one that did, so the
/// due items are moved out under the lock and executed after it is released. Work queued *by* a
/// callback that is due immediately therefore runs on the **next** frame, not this one — a
/// one-frame delay rather than re-entrancy.
/// Hands `work` to the loop's own thread, to run at or after `after` from now.
///
/// Returns whether a loop in this process is in a position to run it: `true` when a loop has
/// driven a frame, so it will reach the queue on its next one — the same fact
/// [`crate::platform::stop::loop_has_ticked`] reports, and the same reason
/// [`crate::quit`](crate::quit) reports it for a stop request. `false` means no loop has ticked
/// yet; **the work is still queued** and a loop started later runs it (after its deadline), which
/// is what makes "schedule the ending, then run the loop" work for a host that does both from one
/// thread.
///
/// # A panicking callback
///
/// A callback that panics propagates: it is host code running on the loop's thread, exactly as a
/// signal handler is, and swallowing the panic here would turn "your heartbeat is broken" into a
/// session that quietly stops beating. The queue itself survives a panic (the poisoned lock is
/// taken with `into_inner`), so a host that catches the unwind at its own boundary can keep going
/// with the remaining work intact.
/// Runs everything that has come due on the process-global queue. Called by the frame tick, on
/// the loop's own thread. See [`run_due`] for the rule it applies.
pub