mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-01 00:00:10 +02:00
Introduce the Tokio runtime: Reactor + Threadpool (#141)
This patch is an intial implementation of the Tokio runtime. The Tokio runtime provides an out of the box configuration for running I/O heavy asynchronous applications. As of now, the Tokio runtime is a combination of a work-stealing thread pool as well as a background reactor to drive I/O resources. This patch also includes tokio-executor, a hopefully short lived crate that is based on the futures 0.2 executor RFC. * Implement `Park` for `Reactor` This enables the reactor to be used as the thread parker for executors. This also adds an `Error` component to `Park`. With this change, a `Reactor` and a `CurrentThread` can be combined to achieve the capabilities of tokio-core.
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
extern crate futures;
|
||||
extern crate tokio_threadpool;
|
||||
extern crate env_logger;
|
||||
|
||||
use tokio_threadpool::*;
|
||||
use futures::future::{self, Executor};
|
||||
|
||||
use std::sync::mpsc;
|
||||
|
||||
const ITER: usize = 2_000_000;
|
||||
// const ITER: usize = 30;
|
||||
|
||||
fn chained_spawn() {
|
||||
let pool = ThreadPool::new();
|
||||
let tx = pool.sender().clone();
|
||||
|
||||
fn spawn(tx: Sender, res_tx: mpsc::Sender<()>, n: usize) {
|
||||
if n == 0 {
|
||||
res_tx.send(()).unwrap();
|
||||
} else {
|
||||
let tx2 = tx.clone();
|
||||
tx.execute(future::lazy(move || {
|
||||
spawn(tx2, res_tx, n - 1);
|
||||
Ok(())
|
||||
})).ok().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
loop {
|
||||
println!("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
|
||||
let (res_tx, res_rx) = mpsc::channel();
|
||||
|
||||
for _ in 0..10 {
|
||||
spawn(tx.clone(), res_tx.clone(), ITER);
|
||||
}
|
||||
|
||||
for _ in 0..10 {
|
||||
res_rx.recv().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let _ = ::env_logger::init();
|
||||
chained_spawn();
|
||||
}
|
||||
Reference in New Issue
Block a user