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
use std::{future::Future, sync::Arc, sync::atomic::AtomicUsize, sync::atomic::Ordering};

// The Callbacks static holds a pointer to the global callbacks. It is protected by
// the STATE static which determines whether `CBS` has been initialized yet.
static mut CBS: Option<Arc<dyn CallbacksApi>> = None;

static STATE: AtomicUsize = AtomicUsize::new(0);

// There are three different states that we care about: the callback's
// uninitialized, the callback's initializing (set_cbs's been called but
// CBS hasn't actually been set yet), or the callbacks's active.
const UNINITIALIZED: usize = 0;
const INITIALIZING: usize = 1;
const INITIALIZED: usize = 2;

trait CallbacksApi {
    fn before(&self) -> Option<*const ()>;
    fn enter(&self, _: *const ()) -> *const ();
    fn exit(&self, _: *const ());
    fn after(&self, _: *const ());
}

#[allow(clippy::struct_field_names)]
struct Callbacks<A, B, C, D> {
    f_before: A,
    f_enter: B,
    f_exit: C,
    f_after: D,
}

impl<A, B, C, D> CallbacksApi for Callbacks<A, B, C, D>
where
    A: Fn() -> Option<*const ()> + 'static,
    B: Fn(*const ()) -> *const () + 'static,
    C: Fn(*const ()) + 'static,
    D: Fn(*const ()) + 'static,
{
    fn before(&self) -> Option<*const ()> {
        (self.f_before)()
    }
    fn enter(&self, d: *const ()) -> *const () {
        (self.f_enter)(d)
    }
    fn exit(&self, d: *const ()) {
        (self.f_exit)(d);
    }
    fn after(&self, d: *const ()) {
        (self.f_after)(d);
    }
}

pub(crate) struct Data {
    cb: &'static dyn CallbacksApi,
    ptr: *const (),
}

impl Data {
    #[allow(clippy::if_not_else)]
    pub(crate) fn load() -> Option<Data> {
        // `Acquire` pairs with the `Release` store in `set_cbs`, so `CBS` is visible
        let cb = if STATE.load(Ordering::Acquire) != INITIALIZED {
            None
        } else {
            #[allow(static_mut_refs)]
            unsafe {
                Some(CBS.as_ref().map(AsRef::as_ref).unwrap())
            }
        };

        if let Some(cb) = cb
            && let Some(ptr) = cb.before()
        {
            return Some(Data { cb, ptr });
        }
        None
    }

    pub(crate) fn run<F, R>(&mut self, f: F) -> R
    where
        F: FnOnce() -> R,
    {
        struct Exit(&'static dyn CallbacksApi, *const ());

        impl Drop for Exit {
            fn drop(&mut self) {
                self.0.exit(self.1);
            }
        }

        // `exit` also runs if `f` panics, so the context set by `enter` is restored
        let _exit = Exit(self.cb, self.cb.enter(self.ptr));
        f()
    }
}

/// Wraps a future so that task callbacks, if registered, run around each poll.
///
/// Always returns the same future type, so spawning code is generated once
/// per future rather than once per callback mode.
pub(crate) fn wrap<F: Future>(fut: F) -> impl Future<Output = F::Output> {
    let mut data = Data::load();
    async move {
        let mut f = std::pin::pin!(fut);
        std::future::poll_fn(|cx| match data.as_mut() {
            Some(data) => data.run(|| f.as_mut().poll(cx)),
            None => f.as_mut().poll(cx),
        })
        .await
    }
}

#[cfg(any(feature = "tokio", feature = "compio"))]
pub(crate) use remote::RemoteJoin;

#[cfg(any(feature = "tokio", feature = "compio"))]
mod remote {
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::task::{Context, Poll};
    use std::{future::Future, future::poll_fn, pin::Pin, pin::pin, sync::Arc};

    use async_channel::Sender;
    use atomic_waker::AtomicWaker;

    use crate::arbiter::ArbiterCommand;

    #[derive(Debug, Default)]
    struct Cancel {
        cancelled: AtomicBool,
        waker: AtomicWaker,
    }

    /// A task spawned on an arbiter from any thread, cancelling it drops the
    /// future on the arbiter thread.
    #[derive(Debug)]
    pub(crate) struct RemoteJoin<T> {
        rx: oneshot::AsyncReceiver<T>,
        cancel: Arc<Cancel>,
    }

