Join with worker threads on device drop

This commit is contained in:
Mathias Hall-Andersen
2019-08-28 16:27:26 +02:00
parent 8e1a2cabd3
commit 6785aa4cb5
6 changed files with 91 additions and 30 deletions

View File

@@ -7,11 +7,60 @@ mod types;
use hjul::*;
use std::error::Error;
use std::fmt;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use sodiumoxide;
use types::KeyPair;
use types::{Bind, KeyPair};
struct Test {}
impl Bind for Test {
type Error = BindError;
type Endpoint = SocketAddr;
fn new() -> Test {
Test {}
}
fn set_port(&self, port: u16) -> Result<(), Self::Error> {
Ok(())
}
fn get_port(&self) -> Option<u16> {
None
}
fn recv(&self, buf: &mut [u8]) -> Result<(usize, Self::Endpoint), Self::Error> {
Ok((0, "127.0.0.1:8080".parse().unwrap()))
}
fn send(&self, buf: &[u8], dst: &Self::Endpoint) -> Result<(), Self::Error> {
Ok(())
}
}
#[derive(Debug)]
enum BindError {}
impl Error for BindError {
fn description(&self) -> &str {
"Generic Bind Error"
}
fn source(&self) -> Option<&(dyn Error + 'static)> {
None
}
}
impl fmt::Display for BindError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Not Possible")
}
}
#[derive(Debug, Clone)]
struct PeerTimer {
@@ -24,7 +73,7 @@ fn main() {
// choose optimal crypto implementations for platform
sodiumoxide::init().unwrap();
{
let router = router::Device::new(
4,
|t: &PeerTimer, data: bool, sent: bool| t.a.reset(Duration::from_millis(1000)),
@@ -41,3 +90,6 @@ fn main() {
println!("{:?}", pt);
}
println!("joined");
}

View File

@@ -9,7 +9,8 @@ use crossbeam_deque::{Injector, Steal, Stealer, Worker};
use spin;
use treebitmap::IpLookupTable;
use super::super::types::KeyPair;
use super::super::types::{Bind, KeyPair, Tun};
use super::anti_replay::AntiReplay;
use super::peer;
use super::peer::{Peer, PeerInner};
@@ -62,16 +63,15 @@ impl<T: Opaque, S: Callback<T>, R: Callback<T>, K: KeyCallback<T>> Drop for Devi
let device = &self.0;
device.running.store(false, Ordering::SeqCst);
// eat all parallel jobs
while match device.injector.steal() {
Steal::Empty => true,
// join all worker threads
while match self.1.pop() {
Some(handle) => {
handle.thread().unpark();
handle.join().unwrap();
true
}
_ => false,
} {}
// unpark all threads
for handle in &self.1 {
handle.thread().unpark();
}
}
}

View File

@@ -17,3 +17,9 @@ impl<T, F> Callback<T> for F where F: Fn(&T, bool, bool) -> () + Sync + Send + '
pub trait KeyCallback<T>: Fn(&T) -> () + Sync + Send + 'static {}
impl<T, F> KeyCallback<T> for F where F: Fn(&T) -> () + Sync + Send + 'static {}
pub trait TunCallback<T>: Fn(&T, bool, bool) -> () + Sync + Send + 'static {}
pub trait BindCallback<T>: Fn(&T, bool, bool) -> () + Sync + Send + 'static {}
pub trait Endpoint: Send + Sync {}

View File

@@ -208,7 +208,7 @@ pub fn worker_parallel<T: Opaque, S: Callback<T>, R: Callback<T>, K: KeyCallback
local: Worker<JobParallel>, // local job queue (local to thread)
stealers: Vec<Stealer<JobParallel>>, // stealers (from other threads)
) {
while !device.running.load(Ordering::SeqCst) {
while device.running.load(Ordering::SeqCst) {
match find_task(&local, &device.injector, &stealers) {
Some(job) => {
let (handle, buf) = job;
@@ -262,7 +262,6 @@ pub fn worker_parallel<T: Opaque, S: Callback<T>, R: Callback<T>, K: KeyCallback
handle.thread().unpark();
}
None => {
// no jobs, park the worker
device.parked.store(true, Ordering::Release);
thread::park();
}

View File

@@ -4,3 +4,5 @@ use std::net::SocketAddr;
* is to simply use SocketAddr directly as the endpoint.
*/
pub trait Endpoint: Into<SocketAddr> {}
impl<T> Endpoint for T where T: Into<SocketAddr> {}

View File

@@ -21,7 +21,9 @@ pub trait Bind: Send + Sync {
fn set_port(&self, port: u16) -> Result<(), Self::Error>;
/// Returns the current port of the bind
fn get_port(&self) -> u16;
fn recv(&self, dst: &mut [u8]) -> Self::Endpoint;
fn send(&self, src: &[u8], dst: &Self::Endpoint);
fn get_port(&self) -> Option<u16>;
fn recv(&self, buf: &mut [u8]) -> Result<(usize, Self::Endpoint), Self::Error>;
fn send(&self, buf: &[u8], dst: &Self::Endpoint) -> Result<(), Self::Error>;
}