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
use crate::time::driver::Entry;
use crate::time::wheel;
use std::ptr;
use std::sync::Arc;
#[derive(Debug)]
pub(crate) struct Stack {
head: Option<Arc<Entry>>,
}
impl Default for Stack {
fn default() -> Stack {
Stack { head: None }
}
}
impl wheel::Stack for Stack {
type Owned = Arc<Entry>;
type Borrowed = Entry;
type Store = ();
fn is_empty(&self) -> bool {
self.head.is_none()
}
fn push(&mut self, entry: Self::Owned, _: &mut Self::Store) {
let ptr: *const Entry = &*entry as *const _;
let old = self.head.take();
unsafe {
debug_assert!((*entry.next_stack.get()).is_none());
debug_assert!((*entry.prev_stack.get()).is_null());
if let Some(ref entry) = old.as_ref() {
debug_assert!({
ptr != &***entry as *const _
});
*entry.prev_stack.get() = ptr;
}
*entry.next_stack.get() = old;
}
self.head = Some(entry);
}
fn pop(&mut self, _: &mut ()) -> Option<Arc<Entry>> {
let entry = self.head.take();
unsafe {
if let Some(entry) = entry.as_ref() {
self.head = (*entry.next_stack.get()).take();
if let Some(entry) = self.head.as_ref() {
*entry.prev_stack.get() = ptr::null();
}
*entry.prev_stack.get() = ptr::null();
}
}
entry
}
fn remove(&mut self, entry: &Entry, _: &mut ()) {
unsafe {
debug_assert!({
let mut next = self.head.as_ref();
let mut contains = false;
while let Some(n) = next {
if entry as *const _ == &**n as *const _ {
debug_assert!(!contains);
contains = true;
}
next = (*n.next_stack.get()).as_ref();
}
contains
});
let next = (*entry.next_stack.get()).take();
if let Some(next) = next.as_ref() {
(*next.prev_stack.get()) = *entry.prev_stack.get();
}
if let Some(prev) = (*entry.prev_stack.get()).as_ref() {
*prev.next_stack.get() = next;
} else {
self.head = next;
}
*entry.prev_stack.get() = ptr::null();
}
}
fn when(item: &Entry, _: &()) -> u64 {
item.when_internal().expect("invalid internal state")
}
}