stuck 0.4.1

Multi-threading scheduled task facility building on cooperative stackful coroutine
Documentation
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
//! Lightweight concurrent execution units based on coroutines.

use std::cell::Cell;
use std::collections::VecDeque;
use std::panic::{self, AssertUnwindSafe};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::{mem, ptr};

use hashbrown::HashMap;
use ignore_result::Ignore;
use static_assertions::assert_impl_all;

pub use self::session::{session, Session, SessionWaker};
use crate::coroutine::stack::StackSize;
use crate::coroutine::{self, Coroutine};
use crate::error::JoinError;
use crate::runtime::Scheduler;

mod session;

static TID_COUNTER: AtomicU64 = AtomicU64::new(1);

thread_local! {
    static TASK: Cell<Option<ptr::NonNull<Task>>> = const {  Cell::new(None) };
}

pub(crate) fn task() -> Option<ptr::NonNull<Task>> {
    TASK.with(|cell| cell.get())
}

pub(crate) fn current() -> ptr::NonNull<Task> {
    task().expect("no running task")
}

struct Scope {
    task: ptr::NonNull<Task>,
}

impl Scope {
    fn enter(task: &Task) -> Self {
        TASK.with(|cell| {
            assert!(cell.get().is_none());
            cell.set(Some(ptr::NonNull::from(task)));
        });
        Scope { task: ptr::NonNull::from(task) }
    }
}

impl Drop for Scope {
    fn drop(&mut self) {
        TASK.with(|cell| {
            let task = cell.replace(None).expect("no running task");
            assert!(self.task == task, "running task changed");
        });
    }
}

pub(crate) type FnMain = Box<dyn FnOnce()>;

/// Builder for concurrent task.
#[derive(Default)]
pub struct Builder<'a> {
    stack_size: StackSize,
    scheduler: Option<&'a Scheduler>,
}

// assert_not_impl_any!(Builder<'static>: Send);
assert_impl_all!(Builder<'static>: Send);

impl Builder<'_> {
    /// Constructs a new task builder.
    pub fn new() -> Builder<'static> {
        Builder { stack_size: StackSize::default(), scheduler: None }
    }

    pub(crate) fn with_scheduler(scheduler: &Scheduler) -> Builder<'_> {
        Builder { stack_size: StackSize::default(), scheduler: Some(scheduler) }
    }

    /// Specifies stack size for new task.
    pub fn stack_size(&mut self, stack_size: StackSize) -> &mut Self {
        self.stack_size = stack_size;
        self
    }

    /// Spawns a concurrent task and returns a [JoinHandle] for it.
    ///
    /// See [spawn] for more details
    pub fn spawn<F, T>(&mut self, f: F) -> JoinHandle<T>
    where
        F: FnOnce() -> T,
        F: Send + 'static,
        T: Send + 'static,
    {
        let scheduler = self.scheduler.or_else(Scheduler::try_current).expect("no runtime");
        let (session, waker) = session();
        let handle = JoinHandle::new(session);
        let main: FnMain = Box::new(move || {
            let result = panic::catch_unwind(AssertUnwindSafe(f));
            let task = unsafe { current().as_mut() };
            let co = unsafe { coroutine::current().as_mut() };
            task.status = Status::Completing;
            co.suspend();
            waker.set_result(result);
        });
        let task = Task::new(main, self.stack_size);
        scheduler.sched(task);
        handle
    }
}

/// JoinHandle provides method to retrieve result of associated concurrent task.
pub struct JoinHandle<T: Send + 'static> {
    session: Session<T>,
}

unsafe impl<T: Send + 'static> Send for JoinHandle<T> {}

assert_impl_all!(JoinHandle<()>: Send);

impl<T: Send + 'static> JoinHandle<T> {
    fn new(session: Session<T>) -> JoinHandle<T> {
        JoinHandle { session }
    }

    /// Waits for associated task to finish and returns its result.
    pub fn join(self) -> Result<T, JoinError> {
        let joint = unsafe { self.session.into_joint() };
        joint.join(None::<fn()>).map_err(JoinError::new)
    }
}

