-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
signal.rs
188 lines (164 loc) · 5.07 KB
/
signal.rs
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
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use futures::{Async, Poll};
use futures::task::{self, Task};
use self::lock::Lock;
pub fn new() -> (Giver, Taker) {
let inner = Arc::new(Inner {
state: AtomicUsize::new(STATE_IDLE),
task: Lock::new(None),
});
let inner2 = inner.clone();
(
Giver {
inner: inner,
},
Taker {
inner: inner2,
},
)
}
#[derive(Clone)]
pub struct Giver {
inner: Arc<Inner>,
}
pub struct Taker {
inner: Arc<Inner>,
}
const STATE_IDLE: usize = 0;
const STATE_WANT: usize = 1;
const STATE_GIVE: usize = 2;
const STATE_CLOSED: usize = 3;
struct Inner {
state: AtomicUsize,
task: Lock<Option<Task>>,
}
impl Giver {
pub fn poll_want(&mut self) -> Poll<(), ()> {
loop {
let state = self.inner.state.load(Ordering::SeqCst);
match state {
STATE_WANT => {
// only set to IDLE if it is still Want
self.inner.state.compare_and_swap(
STATE_WANT,
STATE_IDLE,
Ordering::SeqCst,
);
return Ok(Async::Ready(()))
},
STATE_GIVE => {
// we're already waiting, return
return Ok(Async::NotReady)
}
STATE_CLOSED => return Err(()),
// Taker doesn't want anything yet, so park.
_ => {
if let Some(mut locked) = self.inner.task.try_lock() {
// While we have the lock, try to set to GIVE.
let old = self.inner.state.compare_and_swap(
STATE_IDLE,
STATE_GIVE,
Ordering::SeqCst,
);
// If it's not still IDLE, something happened!
// Go around the loop again.
if old == STATE_IDLE {
*locked = Some(task::current());
return Ok(Async::NotReady)
}
} else {
// if we couldn't take the lock, then a Taker has it.
// The *ONLY* reason is because it is in the process of notifying us
// of its want.
//
// We need to loop again to see what state it was changed to.
}
},
}
}
}
pub fn is_canceled(&self) -> bool {
self.inner.state.load(Ordering::SeqCst) == STATE_CLOSED
}
}
impl Taker {
pub fn cancel(&self) {
self.signal(STATE_CLOSED)
}
pub fn want(&self) {
self.signal(STATE_WANT)
}
fn signal(&self, state: usize) {
let old_state = self.inner.state.swap(state, Ordering::SeqCst);
match old_state {
STATE_WANT | STATE_CLOSED | STATE_IDLE => (),
_ => {
loop {
if let Some(mut locked) = self.inner.task.try_lock() {
if let Some(task) = locked.take() {
task.notify();
}
return;
} else {
// if we couldn't take the lock, then a Giver has it.
// The *ONLY* reason is because it is in the process of parking.
//
// We need to loop and take the lock so we can notify this task.
}
}
},
}
}
}
impl Drop for Taker {
fn drop(&mut self) {
self.cancel();
}
}
// a sub module just to protect unsafety
mod lock {
use std::cell::UnsafeCell;
use std::ops::{Deref, DerefMut};
use std::sync::atomic::{AtomicBool, Ordering};
pub struct Lock<T> {
is_locked: AtomicBool,
value: UnsafeCell<T>,
}
impl<T> Lock<T> {
pub fn new(val: T) -> Lock<T> {
Lock {
is_locked: AtomicBool::new(false),
value: UnsafeCell::new(val),
}
}
pub fn try_lock(&self) -> Option<Locked<T>> {
if !self.is_locked.swap(true, Ordering::SeqCst) {
Some(Locked { lock: self })
} else {
None
}
}
}
unsafe impl<T: Send> Send for Lock<T> {}
unsafe impl<T: Send> Sync for Lock<T> {}
pub struct Locked<'a, T: 'a> {
lock: &'a Lock<T>,
}
impl<'a, T> Deref for Locked<'a, T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.lock.value.get() }
}
}
impl<'a, T> DerefMut for Locked<'a, T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.lock.value.get() }
}
}
impl<'a, T> Drop for Locked<'a, T> {
fn drop(&mut self) {
self.lock.is_locked.store(false, Ordering::SeqCst);
}
}
}