1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#![feature(futures_api)] use futures::Future; use futures::executor::{ThreadPool, ThreadPoolBuilder}; use futures::task::{SpawnExt}; use config::Config; use riker::kernel::Dispatcher; pub struct ThreadPoolDispatcher { inner: ThreadPool, } impl Dispatcher for ThreadPoolDispatcher { fn new(config: &Config, _: bool) -> ThreadPoolDispatcher { let config = ThreadPoolConfig::from(config); ThreadPoolDispatcher { inner: ThreadPoolBuilder::new() .pool_size(config.pool_size) .name_prefix("pool-thread-#") .create() .unwrap() } } fn execute<F>(&mut self, f: F) where F: Future<Output = ()> + Send + 'static { let _ = self.inner.spawn(f); } } struct ThreadPoolConfig { pool_size: usize, } impl<'a> From<&'a Config> for ThreadPoolConfig { fn from(config: &Config) -> Self { ThreadPoolConfig { pool_size: config.get_int("dispatcher.pool_size").unwrap() as usize } } }