Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure fn passed to generator completes #318

Merged
merged 1 commit into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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