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
//! Thread-pool options shared by the surfaces (Task 11.1.2).
/// Worker-thread policy for the algorithms that have a parallel path.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ParallelOptions {
/// Number of workers; `None` uses the Rayon default.
pub threads: Option<usize>,
}
impl ParallelOptions {
/// Creates options with an optional worker count.
#[must_use]
pub const fn new(threads: Option<usize>) -> Self {
Self { threads }
}
/// Runs `function` inside a scoped pool with the requested worker count.
///
/// Results are independent of the thread count because the parallel
/// reductions keep a fixed order (AGENTS.md rule 6).
///
/// # Errors
///
/// Returns a message when the pool cannot be built.
#[cfg(feature = "parallel")]
pub fn install<T, F>(&self, function: F) -> Result<T, String>
where
F: FnOnce() -> T + Send,
T: Send,
{
match self.threads {
None => Ok(function()),
Some(threads) => {
let pool = rayon::ThreadPoolBuilder::new()
.num_threads(threads)
.build()
.map_err(|error| format!("could not build the thread pool: {error}"))?;
Ok(pool.install(function))
}
}
}
/// Runs `function` directly when the parallel feature is off.
///
/// # Errors
///
/// Never returns an error in this build.
#[cfg(not(feature = "parallel"))]
pub fn install<T, F>(&self, function: F) -> Result<T, String>
where
F: FnOnce() -> T + Send,
T: Send,
{
Ok(function())
}
}
/// Installs the process-wide Rayon pool with `threads` workers.
///
/// Returns `true` when the pool was installed and `false` when this build has
/// no parallel feature. A pool can be installed only once per process.
///
/// # Errors
///
/// Returns a message when the pool was already installed or cannot be built.
#[cfg(feature = "parallel")]
pub fn set_global_threads(threads: usize) -> Result<bool, String> {
rayon::ThreadPoolBuilder::new()
.num_threads(threads)
.build_global()
.map(|()| true)
.map_err(|error| format!("could not install the global thread pool: {error}"))
}
/// No-op when the parallel feature is off.
///
/// # Errors
///
/// Never returns an error in this build.
#[cfg(not(feature = "parallel"))]
pub fn set_global_threads(_threads: usize) -> Result<bool, String> {
Ok(false)
}