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
//! Cooperative cancellation + progress observation for long-running boolean evaluations.
//!
//! Manifold operations are lazy: building a CSG tree is cheap, and the
//! actual evaluation happens when you query results. Attach an
//! [`ExecutionContext`] with [`Manifold::with_context`](crate::Manifold::with_context),
//! then call an eager operation that consumes it, such as
//! [`Manifold::status`](crate::Manifold::status) or `refine*`. An
//! [`ExecutionContext`] lets you observe an in-flight evaluation from another
//! thread and ask it to stop early.
//!
//! Cancellation is **sticky** (once cancelled, stays cancelled) and granular
//! per-boolean (the upstream kernel checks the cancel flag at boolean
//! boundaries; it doesn't interrupt a single boolean mid-flight). Progress
//! is reported as a fraction in `[0.0, 1.0]`.
//!
//! The C API documents the underlying `ExecutionContext` as safe to read
//! and write from any thread, so [`ExecutionContext`] is `Send` + `Sync`
//! and can be wrapped in [`Arc`](std::sync::Arc) to share between the
//! evaluator thread and a controller/observer thread.
//!
//! ```no_run
//! use std::sync::Arc;
//! use std::thread;
//! use std::time::Duration;
//! use manifold_csg::{ExecutionContext, Manifold};
//!
//! let ctx = Arc::new(ExecutionContext::new());
//! let cancel = Arc::clone(&ctx);
//!
//! // Cancel the evaluation if it takes longer than 100ms.
//! thread::spawn(move || {
//! thread::sleep(Duration::from_millis(100));
//! cancel.cancel();
//! });
//!
//! let result = Manifold::cube(1.0, 1.0, 1.0, true);
//! let status = result.with_context(&ctx).status();
//! // `status` will be `Ok(())` for trivial work that finishes before cancel
//! // fires; for a heavy boolean tree it would surface cancellation as an error.
//! # let _ = status;
//! ```
//!
//! Available since manifold3d's post-v3.4.1 master.
use ;
/// Observes progress and allows cooperative cancellation of long-running
/// boolean evaluations. See the [module docs](self) for usage.
// SAFETY: The C API explicitly documents `ExecutionContext` as safe to
// read/write from any thread; the upstream C++ implementation
// synchronizes the cancel flag and progress counter internally.
unsafe
// SAFETY: Same justification as `Send` — all accessors (`cancel`,
// `cancelled`, `progress`) are documented thread-safe at the C boundary.
unsafe