// Yielding point.
pub(crate) trait Yielding {
    fn interrupt(&self, reason: &'static str) -> bool;
}

static INTERRUPTIBLE: Interruptible = Interruptible {};
static UNINTERRUPTIBLE: Uninterruptible = Uninterruptible {};

struct Interruptible {}
struct Uninterruptible {}

impl Yielding for Interruptible {
    fn interrupt(&self, _reason: &'static str) -> bool {
        true
    }
}

impl Yielding for Uninterruptible {
    fn interrupt(&self, _reason: &'static str) -> bool {
        false
    }
}

pub(crate) enum Interruption<'a, T: FnOnce() = fn()> {
    Cancellation(T),
    Interruptible(&'a dyn Yielding),
    Uninterruptible,
}

#[derive(PartialEq, Eq, Clone, Copy, Debug)]
enum Status {
    Running,
    Completing,
    Completed,
}

pub(crate) struct Task {
    id: u64,

    // main is special as task will terminate after main terminated
    main: ptr::NonNull<Coroutine>,

    status: Status,

    yielding: bool,

    // We are running
    running_coroutines: VecDeque<ptr::NonNull<Coroutine>>,

    // Yielding cpu in this run.
    yielding_coroutines: Vec<ptr::NonNull<Coroutine>>,

    // Suspending for events inside this task
    suspending_coroutines: HashMap<ptr::NonNull<Coroutine>, &'static dyn Yielding>,

    // Blocking for events outside this task
    blocking_coroutines: HashMap<ptr::NonNull<Coroutine>, &'static dyn Yielding>,

    // Unblocking by events from outside this task
    unblocking_coroutines: Mutex<Vec<ptr::NonNull<Coroutine>>>,

    // Guarded by above mutex.
    running: Cell<bool>,
}

unsafe impl Sync for Task {}
unsafe impl Send for Task {}

pub(crate) enum SchedFlow {
    Yield,
    Block,
    Cease,
}

impl Task {
    fn new(f: Box<dyn FnOnce()>, stack_size: StackSize) -> Arc<Task> {
        let main = Coroutine::new(f, stack_size);
        Arc::new(Self::with_main(main))
    }

    fn with_main(main: Box<Coroutine>) -> Task {
        let co = ptr::NonNull::from(Box::leak(main));
        let id = TID_COUNTER.fetch_add(1, Ordering::Relaxed);
        Task {
            id,
            main: co,
            status: Status::Running,
            yielding: false,
            running_coroutines: VecDeque::from([co]),
            yielding_coroutines: Vec::with_capacity(5),
            suspending_coroutines: HashMap::new(),
            blocking_coroutines: HashMap::new(),
            unblocking_coroutines: Mutex::new(Default::default()),
            running: Cell::new(true),
        }
    }

    pub fn id(&self) -> u64 {
        self.id
    }

    fn drop_coroutine(co: ptr::NonNull<Coroutine>) {
        drop(unsafe { Box::from_raw(co.as_ptr()) });
    }

    fn run_coroutine(&mut self, mut co: ptr::NonNull<Coroutine>) -> coroutine::Status {
        let status = unsafe { co.as_mut().resume() };
        if status != coroutine::Status::Completed {
            return status;
        }
        if co == self.main {
            self.status = Status::Completed;
        }
        Self::drop_coroutine(co);
        status
    }

    pub fn unblock(&mut self, block: bool) -> bool {
        if self.blocking_coroutines.is_empty() {
            return false;
        }
        let mut unblockings = self.unblocking_coroutines.lock().unwrap();
        let mut unblocked = Vec::new();
        mem::swap(&mut unblocked, &mut unblockings);
        if block && unblocked.is_empty() {
            self.running.set(false);
            return false;
        }
        drop(unblockings);
        for co in unblocked.into_iter() {
            if self.blocking_coroutines.remove(&co).is_some() {
                self.running_coroutines.push_back(co);
            }
        }
        true
    }

