Skip to main content

orx_parallel/pools/
global_pool.rs

1use crate::pools::pool_impl::*;
2#[cfg(feature = "std")]
3use std::sync::LazyLock;
4
5// PERSISTENT_POOL - Feature precedence:
6// 1. wasm (strongest) - on wasm32 target
7// 2. persistent-pool-rayon
8// 3. transient-pool
9// 4. default - BasicPool (if std) or SequentialPool (if no-std)
10
11// 1. wasm on wasm32
12#[cfg(all(feature = "std", feature = "wasm", target_arch = "wasm32"))]
13static PERSISTENT_POOL: LazyLock<WasmWebPool> = LazyLock::new(Default::default);
14
15// 2. persistent-pool-rayon (and not wasm on wasm32)
16#[cfg(all(
17    feature = "std",
18    feature = "persistent-pool-rayon",
19    not(all(feature = "wasm", target_arch = "wasm32")),
20))]
21static PERSISTENT_POOL: LazyLock<rayon_core::ThreadPool> =
22    LazyLock::new(build_default_rayon_thread_pool);
23
24// 3. transient-pool (and not wasm on wasm32, and not persistent-pool-rayon)
25#[cfg(all(
26    feature = "std",
27    feature = "transient-pool",
28    not(all(feature = "wasm", target_arch = "wasm32")),
29    not(feature = "persistent-pool-rayon"),
30))]
31static PERSISTENT_POOL: LazyLock<OncePool> = LazyLock::new(Default::default);
32
33// 4a. default with std (and not wasm on wasm32, and not persistent-pool-rayon, and not transient-pool)
34#[cfg(all(
35    feature = "std",
36    not(all(feature = "wasm", target_arch = "wasm32")),
37    not(feature = "persistent-pool-rayon"),
38    not(feature = "transient-pool"),
39))]
40static PERSISTENT_POOL: LazyLock<BasicPool> = LazyLock::new(Default::default);
41
42// 4b. default without std (fallback - sequential pool)
43#[cfg(all(
44    not(feature = "std"),
45    not(all(feature = "wasm", target_arch = "wasm32")),
46    not(feature = "persistent-pool-rayon"),
47    not(feature = "transient-pool"),
48))]
49static PERSISTENT_POOL: SequentialPool = SequentialPool;
50
51// DEFAULT POOL
52
53// 1. wasm on wasm32
54#[cfg(all(feature = "std", feature = "wasm", target_arch = "wasm32"))]
55/// Default thread pool
56pub type DefaultPool = &'static WasmWebPool;
57
58// 2. persistent-pool-rayon (and not wasm on wasm32)
59#[cfg(all(
60    feature = "std",
61    feature = "persistent-pool-rayon",
62    not(all(feature = "wasm", target_arch = "wasm32")),
63))]
64/// Default thread pool
65pub type DefaultPool = &'static rayon_core::ThreadPool;
66
67// 3. transient-pool (and not wasm on wasm32, and not persistent-pool-rayon)
68#[cfg(all(
69    feature = "std",
70    feature = "transient-pool",
71    not(all(feature = "wasm", target_arch = "wasm32")),
72    not(feature = "persistent-pool-rayon"),
73))]
74/// Default thread pool
75pub type DefaultPool = &'static OncePool;
76
77// 4a. default with std (and not wasm on wasm32, and not persistent-pool-rayon, and not transient-pool)
78#[cfg(all(
79    feature = "std",
80    not(all(feature = "wasm", target_arch = "wasm32")),
81    not(feature = "persistent-pool-rayon"),
82    not(feature = "transient-pool"),
83))]
84/// Default thread pool
85pub type DefaultPool = &'static BasicPool;
86
87// 4b. default without std (fallback - sequential pool)
88#[cfg(all(
89    not(feature = "std"),
90    not(all(feature = "wasm", target_arch = "wasm32")),
91    not(feature = "persistent-pool-rayon"),
92    not(feature = "transient-pool"),
93))]
94/// Default thread pool
95pub type DefaultPool = &'static SequentialPool;
96
97// GET POOL
98
99// 1. wasm on wasm32
100#[cfg(all(feature = "std", feature = "wasm", target_arch = "wasm32"))]
101pub(crate) fn global_pool() -> DefaultPool {
102    &PERSISTENT_POOL
103}
104
105// 2. persistent-pool-rayon (and not wasm on wasm32)
106#[cfg(all(
107    feature = "std",
108    feature = "persistent-pool-rayon",
109    not(all(feature = "wasm", target_arch = "wasm32")),
110))]
111pub(crate) fn global_pool() -> DefaultPool {
112    &PERSISTENT_POOL
113}
114
115// 3. transient-pool (and not wasm on wasm32, and not persistent-pool-rayon)
116#[cfg(all(
117    feature = "std",
118    feature = "transient-pool",
119    not(all(feature = "wasm", target_arch = "wasm32")),
120    not(feature = "persistent-pool-rayon"),
121))]
122pub(crate) fn global_pool() -> DefaultPool {
123    &PERSISTENT_POOL
124}
125
126// 4a. default with std (and not wasm on wasm32, and not persistent-pool-rayon, and not transient-pool)
127#[cfg(all(
128    feature = "std",
129    not(all(feature = "wasm", target_arch = "wasm32")),
130    not(feature = "persistent-pool-rayon"),
131    not(feature = "transient-pool"),
132))]
133pub(crate) fn global_pool() -> DefaultPool {
134    &PERSISTENT_POOL
135}
136
137// 4b. default without std (fallback - sequential pool)
138#[cfg(all(
139    not(feature = "std"),
140    not(all(feature = "wasm", target_arch = "wasm32")),
141    not(feature = "persistent-pool-rayon"),
142    not(feature = "transient-pool"),
143))]
144pub(crate) fn global_pool() -> DefaultPool {
145    &PERSISTENT_POOL
146}