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

fix(dispatcher): replace event with oneshot channel #368

Merged
merged 2 commits into from
Dec 19, 2024
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
2 changes: 1 addition & 1 deletion compio-dispatcher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[dependencies]
# Workspace dependencies
compio-driver = { workspace = true }
compio-runtime = { workspace = true, features = ["event", "time"] }
compio-runtime = { workspace = true }

flume = { workspace = true }
futures-channel = { workspace = true }
Expand Down
19 changes: 8 additions & 11 deletions compio-dispatcher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ use std::{
io,
num::NonZeroUsize,
panic::resume_unwind,
sync::{Arc, Mutex},
thread::{JoinHandle, available_parallelism},
};

use compio_driver::{AsyncifyPool, DispatchError, Dispatchable, ProactorBuilder};
use compio_runtime::{JoinHandle as CompioJoinHandle, Runtime, event::Event};
use compio_runtime::{JoinHandle as CompioJoinHandle, Runtime};
use flume::{Sender, unbounded};
use futures_channel::oneshot;

Expand Down Expand Up @@ -191,25 +190,23 @@ impl Dispatcher {
/// thread panicked, this method will resume the panic.
pub async fn join(self) -> io::Result<()> {
drop(self.sender);
let results = Arc::new(Mutex::new(vec![]));
let event = Event::new();
let handle = event.handle();
let (tx, rx) = oneshot::channel::<Vec<_>>();
if let Err(f) = self.pool.dispatch({
let results = results.clone();
move || {
*results.lock().unwrap() = self
let results = self
.threads
.into_iter()
.map(|thread| thread.join())
.collect();
handle.notify();
tx.send(results).ok();
}
Berrysoft marked this conversation as resolved.
Show resolved Hide resolved
}) {
std::thread::spawn(f.0);
}
event.wait().await;
let mut guard = results.lock().unwrap();
for res in std::mem::take::<Vec<std::thread::Result<()>>>(guard.as_mut()) {
let results = rx.await.map_err(|_| {
io::Error::new(io::ErrorKind::Other, "the join task cancelled unexpectedly")
})?;
for res in results {
res.unwrap_or_else(|e| resume_unwind(e));
}
Ok(())
Expand Down
Loading