    fn interrupt(&mut self, msg: &'static str) {
        for co in self.yielding_coroutines.drain(..) {
            self.running_coroutines.push_back(co);
        }
        for (mut co, yielding) in self.suspending_coroutines.drain() {
            yielding.interrupt(msg);
            self.running_coroutines.push_back(co);
            let co = unsafe { co.as_mut() };
            co.status = co.status.into_abort();
        }
        for (mut co, _) in self
            .blocking_coroutines
            .drain_filter(|co, yielding| unsafe { !co.as_ref().is_cancelling() && yielding.interrupt(msg) })
        {
            self.running_coroutines.push_back(co);
            let co = unsafe { co.as_mut() };
            co.status = co.status.into_abort();
        }
        self.unblock(false);
    }

    pub fn abort(&mut self, msg: &'static str) {
        loop {
            self.interrupt(msg);
            if self.running_coroutines.is_empty() {
                if !self.blocking_coroutines.is_empty() {
                    // Someone else win session wakeup. Let's snooze.
                    std::hint::spin_loop();
                    continue;
                }
                break;
            }
            while let Some(co) = self.running_coroutines.pop_front() {
                self.run_coroutine(co);
            }
        }
        let status = self.run_coroutine(self.main);
        assert_eq!(status, coroutine::Status::Completed);
        assert_eq!(self.status, Status::Completed);
    }

    // Grab this task to runq. Return false if waker win.
    pub fn grab(&self) -> bool {
        let _locker = self.unblocking_coroutines.lock().unwrap();
        !self.running.replace(true)
    }

    pub fn sched(&mut self) -> SchedFlow {
        let _scope = Scope::enter(self);
        self.running_coroutines.extend(self.yielding_coroutines.drain(..));
        self.unblock(false);
        while !self.yielding && self.status == Status::Running && !self.running_coroutines.is_empty() {
            let co = unsafe { self.running_coroutines.pop_front().unwrap_unchecked() };
            self.run_coroutine(co);
        }
        if self.status == Status::Completing {
            self.abort("task main terminated");
        }
        self.yielding = false;
        if !self.yielding_coroutines.is_empty() {
            SchedFlow::Yield
        } else if self.blocking_coroutines.is_empty() {
            if !self.suspending_coroutines.is_empty() {
                self.abort("deadlock suspending coroutines");
            }
            SchedFlow::Cease
        } else if self.unblock(true) {
            SchedFlow::Yield
        } else {
            SchedFlow::Block
        }
    }

    pub fn resume(&mut self, co: ptr::NonNull<Coroutine>) {
        if self.suspending_coroutines.remove(&co).is_some() {
            self.running_coroutines.push_back(co);
        }
    }

