|
|
@ -1,38 +1,28 @@ |
|
|
use std::io;
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
//! This module provides a facade for an abstract concurrent job executor.
|
|
|
|
|
|
//!
|
|
|
|
|
|
//! Mostly here to insulate the rest of this crate from the exact details of
|
|
|
|
|
|
//! the executor implementation.
|
|
|
|
|
|
|
|
|
use threadpool;
|
|
|
use threadpool;
|
|
|
|
|
|
|
|
|
use crate::context::Context;
|
|
|
|
|
|
|
|
|
|
|
|
/// Default number of threads spawned by Executor instances
|
|
|
/// Default number of threads spawned by Executor instances
|
|
|
const NUM_THREADS: usize = 8;
|
|
|
const NUM_THREADS: usize = 8;
|
|
|
|
|
|
|
|
|
/// The trait of objects that can be run by an Executor.
|
|
|
/// The trait of objects that can be run by an Executor.
|
|
|
///
|
|
|
|
|
|
/// NOTE: Intended to be used by boxed objects, so that self's contents can be
|
|
|
|
|
|
/// moved by `execute` without running into "unknown size at compiled time"
|
|
|
|
|
|
/// E0161 errors.
|
|
|
|
|
|
pub trait Job: Send {
|
|
|
pub trait Job: Send {
|
|
|
/// Executes self in the given context.
|
|
|
|
|
|
/// Errors do not crash the process, but are error-logged.
|
|
|
|
|
|
fn execute(self: Box<Self>, context: &Context) -> io::Result<()>;
|
|
|
|
|
|
|
|
|
fn execute(self: Box<Self>);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/// The central executor object that drives the client process.
|
|
|
|
|
|
|
|
|
/// A concurrent job execution engine.
|
|
|
pub struct Executor {
|
|
|
pub struct Executor {
|
|
|
/// The context against which jobs are executed.
|
|
|
|
|
|
context: Arc<Context>,
|
|
|
|
|
|
|
|
|
|
|
|
/// Executes the jobs.
|
|
|
/// Executes the jobs.
|
|
|
pool: threadpool::ThreadPool,
|
|
|
pool: threadpool::ThreadPool,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl Executor {
|
|
|
impl Executor {
|
|
|
/// Builds a new executor with an empty context a default number of threads.
|
|
|
|
|
|
|
|
|
/// Builds a new executor with a default number of threads.
|
|
|
pub fn new() -> Self {
|
|
|
pub fn new() -> Self {
|
|
|
Self {
|
|
|
Self {
|
|
|
context: Arc::new(Context::new()),
|
|
|
|
|
|
pool: threadpool::Builder::new()
|
|
|
pool: threadpool::Builder::new()
|
|
|
.num_threads(NUM_THREADS)
|
|
|
.num_threads(NUM_THREADS)
|
|
|
.thread_name("Executor".to_string())
|
|
|
.thread_name("Executor".to_string())
|
|
|
@ -42,36 +32,25 @@ impl Executor { |
|
|
|
|
|
|
|
|
/// Schedules execution of the given job on this executor.
|
|
|
/// Schedules execution of the given job on this executor.
|
|
|
pub fn schedule(&self, job: Box<dyn Job>) {
|
|
|
pub fn schedule(&self, job: Box<dyn Job>) {
|
|
|
let context = self.context.clone();
|
|
|
|
|
|
self.pool.execute(move || {
|
|
|
|
|
|
if let Err(error) = job.execute(&*context) {
|
|
|
|
|
|
error!("Executable returned error: {:?}", error)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
self.pool.execute(move || job.execute());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/// Blocks until all scheduled jobs are executed, then returns the context.
|
|
|
|
|
|
pub fn join(self) -> Context {
|
|
|
|
|
|
|
|
|
/// Blocks until all scheduled jobs are executed.
|
|
|
|
|
|
pub fn join(self) {
|
|
|
self.pool.join();
|
|
|
self.pool.join();
|
|
|
|
|
|
|
|
|
// Once the pool is joined, no-one should be holding on to copies of
|
|
|
|
|
|
// `self.context` anymore, so we unwrap() here.
|
|
|
|
|
|
Arc::try_unwrap(self.context).unwrap()
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
#[cfg(test)]
|
|
|
mod tests {
|
|
|
mod tests {
|
|
|
use std::io;
|
|
|
use std::io;
|
|
|
use std::sync::{Arc, Barrier, Mutex};
|
|
|
|
|
|
|
|
|
use std::sync::{Arc, Barrier};
|
|
|
|
|
|
|
|
|
use super::{Context, Executor, Job};
|
|
|
|
|
|
|
|
|
use super::{Executor, Job};
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
#[test]
|
|
|
fn immediate_join_returns_empty_context() {
|
|
|
|
|
|
let context = Executor::new().join();
|
|
|
|
|
|
assert_eq!(context.users.lock().get_list(), vec![]);
|
|
|
|
|
|
assert_eq!(context.rooms.lock().get_room_list(), vec![]);
|
|
|
|
|
|
|
|
|
fn immediate_join() {
|
|
|
|
|
|
Executor::new().join()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
struct Waiter {
|
|
|
struct Waiter {
|
|
|
@ -79,9 +58,8 @@ mod tests { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl Job for Waiter {
|
|
|
impl Job for Waiter {
|
|
|
fn execute(self: Box<Self>, context: &Context) -> io::Result<()> {
|
|
|
|
|
|
|
|
|
fn execute(self: Box<Self>) {
|
|
|
self.barrier.wait();
|
|
|
self.barrier.wait();
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -91,20 +69,13 @@ mod tests { |
|
|
|
|
|
|
|
|
let barrier = Arc::new(Barrier::new(2));
|
|
|
let barrier = Arc::new(Barrier::new(2));
|
|
|
|
|
|
|
|
|
let waiter1 = Box::new(Waiter {
|
|
|
|
|
|
|
|
|
executor.schedule(Box::new(Waiter {
|
|
|
barrier: barrier.clone(),
|
|
|
barrier: barrier.clone(),
|
|
|
});
|
|
|
|
|
|
let waiter2 = Box::new(Waiter {
|
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
|
executor.schedule(Box::new(Waiter {
|
|
|
barrier: barrier.clone(),
|
|
|
barrier: barrier.clone(),
|
|
|
});
|
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
executor.schedule(waiter1);
|
|
|
|
|
|
executor.schedule(waiter2);
|
|
|
|
|
|
|
|
|
|
|
|
let context = executor.join();
|
|
|
|
|
|
assert_eq!(context.users.lock().get_list(), vec![]);
|
|
|
|
|
|
assert_eq!(context.rooms.lock().get_room_list(), vec![]);
|
|
|
|
|
|
|
|
|
executor.join();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
// TODO: Add a test that exercises modifying Context.
|
|
|
|
|
|
}
|
|
|
}
|