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
mod fiber;
mod yielder;

pub use self::{fiber::FiberProc, yielder::Yielder};

use crate::{fib::FiberState, sv::Switch, thr::ThrSv};
use core::mem::ManuallyDrop;

pub union Data<I, O> {
    input: ManuallyDrop<I>,
    output: ManuallyDrop<O>,
    _align: [u32; 0],
}

type ProcData<I, Y, R> = Data<I, FiberState<Y, R>>;

/// Creates a stackful fiber from the closure `f`.
///
/// The first argument to the closure is
/// [`Fiber::Input`](crate::fib::Fiber::Input).
///
/// This type of fiber yields on each [`proc_yield`](Yielder::proc_yield) call
/// on the second [`Yielder`] argument.
///
/// # Panics
///
/// * If MPU not present.
/// * If `stack_size` is insufficient to store the initial frame.
#[inline]
pub fn new_proc<Sv, I, Y, R, F>(stack_size: usize, f: F) -> FiberProc<Sv, I, Y, R, F>
where
    Sv: Switch<ProcData<I, Y, R>>,
    F: FnMut(I, Yielder<Sv, I, Y, R>) -> R,
    F: Send + 'static,
    I: Send + 'static,
    Y: Send + 'static,
    R: Send + 'static,
{
    unsafe { FiberProc::new(stack_size, false, false, f) }
}

/// Creates a stackful fiber from the closure `f`, without memory protection.
///
/// The first argument to the closure is
/// [`Fiber::Input`](crate::fib::Fiber::Input).
///
/// This type of fiber yields on each [`proc_yield`](Yielder::proc_yield) call
/// on the second [`Yielder`] argument.
///
/// # Safety
///
/// Stack overflow is unchecked.
///
/// # Panics
///
/// * If `stack_size` is insufficient to store the initial frame.
#[inline]
pub unsafe fn new_proc_unchecked<Sv, I, Y, R, F>(
    stack_size: usize,
    f: F,
) -> FiberProc<Sv, I, Y, R, F>
where
    Sv: Switch<ProcData<I, Y, R>>,
    F: FnMut(I, Yielder<Sv, I, Y, R>) -> R,
    F: Send + 'static,
    I: Send + 'static,
    Y: Send + 'static,
    R: Send + 'static,
{
    unsafe { FiberProc::new(stack_size, false, true, f) }
}

/// Creates a stackful fiber from the closure `f`, which will run in
/// unprivileged mode.
///
/// The first argument to the closure is
/// [`Fiber::Input`](crate::fib::Fiber::Input).
///
/// This type of fiber yields on each [`proc_yield`](Yielder::proc_yield) call
/// on the second [`Yielder`] argument.
///
/// # Panics
///
/// * If MPU not present.
/// * If `stack_size` is insufficient to store the initial frame.
#[inline]
pub fn new_proc_unprivileged<Sv, I, Y, R, F>(stack_size: usize, f: F) -> FiberProc<Sv, I, Y, R, F>
where
    Sv: Switch<ProcData<I, Y, R>>,
    F: FnMut(I, Yielder<Sv, I, Y, R>) -> R,
    F: Send + 'static,
    I: Send + 'static,
    Y: Send + 'static,
    R: Send + 'static,
{
    unsafe { FiberProc::new(stack_size, true, false, f) }
}

/// Creates a stackful fiber from the closure `f`, which will run in
/// unprivileged mode, without memory protection.
///
/// The first argument to the closure is
/// [`Fiber::Input`](crate::fib::Fiber::Input).
///
/// This type of fiber yields on each [`proc_yield`](Yielder::proc_yield) call
/// on the second [`Yielder`] argument.
///
/// # Safety
///
/// Stack overflow is unchecked.
///
/// # Panics
///
/// * If `stack_size` is insufficient to store the initial frame.
#[inline]
pub unsafe fn new_proc_unprivileged_unchecked<Sv, I, Y, R, F>(
    stack_size: usize,
    f: F,
) -> FiberProc<Sv, I, Y, R, F>
where
    Sv: Switch<ProcData<I, Y, R>>,
    F: FnMut(I, Yielder<Sv, I, Y, R>) -> R,
    F: Send + 'static,
    I: Send + 'static,
    Y: Send + 'static,
    R: Send + 'static,
{
    unsafe { FiberProc::new(stack_size, true, true, f) }
}

/// Extends [`ThrToken`](crate::thr::ThrToken) types with `add_proc` methods.
pub trait ThrFiberProc: ThrSv {
    /// Adds a stackful fiber for the closure `f` to the fiber chain.
    ///
    /// # Panics
    ///
    /// * If MPU not present.
    /// * If `stack_size` is insufficient to store the initial frame.
    #[inline]
    fn add_proc<F>(self, stack_size: usize, mut f: F)
    where
        F: FnMut(Yielder<Self::Sv, (), (), ()>),
        F: Send + 'static,
        Self::Sv: Switch<ProcData<(), (), ()>>,
    {
        self.add_fib(new_proc(stack_size, move |(), yielder| f(yielder)))
    }

    /// Adds a stackful fiber for the closure `f` to the fiber chain, without
    /// memory protection.
    ///
    /// # Safety
    ///
    /// Stack overflow is unchecked.
    ///
    /// # Panics
    ///
    /// * If `stack_size` is insufficient to store the initial frame.
    #[inline]
    unsafe fn add_proc_unchecked<F>(self, stack_size: usize, mut f: F)
    where
        F: FnMut(Yielder<Self::Sv, (), (), ()>),
        F: Send + 'static,
        Self::Sv: Switch<ProcData<(), (), ()>>,
    {
        self.add_fib(unsafe { new_proc_unchecked(stack_size, move |(), yielder| f(yielder)) })
    }

    /// Adds a stackful fiber for the closure `f` to the fiber chain, which will
    /// run in unprivileged mode.
    ///
    /// # Panics
    ///
    /// * If MPU not present.
    /// * If `stack_size` is insufficient to store the initial frame.
    #[inline]
    fn add_proc_unprivileged<F>(self, stack_size: usize, mut f: F)
    where
        F: FnMut(Yielder<Self::Sv, (), (), ()>),
        F: Send + 'static,
        Self::Sv: Switch<ProcData<(), (), ()>>,
    {
        self.add_fib(new_proc_unprivileged(stack_size, move |(), yielder| f(yielder)))
    }

    /// Adds a stackful fiber for the closure `f` to the fiber chain, which will
    /// run in unprivileged mode, without memory protection.
    ///
    /// # Safety
    ///
    /// Stack overflow is unchecked.
    ///
    /// # Panics
    ///
    /// * If `stack_size` is insufficient to store the initial frame.
    #[inline]
    unsafe fn add_proc_unprivileged_unchecked<F>(self, stack_size: usize, mut f: F)
    where
        F: FnMut(Yielder<Self::Sv, (), (), ()>),
        F: Send + 'static,
        Self::Sv: Switch<ProcData<(), (), ()>>,
    {
        self.add_fib(unsafe {
            new_proc_unprivileged_unchecked(stack_size, move |(), yielder| f(yielder))
        })
    }
}

impl<T: ThrSv> ThrFiberProc for T {}

impl<I, O> Data<I, O> {
    fn from_input(input: I) -> Self {
        Self { input: ManuallyDrop::new(input) }
    }

    fn from_output(output: O) -> Self {
        Self { output: ManuallyDrop::new(output) }
    }

    unsafe fn into_input(self) -> I {
        unsafe { ManuallyDrop::into_inner(self.input) }
    }

    unsafe fn into_output(self) -> O {
        unsafe { ManuallyDrop::into_inner(self.output) }
    }
}