Skip to content

Commit 3767329

Browse files
committed
Add enum Yield for the return status of yields
1 parent 9fa93e0 commit 3767329

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

rayon-core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub use self::spawn::{spawn, spawn_fifo};
102102
pub use self::thread_pool::current_thread_has_pending_tasks;
103103
pub use self::thread_pool::current_thread_index;
104104
pub use self::thread_pool::ThreadPool;
105-
pub use self::thread_pool::{yield_local, yield_now};
105+
pub use self::thread_pool::{yield_local, yield_now, Yield};
106106

107107
use self::registry::{CustomSpawn, DefaultSpawn, ThreadSpawn};
108108

rayon-core/src/registry.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::sleep::Sleep;
66
use crate::unwind;
77
use crate::{
88
ErrorKind, ExitHandler, PanicHandler, StartHandler, ThreadPoolBuildError, ThreadPoolBuilder,
9+
Yield,
910
};
1011
use crossbeam_deque::{Injector, Steal, Stealer, Worker};
1112
use std::cell::Cell;
@@ -848,23 +849,23 @@ impl WorkerThread {
848849
.or_else(|| self.registry.pop_injected_job(self.index))
849850
}
850851

851-
pub(super) fn yield_now(&self) -> bool {
852+
pub(super) fn yield_now(&self) -> Yield {
852853
match self.find_work() {
853854
Some(job) => unsafe {
854855
self.execute(job);
855-
true
856+
Yield::Executed
856857
},
857-
None => false,
858+
None => Yield::Idle,
858859
}
859860
}
860861

861-
pub(super) fn yield_local(&self) -> bool {
862+
pub(super) fn yield_local(&self) -> Yield {
862863
match self.take_local_job() {
863864
Some(job) => unsafe {
864865
self.execute(job);
865-
true
866+
Yield::Executed
866867
},
867-
None => false,
868+
None => Yield::Idle,
868869
}
869870
}
870871

rayon-core/src/thread_pool/mod.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -343,21 +343,23 @@ impl ThreadPool {
343343
/// Cooperatively yields execution to Rayon.
344344
///
345345
/// This is similar to the general [`yield_now()`], but only if the current
346-
/// thread is part of *this* thread pool. Returns `Some(true)` if anything
347-
/// was executed, `Some(false)` if nothing was available, or `None` if the
348-
/// current thread is not part of this pool.
349-
pub fn yield_now(&self) -> Option<bool> {
346+
/// thread is part of *this* thread pool.
347+
///
348+
/// Returns `Some(Yield::Executed)` if anything was executed, `Some(Yield::Idle)` if
349+
/// nothing was available, or `None` if this thread is not part of any pool at all.
350+
pub fn yield_now(&self) -> Option<Yield> {
350351
let curr = self.registry.current_thread()?;
351352
Some(curr.yield_now())
352353
}
353354

354355
/// Cooperatively yields execution to local Rayon work.
355356
///
356357
/// This is similar to the general [`yield_local()`], but only if the current
357-
/// thread is part of *this* thread pool. Returns `Some(true)` if anything
358-
/// was executed, `Some(false)` if nothing was available, or `None` if the
359-
/// current thread is not part of this pool.
360-
pub fn yield_local(&self) -> Option<bool> {
358+
/// thread is part of *this* thread pool.
359+
///
360+
/// Returns `Some(Yield::Executed)` if anything was executed, `Some(Yield::Idle)` if
361+
/// nothing was available, or `None` if this thread is not part of any pool at all.
362+
pub fn yield_local(&self) -> Option<Yield> {
361363
let curr = self.registry.current_thread()?;
362364
Some(curr.yield_local())
363365
}
@@ -433,9 +435,9 @@ pub fn current_thread_has_pending_tasks() -> Option<bool> {
433435
/// that call. If you are implementing a polling loop, you may want to also
434436
/// yield to the OS scheduler yourself if no Rayon work was found.
435437
///
436-
/// Returns `Some(true)` if anything was executed, `Some(false)` if nothing was
437-
/// available, or `None` if this thread is not part of any pool at all.
438-
pub fn yield_now() -> Option<bool> {
438+
/// Returns `Some(Yield::Executed)` if anything was executed, `Some(Yield::Idle)` if
439+
/// nothing was available, or `None` if this thread is not part of any pool at all.
440+
pub fn yield_now() -> Option<Yield> {
439441
unsafe {
440442
let thread = WorkerThread::current().as_ref()?;
441443
Some(thread.yield_now())
@@ -450,11 +452,20 @@ pub fn yield_now() -> Option<bool> {
450452
///
451453
/// This is similar to [`yield_now()`], but does not steal from other threads.
452454
///
453-
/// Returns `Some(true)` if anything was executed, `Some(false)` if nothing was
454-
/// available, or `None` if this thread is not part of any pool at all.
455-
pub fn yield_local() -> Option<bool> {
455+
/// Returns `Some(Yield::Executed)` if anything was executed, `Some(Yield::Idle)` if
456+
/// nothing was available, or `None` if this thread is not part of any pool at all.
457+
pub fn yield_local() -> Option<Yield> {
456458
unsafe {
457459
let thread = WorkerThread::current().as_ref()?;
458460
Some(thread.yield_local())
459461
}
460462
}
463+
464+
/// Result of [`yield_now()`] or [`yield_local()`].
465+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
466+
pub enum Yield {
467+
/// Work was found and executed.
468+
Executed,
469+
/// No available work was found.
470+
Idle,
471+
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub use rayon_core::{in_place_scope, scope, Scope};
124124
pub use rayon_core::{in_place_scope_fifo, scope_fifo, ScopeFifo};
125125
pub use rayon_core::{join, join_context};
126126
pub use rayon_core::{spawn, spawn_fifo};
127-
pub use rayon_core::{yield_local, yield_now};
127+
pub use rayon_core::{yield_local, yield_now, Yield};
128128

129129
/// We need to transmit raw pointers across threads. It is possible to do this
130130
/// without any unsafe code by converting pointers to usize or to AtomicPtr<T>

0 commit comments

Comments
 (0)