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
//! What holds a session between turns: its queued settings changes or its
//! delete, marked on its turn lock so no request holds the lock across its
//! awaits.
use std::{num::NonZeroUsize, sync::Arc};
use super::{LoadedSession, TurnSlot};
/// What holds a session: its queued settings changes or its delete, which
/// exclude each other. The turn lock is a std mutex no request holds across
/// its awaits, so a request marks its hold in [`TurnSlot::hold`] instead of
/// holding the lock.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Hold {
/// This many changes to the session's settings are queued on its
/// runtime or applying (see [`super::configure`]). A prompt still starts a
/// turn, which queues on the runtime behind them.
Configuring(NonZeroUsize),
/// A delete is unloading the session to delete its files; no turn
/// starts and no change queues meanwhile.
Deleting,
}
/// A request's part of a session's [`Hold`], freed when dropped, whether
/// the request finished, failed, or was dropped.
pub(super) struct Held(Arc<LoadedSession>);
impl Held {
/// Counts one more change queued on `session`, or `None` while a delete
/// holds it. `turn` is `session`'s turn lock, held.
pub(super) fn change(session: &Arc<LoadedSession>, turn: &mut TurnSlot) -> Option<Self> {
let queued = match turn.hold {
None => NonZeroUsize::MIN,
Some(Hold::Configuring(queued)) => queued
.checked_add(1)
.expect("each queued change is a request in flight"),
Some(Hold::Deleting) => return None,
};
turn.hold = Some(Hold::Configuring(queued));
Some(Self(Arc::clone(session)))
}
/// Holds `session` for its delete. `turn` is `session`'s turn lock,
/// held, with nothing holding the session.
pub(super) fn delete(session: &Arc<LoadedSession>, turn: &mut TurnSlot) -> Self {
debug_assert_eq!(turn.hold, None, "a delete holds only a free session");
turn.hold = Some(Hold::Deleting);
Self(Arc::clone(session))
}
}
impl Drop for Held {
/// Takes the session's turn lock, a std mutex: never drop a `Held`
/// while holding that lock's guard, which would deadlock.
fn drop(&mut self) {
let mut turn = self.0.turn();
// Changes and a delete exclude each other, so the hold in place is
// the kind this one is part of.
turn.hold = match turn.hold {
Some(Hold::Configuring(queued)) => {
NonZeroUsize::new(queued.get() - 1).map(Hold::Configuring)
}
Some(Hold::Deleting) | None => None,
};
}
}