moirai-executor 0.7.1

Hybrid executor implementation for Moirai concurrency library
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
//! Scheduled job representation.

use core::{
    mem::{MaybeUninit, align_of, size_of},
    ptr,
};
use std::panic::{AssertUnwindSafe, catch_unwind};

const INLINE_JOB_WORDS: usize = 14;

/// Job stored inside worker queues.
pub(crate) struct ScheduledJob {
    job: InlineJob,
}

#[repr(C)]
pub(crate) struct InlineJob {
    storage: InlineJobStorage,
    execute: unsafe fn(*mut InlineJobStorage, usize) -> bool,
    drop: unsafe fn(*mut InlineJobStorage),
}

#[repr(C)]
struct InlineJobStorage {
    words: [MaybeUninit<usize>; INLINE_JOB_WORDS],
}

struct ScopedJob<F, Complete> {
    task: F,
    complete: Complete,
}

impl ScheduledJob {
    /// Create a scheduled job from a worker-aware closure.
    pub(crate) fn new<F>(task: F) -> Self
    where
        F: FnOnce(usize) + Send + 'static,
    {
        if inline_job_fits::<F>() {
            Self {
                job: InlineJob::new(task),
            }
        } else {
            Self {
                job: InlineJob::new(boxed_job(task)),
            }
        }
    }

    /// Create a scheduled job from a scoped worker-aware closure.
    ///
    /// # Safety
    ///
    /// The caller must prove that every queued job is either executed or
    /// dropped before the borrowed scope ends.
    pub(crate) unsafe fn new_scoped<'scope, F>(task: F) -> Self
    where
        F: FnOnce(usize) + Send + 'scope,
    {
        if inline_job_fits::<F>() {
            Self {
                job: InlineJob::new(task),
            }
        } else {
            Self {
                job: InlineJob::new(boxed_job(task)),
            }
        }
    }

    /// Create a scoped job whose completion is published after task teardown.
    ///
    /// `complete` runs after the borrowing task's call frame and captures have
    /// been destroyed. If the queued job is dropped without execution, the
    /// completion closure is dropped without being called.
    ///
    /// # Safety
    ///
    /// The caller must prove that every queued job is either executed or
    /// dropped before the borrowed scope ends. `complete` must release the
    /// scope's corresponding pending-task registration when called or dropped.
    pub(crate) unsafe fn new_scoped_with_completion<'scope, F, Complete>(
        task: F,
        complete: Complete,
    ) -> Self
    where
        F: FnOnce(usize) + Send + 'scope,
        Complete: FnOnce(bool) + Send + 'scope,
    {
        Self {
            job: InlineJob::new_scoped(task, complete),
        }
    }

    pub(crate) fn execute(mut self, worker_id: usize) -> bool {
        self.job.execute(worker_id)
    }
}

impl InlineJob {
    fn new<F>(task: F) -> Self
    where
        F: FnOnce(usize) + Send,
    {
        Self::new_with(task, execute_inline::<F>)
    }

    fn new_scoped<F, Complete>(task: F, complete: Complete) -> Self
    where
        F: FnOnce(usize) + Send,
        Complete: FnOnce(bool) + Send,
    {
        let scoped = ScopedJob { task, complete };
        if inline_job_fits::<ScopedJob<F, Complete>>() {
            Self::new_with(scoped, execute_scoped_inline::<F, Complete>)
        } else {
            Self::new_with(Box::new(scoped), execute_boxed_scoped::<F, Complete>)
        }
    }

    fn new_with<F>(task: F, execute: unsafe fn(*mut InlineJobStorage, usize) -> bool) -> Self
    where
        F: Send,
    {
        // Checked in release too, not just under `debug_assert`: this is a safe
        // function whose precondition is UB-critical, since an `F` too large for
        // the storage would be written past it, and a debug-only guard
        // disappears exactly where the consequence stops being a panic.
        //
        // It costs nothing. Both operands are compile-time constants, so for a
        // type that fits this folds to `assert!(true)` and leaves no code.
        //
        // A `const` assertion cannot be used here even though the operands are
        // constant: `ScheduledJob::new` picks between the inline and boxed forms
        // at runtime, and the inline branch is still *instantiated* for an
        // oversized closure it never takes. Asserting at compile time therefore
        // rejects the boxed path's own callers.
        assert!(inline_job_fits::<F>());
        let mut job = Self {
            storage: InlineJobStorage::new(),
            execute,
            drop: drop_inline::<F>,
        };

        // Safety: `inline_job_fits` proves size and alignment fit the storage.
        unsafe {
            job.storage.as_mut_ptr::<F>().write(task);
        }

        job
    }

    fn execute(&mut self, worker_id: usize) -> bool {
        self.drop = drop_consumed;
        // Safety: `InlineJob::new` initialized storage as the same `F` used to
        // create this function pointer. Replacing `drop` before execution
        // prevents a second drop after `execute_inline` moves the closure out.
        unsafe { (self.execute)(&mut self.storage, worker_id) }
    }
}

