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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
use std::prelude::v1::*;
use std::cell::Cell;
use std::fmt;
use std::marker::PhantomData;
use std::mem;
use std::ptr;
use std::sync::{Arc, Mutex, Condvar, Once, ONCE_INIT};
use std::sync::atomic::{AtomicUsize, Ordering};
use {Future, Stream, Sink, Poll, Async, StartSend, AsyncSink};
use super::core;
use super::{BorrowedTask, NotifyHandle, Spawn, spawn, Notify, UnsafeNotify};
mod unpark_mutex;
pub use self::unpark_mutex::UnparkMutex;
mod data;
pub use self::data::*;
mod task_rc;
#[allow(deprecated)]
#[cfg(feature = "with-deprecated")]
pub use self::task_rc::TaskRc;
pub use task_impl::core::init;
thread_local!(static CURRENT_TASK: Cell<*mut u8> = Cell::new(ptr::null_mut()));
static INIT: Once = ONCE_INIT;
pub fn get_ptr() -> Option<*mut u8> {
if core::is_get_ptr(0x1) {
Some(CURRENT_TASK.with(|c| c.get()))
} else {
core::get_ptr()
}
}
fn tls_slot() -> *const Cell<*mut u8> {
CURRENT_TASK.with(|c| c as *const _)
}
pub fn set<'a, F, R>(task: &BorrowedTask<'a>, f: F) -> R
where F: FnOnce() -> R
{
INIT.call_once(|| unsafe {
let get = mem::transmute::<usize, _>(0x1);
let set = mem::transmute::<usize, _>(0x2);
init(get, set);
});
if core::is_get_ptr(0x1) {
struct Reset(*const Cell<*mut u8>, *mut u8);
impl Drop for Reset {
#[inline]
fn drop(&mut self) {
unsafe {
(*self.0).set(self.1);
}
}
}
unsafe {
let slot = tls_slot();
let _reset = Reset(slot, (*slot).get());
(*slot).set(task as *const _ as *mut u8);
f()
}
} else {
core::set(task, f)
}
}
#[derive(Copy, Clone)]
#[allow(deprecated)]
pub enum BorrowedUnpark<'a> {
Old(&'a Arc<Unpark>),
New(core::BorrowedUnpark<'a>),
}
#[derive(Copy, Clone)]
#[allow(deprecated)]
pub enum BorrowedEvents<'a> {
None,
One(&'a UnparkEvent, &'a BorrowedEvents<'a>),
}
#[derive(Clone)]
pub enum TaskUnpark {
#[allow(deprecated)]
Old(Arc<Unpark>),
New(core::TaskUnpark),
}
#[derive(Clone)]
#[allow(deprecated)]
pub enum UnparkEvents {
None,
One(UnparkEvent),
Many(Box<[UnparkEvent]>),
}
impl<'a> BorrowedUnpark<'a> {
#[inline]
pub fn new(f: &'a Fn() -> NotifyHandle, id: usize) -> BorrowedUnpark<'a> {
BorrowedUnpark::New(core::BorrowedUnpark::new(f, id))
}
#[inline]
pub fn to_owned(&self) -> TaskUnpark {
match *self {
BorrowedUnpark::Old(old) => TaskUnpark::Old(old.clone()),
BorrowedUnpark::New(new) => TaskUnpark::New(new.to_owned()),
}
}
}
impl<'a> BorrowedEvents<'a> {
#[inline]
pub fn new() -> BorrowedEvents<'a> {
BorrowedEvents::None
}
#[inline]
pub fn to_owned(&self) -> UnparkEvents {
let mut one_event = None;
let mut list = Vec::new();
let mut cur = self;
while let BorrowedEvents::One(event, next) = *cur {
let event = event.clone();
match one_event.take() {
None if list.len() == 0 => one_event = Some(event),
None => list.push(event),
Some(event2) => {
list.push(event2);
list.push(event);
}
}
cur = next;
}
match one_event {
None if list.len() == 0 => UnparkEvents::None,
None => UnparkEvents::Many(list.into_boxed_slice()),
Some(e) => UnparkEvents::One(e),
}
}
}
impl UnparkEvents {
pub fn notify(&self) {
match *self {
UnparkEvents::None => {}
UnparkEvents::One(ref e) => e.unpark(),
UnparkEvents::Many(ref list) => {
for event in list.iter() {
event.unpark();
}
}
}
}
pub fn will_notify(&self, events: &BorrowedEvents) -> bool {
match *self {
UnparkEvents::None => {}
_ => return false,
}
match *events {
BorrowedEvents::None => return true,
_ => {},
}
return false
}
}
#[allow(deprecated)]
impl TaskUnpark {
pub fn notify(&self) {
match *self {
TaskUnpark::Old(ref old) => old.unpark(),
TaskUnpark::New(ref new) => new.notify(),
}
}
pub fn will_notify(&self, unpark: &BorrowedUnpark) -> bool {
match (unpark, self) {
(&BorrowedUnpark::Old(old1), &TaskUnpark::Old(ref old2)) => {
&**old1 as *const Unpark == &**old2 as *const Unpark
}
(&BorrowedUnpark::New(ref new1), &TaskUnpark::New(ref new2)) => {
new2.will_notify(new1)
}
_ => false,
}
}
}
impl<F: Future> Spawn<F> {
#[doc(hidden)]
#[deprecated(note = "recommended to use `poll_future_notify` instead")]
#[allow(deprecated)]
pub fn poll_future(&mut self, unpark: Arc<Unpark>) -> Poll<F::Item, F::Error> {
self.enter(BorrowedUnpark::Old(&unpark), |f| f.poll())
}
pub fn wait_future(&mut self) -> Result<F::Item, F::Error> {
ThreadNotify::with_current(|notify| {
loop {
match self.poll_future_notify(notify, 0)? {
Async::NotReady => notify.park(),
Async::Ready(e) => return Ok(e),
}
}
})
}
#[doc(hidden)]
#[deprecated]
#[allow(deprecated)]
pub fn execute(self, exec: Arc<Executor>)
where F: Future<Item=(), Error=()> + Send + 'static,
{
exec.clone().execute(Run {
spawn: spawn(Box::new(self.into_inner())),
inner: Arc::new(RunInner {
exec: exec,
mutex: UnparkMutex::new()
}),
})
}
}
impl<S: Stream> Spawn<S> {
#[deprecated(note = "recommended to use `poll_stream_notify` instead")]
#[allow(deprecated)]
#[doc(hidden)]
pub fn poll_stream(&mut self, unpark: Arc<Unpark>)
-> Poll<Option<S::Item>, S::Error> {
self.enter(BorrowedUnpark::Old(&unpark), |s| s.poll())
}
pub fn wait_stream(&mut self) -> Option<Result<S::Item, S::Error>> {
ThreadNotify::with_current(|notify| {
loop {
match self.poll_stream_notify(notify, 0) {
Ok(Async::NotReady) => notify.park(),
Ok(Async::Ready(Some(e))) => return Some(Ok(e)),
Ok(Async::Ready(None)) => return None,
Err(e) => return Some(Err(e)),
}
}
})
}
}
impl<S: Sink> Spawn<S> {
#[doc(hidden)]
#[deprecated(note = "recommended to use `start_send_notify` instead")]
#[allow(deprecated)]
pub fn start_send(&mut self, value: S::SinkItem, unpark: &Arc<Unpark>)
-> StartSend<S::SinkItem, S::SinkError> {
self.enter(BorrowedUnpark::Old(unpark), |s| s.start_send(value))
}
#[deprecated(note = "recommended to use `poll_flush_notify` instead")]
#[allow(deprecated)]
#[doc(hidden)]
pub fn poll_flush(&mut self, unpark: &Arc<Unpark>)
-> Poll<(), S::SinkError> {
self.enter(BorrowedUnpark::Old(unpark), |s| s.poll_complete())
}
pub fn wait_send(&mut self, mut value: S::SinkItem)
-> Result<(), S::SinkError> {
ThreadNotify::with_current(|notify| {
loop {
value = match self.start_send_notify(value, notify, 0)? {
AsyncSink::NotReady(v) => v,
AsyncSink::Ready => return Ok(()),
};
notify.park();
}
})
}
pub fn wait_flush(&mut self) -> Result<(), S::SinkError> {
ThreadNotify::with_current(|notify| {
loop {
if self.poll_flush_notify(notify, 0)?.is_ready() {
return Ok(())
}
notify.park();
}
})
}
pub fn wait_close(&mut self) -> Result<(), S::SinkError> {
ThreadNotify::with_current(|notify| {
loop {
if self.close_notify(notify, 0)?.is_ready() {
return Ok(())
}
notify.park();
}
})
}
}
#[deprecated(note = "recommended to use `Notify` instead")]
pub trait Unpark: Send + Sync {
fn unpark(&self);
}
#[deprecated]
#[allow(deprecated)]
pub trait Executor: Send + Sync + 'static {
fn execute(&self, r: Run);
}
#[deprecated]
pub struct Run {
spawn: Spawn<Box<Future<Item = (), Error = ()> + Send>>,
inner: Arc<RunInner>,
}
#[allow(deprecated)]
struct RunInner {
mutex: UnparkMutex<Run>,
exec: Arc<Executor>,
}
#[allow(deprecated)]
impl Run {
pub fn run(self) {
let Run { mut spawn, inner } = self;
unsafe {
inner.mutex.start_poll();
loop {
match spawn.poll_future_notify(&inner, 0) {
Ok(Async::NotReady) => {}
Ok(Async::Ready(())) |
Err(()) => return inner.mutex.complete(),
}
let run = Run { spawn: spawn, inner: inner.clone() };
match inner.mutex.wait(run) {
Ok(()) => return,
Err(r) => spawn = r.spawn,
}
}
}
}
}
#[allow(deprecated)]
impl fmt::Debug for Run {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Run")
.field("contents", &"...")
.finish()
}
}
#[allow(deprecated)]
impl Notify for RunInner {
fn notify(&self, _id: usize) {
match self.mutex.notify() {
Ok(run) => self.exec.execute(run),
Err(()) => {}
}
}
}
struct ThreadNotify {
state: AtomicUsize,
mutex: Mutex<()>,
condvar: Condvar,
}
const IDLE: usize = 0;
const NOTIFY: usize = 1;
const SLEEP: usize = 2;
thread_local! {
static CURRENT_THREAD_NOTIFY: Arc<ThreadNotify> = Arc::new(ThreadNotify {
state: AtomicUsize::new(IDLE),
mutex: Mutex::new(()),
condvar: Condvar::new(),
});
}
impl ThreadNotify {
fn with_current<F, R>(f: F) -> R
where F: FnOnce(&Arc<ThreadNotify>) -> R,
{
CURRENT_THREAD_NOTIFY.with(|notify| f(notify))
}
fn park(&self) {
match self.state.compare_and_swap(NOTIFY, IDLE, Ordering::SeqCst) {
NOTIFY => return,
IDLE => {},
_ => unreachable!(),
}
let mut m = self.mutex.lock().unwrap();
match self.state.compare_and_swap(IDLE, SLEEP, Ordering::SeqCst) {
NOTIFY => {
self.state.store(IDLE, Ordering::SeqCst);
return;
}
IDLE => {},
_ => unreachable!(),
}
loop {
m = self.condvar.wait(m).unwrap();
if NOTIFY == self.state.compare_and_swap(NOTIFY, IDLE, Ordering::SeqCst) {
return;
}
}
}
}
impl Notify for ThreadNotify {
fn notify(&self, _unpark_id: usize) {
match self.state.compare_and_swap(IDLE, NOTIFY, Ordering::SeqCst) {
IDLE | NOTIFY => return,
SLEEP => {}
_ => unreachable!(),
}
let _m = self.mutex.lock().unwrap();
match self.state.compare_and_swap(SLEEP, NOTIFY, Ordering::SeqCst) {
SLEEP => {}
_ => return,
}
self.condvar.notify_one();
}
}
#[deprecated(note = "recommended to use `FuturesUnordered` instead")]
#[allow(deprecated)]
pub fn with_unpark_event<F, R>(event: UnparkEvent, f: F) -> R
where F: FnOnce() -> R
{
super::with(|task| {
let new_task = BorrowedTask {
id: task.id,
unpark: task.unpark,
events: BorrowedEvents::One(&event, &task.events),
map: task.map,
};
super::set(&new_task, f)
})
}
#[derive(Clone)]
#[deprecated(note = "recommended to use `FuturesUnordered` instead")]
#[allow(deprecated)]
pub struct UnparkEvent {
set: Arc<EventSet>,
item: usize,
}
#[allow(deprecated)]
impl UnparkEvent {
#[deprecated(note = "recommended to use `FuturesUnordered` instead")]
pub fn new(set: Arc<EventSet>, id: usize) -> UnparkEvent {
UnparkEvent {
set: set,
item: id,
}
}
fn unpark(&self) {
self.set.insert(self.item);
}
}
#[allow(deprecated)]
impl fmt::Debug for UnparkEvent {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("UnparkEvent")
.field("set", &"...")
.field("item", &self.item)
.finish()
}
}
#[deprecated(since="0.1.18", note = "recommended to use `FuturesUnordered` instead")]
pub trait EventSet: Send + Sync + 'static {
fn insert(&self, id: usize);
}
struct ArcWrapped<T>(PhantomData<T>);
impl<T: Notify + 'static> Notify for ArcWrapped<T> {
fn notify(&self, id: usize) {
unsafe {
let me: *const ArcWrapped<T> = self;
T::notify(&*(&me as *const *const ArcWrapped<T> as *const Arc<T>),
id)
}
}
fn clone_id(&self, id: usize) -> usize {
unsafe {
let me: *const ArcWrapped<T> = self;
T::clone_id(&*(&me as *const *const ArcWrapped<T> as *const Arc<T>),
id)
}
}
fn drop_id(&self, id: usize) {
unsafe {
let me: *const ArcWrapped<T> = self;
T::drop_id(&*(&me as *const *const ArcWrapped<T> as *const Arc<T>),
id)
}
}
}
unsafe impl<T: Notify + 'static> UnsafeNotify for ArcWrapped<T> {
unsafe fn clone_raw(&self) -> NotifyHandle {
let me: *const ArcWrapped<T> = self;
let arc = (*(&me as *const *const ArcWrapped<T> as *const Arc<T>)).clone();
NotifyHandle::from(arc)
}
unsafe fn drop_raw(&self) {
let mut me: *const ArcWrapped<T> = self;
let me = &mut me as *mut *const ArcWrapped<T> as *mut Arc<T>;
ptr::drop_in_place(me);
}
}
impl<T> From<Arc<T>> for NotifyHandle
where T: Notify + 'static,
{
fn from(rc: Arc<T>) -> NotifyHandle {
unsafe {
let ptr = mem::transmute::<Arc<T>, *mut ArcWrapped<T>>(rc);
NotifyHandle::new(ptr)
}
}
}
#[cfg(feature = "nightly")]
mod nightly {
use super::{TaskUnpark, UnparkEvents};
use core::marker::Unpin;
impl Unpin for TaskUnpark {}
impl Unpin for UnparkEvents {}
}