Skip to content

Commit

Permalink
[thread_executor] Fix thread sleep in work loop (#73)
Browse files Browse the repository at this point in the history
### Summary

Fix the loop in the thread executor to only sleep when no work has been
done on an iteration. Because only one message per actor is processed
per loop (for fairness or whatever), it's very possible/likely for there
to be more message waiting. In those instances, we don't need to sleep.

### Motivation

Found this issue while working on #29. We shouldn't sleep when there is
still work to be done (potentially). Even though the thread-executor
will eventually be replaced with a tokio runtime, should still fix this
up.

### Test Plan

Existing examples thoroughly exercise this.
  • Loading branch information
JohnMurray committed Jul 2, 2023
1 parent 5aa5573 commit 6c44774
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/executor/thread_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,14 @@ impl Executor for ThreadExecutor {
}
// Iterate over the actor-cells and check if there are any non-empty mailboxes.
// If one is found, process a message from it.
let mut messages_processed = 0;
for (_, cell) in self.actor_cells.iter_mut() {
if !cell.is_shutdown() {
// TODO: Forward messages to dead-letter-queue
continue;
}
if !cell.mailbox.is_empty() {
messages_processed += 1;
let result = cell.mailbox.try_recv();
if let Ok(letter) = result {
trace!("[{}] processing message: {:?}", &cell.address, &letter);
Expand All @@ -125,8 +127,12 @@ impl Executor for ThreadExecutor {
}
}
}
trace!("nothing to do, sleeping...");
thread::sleep(Duration::from_millis(SLEEP_DURATION_MS));

// If no messages were processed, sleep for a bit to avoid busy-waiting
if messages_processed == 0 {
trace!("nothing to do, sleeping...");
thread::sleep(Duration::from_millis(SLEEP_DURATION_MS));
}
}

self.runtime_manager.notify_shutdown(self.name);
Expand Down

0 comments on commit 6c44774

Please sign in to comment.