    impl<T: Send + 'static> RemoteJoin<T> {
        pub(crate) fn spawn<F>(sender: &Sender<ArbiterCommand>, future: F) -> Self
        where
            F: Future<Output = T> + Send + 'static,
        {
            let (tx, rx) = oneshot::async_channel();
            let cancel = Arc::new(Cancel::default());
            let state = cancel.clone();
            let _ = sender.try_send(ArbiterCommand::Execute(Box::pin(async move {
                let mut fut = pin!(future);
                let res = poll_fn(|cx| {
                    state.waker.register(cx.waker());
                    if state.cancelled.load(Ordering::Acquire) {
                        Poll::Ready(None)
                    } else {
                        fut.as_mut().poll(cx).map(Some)
                    }
                })
                .await;
                if let Some(res) = res {
                    let _ = tx.send(res);
                }
            })));
            Self { rx, cancel }
        }
    }

    impl<T> RemoteJoin<T> {
        /// Cancels the task, a task that did not start never runs.
        pub(crate) fn cancel(self) {
            self.cancel.cancelled.store(true, Ordering::Release);
            self.cancel.waker.wake();
        }

        pub(crate) fn is_finished(&self) -> bool {
            self.rx.is_closed()
        }
    }

    impl<T> Future for RemoteJoin<T> {
        type Output = Result<T, oneshot::RecvError>;

        fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
            Pin::new(&mut self.rx).poll(cx)
        }
    }
}

impl Drop for Data {
    fn drop(&mut self) {
        self.cb.after(self.ptr);
    }
}

/// # Safety
///
/// The user must ensure that the pointer returned by `before` has a `'static` lifetime.
/// This pointer will be owned by the spawned task for the duration of that task, and
/// ownership will be returned to the user at the end of the task via `after`.
/// The pointer remains opaque to the runtime.
///
/// Does nothing if task callbacks have already been set, use
/// [`task_opt_callbacks`] to check whether they were set.
pub unsafe fn task_callbacks<FBefore, FEnter, FExit, FAfter>(
    f_before: FBefore,
    f_enter: FEnter,
    f_exit: FExit,
    f_after: FAfter,
) where
    FBefore: Fn() -> Option<*const ()> + 'static + Sync,
    FEnter: Fn(*const ()) -> *const () + 'static + Sync,
    FExit: Fn(*const ()) + 'static + Sync,
    FAfter: Fn(*const ()) + 'static + Sync,
{
    let new = Arc::new(Callbacks {
        f_before,
        f_enter,
        f_exit,
        f_after,
    });
    let _ = set_cbs(new);
}

/// # Safety
///
/// The user must ensure that the pointer returned by `before` has a `'static` lifetime.
/// This pointer will be owned by the spawned task for the duration of that task, and
/// ownership will be returned to the user at the end of the task via `after`.
/// The pointer remains opaque to the runtime.
///
/// Returns false if task callbacks have already been set.
pub unsafe fn task_opt_callbacks<FBefore, FEnter, FExit, FAfter>(
    f_before: FBefore,
    f_enter: FEnter,
    f_exit: FExit,
    f_after: FAfter,
) -> bool
where
    FBefore: Fn() -> Option<*const ()> + Sync + 'static,
    FEnter: Fn(*const ()) -> *const () + Sync + 'static,
    FExit: Fn(*const ()) + Sync + 'static,
    FAfter: Fn(*const ()) + Sync + 'static,
{
    let new = Arc::new(Callbacks {
        f_before,
        f_enter,
        f_exit,
        f_after,
    });
    set_cbs(new).is_ok()
}