impl Drop for InlineJob {
    fn drop(&mut self) {
        // Safety: storage contains the initialized closure until `execute`
        // swaps this function pointer to `drop_consumed`.
        unsafe {
            (self.drop)(&mut self.storage);
        }
    }
}

impl InlineJobStorage {
    fn new() -> Self {
        Self {
            words: [MaybeUninit::uninit(); INLINE_JOB_WORDS],
        }
    }

    fn as_mut_ptr<T>(&mut self) -> *mut T {
        self.words.as_mut_ptr().cast::<T>()
    }
}

const fn inline_job_fits<F>() -> bool {
    size_of::<F>() <= size_of::<InlineJobStorage>()
        && align_of::<F>() <= align_of::<InlineJobStorage>()
}

fn boxed_job<F>(task: F) -> impl FnOnce(usize) + Send
where
    F: FnOnce(usize) + Send,
{
    let task = Box::new(task);
    move |worker_id| task(worker_id)
}

/// Run `task`, reporting whether it returned.
///
/// A panic payload is dropped here, inside a second catch: a payload whose `Drop`
/// panics would otherwise unwind out of the job past the completion accounting
/// (a scope waiter would then return while scoped jobs still borrow its frame),
/// so that case aborts, as `std::thread::scope` does.
fn run_caught(task: impl FnOnce()) -> bool {
    match catch_unwind(AssertUnwindSafe(task)) {
        Ok(()) => true,
        Err(payload) => {
            if catch_unwind(AssertUnwindSafe(move || drop(payload))).is_err() {
                std::process::abort();
            }
            false
        }
    }
}

unsafe fn execute_inline<F>(storage: *mut InlineJobStorage, worker_id: usize) -> bool
where
    F: FnOnce(usize) + Send,
{
    // Safety: `InlineJob::new` initialized this storage as `F`; execute reads
    // it exactly once before marking the inline job consumed.
    let task = unsafe { ptr::read((*storage).as_mut_ptr::<F>()) };
    run_caught(|| task(worker_id))
}

unsafe fn execute_scoped_inline<F, Complete>(
    storage: *mut InlineJobStorage,
    worker_id: usize,
) -> bool
where
    F: FnOnce(usize) + Send,
    Complete: FnOnce(bool) + Send,
{
    // SAFETY: `InlineJob::new_scoped` initialized the storage with this exact
    // scoped wrapper type and execution consumes it once.
    let ScopedJob { task, complete } =
        unsafe { ptr::read((*storage).as_mut_ptr::<ScopedJob<F, Complete>>()) };
    let succeeded = run_scoped_task(task, worker_id);
    complete(succeeded);
    succeeded
}

unsafe fn execute_boxed_scoped<F, Complete>(
    storage: *mut InlineJobStorage,
    worker_id: usize,
) -> bool
where
    F: FnOnce(usize) + Send,
    Complete: FnOnce(bool) + Send,
{
    // SAFETY: `InlineJob::new_scoped` initialized the storage with a box of
    // this exact scoped wrapper type and execution consumes it once.
    let scoped = unsafe { ptr::read((*storage).as_mut_ptr::<Box<ScopedJob<F, Complete>>>()) };
    let ScopedJob { task, complete } = *scoped;
    let succeeded = run_scoped_task(task, worker_id);
    complete(succeeded);
    succeeded
}

/// Run a scoped job's task and drop its captures, reporting whether it returned.
///
/// The completion callback is deliberately not a parameter. `complete` releases
/// the scope waiter, which may then free the frame the task borrows from; a
/// by-value argument holding those borrows stays protected until its function
/// returns, so calling `complete` from inside this function would free the
/// borrowed frame while the protection is live. The callers invoke `complete`
/// after this returns, when nothing borrowing the scope remains.
fn run_scoped_task<F>(task: F, worker_id: usize) -> bool
where
    F: FnOnce(usize) + Send,
{
    run_caught(move || task(worker_id))
}

unsafe fn drop_inline<F>(storage: *mut InlineJobStorage) {
    // Safety: called only when the inline job was not consumed.
    unsafe {
        ptr::drop_in_place((*storage).as_mut_ptr::<F>());
    }
}

unsafe fn drop_consumed(_: *mut InlineJobStorage) {}

