-
Notifications
You must be signed in to change notification settings - Fork 18
/
process.rs
350 lines (284 loc) · 10.8 KB
/
process.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#![allow(dead_code, unused_variables, unused_imports)]
use {Async, Stream, Sender, AsyncResult, AsyncError};
use syncbox::ArrayQueue;
use std::{mem, ops};
use std::cell::UnsafeCell;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
const MAX_IN_FLIGHT: usize = (1 << 16) - 1;
pub fn process<T, U, F>(source: Stream<T, U::Error>, in_flight: usize, action: F) -> Stream<U::Value, U::Error>
where T: Send + 'static,
U: Async,
F: FnMut(T) -> U + Send + 'static {
// New stream
let (tx, rx) = Stream::pair();
// Wait for consumer interest
if in_flight > 0 {
tx.receive(move |res| {
if let Ok(tx) = res {
setup(source, in_flight, action, tx);
}
});
}
rx
}
fn setup<T, U, F>(source: Stream<T, U::Error>, in_flight: usize, action: F, dest: Sender<U::Value, U::Error>)
where T: Send + 'static,
U: Async,
F: FnMut(T) -> U + Send + 'static {
let mut inner = Inner::new(source, in_flight, action, dest);
inner.maybe_process_next(false);
}
struct Core<T: Send + 'static, U: Async, F> {
max: usize,
queue: ArrayQueue<AsyncResult<U::Value, U::Error>>,
sender: Option<Sender<U::Value, U::Error>>,
source: Option<Source<T, U, F>>,
consume_state: AtomicState,
produce_state: AtomicState,
}
struct Source<T: Send + 'static, U: Async, F> {
stream: Stream<T, U::Error>,
action: F,
}
struct Inner<T: Send + 'static, U: Async, F>(Arc<UnsafeCell<Core<T, U, F>>>);
impl<T: Send + 'static, U: Async, F: FnMut(T) -> U + Send + 'static> Inner<T, U, F> {
fn new(source: Stream<T, U::Error>,
in_flight: usize,
action: F,
dest: Sender<U::Value, U::Error>) -> Inner<T, U, F> {
let core = Core {
max: in_flight,
queue: ArrayQueue::with_capacity(in_flight),
source: Some(Source { stream: source, action: action }),
sender: Some(dest),
consume_state: AtomicState::new(),
produce_state: AtomicState::new(),
};
Inner(Arc::new(UnsafeCell::new(core)))
}
fn maybe_process_next(&mut self, dec_in_flight: bool) {
// Attempt to acquire the consume lock and increment the number of
// in-flight queries. An acquire ordering is used to ensure that the
// source value is readable.
if self.try_acquire_consume_lock(dec_in_flight, Ordering::Acquire) {
// Access to the source has been acquired
let Source { stream, mut action } = self.source.take().expect("source should be present");
let mut inner = self.clone();
// Wait for the next value to be provided
stream.receive(move |res| {
match res {
Ok(Some((val, rest))) => {
// Process the value and wait for the result
let val = action(val);
// Return the source. Release ordering is used to flush
// the memory so that another thread may access it.
inner.source = Some(Source { stream: rest, action: action });
inner.consume_state.release_lock(Ordering::Release);
let mut inner2 = inner.clone();
// Wait for the result
val.receive(move |res| {
match res {
Ok(val) => {
inner2.receive_value(Ok(val));
}
Err(err) => {
inner2.receive_value(Err(err));
}
}
});
// Since the results are buffered, attempt to read
// another value immediately
inner.maybe_process_next(false);
}
Ok(None) => {}
Err(AsyncError::Failed(_)) => {
unimplemented!();
}
_ => unimplemented!(),
}
});
}
}
fn receive_value(&mut self, val: AsyncResult<U::Value, U::Error>) {
// Push the value onto the queue
self.queue.push(val).ok()
.expect("value queue should never run out of capacity");
// Increment the number of values pending in the queue
//
// TODO: if the stream has failed, discard the value by popping from
// the queue
if self.acquire_produce_lock_or_inc_in_flight(Ordering::Acquire) {
// Produce lock has been acquired
self.send_value();
}
}
fn send_value(&mut self) {
let sender = self.sender.take().expect("expected sender to be sender");
let value = self.queue.pop().expect("expected value to be in queue");
match value {
Ok(value) => {
let mut inner = self.clone();
sender.send(value).receive(move |res| {
match res {
Ok(sender) => {
// Save off the sender in case the lock is released
inner.sender = Some(sender);
if inner.release_lock_if_idle(Ordering::Release) {
// in-flight has been decremented, also, even though
// the previous memory fence is a release, the only
// memory that is accessed has been set previously in
// the current thread
inner.send_value();
}
}
Err(_) => {
// TODO: Transition to a canceled state and discard
// currently pending values
}
}
});
}
Err(AsyncError::Failed(e)) => sender.fail(e),
Err(AsyncError::Aborted) => sender.abort(),
}
// Will decrement the consumer in-flight and potentially start
// processing another value
self.maybe_process_next(true);
}
fn try_acquire_consume_lock(&self, dec_in_flight: bool, order: Ordering) -> bool {
let mut old = self.consume_state.load(Ordering::Relaxed);
loop {
// If the consume lock has already been acquired and in-flight is
// not being decremented, then the state does not need to
// transition. Nothing else to do, the lock has not been acquired.
if (old.has_lock() || old.in_flight() == self.max) && !dec_in_flight {
return false;
}
let new = if old.has_lock() {
debug_assert!(dec_in_flight, "state transition bug");
// The lock coul dnot be acquired, but the num in-flight must
// be decremented
old.dec_in_flight()
} else {
debug_assert!(old.in_flight() < self.max || dec_in_flight, "state transition bug");
if dec_in_flight {
old.with_lock()
} else {
old.inc_in_flight().with_lock()
}
};
let act = self.consume_state.compare_and_swap(old, new, order);
if act == old {
return !old.has_lock();
}
old = act;
}
}
fn try_acquire_produce_lock(&self, order: Ordering) -> bool {
let mut old = self.produce_state.load(order);
loop {
if old.has_lock() {
return false;
}
let act = self.produce_state.compare_and_swap(old, old.with_lock(), order);
if act == old {
return true;
}
old = act
}
}
// Increment the in-flight counter and attempt to acquire the produce lock
fn acquire_produce_lock_or_inc_in_flight(&self, order: Ordering) -> bool {
let mut old = self.produce_state.load(Ordering::Relaxed);
loop {
let new = if old.has_lock() {
old.inc_in_flight()
} else {
old.with_lock()
};
let act = self.produce_state.compare_and_swap(old, new, order);
if act == old {
return !old.has_lock();
}
old = act;
}
}
fn release_lock_if_idle(&self, order: Ordering) -> bool {
let mut old = self.produce_state.load(Ordering::Relaxed);
loop {
let new = if old.in_flight() > 0 {
old.dec_in_flight()
} else {
old.without_lock()
};
let act = self.produce_state.compare_and_swap(old, new, order);
if act == old {
return new.has_lock();
}
old = act;
}
}
}
impl<T: Send + 'static, U: Async, F> ops::Deref for Inner<T, U, F> {
type Target = Core<T, U, F>;
fn deref(&self) -> &Core<T, U, F> {
unsafe { &*self.0.get() }
}
}
impl<T: Send + 'static, U: Async, F> ops::DerefMut for Inner<T, U, F> {
fn deref_mut(&mut self) -> &mut Core<T, U, F> {
unsafe { &mut *self.0.get() }
}
}
impl<T: Send + 'static, U: Async, F> Clone for Inner<T, U, F> {
fn clone(&self) -> Inner<T, U, F> {
Inner(self.0.clone())
}
}
unsafe impl<T: Send, U: Async, F> Send for Inner<T, U, F> { }
unsafe impl<T: Send, U: Async, F> Sync for Inner<T, U, F> { }
const LOCK: usize = 1 << 31;
#[derive(Copy, Clone, Eq, PartialEq)]
struct State(usize);
impl State {
fn in_flight(&self) -> usize {
self.0 & MAX_IN_FLIGHT
}
fn inc_in_flight(&self) -> State {
assert!(self.in_flight() < MAX_IN_FLIGHT);
State(self.0 + 1)
}
fn dec_in_flight(&self) -> State {
assert!(self.in_flight() > 0);
State(self.0 - 1)
}
fn has_lock(&self) -> bool {
self.0 & LOCK == LOCK
}
fn with_lock(&self) -> State {
State(self.0 | LOCK)
}
fn without_lock(&self) -> State {
State(!LOCK & self.0)
}
}
struct AtomicState {
atomic: AtomicUsize,
}
impl AtomicState {
fn new() -> AtomicState {
AtomicState { atomic: AtomicUsize::new(0) }
}
fn load(&self, order: Ordering) -> State {
let val = self.atomic.load(order);
State(val)
}
fn compare_and_swap(&self, old: State, new: State, order: Ordering) -> State {
let val = self.atomic.compare_and_swap(old.0, new.0, order);
State(val)
}
fn release_lock(&self, order: Ordering) {
self.atomic.fetch_sub(LOCK, order);
}
}