fn set_cbs(cbs: Arc<dyn CallbacksApi>) -> Result<(), ()> {
    match STATE.compare_exchange(
        UNINITIALIZED,
        INITIALIZING,
        Ordering::Acquire,
        Ordering::Relaxed,
    ) {
        Ok(UNINITIALIZED) => {
            unsafe {
                CBS = Some(cbs);
            }
            STATE.store(INITIALIZED, Ordering::Release);
            Ok(())
        }
        Err(INITIALIZING) => {
            while STATE.load(Ordering::Relaxed) == INITIALIZING {
                std::hint::spin_loop();
            }
            Err(())
        }
        _ => Err(()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn load_callbacks_set_by_other_thread() {
        let hnd = std::thread::spawn(|| unsafe {
            task_opt_callbacks(|| Some(std::ptr::null()), |p| p, |_| {}, |_| {})
        });
        // observes callbacks set by another thread without synchronization
        while Data::load().is_none() {
            std::hint::spin_loop();
        }
        assert!(hnd.join().unwrap());
    }

    #[test]
    fn exit_runs_when_poll_panics() {
        use std::{cell::Cell, panic, pin::pin, task::Context, task::Waker};

        // callbacks can be set once per process, so check them in a child process
        const CHILD: &str = "NTEX_RT_TASK_EXIT_CHILD";
        if std::env::var_os(CHILD).is_none() {
            let out = std::process::Command::new(std::env::current_exe().unwrap())
                .args(["--exact", "task::tests::exit_runs_when_poll_panics"])
                .env(CHILD, "1")
                .output()
                .unwrap();
            assert!(
                out.status.success(),
                "{}",
                String::from_utf8_lossy(&out.stdout)
            );
            return;
        }

        thread_local! {
            static DEPTH: Cell<isize> = const { Cell::new(0) };
            static AFTER: Cell<usize> = const { Cell::new(0) };
        }
        assert!(unsafe {
            task_opt_callbacks(
                || Some(std::ptr::null()),
                |p| {
                    DEPTH.set(DEPTH.get() + 1);
                    p
                },
                |_| DEPTH.set(DEPTH.get() - 1),
                |_| AFTER.set(AFTER.get() + 1),
            )
        });

        let mut fut = Box::pin(wrap(async { panic!("poll panic") }));
        let res = panic::catch_unwind(panic::AssertUnwindSafe(|| {
            let _ = fut.as_mut().poll(&mut Context::from_waker(Waker::noop()));
        }));
        assert!(res.is_err());
        assert_eq!(DEPTH.get(), 0);
        drop(fut);
        assert_eq!(AFTER.get(), 1);

        let mut fut = pin!(wrap(async { 1 }));
        let res = fut.as_mut().poll(&mut Context::from_waker(Waker::noop()));
        assert_eq!(res, std::task::Poll::Ready(1));
        assert_eq!(DEPTH.get(), 0);
    }

    #[test]
    fn callbacks_set_once_and_wrap_spawned_tasks() {
        use std::{cell::Cell, sync::atomic::AtomicUsize};

        use crate::{System, testing::TestRunner};

        const CHILD: &str = "NTEX_RT_TASK_CALLBACKS_CHILD";
        static DATA: u8 = 0;
        static CTX: u8 = 0;
        static BEFORE: AtomicUsize = AtomicUsize::new(0);
        static AFTER: AtomicUsize = AtomicUsize::new(0);
        thread_local! {
            static TRACK: Cell<bool> = const { Cell::new(false) };
            static INSIDE: Cell<bool> = const { Cell::new(false) };
        }
        fn data() -> *const () {
            (&raw const DATA).cast()
        }
        fn ctx() -> *const () {
            (&raw const CTX).cast()
        }

        // callbacks can be set once per process, so check them in a child process
        if std::env::var_os(CHILD).is_none() {
            let out = std::process::Command::new(std::env::current_exe().unwrap())
                .args([
                    "--exact",
                    "task::tests::callbacks_set_once_and_wrap_spawned_tasks",
                ])
                .env(CHILD, "1")
                .output()
                .unwrap();
            assert!(
                out.status.success(),
                "{}",
                String::from_utf8_lossy(&out.stdout)
            );
            return;
        }

        unsafe {
            task_callbacks(
                || {
                    TRACK.get().then(|| {
                        BEFORE.fetch_add(1, Ordering::Relaxed);
                        data()
                    })
                },
                |p| {
                    assert_eq!(p, data());
                    INSIDE.set(true);
                    ctx()
                },
                |p| {
                    // receives the value returned by `enter`
                    assert_eq!(p, ctx());
                    INSIDE.set(false);
                },
                |p| {
                    assert_eq!(p, data());
                    AFTER.fetch_add(1, Ordering::Relaxed);
                },
            );
        }
        // callbacks are set once, later registrations are ignored
        unsafe {
            task_callbacks(
                || unreachable!(),
                |_| unreachable!(),
                |_| unreachable!(),
                |_| unreachable!(),
            );
            assert!(!task_opt_callbacks(
                || unreachable!(),
                |_| unreachable!(),
                |_| unreachable!(),
                |_| unreachable!(),
            ));
        }

        System::new("callbacks", TestRunner).block_on(async {
            TRACK.set(true);
            let hnd = crate::spawn(async { INSIDE.get() });
            TRACK.set(false);
            assert_eq!(BEFORE.load(Ordering::Relaxed), 1);
            // polled with the context set by `enter`
            assert!(hnd.await.unwrap());
            assert!(!INSIDE.get());
            assert_eq!(AFTER.load(Ordering::Relaxed), 1);

            // `before` returned `None`, the task runs without callbacks
            assert!(!crate::spawn(async { INSIDE.get() }).await.unwrap());
            assert_eq!(BEFORE.load(Ordering::Relaxed), 1);
            assert_eq!(AFTER.load(Ordering::Relaxed), 1);
        });
    }
}