Skip to content

Commit f46f1d4

Browse files
committed
Try local jobs first in wait_until_cold
If a worker thread has jobs in its own queue, then it's not really headed for an inactive / sleepy state yet, so we can avoid modifying the registry-wide sleep state. (cherry picked from commit 99e6fc1)
1 parent 65574c3 commit f46f1d4

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

rayon-core/src/registry.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -855,23 +855,33 @@ impl WorkerThread {
855855
// accesses, which would be *very bad*
856856
let abort_guard = unwind::AbortIfPanic;
857857

858-
let mut idle_state = self.registry.sleep.start_looking(self.index, latch);
859-
while !latch.probe() {
860-
if let Some(job) = self.find_work() {
861-
self.registry.sleep.work_found(idle_state);
858+
'outer: while !latch.probe() {
859+
// Check for local work *before* we start marking ourself idle,
860+
// especially to avoid modifying shared sleep state.
861+
if let Some(job) = self.take_local_job() {
862862
self.execute(job);
863-
idle_state = self.registry.sleep.start_looking(self.index, latch);
864-
} else {
865-
self.registry
866-
.sleep
867-
.no_work_found(&mut idle_state, latch, &self)
863+
continue;
868864
}
869-
}
870865

871-
// If we were sleepy, we are not anymore. We "found work" --
872-
// whatever the surrounding thread was doing before it had to
873-
// wait.
874-
self.registry.sleep.work_found(idle_state);
866+
let mut idle_state = self.registry.sleep.start_looking(self.index, latch);
867+
while !latch.probe() {
868+
if let Some(job) = self.find_work() {
869+
self.registry.sleep.work_found(idle_state);
870+
self.execute(job);
871+
// The job might have injected local work, so go back to the outer loop.
872+
continue 'outer;
873+
} else {
874+
self.registry
875+
.sleep
876+
.no_work_found(&mut idle_state, latch, &self)
877+
}
878+
}
879+
880+
// If we were sleepy, we are not anymore. We "found work" --
881+
// whatever the surrounding thread was doing before it had to wait.
882+
self.registry.sleep.work_found(idle_state);
883+
break;
884+
}
875885

876886
self.log(|| ThreadSawLatchSet {
877887
worker: self.index,

0 commit comments

Comments
 (0)