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
use crate::common::Current;
use crate::scheduler::SchedulerImpl;
use std::ffi::c_void;

thread_local! {
    static SCHEDULER: std::cell::RefCell<std::collections::VecDeque<*const c_void>> = std::cell::RefCell::new(std::collections::VecDeque::new());
}

impl<'s> Current<'s> for SchedulerImpl<'s> {
    #[allow(clippy::ptr_as_ptr)]
    fn init_current(current: &Self)
    where
        Self: Sized,
    {
        SCHEDULER.with(|s| {
            s.borrow_mut()
                .push_front(current as *const _ as *const c_void);
        });
    }

    fn current() -> Option<&'s Self>
    where
        Self: Sized,
    {
        SCHEDULER.with(|s| {
            s.borrow()
                .front()
                .map(|ptr| unsafe { &*(*ptr).cast::<SchedulerImpl<'s>>() })
        })
    }

    fn clean_current()
    where
        Self: Sized,
    {
        SCHEDULER.with(|s| _ = s.borrow_mut().pop_front());
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::common::Named;
    use crate::scheduler::{SchedulableCoroutine, SchedulableSuspender, Scheduler};

    #[test]
    fn test_current() -> std::io::Result<()> {
        let parent_name = "parent";
        let scheduler = SchedulerImpl::new(
            String::from(parent_name),
            crate::constants::DEFAULT_STACK_SIZE,
        );
        _ = scheduler.submit_co(
            move |_, _| {
                assert!(SchedulableCoroutine::current().is_some());
                assert!(SchedulableSuspender::current().is_some());
                assert_eq!(parent_name, SchedulerImpl::current().unwrap().get_name());
                assert_eq!(parent_name, SchedulerImpl::current().unwrap().get_name());

                let child_name = "child";
                let scheduler = SchedulerImpl::new(
                    String::from(child_name),
                    crate::constants::DEFAULT_STACK_SIZE,
                );
                _ = scheduler
                    .submit_co(
                        move |_, _| {
                            assert!(SchedulableCoroutine::current().is_some());
                            assert!(SchedulableSuspender::current().is_some());
                            assert_eq!(child_name, SchedulerImpl::current().unwrap().get_name());
                            assert_eq!(child_name, SchedulerImpl::current().unwrap().get_name());
                            None
                        },
                        None,
                    )
                    .unwrap();
                scheduler.try_schedule().unwrap();

                assert_eq!(parent_name, SchedulerImpl::current().unwrap().get_name());
                assert_eq!(parent_name, SchedulerImpl::current().unwrap().get_name());
                None
            },
            None,
        )?;
        scheduler.try_schedule()
    }
}