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
//! Primitive synchronization operation types.
use crate::wait_queue::WaitQueue;
/// Operation types.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum Opcode {
/// Acquires exclusive ownership.
Exclusive,
/// Acquires shared ownership.
Shared,
/// Barrier operation where the boolean flag indicates whether the operation is wait-only.
Barrier(bool),
/// Acquires semaphores.
Semaphore(u8),
/// Waits until the desired resources are available.
Wait(u8),
}
impl Opcode {
/// Checks if the resource expressed in `self` can be released from `state`.
#[inline]
pub(crate) const fn can_release(self, state: usize) -> bool {
match self {
Opcode::Exclusive => {
let data = state & WaitQueue::DATA_MASK;
data == WaitQueue::DATA_MASK
}
Opcode::Shared => {
let data = state & WaitQueue::DATA_MASK;
data >= 1 && data != WaitQueue::DATA_MASK
}
Opcode::Semaphore(count) => {
let data = state & WaitQueue::DATA_MASK;
let count = count as usize;
data >= count
}
Opcode::Barrier(_) | Opcode::Wait(_) => true,
}
}
/// Converts the operation mode into a `usize` value representing the desired resources.
#[inline]
pub(crate) const fn desired_count(self) -> usize {
match self {
Opcode::Exclusive => WaitQueue::DATA_MASK,
Opcode::Shared => 1,
Opcode::Barrier(wait_only) => {
if wait_only {
0
} else {
1
}
}
Opcode::Semaphore(count) | Opcode::Wait(count) => {
let count = count as usize;
debug_assert!(count < WaitQueue::LOCKED_FLAG);
count
}
}
}
/// Converts the operation mode into a `usize` value representing the resource held by the
/// corresponding synchronization primitive.
#[inline]
pub(crate) const fn acquired_count(self) -> usize {
match self {
Opcode::Exclusive => WaitQueue::DATA_MASK,
Opcode::Shared => 1,
Opcode::Semaphore(count) => {
let count = count as usize;
debug_assert!(count < WaitQueue::LOCKED_FLAG);
count
}
Opcode::Barrier(_) | Opcode::Wait(_) => 0,
}
}
}