|
@@ -1 +1,20 @@
|
|
|
+use crossbeam_deque::{Injector, Stealer, Worker};
|
|
|
+use std::iter;
|
|
|
|
|
|
+fn find_task<T>(local: &Worker<T>, global: &Injector<T>, stealers: &[Stealer<T>]) -> Option<T> {
|
|
|
+ // Pop a task from the local queue, if not empty.
|
|
|
+ local.pop().or_else(|| {
|
|
|
+ // Otherwise, we need to look for a task elsewhere.
|
|
|
+ iter::repeat_with(|| {
|
|
|
+ // Try stealing a batch of tasks from the global queue.
|
|
|
+ global
|
|
|
+ .steal_batch_and_pop(local)
|
|
|
+ // Or try stealing a task from one of the other threads.
|
|
|
+ .or_else(|| stealers.iter().map(|s| s.steal()).collect())
|
|
|
+ })
|
|
|
+ // Loop while no task was stolen and any steal operation needs to be retried.
|
|
|
+ .find(|s| !s.is_retry())
|
|
|
+ // Extract the stolen task, if there is one.
|
|
|
+ .and_then(|s| s.success())
|
|
|
+ })
|
|
|
+}
|