boxddd 0.4.0

Safe, ergonomic Rust bindings for Box3D
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
use crate::core::callback_state::{CallbackFailure, CallbackInvocationSlot, SharedCallbackState};
use crate::error::Error;
use boxddd_sys::ffi;
#[cfg(not(target_arch = "wasm32"))]
use std::ffi::CStr;
use std::os::raw::{c_char, c_void};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
#[cfg(not(target_arch = "wasm32"))]
use std::thread::{self, JoinHandle};

/// Safe task-system adapter installed on a Box3D world definition.
///
/// Box3D calls `enqueueTask` during `b3World_Step` and later calls `finishTask`
/// for every non-null task handle returned by enqueue. `finishTask` must block
/// until that task has completed; schedulers that cannot provide this blocking
/// guarantee must not be installed through this adapter. Do not call
/// `World::step` from a job system worker that cannot park or join child
/// work; that scheduler shape can deadlock when Box3D asks `finishTask` to wait.
#[derive(Clone, Debug)]
pub struct TaskSystem {
    inner: Arc<TaskSystemInner>,
}

impl TaskSystem {
    /// Creates the blocking-thread scheduler.
    ///
    /// Each Box3D task runs on a dedicated blocking operating-system thread and
    /// is joined by the corresponding Box3D `finishTask` callback. Panics from
    /// task callbacks are contained and reported by [`crate::World::step`] as
    /// [`Error::CallbackPanicked`].
    ///
    /// Browser and WASI targets return [`Error::UnsupportedOnWasm`] because
    /// Box3D requires `finishTask` to block until child tasks complete.
    #[inline]
    pub fn blocking_threads() -> crate::Result<Self> {
        #[cfg(target_arch = "wasm32")]
        {
            Err(Error::UnsupportedOnWasm)
        }
        #[cfg(not(target_arch = "wasm32"))]
        {
            Ok(Self::new(FaultMode::None))
        }
    }

    /// Returns counters for diagnostics and tests.
    #[inline]
    pub fn stats(&self) -> TaskSystemStats {
        self.inner.stats()
    }

    #[doc(hidden)]
    #[inline]
    pub fn __guard_rejections_for_test(&self) -> usize {
        self.inner.guard_rejections.load(Ordering::Relaxed)
    }

    #[inline]
    fn new(fault_mode: FaultMode) -> Self {
        Self {
            inner: Arc::new(TaskSystemInner::new(fault_mode)),
        }
    }

    #[doc(hidden)]
    #[inline]
    pub fn __panic_on_enqueue_for_test() -> Self {
        Self::new(FaultMode::PanicOnEnqueue)
    }

    #[doc(hidden)]
    #[inline]
    pub fn __panic_on_task_for_test() -> Self {
        Self::new(FaultMode::PanicOnTask)
    }

    #[doc(hidden)]
    #[inline]
    pub fn __panic_on_finish_for_test() -> Self {
        Self::new(FaultMode::PanicOnFinish)
    }

    #[doc(hidden)]
    #[inline]
    pub fn __check_callback_guard_for_test() -> Self {
        Self::new(FaultMode::CheckCallbackGuard)
    }
}

/// Stable native task context owned by one World.
///
/// The scheduler can be shared by many worlds, while each installed context
/// points at that world's invocation slot.
#[derive(Debug)]
pub(crate) struct InstalledTaskContext {
    scheduler: Arc<TaskSystemInner>,
    invocation: CallbackInvocationSlot,
}

impl InstalledTaskContext {
    pub(crate) fn new(task_system: &TaskSystem, invocation: CallbackInvocationSlot) -> Box<Self> {
        Box::new(Self {
            scheduler: Arc::clone(&task_system.inner),
            invocation,
        })
    }

    #[inline]
    fn raw_context(&self) -> *mut c_void {
        self as *const Self as *mut c_void
    }
}

/// Snapshot of a [`TaskSystem`]'s task counters.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct TaskSystemStats {
    /// Number of tasks enqueued.
    pub enqueued: usize,
    /// Number of tasks started.
    pub started: usize,
    /// Number of tasks completed.
    pub completed: usize,
    /// Number of `finishTask` callbacks observed.
    pub finished: usize,
    /// Whether any task callback has panicked since this scheduler was created.
    pub panicked: bool,
}

#[derive(Debug)]
struct TaskSystemInner {
    fault_mode: FaultMode,
    panicked: AtomicBool,
    enqueued: AtomicUsize,
    started: AtomicUsize,
    completed: AtomicUsize,
    finished: AtomicUsize,
    guard_rejections: AtomicUsize,
}

