rayon_core/spawn/mod.rs
1use crate::job::*;
2use crate::registry::Registry;
3use crate::unwind;
4use std::mem;
5use std::sync::Arc;
6
7/// Puts the task into the Rayon thread pool's job queue in the "static"
8/// or "global" scope. Just like a standard thread, this task is not
9/// tied to the current stack frame, and hence it cannot hold any
10/// references other than those with `'static` lifetime. If you want
11/// to spawn a task that references stack data, use [the `scope()`
12/// function] to create a scope.
13///
14/// [the `scope()` function]: crate::scope()
15///
16/// Since tasks spawned with this function cannot hold references into
17/// the enclosing stack frame, you almost certainly want to use a
18/// `move` closure as their argument (otherwise, the closure will
19/// typically hold references to any variables from the enclosing
20/// function that you happen to use).
21///
22/// This API assumes that the closure is executed purely for its
23/// side-effects (i.e., it might send messages, modify data protected
24/// by a mutex, or some such thing).
25///
26/// There is no guaranteed order of execution for spawns, given that
27/// other threads may steal tasks at any time. However, they are
28/// generally prioritized in a LIFO order on the thread from which
29/// they were spawned. Other threads always steal from the other end of
30/// the deque, like FIFO order. The idea is that "recent" tasks are
31/// most likely to be fresh in the local CPU's cache, while other
32/// threads can steal older "stale" tasks. For an alternate approach,
33/// consider [`spawn_fifo()`] instead.
34///
35/// # Panic handling
36///
37/// If this closure should panic, the resulting panic will be
38/// propagated to the panic handler registered in the `ThreadPoolBuilder`,
39/// if any. See [`ThreadPoolBuilder::panic_handler()`] for more
40/// details.
41///
42/// [`ThreadPoolBuilder::panic_handler()`]: crate::ThreadPoolBuilder::panic_handler()
43///
44/// # Examples
45///
46/// This code creates a Rayon task that increments a global counter.
47///
48/// ```rust
49/// # use rayon_core as rayon;
50/// use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
51///
52/// static GLOBAL_COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;
53///
54/// rayon::spawn(move || {
55/// GLOBAL_COUNTER.fetch_add(1, Ordering::SeqCst);
56/// });
57/// ```
58pub fn spawn<F>(func: F)
59where
60 F: FnOnce() + Send + 'static,
61{
62 // We assert that current registry has not terminated.
63 unsafe { spawn_in(func, &Registry::current()) }
64}
65
66/// Spawns an asynchronous job in `registry.`
67///
68/// Unsafe because `registry` must not yet have terminated.
69pub(super) unsafe fn spawn_in<F>(func: F, registry: &Arc<Registry>)
70where
71 F: FnOnce() + Send + 'static,
72{
73 // We assert that this does not hold any references (we know
74 // this because of the `'static` bound in the interface);
75 // moreover, we assert that the code below is not supposed to
76 // be able to panic, and hence the data won't leak but will be
77 // enqueued into some deque for later execution.
78 let abort_guard = unwind::AbortIfPanic; // just in case we are wrong, and code CAN panic
79 let job_ref = spawn_job(func, registry);
80 registry.inject_or_push(job_ref);
81 mem::forget(abort_guard);
82}
83
84unsafe fn spawn_job<F>(func: F, registry: &Arc<Registry>) -> JobRef
85where
86 F: FnOnce() + Send + 'static,
87{
88 // Ensure that registry cannot terminate until this job has
89 // executed. This ref is decremented at the (*) below.
90 registry.increment_terminate_count();
91
92 HeapJob::new({
93 let registry = Arc::clone(registry);
94 move || {
95 registry.catch_unwind(func);
96 registry.terminate(); // (*) permit registry to terminate now
97 }
98 })
99 .into_static_job_ref()
100}
101
102/// Fires off a task into the Rayon thread pool in the "static" or
103/// "global" scope. Just like a standard thread, this task is not
104/// tied to the current stack frame, and hence it cannot hold any
105/// references other than those with `'static` lifetime. If you want
106/// to spawn a task that references stack data, use [the `scope_fifo()`
107/// function] to create a scope.
108///
109/// The behavior is essentially the same as [the `spawn`
110/// function], except that calls from the same thread
111/// will be prioritized in FIFO order. This is similar to the now-
112/// deprecated [`breadth_first`] option, except the effect is isolated
113/// to relative `spawn_fifo` calls, not all thread-pool tasks.
114///
115/// For more details on this design, see Rayon [RFC #1].
116///
117/// [the `scope_fifo()` function]: crate::scope_fifo()
118/// [the `spawn` function]: crate::spawn()
119/// [`breadth_first`]: crate::ThreadPoolBuilder::breadth_first
120/// [RFC #1]: https://github.com/rayon-rs/rfcs/blob/main/accepted/rfc0001-scope-scheduling.md
121///
122/// # Panic handling
123///
124/// If this closure should panic, the resulting panic will be
125/// propagated to the panic handler registered in the `ThreadPoolBuilder`,
126/// if any. See [`ThreadPoolBuilder::panic_handler()`] for more
127/// details.
128///
129/// [`ThreadPoolBuilder::panic_handler()`]: crate::ThreadPoolBuilder::panic_handler
130pub fn spawn_fifo<F>(func: F)
131where
132 F: FnOnce() + Send + 'static,
133{
134 // We assert that current registry has not terminated.
135 unsafe { spawn_fifo_in(func, &Registry::current()) }
136}
137
138/// Spawns an asynchronous FIFO job in `registry.`
139///
140/// Unsafe because `registry` must not yet have terminated.
141pub(super) unsafe fn spawn_fifo_in<F>(func: F, registry: &Arc<Registry>)
142where
143 F: FnOnce() + Send + 'static,
144{
145 // We assert that this does not hold any references (we know
146 // this because of the `'static` bound in the interface);
147 // moreover, we assert that the code below is not supposed to
148 // be able to panic, and hence the data won't leak but will be
149 // enqueued into some deque for later execution.
150 let abort_guard = unwind::AbortIfPanic; // just in case we are wrong, and code CAN panic
151 let job_ref = spawn_job(func, registry);
152
153 // If we're in the pool, use our thread's private fifo for this thread to execute
154 // in a locally-FIFO order. Otherwise, just use the pool's global injector.
155 match registry.current_thread() {
156 Some(worker) => worker.push_fifo(job_ref),
157 None => registry.inject(job_ref),
158 }
159 mem::forget(abort_guard);
160}
161
162#[cfg(test)]
163mod test;