Skip to content

Commit

Permalink
Instead of previous commit, join worker threads in drop
Browse files Browse the repository at this point in the history
Includes a test for whether any of them is the current thread
before joining.

Signed-off-by: Chris Beck <beck.ct@gmail.com>
  • Loading branch information
cbeck88 committed Apr 11, 2020
1 parent 154050d commit 1e473da
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,6 @@ impl Environment {
let idx = self.idx.fetch_add(1, Ordering::Relaxed);
self.cqs[idx % self.cqs.len()].clone()
}

/// Shutdown the completion queues and join all threads
pub fn shutdown_and_join(&mut self) {
for cq in self.completion_queues() {
cq.shutdown();
}

for handle in self._handles.drain(..) {
handle.join().unwrap();
}
}
}

impl Drop for Environment {
Expand All @@ -148,6 +137,15 @@ impl Drop for Environment {
// it's safe to shutdown more than once.
cq.shutdown()
}

// Join our threads when we leave scope
// Try not to join the current thread
let current_thread_id = std::thread::current().id();
for handle in self._handles.drain(..) {
if handle.thread().id() != current_thread_id {
handle.join().unwrap();
}
}
}
}

Expand All @@ -157,7 +155,7 @@ mod tests {

#[test]
fn test_basic_loop() {
let mut env = Environment::new(2);
let env = Environment::new(2);

let q1 = env.pick_cq();
let q2 = env.pick_cq();
Expand All @@ -174,6 +172,5 @@ mod tests {
}

assert_eq!(env.completion_queues().len(), 2);
env.shutdown_and_join();
}
}

0 comments on commit 1e473da

Please sign in to comment.