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
//! The map's latest update for the renderer that draws it.
//!
//! The map publishes an [`UpdateParameters`] after each change that a frame
//! shows, and a renderer, on the map's thread or another, draws the latest
//! one it finds. An update replaces the one before it, whether or not a
//! renderer drew that one, and the slot keeps the latest while no renderer
//! reads it.
//!
//! A command group holds the updates back: while one is open, what the map
//! publishes waits, and the latest of it shows when the outermost group ends.
//! A hold, which the map's owner sets apart from the groups, does the same.
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use mln_util::error::Error;
use mln_util::event_queue::Waker;
use parking_lot::Mutex;
use crate::update_parameters::UpdateParameters;
/// An update that a slot published.
#[derive(Clone)]
pub struct Update {
/// Counts the updates that the slot published, this one included.
pub generation: u64,
pub parameters: Arc<UpdateParameters>,
}
#[derive(Default)]
struct State {
generation: u64,
latest: Option<Arc<UpdateParameters>>,
/// The latest update published while a group or the hold was open.
held: Option<Arc<UpdateParameters>>,
group_depth: u32,
hold: bool,
closed: bool,
}
#[derive(Default)]
struct Shared {
state: Mutex<State>,
/// The readers' wakers, by reader.
wakers: Mutex<Vec<(u64, Waker)>>,
next_reader: AtomicU64,
}
impl Shared {
fn reader(self: &Arc<Self>) -> UpdateReader {
UpdateReader { shared: Arc::clone(self), id: self.next_reader.fetch_add(1, Ordering::Relaxed) }
}
/// Runs the wakers, with nothing locked.
fn wake(&self) {
let wakers: Vec<Waker> = self.wakers.lock().iter().map(|(_, waker)| Arc::clone(waker)).collect();
for waker in wakers {
waker();
}
}
}
/// The writing side, which the map owns. Dropping it closes the slot: its
/// readers see no update from then on.
#[derive(Default)]
pub struct UpdateSlot {
shared: Arc<Shared>,
}
// The map and a renderer on another thread share the slot.
const _: () = {
const fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<UpdateSlot>();
assert_send_sync::<UpdateReader>();
};
impl UpdateSlot {
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// A reader of this slot's updates.
#[must_use]
pub fn reader(&self) -> UpdateReader {
self.shared.reader()
}
/// Makes `parameters` the latest update, or, while a group or the hold
/// is open, the update that shows when the last of them ends.
pub fn publish(&self, parameters: Arc<UpdateParameters>) {
{
let mut state = self.shared.state.lock();
if state.group_depth > 0 || state.hold {
state.held = Some(parameters);
return;
}
Self::show(&mut state, parameters);
}
self.shared.wake();
}
fn show(state: &mut State, parameters: Arc<UpdateParameters>) {
state.generation += 1;
state.latest = Some(parameters);
}
/// Counts the updates that readers could see.
#[must_use]
pub fn generation(&self) -> u64 {
self.shared.state.lock().generation
}
/// The generation of the update that shows what was published last:
/// the current one, or while a group holds an update back, the one its
/// end shows.
#[must_use]
pub fn pending_generation(&self) -> u64 {
let state = self.shared.state.lock();
state.generation + u64::from(state.held.is_some())
}
/// Opens a command group. Groups nest.
pub fn begin_group(&self) {
self.shared.state.lock().group_depth += 1;
}
/// Closes the innermost command group. When it was the outermost, the
/// latest update published inside it shows. Fails with
/// [`ErrorKind::InvalidState`](mln_util::ErrorKind) when no group is
/// open.
pub fn end_group(&self) -> Result<(), Error> {
let shown = {
let mut state = self.shared.state.lock();
if state.group_depth == 0 {
return Err(Error::invalid_state("the map has no open command group"));
}
state.group_depth -= 1;
Self::show_held(&mut state)
};
if shown {
self.shared.wake();
}
Ok(())
}
/// Shows the held update once neither a group nor the hold keeps it.
fn show_held(state: &mut State) -> bool {
if state.group_depth > 0 || state.hold {
return false;
}
let Some(parameters) = state.held.take() else { return false };
Self::show(state, parameters);
true
}
/// Holds the updates back, as a group does, until
/// [`release`](Self::release). It doesn't nest, and it doesn't count
/// as a group.
pub fn hold(&self) {
self.shared.state.lock().hold = true;
}
/// Ends the hold: the latest update published during it shows, unless
/// a group still holds it.
pub fn release(&self) {
let shown = {
let mut state = self.shared.state.lock();
state.hold = false;
Self::show_held(&mut state)
};
if shown {
self.shared.wake();
}
}
/// How many command groups are open.
#[must_use]
pub fn group_depth(&self) -> u32 {
self.shared.state.lock().group_depth
}
}
impl Drop for UpdateSlot {
fn drop(&mut self) {
{
let mut state = self.shared.state.lock();
state.closed = true;
state.latest = None;
state.held = None;
}
self.shared.wake();
}
}
/// The reading side of an [`UpdateSlot`], for the renderer that draws the
/// map. Clones read the same slot, each with a waker of its own.
pub struct UpdateReader {
shared: Arc<Shared>,
id: u64,
}
impl Clone for UpdateReader {
/// Another reader of the same slot, without a waker.
fn clone(&self) -> Self {
self.shared.reader()
}
}
impl Drop for UpdateReader {
fn drop(&mut self) {
self.set_waker(None);
}
}
impl UpdateReader {
/// The latest update, if the map published one and is still there.
#[must_use]
pub fn latest(&self) -> Option<Update> {
let state = self.shared.state.lock();
let parameters = state.latest.clone()?;
Some(Update { generation: state.generation, parameters })
}
/// The generation of the latest update, or 0 before the first.
#[must_use]
pub fn generation(&self) -> u64 {
self.shared.state.lock().generation
}
/// Whether the map that published to the slot is gone.
#[must_use]
pub fn is_closed(&self) -> bool {
self.shared.state.lock().closed
}
/// Sets the waker that runs when an update shows or the slot closes,
/// until the reader drops. It runs on the map's thread, with the slot
/// unlocked, and schedules the reader's work rather than drawing there.
pub fn set_waker(&self, waker: Option<Waker>) {
let mut wakers = self.shared.wakers.lock();
wakers.retain(|(id, _)| *id != self.id);
if let Some(waker) = waker {
wakers.push((self.id, waker));
}
}
/// Whether both read the same slot.
#[must_use]
pub fn ptr_eq(&self, other: &UpdateReader) -> bool {
Arc::ptr_eq(&self.shared, &other.shared)
}
}