impl TaskSystemInner {
    fn new(fault_mode: FaultMode) -> Self {
        Self {
            fault_mode,
            panicked: AtomicBool::new(false),
            enqueued: AtomicUsize::new(0),
            started: AtomicUsize::new(0),
            completed: AtomicUsize::new(0),
            finished: AtomicUsize::new(0),
            guard_rejections: AtomicUsize::new(0),
        }
    }

    fn stats(&self) -> TaskSystemStats {
        TaskSystemStats {
            enqueued: self.enqueued.load(Ordering::Relaxed),
            started: self.started.load(Ordering::Relaxed),
            completed: self.completed.load(Ordering::Relaxed),
            finished: self.finished.load(Ordering::Relaxed),
            panicked: self.panicked.load(Ordering::Relaxed),
        }
    }

    fn mark_panicked(&self, failures: &SharedCallbackState) {
        self.panicked.store(true, Ordering::Relaxed);
        failures.record(CallbackFailure::Panicked);
    }

    fn run_task(&self, failures: &SharedCallbackState, invocation: TaskInvocation) {
        self.started.fetch_add(1, Ordering::Relaxed);
        failures.invoke((), || {
            if self.fault_mode == FaultMode::CheckCallbackGuard
                && matches!(
                    crate::core::callback_state::check_not_in_callback(),
                    Err(Error::InCallback)
                )
            {
                self.guard_rejections.fetch_add(1, Ordering::Relaxed);
            }
            unsafe { (invocation.task)(invocation.task_context) };
            if self.fault_mode == FaultMode::PanicOnTask {
                self.mark_panicked(failures);
                panic!("injected Box3D task panic");
            }
        });
        self.completed.fetch_add(1, Ordering::Relaxed);
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum FaultMode {
    #[cfg(not(target_arch = "wasm32"))]
    None,
    PanicOnEnqueue,
    PanicOnTask,
    PanicOnFinish,
    CheckCallbackGuard,
}

#[derive(Clone, Copy)]
struct TaskInvocation {
    task: unsafe extern "C" fn(*mut c_void),
    task_context: *mut c_void,
}

unsafe impl Send for TaskInvocation {}

#[cfg(not(target_arch = "wasm32"))]
struct TaskHandle {
    join: Option<JoinHandle<()>>,
    failures: SharedCallbackState,
    scheduler: Arc<TaskSystemInner>,
}

#[cfg(not(target_arch = "wasm32"))]
pub(crate) unsafe extern "C" fn enqueue_task(
    task: ffi::b3TaskCallback,
    task_context: *mut c_void,
    user_context: *mut c_void,
    task_name: *const c_char,
) -> *mut c_void {
    let Some(context) = (unsafe { task_context_from_raw_checked(user_context) }) else {
        return std::ptr::null_mut();
    };
    let scheduler = &context.scheduler;
    let failures = context.invocation.current().unwrap_or_default();
    failures.invoke(std::ptr::null_mut(), || {
        scheduler.enqueued.fetch_add(1, Ordering::Relaxed);
        let Some(task) = task else {
            failures.record(CallbackFailure::InvalidNativeInput);
            return std::ptr::null_mut();
        };
        let invocation = TaskInvocation { task, task_context };
        if scheduler.fault_mode == FaultMode::PanicOnEnqueue {
            scheduler.run_task(&failures, invocation);
            scheduler.mark_panicked(&failures);
            panic!("injected Box3D enqueue panic");
        }

        let scheduler_for_task = Arc::clone(&context.scheduler);
        let failures_for_task = failures.clone();
        let thread_name = task_thread_name(task_name);
        match thread::Builder::new()
            .name(thread_name)
            .spawn(move || scheduler_for_task.run_task(&failures_for_task, invocation))
        {
            Ok(join) => Box::into_raw(Box::new(TaskHandle {
                join: Some(join),
                failures: failures.clone(),
                scheduler: Arc::clone(&context.scheduler),
            }))
            .cast(),
            Err(_) => {
                scheduler.run_task(&failures, invocation);
                std::ptr::null_mut()
            }
        }
    })
}

#[cfg(target_arch = "wasm32")]
pub(crate) unsafe extern "C" fn enqueue_task(
    task: ffi::b3TaskCallback,
    task_context: *mut c_void,
    user_context: *mut c_void,
    _task_name: *const c_char,
) -> *mut c_void {
    let Some(context) = (unsafe { task_context_from_raw_checked(user_context) }) else {
        return std::ptr::null_mut();
    };
    let scheduler = &context.scheduler;
    let failures = context.invocation.current().unwrap_or_default();
    failures.invoke((), || {
        scheduler.enqueued.fetch_add(1, Ordering::Relaxed);
        let Some(task) = task else {
            failures.record(CallbackFailure::InvalidNativeInput);
            return;
        };
        scheduler.run_task(&failures, TaskInvocation { task, task_context });
    });
    std::ptr::null_mut()
}

#[cfg(not(target_arch = "wasm32"))]
pub(crate) unsafe extern "C" fn finish_task(user_task: *mut c_void, user_context: *mut c_void) {
    if user_task.is_null() {
        return;
    }
    let mut handle = unsafe { Box::from_raw(user_task.cast::<TaskHandle>()) };
    let failures = handle.failures.clone();
    let scheduler = Arc::clone(&handle.scheduler);
    failures.invoke((), || {
        if user_context.is_null() {
            failures.record(CallbackFailure::InvalidNativeInput);
        }
        if let Some(join) = handle.join.take()
            && join.join().is_err()
        {
            scheduler.mark_panicked(&failures);
        }
        scheduler.finished.fetch_add(1, Ordering::Relaxed);
        if scheduler.fault_mode == FaultMode::PanicOnFinish {
            scheduler.mark_panicked(&failures);
            panic!("injected Box3D finish panic");
        }
    });
}

#[cfg(target_arch = "wasm32")]
pub(crate) unsafe extern "C" fn finish_task(user_task: *mut c_void, user_context: *mut c_void) {
    let Some(context) = (unsafe { task_context_from_raw_checked(user_context) }) else {
        return;
    };
    let scheduler = &context.scheduler;
    let failures = context.invocation.current().unwrap_or_default();
    failures.invoke((), || {
        if !user_task.is_null() {
            failures.record(CallbackFailure::InvalidNativeInput);
        }
        scheduler.finished.fetch_add(1, Ordering::Relaxed);
    });
}

#[inline]
pub(crate) fn install_callbacks(raw_def: &mut ffi::b3WorldDef, context: &InstalledTaskContext) {
    raw_def.enqueueTask = Some(enqueue_task);
    raw_def.finishTask = Some(finish_task);
    raw_def.userTaskContext = context.raw_context();
}

unsafe fn task_context_from_raw<'a>(context: *mut c_void) -> &'a InstalledTaskContext {
    debug_assert!(!context.is_null());
    unsafe { &*context.cast::<InstalledTaskContext>() }
}

unsafe fn task_context_from_raw_checked<'a>(
    context: *mut c_void,
) -> Option<&'a InstalledTaskContext> {
    if context.is_null() {
        None
    } else {
        Some(unsafe { task_context_from_raw(context) })
    }
}

