ifc_lite_processing/
geometry_pool.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! The one home for the large-stack rayon pool that native geometry runs on
6//! (EDGE-FFI-1 / EDGE-FFI-3, #7673).
7//!
8//! Per-element geometry runs inside `process_geometry` and the analytic
9//! extractors via `par_iter`, so the deep recursion (nested boolean clipping,
10//! the exact CSG kernel, long placement and mapped-item chains) lives on the
11//! *rayon worker* stacks, not the caller's. A stack overflow is not a panic: it
12//! hits the guard page and aborts the whole host (the Rhino process behind the
13//! FFI, the Python interpreter, every request on the server), and no
14//! `catch_unwind` or `join` can intercept it. Spawning one big-stack OS thread
15//! and calling the pipeline from it, as the Python bindings used to, only
16//! enlarges the serial part; the `par_iter` jobs still run on rayon's global
17//! pool with default stacks.
18//!
19//! Consumers:
20//! - FFI and Python run their whole call through [`run_on_geometry_pool`], so
21//!   the entry closure and every nested `par_iter` run on these workers.
22//! - The server builds its global pool from [`geometry_pool_builder`], so every
23//!   rayon job it runs, CSG included, already has the large stack.
24
25use std::sync::OnceLock;
26
27use rayon::{ThreadPool, ThreadPoolBuildError, ThreadPoolBuilder};
28
29/// Stack size for every rayon worker that runs geometry (256 MiB).
30///
31/// Reserved address space, not committed memory: a worker only touches the
32/// pages its recursion actually reaches.
33pub const GEOMETRY_STACK_BYTES: usize = 256 * 1024 * 1024;
34
35/// A [`ThreadPoolBuilder`] whose workers have [`GEOMETRY_STACK_BYTES`] stacks.
36/// Callers may add `num_threads` and then `build` or `build_global`.
37pub fn geometry_pool_builder() -> ThreadPoolBuilder {
38    ThreadPoolBuilder::new()
39        .stack_size(GEOMETRY_STACK_BYTES)
40        .thread_name(|i| format!("ifc-lite-geometry-{i}"))
41}
42
43/// The process-wide large-stack pool, built on first use.
44///
45/// An `Err` when the OS refuses to spawn the threads; never a panic, because
46/// the FFI calls this on an `extern "C"` path where an unwind aborts the host.
47/// A failed build is not cached, so a later call retries. Two first calls
48/// racing may each build a pool; the loser is dropped.
49pub fn geometry_pool() -> Result<&'static ThreadPool, ThreadPoolBuildError> {
50    static POOL: OnceLock<ThreadPool> = OnceLock::new();
51    get_or_build(&POOL, || geometry_pool_builder().build())
52}
53
54pub(crate) fn get_or_build<E>(
55    cell: &'static OnceLock<ThreadPool>,
56    build: impl FnOnce() -> Result<ThreadPool, E>,
57) -> Result<&'static ThreadPool, E> {
58    if let Some(pool) = cell.get() {
59        return Ok(pool);
60    }
61    let pool = build()?;
62    Ok(cell.get_or_init(|| pool))
63}
64
65/// Run `work` on [`geometry_pool`], so it and every rayon job it starts run on
66/// large-stack workers. Blocks the calling thread until `work` returns.
67///
68/// A panic in `work` is resumed on the calling thread (rayon's `install`), so
69/// a caller that must not unwind wraps this in `catch_unwind`.
70pub fn run_on_geometry_pool<T: Send>(
71    work: impl FnOnce() -> T + Send,
72) -> Result<T, ThreadPoolBuildError> {
73    Ok(geometry_pool()?.install(work))
74}