#[cfg(test)]
mod scoped_tests;

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

    use super::{INLINE_JOB_WORDS, InlineJob, InlineJobStorage, ScheduledJob, inline_job_fits};

    struct DropCounter {
        drops: Arc<AtomicUsize>,
    }

    impl Drop for DropCounter {
        fn drop(&mut self) {
            self.drops.fetch_add(1, Ordering::Relaxed);
        }
    }

    struct OversizedCapture {
        counter: DropCounter,
        padding: [usize; INLINE_JOB_WORDS],
    }

    impl OversizedCapture {
        fn run(self, worker_id: usize) {
            assert_eq!(self.padding[0], worker_id);
            drop(self.counter);
        }
    }

    #[repr(align(16))]
    struct OverAlignedCapture {
        counter: DropCounter,
    }

    impl OverAlignedCapture {
        fn run(self, worker_id: usize) {
            assert_eq!(worker_id, 0);
            drop(self.counter);
        }
    }

    enum ExpectedStorage {
        Inline,
        Boxed,
    }

    fn assert_dropped_once_before_and_after_execution<F, M>(expected: ExpectedStorage, make_task: M)
    where
        F: FnOnce(usize) + Send + 'static,
        M: Fn(Arc<AtomicUsize>) -> F,
    {
        match expected {
            ExpectedStorage::Inline => assert!(inline_job_fits::<F>()),
            ExpectedStorage::Boxed => assert!(!inline_job_fits::<F>()),
        }

        let unexecuted_drops = Arc::new(AtomicUsize::new(0));
        drop(ScheduledJob::new(make_task(Arc::clone(&unexecuted_drops))));
        assert_eq!(unexecuted_drops.load(Ordering::Relaxed), 1);

        let executed_drops = Arc::new(AtomicUsize::new(0));
        let job = ScheduledJob::new(make_task(Arc::clone(&executed_drops)));
        assert!(job.execute(0));
        assert_eq!(executed_drops.load(Ordering::Relaxed), 1);
    }

    fn scheduled_job_requiring_box<F>(task: F) -> ScheduledJob
    where
        F: FnOnce(usize) + Send + 'static,
    {
        assert!(!inline_job_fits::<F>());
        ScheduledJob::new(task)
    }

    #[test]
    fn inline_job_uses_natural_alignment_with_same_capacity() {
        let word_size = core::mem::size_of::<usize>();

        assert_eq!(
            core::mem::size_of::<InlineJobStorage>(),
            INLINE_JOB_WORDS * word_size
        );
        assert_eq!(
            core::mem::size_of::<InlineJob>(),
            (INLINE_JOB_WORDS + 2) * word_size
        );
        assert_eq!(
            core::mem::size_of::<ScheduledJob>(),
            core::mem::size_of::<InlineJob>()
        );
        assert_eq!(
            core::mem::align_of::<InlineJob>(),
            core::mem::align_of::<InlineJobStorage>()
        );
    }

    #[test]
    fn small_job_uses_inline_storage() {
        let observed = Arc::new(AtomicUsize::new(0));
        let job = {
            let observed = Arc::clone(&observed);
            ScheduledJob::new(move |worker_id| {
                observed.store(worker_id + 1, Ordering::Relaxed);
            })
        };

        assert!(job.execute(6));
        assert_eq!(observed.load(Ordering::Relaxed), 7);
    }

    #[test]
    fn oversized_job_uses_boxed_inline_trampoline() {
        let observed = Arc::new(AtomicUsize::new(0));
        let job = {
            let observed = Arc::clone(&observed);
            let values = [1usize; INLINE_JOB_WORDS + 1];
            assert!(core::mem::size_of_val(&values) > core::mem::size_of::<InlineJobStorage>());
            ScheduledJob::new(move |worker_id| {
                observed.store(values[0] + worker_id, Ordering::Relaxed);
            })
        };

        assert!(job.execute(4));
        assert_eq!(observed.load(Ordering::Relaxed), 5);
    }

    #[test]
    fn maximum_inline_capacity_job_uses_inline_storage() {
        let values = [1usize; INLINE_JOB_WORDS];
        let job = ScheduledJob::new(move |worker_id| {
            assert_eq!(values[worker_id], 1);
        });

        assert!(job.execute(0));
    }

    #[test]
    fn over_aligned_job_uses_typed_boxed_trampoline() {
        let observed = Arc::new(AtomicUsize::new(0));
        #[repr(align(16))]
        struct ObservedCapture(Arc<AtomicUsize>);

        impl ObservedCapture {
            fn record(self, worker_id: usize) {
                self.0.store(worker_id + 1, Ordering::Relaxed);
            }
        }

        let capture = ObservedCapture(Arc::clone(&observed));
        let job = scheduled_job_requiring_box(move |worker_id| capture.record(worker_id));

        assert!(job.execute(8));
        assert_eq!(observed.load(Ordering::Relaxed), 9);
    }

    #[test]
    fn inline_job_drops_capture_once_before_and_after_execution() {
        assert_dropped_once_before_and_after_execution(ExpectedStorage::Inline, |drops| {
            let counter = DropCounter { drops };
            move |_| drop(counter)
        });
    }

    #[test]
    fn oversized_job_drops_capture_once_before_and_after_execution() {
        assert_dropped_once_before_and_after_execution(ExpectedStorage::Boxed, |drops| {
            let capture = OversizedCapture {
                counter: DropCounter { drops },
                padding: [0; INLINE_JOB_WORDS],
            };
            move |worker_id| capture.run(worker_id)
        });
    }

    #[test]
    fn over_aligned_job_drops_capture_once_before_and_after_execution() {
        assert_dropped_once_before_and_after_execution(ExpectedStorage::Boxed, |drops| {
            let capture = OverAlignedCapture {
                counter: DropCounter { drops },
            };
            move |worker_id| capture.run(worker_id)
        });
    }

    #[test]
    fn inline_job_reports_panic_as_failure() {
        let job = ScheduledJob::new(|_| panic!("job panic"));

        assert!(!job.execute(0));
    }
}