#[cfg(not(target_arch = "wasm32"))]
fn task_thread_name(task_name: *const c_char) -> String {
    let suffix: String = if task_name.is_null() {
        "unnamed".into()
    } else {
        unsafe { CStr::from_ptr(task_name) }
            .to_string_lossy()
            .chars()
            .map(|ch| if ch == '\0' { '_' } else { ch })
            .take(48)
            .collect()
    };
    format!("boxddd-task-{suffix}")
}

#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
    use super::*;

    #[test]
    fn finish_task_joins_and_reclaims_without_a_context_pointer() {
        let scheduler = Arc::new(TaskSystemInner::new(FaultMode::None));
        let failures = SharedCallbackState::default();
        let completed = Arc::new(AtomicBool::new(false));
        let completed_by_task = Arc::clone(&completed);
        let join = thread::spawn(move || {
            completed_by_task.store(true, Ordering::Release);
        });
        let handle = Box::new(TaskHandle {
            join: Some(join),
            failures: failures.clone(),
            scheduler: Arc::clone(&scheduler),
        });

        unsafe {
            finish_task(Box::into_raw(handle).cast(), std::ptr::null_mut());
        }

        assert!(completed.load(Ordering::Acquire));
        assert_eq!(scheduler.finished.load(Ordering::Relaxed), 1);
        assert_eq!(failures.result(), Err(Error::NativeFailure));
    }

    #[test]
    fn enqueue_task_rejects_an_empty_native_callback() {
        let task_system = TaskSystem::new(FaultMode::None);
        let invocation = CallbackInvocationSlot::default();
        let context = InstalledTaskContext::new(&task_system, invocation.clone());
        let failures = SharedCallbackState::default();
        let _guard = invocation.install(failures.clone()).unwrap();

        let handle = unsafe {
            enqueue_task(
                None,
                std::ptr::null_mut(),
                context.raw_context(),
                std::ptr::null(),
            )
        };

        assert!(handle.is_null());
        assert_eq!(failures.result(), Err(Error::NativeFailure));
    }
}