    pub fn suspend(&mut self, mut co: ptr::NonNull<Coroutine>, yielding: &dyn Yielding) {
        assert!(co == coroutine::current(), "suspend: running coroutine changed");
        let yielding = unsafe { std::mem::transmute::<&dyn Yielding, &'_ dyn Yielding>(yielding) };
        self.suspending_coroutines.insert(co, yielding);
        let co = unsafe { co.as_mut() };
        co.suspend();
    }

    fn block<F: FnOnce()>(&mut self, mut co: ptr::NonNull<Coroutine>, interruption: Interruption<'_, F>) {
        assert!(co == coroutine::current(), "Session.block: running coroutine changed");
        match interruption {
            Interruption::Interruptible(yielding) => {
                let yielding = unsafe { std::mem::transmute::<&dyn Yielding, &'_ dyn Yielding>(yielding) };
                self.blocking_coroutines.insert(co, yielding);
                let co = unsafe { co.as_mut() };
                co.suspend();
            },
            Interruption::Uninterruptible => {
                self.blocking_coroutines.insert(co, &UNINTERRUPTIBLE);
                let co = unsafe { co.as_mut() };
                co.suspend();
            },
            Interruption::Cancellation(cancellation) => {
                self.blocking_coroutines.insert(co, &INTERRUPTIBLE);
                let co = unsafe { co.as_mut() };
                co.suspend();
                if co.status == coroutine::Status::Aborting {
                    co.status = coroutine::Status::Cancelling;
                    std::panic::catch_unwind(AssertUnwindSafe(cancellation)).ignore();
                    co.status = coroutine::Status::Aborting;
                    // Cancellation completed, wakeup caller to check its completion.
                }
            },
        }
    }

    pub fn yield_coroutine(&mut self, mut co: ptr::NonNull<Coroutine>) {
        if self.status != Status::Running {
            return;
        }
        self.yielding_coroutines.push(co);
        let co = unsafe { co.as_mut() };
        co.suspend();
    }

    fn yield_task(&mut self) {
        self.yielding = true;
        let co = coroutine::current();
        self.yield_coroutine(co);
    }

    fn wake(&self, co: ptr::NonNull<Coroutine>) -> bool {
        let mut unblockings = self.unblocking_coroutines.lock().unwrap();
        let waking = if unblockings.is_empty() && !self.running.get() {
            self.running.set(true);
            true
        } else {
            false
        };
        unblockings.push(co);
        mem::drop(unblockings);
        waking
    }

    pub fn spawn(&mut self, f: impl FnOnce() + 'static, stack_size: StackSize) {
        if self.status != Status::Running {
            return;
        }
        let f = Box::new(f);
        let co = ptr::NonNull::from(Box::leak(Coroutine::new(f, stack_size)));
        self.running_coroutines.push_back(co);
    }
}

/// Yields task for next scheduling cycle.
pub fn yield_now() {
    let t = unsafe { current().as_mut() };
    t.yield_task();
}

/// Spawns a concurrent task and returns a [JoinHandle] for it.
///
/// The given fn serves as `main` in spawned task. All other coroutines in that task will be
/// aborted through [panic!] when given fn completed.
pub fn spawn<F, T>(f: F) -> JoinHandle<T>
where
    F: FnOnce() -> T,
    F: Send + 'static,
    T: Send + 'static,
{
    Builder::new().spawn(f)
}

#[cfg(test)]
mod tests {
    use std::sync::{Arc, Mutex};

    use pretty_assertions::assert_eq;

    use crate::{coroutine, task, time};

    #[crate::test(crate = "crate", parallelism = 1)]
    fn yield_now() {
        let five = task::spawn(|| {
            let shared_value = Arc::new(Mutex::new(0));
            let shared_task_value = shared_value.clone();
            let shared_coroutine_value = shared_value.clone();
            coroutine::spawn(move || {
                let mut value = shared_coroutine_value.lock().unwrap();
                if *value == 0 {
                    *value = 6;
                }
            });
            task::spawn(move || {
                let mut value = shared_task_value.lock().unwrap();
                if *value == 0 {
                    *value = 5;
                }
            });
            task::yield_now();
            let value = shared_value.lock().unwrap();
            *value
        });
        assert_eq!(5, five.join().unwrap());
    }

    #[crate::test(crate = "crate")]
    fn panic() {
        const REASON: &'static str = "oooooops";
        let t = task::spawn(|| panic!("{}", REASON));
        let err = t.join().unwrap_err();
        assert!(err.to_string().contains(REASON))
    }

    #[crate::test(crate = "crate")]
    fn main_coroutine() {
        use std::cell::Cell;
        use std::rc::Rc;
        use std::sync::atomic::{AtomicI32, Ordering};
        use std::time::Duration;

        use scopeguard::defer;

        let read = Arc::new(AtomicI32::new(0));
        let write = read.clone();
        let t = task::spawn(|| {
            let cell = Rc::new(Cell::new(0));
            coroutine::spawn({
                let cell = cell.clone();
                move || {
                    defer! {
                        std::thread::sleep(Duration::from_secs(2));
                        write.store(cell.get(), Ordering::Relaxed);
                    }
                    time::sleep(Duration::from_secs(20));
                    cell.set(10);
                }
            });
            coroutine::spawn({
                let cell = cell.clone();
                move || {
                    cell.set(5);
                }
            })
            .join()
            .unwrap();
            cell.get()
        });
        assert_eq!(t.join().unwrap(), 5);
        assert_eq!(read.load(Ordering::Relaxed), 5);
    }
}