Skip to content

Commit

Permalink
[actor shutdown] Add a context macro to thread_executor (#72)
Browse files Browse the repository at this point in the history
### Summary

Introduces a new `context!` macro in thread_executor that just makes
constructing context objects less visually intrusive (8 lines per
instantiation!). Code is much easier to read now.

### Motivation

Part of #29.

The changes for actor shutdown will introduce more instances in which a
context object will be created from a system context. It made sense to
invest in a macro here to save some typing. Took a few iterations to get
this working right (so many TIL moments).

### Test Plan

If it compiles it's good. This change should be functionally equivalent
to what existed before.
  • Loading branch information
JohnMurray committed Jul 2, 2023
1 parent ffe59f3 commit 5aa5573
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/executor/thread_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ impl ThreadExecutor {
}
}
}

// Macro for quickly constructing a context object within the thread executor. The construction
// of the context almost always looks the same, just some slight differences with the sender.
macro_rules! context {
($self:tt, $cell:tt, $sender:path) => {
context!($self, $cell, ($sender))
};
($self:tt, $cell:tt, $sender:expr) => {
Context {
address: &$cell.address,
runtime_manager: &$self.runtime_manager,
executor_command_channel: &$self.command_channel,
parent: &$cell.parent,
children: &mut $cell.children,
sender: &$sender,
}
};
}

impl Executor for ThreadExecutor {
fn run(mut self) {
const SLEEP_DURATION_MS: u64 = 1;
Expand All @@ -75,14 +94,8 @@ impl Executor for ThreadExecutor {
debug!("received actor assignment for {}", &cell.address.uri);
self.assert_unique_address(&cell.address.uri);
trace!("calling before_start for actor {}", &cell.address.uri);
cell.actor.before_start(Context {
address: &cell.address,
runtime_manager: &self.runtime_manager,
executor_command_channel: &self.command_channel,
parent: &cell.parent,
children: &mut cell.children,
sender: &SenderType::System,
});
cell.actor
.before_start(context!(self, cell, SenderType::System));
self.actor_cells.insert(cell.address.uri.clone(), cell);
}
ExecutorCommands::ShutdownActor(address) => {
Expand All @@ -107,17 +120,8 @@ impl Executor for ThreadExecutor {
let result = cell.mailbox.try_recv();
if let Ok(letter) = result {
trace!("[{}] processing message: {:?}", &cell.address, &letter);
cell.actor.receive(
Context {
address: &cell.address,
runtime_manager: &self.runtime_manager,
executor_command_channel: &self.command_channel,
parent: &cell.parent,
children: &mut cell.children,
sender: &letter.sender,
},
letter.payload,
);
cell.actor
.receive(context!(self, cell, letter.sender), letter.payload);
}
}
}
Expand Down

0 comments on commit 5aa5573

Please sign in to comment.