Skip to content

Commit

Permalink
ensure fn passed to generator completes (#318)
Browse files Browse the repository at this point in the history
This avoids forcing the generator to abort, which is not thread-safe.
  • Loading branch information
carllerche authored Jul 21, 2023
1 parent bcf4e84 commit 465f841
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
3 changes: 0 additions & 3 deletions src/rt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,6 @@ pub fn thread_done() {
execution.threads.active_mut().operation = None;
execution.threads.active_mut().set_terminated();
let switch = execution.schedule();

trace!(?thread, ?switch, "thread_done: terminate");

switch
});
}
19 changes: 14 additions & 5 deletions src/rt/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ impl Scheduler {
threads[0].resume();

loop {
if !execution.threads.is_active() {
if execution.threads.is_complete() {
for thread in &mut threads {
thread.resume();
assert!(thread.is_done());
}
return;
}

Expand Down Expand Up @@ -131,12 +135,17 @@ impl Scheduler {
fn spawn_thread(f: Box<dyn FnOnce()>, stack_size: Option<usize>) -> Thread {
let body = move || {
loop {
let f: Option<Box<dyn FnOnce()>> = generator::yield_(()).unwrap();
generator::yield_with(());
f.unwrap()();
let f: Option<Option<Box<dyn FnOnce()>>> = generator::yield_(());

if let Some(f) = f {
generator::yield_with(());
f.unwrap()();
} else {
break;
}
}

// done!();
generator::done!();
};
let mut g = match stack_size {
Some(stack_size) => Gn::new_opt(stack_size, body),
Expand Down
17 changes: 17 additions & 0 deletions src/rt/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,23 @@ impl Set {
self.active.is_some()
}

pub(crate) fn is_complete(&self) -> bool {
if self.active.is_none() {
// All threads should be terminated
for thread in &self.threads {
assert!(
thread.is_terminated(),
"thread not terminated; {:#?}",
thread
);
}

true
} else {
false
}
}

pub(crate) fn active_id(&self) -> Id {
Id::new(self.execution_id, self.active.unwrap())
}
Expand Down

0 comments on commit 465f841

Please sign in to comment.