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
//! One supervision turn per print at a time.
//!
//! An event arriving while a turn for the same print is still running is queued
//! behind it rather than opening a second concurrent conversation: two turns
//! talking to one session at once is two agents disagreeing about one machine.
//!
//! The queue is per print rather than global, because two prints are two
//! conversations and nothing is served by serializing them against each other.
//! It is an asynchronous lock rather than a `std` one because a guard is held
//! across an await and has to be sendable; the wakers are what make a queued
//! caller resume the moment the turn ahead of it returns rather than on a poll.
use core::future::Future;
use core::pin::Pin;
use core::task::{Context, Poll, Waker};
use std::collections::BTreeMap;
use std::sync::Mutex;
use printobserver_types::PrintId;
/// One print's queue: whether a turn holds it, and who is waiting.
#[derive(Debug, Default)]
struct Queue {
/// Whether a turn is running for this print.
held: bool,
/// The callers waiting for it, in the order they arrived.
waiting: Vec<Waker>,
}
/// The per-print turn locks.
#[derive(Debug, Default)]
pub struct TurnLocks {
/// One queue per print that has ever taken a turn.
queues: Mutex<BTreeMap<PrintId, Queue>>,
}
impl TurnLocks {
/// Wait until no turn is running for this print, then hold it.
pub const fn acquire(&self, print_id: PrintId) -> Acquire<'_> {
Acquire {
locks: self,
print_id,
}
}
/// Hold this print's turn when no turn is running for it, without waiting.
///
/// For a caller that may itself be answering the turn that holds it — a
/// read the agent made from inside its own turn — and so must never queue
/// behind that turn.
///
/// # Panics
///
/// When the turn locks are poisoned, which only a panic while they were
/// being read can leave them.
pub fn try_acquire(&self, print_id: PrintId) -> Option<TurnGuard<'_>> {
let mut queues = self.queues.lock().expect("the turn locks are not poisoned");
let queue = queues.entry(print_id).or_default();
if queue.held {
return None;
}
queue.held = true;
drop(queues);
Some(TurnGuard {
locks: self,
print_id,
})
}
/// Release one print's turn and wake whoever is queued behind it.
fn release(&self, print_id: PrintId) {
let woken = {
let mut queues = self.queues.lock().expect("the turn locks are not poisoned");
let queue = queues.entry(print_id).or_default();
queue.held = false;
core::mem::take(&mut queue.waiting)
};
for waker in woken {
waker.wake();
}
}
}
/// The future that waits for one print's turn.
#[derive(Debug)]
pub struct Acquire<'a> {
/// The locks being waited on.
locks: &'a TurnLocks,
/// The print whose turn is being waited for.
print_id: PrintId,
}
impl<'a> Future for Acquire<'a> {
type Output = TurnGuard<'a>;
fn poll(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Self::Output> {
let mut queues = self
.locks
.queues
.lock()
.expect("the turn locks are not poisoned");
let queue = queues.entry(self.print_id).or_default();
if queue.held {
queue.waiting.push(context.waker().clone());
return Poll::Pending;
}
queue.held = true;
drop(queues);
Poll::Ready(TurnGuard {
locks: self.locks,
print_id: self.print_id,
})
}
}
/// The turn one print is running, released when this is dropped.
#[derive(Debug)]
pub struct TurnGuard<'a> {
/// The locks this guard came from.
locks: &'a TurnLocks,
/// The print whose turn is being held.
print_id: PrintId,
}
impl Drop for TurnGuard<'_> {
fn drop(&mut self) {
self.locks.release(self.print_id);
}
}