Skip to content

Commit

Permalink
Reactor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Aug 10, 2024
1 parent d3e907d commit 9805e00
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 32 deletions.
31 changes: 13 additions & 18 deletions muse-lang/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl Vm {
/// This function does not actually execute any code. A call to
/// [`resume`](Self::resume)/[`resume_async`](Self::resume_async) is needed
/// to begin executing the function call.
pub fn prepare(&self, code: &Code, guard: &mut CollectionGuard) -> Result<(), ExecutionError> {
pub fn prepare(&self, code: &Code, guard: &mut CollectionGuard) -> Result<(), Fault> {
self.context(guard).prepare(code)
}

Expand All @@ -151,7 +151,7 @@ impl Vm {
function: &Rooted<Function>,
arity: Arity,
guard: &mut CollectionGuard,
) -> Result<(), ExecutionError> {
) -> Result<(), Fault> {
self.context(guard).prepare_call(function, arity)
}

Expand Down Expand Up @@ -225,7 +225,7 @@ impl Vm {
&'context self,
code: &Code,
guard: &'context mut CollectionGuard<'guard>,
) -> Result<ExecuteAsync<'static, 'context, 'guard>, ExecutionError> {
) -> Result<ExecuteAsync<'static, 'context, 'guard>, Fault> {
MaybeOwnedContext::Owned(self.context(guard)).execute_async(code)
}

Expand Down Expand Up @@ -649,7 +649,7 @@ impl<'context, 'guard> VmContext<'context, 'guard> {
/// This function does not actually execute any code. A call to
/// [`resume`](Self::resume)/[`resume_async`](Self::resume_async) is needed
/// to begin executing the function call.
pub fn prepare(&mut self, code: &Code) -> Result<(), ExecutionError> {
pub fn prepare(&mut self, code: &Code) -> Result<(), Fault> {
let code = self.push_code(code, None);
self.prepare_owned(code)
}
Expand Down Expand Up @@ -725,18 +725,18 @@ impl<'context, 'guard> VmContext<'context, 'guard> {
self.resume_until(Instant::now() + duration)
}

fn prepare_owned(&mut self, code: CodeIndex) -> Result<(), ExecutionError> {
fn prepare_owned(&mut self, code: CodeIndex) -> Result<(), Fault> {
let vm = self.vm();
vm.frames[vm.current_frame].code = Some(code);
vm.frames[vm.current_frame].instruction = 0;

self.allocate(self.code[code.0].code.data.stack_requirement)
.map_err(|err| ExecutionError::new(err, self))?;
self.allocate(self.code[code.0].code.data.stack_requirement)?;
Ok(())
}

fn execute_owned(&mut self, code: CodeIndex) -> Result<Value, ExecutionError> {
self.prepare_owned(code)?;
self.prepare_owned(code)
.map_err(|err| ExecutionError::new(err, self))?;

self.resume()
}
Expand All @@ -747,7 +747,8 @@ impl<'context, 'guard> VmContext<'context, 'guard> {
code: &Code,
) -> Result<ExecuteAsync<'vm, 'context, 'guard>, ExecutionError> {
let code = self.push_code(code, None);
self.prepare_owned(code)?;
self.prepare_owned(code)
.map_err(|err| ExecutionError::new(err, self))?;

Ok(ExecuteAsync(MaybeOwnedContext::Borrowed(self)))
}
Expand Down Expand Up @@ -800,15 +801,9 @@ impl<'context, 'guard> VmContext<'context, 'guard> {
/// This function does not actually execute any code. A call to
/// [`resume`](Self::resume)/[`resume_async`](Self::resume_async) is needed
/// to begin executing the function call.
pub fn prepare_call(
&mut self,
function: &Rooted<Function>,
arity: Arity,
) -> Result<(), ExecutionError> {
pub fn prepare_call(&mut self, function: &Rooted<Function>, arity: Arity) -> Result<(), Fault> {
let Some(body) = function.body(arity) else {
return Err(ExecutionError::Exception(
Fault::IncorrectNumberOfArguments.as_exception(self),
));
return Err(Fault::IncorrectNumberOfArguments);
};
let code = self.push_code(body, Some(function));
self.prepare_owned(code)
Expand Down Expand Up @@ -2879,7 +2874,7 @@ impl<'vm, 'context, 'guard> MaybeOwnedContext<'vm, 'context, 'guard> {
pub fn execute_async(
mut self,
code: &Code,
) -> Result<ExecuteAsync<'vm, 'context, 'guard>, ExecutionError> {
) -> Result<ExecuteAsync<'vm, 'context, 'guard>, Fault> {
let code = self.push_code(code, None);
self.prepare_owned(code)?;

Expand Down
23 changes: 9 additions & 14 deletions muse-reactor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,12 @@ impl Builder<NoWork> {
}
}

pub fn unit<Work>(self) -> Builder<Work> {
assert!(
self.vm_source.is_none(),
"new_vm must be invoked after unit() to ensure the correct ReactorHandle type"
);
pub fn new_vm<F, Work>(self, new_vm: F) -> Builder<Work>
where
F: NewVm<Work>,
{
Builder {
vm_source: None,
vm_source: Some(Arc::new(new_vm)),
threads: self.threads,
thread_name: self.thread_name,
work_queue_limit: self.work_queue_limit,
Expand All @@ -152,14 +151,6 @@ where
self
}

pub fn new_vm<F>(mut self, new_vm: F) -> Self
where
F: NewVm<Work>,
{
self.vm_source = Some(Arc::new(new_vm));
self
}

pub fn finish(self) -> ReactorHandle<Work> {
let (sender, receiver) = if let Some(limit) = self.work_queue_limit {
flume::bounded(limit)
Expand Down Expand Up @@ -1269,6 +1260,8 @@ impl<Work> Clone for ReactorHandle<Work> {
}
}

impl<Work> ContainsNoRefs for ReactorHandle<Work> where Work: WorkUnit {}

#[derive(Debug)]
struct SharedReactorData {
shutdown: AtomicBool,
Expand Down Expand Up @@ -1516,6 +1509,8 @@ pub struct BudgetPoolId(NonZeroUsize);

pub struct BudgetPoolHandle<Work = NoWork>(Arc<BudgetPoolHandleData<Work>>);

impl<Work> ContainsNoRefs for BudgetPoolHandle<Work> where Work: WorkUnit {}

struct BudgetPoolHandleData<Work> {
pool: ReactorBudgetPool,
reactor: ReactorHandle<Work>,
Expand Down

0 comments on commit 9805e00

Please sign in to comment.