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
pub(crate) mod arena;
mod atomic;
mod execution;
mod fn_box;
pub(crate) mod object;
pub(crate) mod oneshot;
mod path;
mod scheduler;
mod synchronize;
pub(crate) mod thread;
mod vv;

use self::fn_box::FnBox;
pub(crate) use self::synchronize::Synchronize;
pub(crate) use self::path::Path;
pub(crate) use self::vv::VersionVec;

pub(crate) use self::execution::Execution;
pub(crate) use self::scheduler::Scheduler;

pub fn spawn<F>(f: F)
where
    F: FnOnce() + 'static,
{
    execution(|execution| {
        execution.new_thread();
    });

    Scheduler::spawn(Box::new(move || {
        f();
        thread_done();
    }));

}

/// Marks the current thread as blocked
pub fn park() {
    execution(|execution| {
        execution.threads.active_mut().set_blocked();
        execution.threads.active_mut().operation = None;
        execution.schedule()
    });

    Scheduler::switch();
}

/// Add an execution branch point.
fn branch<F, R>(f: F) -> R
where
    F: FnOnce(&mut Execution) -> R,
{
    let (ret, switch) = execution(|execution| {
        let ret = f(execution);
        (ret, execution.schedule())
    });

    if switch {
        Scheduler::switch();
    }

    ret
}

fn synchronize<F, R>(f: F) -> R
where
    F: FnOnce(&mut Execution) -> R
{
    execution(|execution| {
        let ret = f(execution);
        execution.threads.active_causality_inc();
        ret
    })
}

/// Yield the thread.
///
/// This enables concurrent algorithms that require other threads to make
/// progress.
pub fn yield_now() {
    let switch = execution(|execution| {
        execution.threads.active_mut().set_yield();
        execution.threads.active_mut().operation = None;
        execution.schedule()
    });

    if switch {
        Scheduler::switch();
    }
}

/// Critical section, may not branch.
pub fn critical<F, R>(f: F) -> R
where
    F: FnOnce() -> R,
{
    struct Reset;

    impl Drop for Reset {
        fn drop(&mut self) {
            execution(|execution| {
                execution.unset_critical();
            });
        }
    }

    let _reset = Reset;

    execution(|execution| {
        execution.set_critical();
    });

    f()
}

pub(crate) fn execution<F, R>(f: F) -> R
where
    F: FnOnce(&mut Execution) -> R
{
    Scheduler::with_execution(f)
}

if_futures! {
    use _futures::Future;
    use _futures::Async::Ready;
    use std::mem::replace;

    /// Block the current thread, driving `f` to completion.
    pub fn wait_future<F>(mut f: F) -> Result<F::Item, F::Error>
    where
        F: Future,
    {
        loop {
            match f.poll() {
                Ok(Ready(val)) => return Ok(val),
                Err(e) => return Err(e),
                _ => {}
            }

            let notified = execution(|execution| {
                replace(
                    &mut execution.threads.active_mut().notified,
                    false)

            });

            if !notified {
                park();
            }
        }
    }
}

pub fn thread_done() {
    execution(|execution| {
        execution.threads.active_mut().set_terminated();
        execution.threads.active_mut().operation = None;
        execution.schedule()
    });
}