@@ -339,6 +339,28 @@ impl ThreadPool {
339339 // We assert that `self.registry` has not terminated.
340340 unsafe { broadcast:: spawn_broadcast_in ( op, & self . registry ) }
341341 }
342+
343+ /// Cooperatively yields execution to Rayon.
344+ ///
345+ /// 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 > {
350+ let curr = self . registry . current_thread ( ) ?;
351+ Some ( curr. yield_now ( ) )
352+ }
353+
354+ /// Cooperatively yields execution to local Rayon work.
355+ ///
356+ /// 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 > {
361+ let curr = self . registry . current_thread ( ) ?;
362+ Some ( curr. yield_local ( ) )
363+ }
342364}
343365
344366impl Drop for ThreadPool {
@@ -400,3 +422,39 @@ pub fn current_thread_has_pending_tasks() -> Option<bool> {
400422 Some ( !curr. local_deque_is_empty ( ) )
401423 }
402424}
425+
426+ /// Cooperatively yields execution to Rayon.
427+ ///
428+ /// If the current thread is part of a rayon thread pool, this looks for a
429+ /// single unit of pending work in the pool, then executes it. Completion of
430+ /// that work might include nested work or further work stealing.
431+ ///
432+ /// This is similar to [`std::thread::yield_now()`], but does not literally make
433+ /// that call. If you are implementing a polling loop, you may want to also
434+ /// yield to the OS scheduler yourself if no Rayon work was found.
435+ ///
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 > {
439+ unsafe {
440+ let thread = WorkerThread :: current ( ) . as_ref ( ) ?;
441+ Some ( thread. yield_now ( ) )
442+ }
443+ }
444+
445+ /// Cooperatively yields execution to local Rayon work.
446+ ///
447+ /// If the current thread is part of a rayon thread pool, this looks for a
448+ /// single unit of pending work in this thread's queue, then executes it.
449+ /// Completion of that work might include nested work or further work stealing.
450+ ///
451+ /// This is similar to [`yield_now()`], but does not steal from other threads.
452+ ///
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 > {
456+ unsafe {
457+ let thread = WorkerThread :: current ( ) . as_ref ( ) ?;
458+ Some ( thread. yield_local ( ) )
459+ }
460+ }
0 commit comments