|
|
@ -1,10 +1,11 @@
|
|
|
|
use std::cell::UnsafeCell;
|
|
|
|
use std::cell::Cell;
|
|
|
|
use std::iter;
|
|
|
|
use std::iter;
|
|
|
|
use std::thread;
|
|
|
|
use std::thread;
|
|
|
|
use std::time::Duration;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
|
|
use crossbeam_deque::{Injector, Stealer, Worker};
|
|
|
|
use crossbeam_deque::{Injector, Stealer, Worker};
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
|
|
|
use once_cell::unsync::OnceCell;
|
|
|
|
|
|
|
|
|
|
|
|
use crate::task::executor::Sleepers;
|
|
|
|
use crate::task::executor::Sleepers;
|
|
|
|
use crate::task::Runnable;
|
|
|
|
use crate::task::Runnable;
|
|
|
@ -32,9 +33,18 @@ static POOL: Lazy<Pool> = Lazy::new(|| {
|
|
|
|
let worker = Worker::new_fifo();
|
|
|
|
let worker = Worker::new_fifo();
|
|
|
|
stealers.push(worker.stealer());
|
|
|
|
stealers.push(worker.stealer());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let proc = Processor {
|
|
|
|
|
|
|
|
worker,
|
|
|
|
|
|
|
|
slot: Cell::new(None),
|
|
|
|
|
|
|
|
slot_runs: Cell::new(0),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
thread::Builder::new()
|
|
|
|
thread::Builder::new()
|
|
|
|
.name("async-std/executor".to_string())
|
|
|
|
.name("async-std/executor".to_string())
|
|
|
|
.spawn(|| abort_on_panic(|| main_loop(worker)))
|
|
|
|
.spawn(|| {
|
|
|
|
|
|
|
|
let _ = PROCESSOR.with(|p| p.set(proc));
|
|
|
|
|
|
|
|
abort_on_panic(|| main_loop());
|
|
|
|
|
|
|
|
})
|
|
|
|
.expect("cannot start a thread driving tasks");
|
|
|
|
.expect("cannot start a thread driving tasks");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -45,59 +55,75 @@ static POOL: Lazy<Pool> = Lazy::new(|| {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// The state of a worker thread.
|
|
|
|
|
|
|
|
struct Processor {
|
|
|
|
|
|
|
|
/// The local task queue.
|
|
|
|
|
|
|
|
worker: Worker<Runnable>,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Contains the next task to run as an optimization that skips queues.
|
|
|
|
|
|
|
|
slot: Cell<Option<Runnable>>,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// How many times in a row tasks have been taked from the slot rather than the queue.
|
|
|
|
|
|
|
|
slot_runs: Cell<u32>,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
thread_local! {
|
|
|
|
thread_local! {
|
|
|
|
/// Local task queue associated with the current worker thread.
|
|
|
|
/// Worker thread state.
|
|
|
|
static QUEUE: UnsafeCell<Option<Worker<Runnable>>> = UnsafeCell::new(None);
|
|
|
|
static PROCESSOR: OnceCell<Processor> = OnceCell::new();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Schedules a new runnable task for execution.
|
|
|
|
/// Schedules a new runnable task for execution.
|
|
|
|
pub(crate) fn schedule(task: Runnable) {
|
|
|
|
pub(crate) fn schedule(task: Runnable) {
|
|
|
|
QUEUE.with(|queue| {
|
|
|
|
PROCESSOR.with(|proc| {
|
|
|
|
let local = unsafe { (*queue.get()).as_ref() };
|
|
|
|
// If the current thread is a worker thread, store it into its task slot or push it into
|
|
|
|
|
|
|
|
// its local task queue. Otherwise, push it into the global task queue.
|
|
|
|
// If the current thread is a worker thread, push the task into its local task queue.
|
|
|
|
match proc.get() {
|
|
|
|
// Otherwise, push it into the global task queue.
|
|
|
|
Some(proc) => {
|
|
|
|
match local {
|
|
|
|
// Replace the task in the slot.
|
|
|
|
None => POOL.injector.push(task),
|
|
|
|
if let Some(task) = proc.slot.replace(Some(task)) {
|
|
|
|
Some(q) => q.push(task),
|
|
|
|
// If the slot already contained a task, push it into the local task queue.
|
|
|
|
|
|
|
|
proc.worker.push(task);
|
|
|
|
|
|
|
|
POOL.sleepers.notify_one();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
None => {
|
|
|
|
// Notify a sleeping worker that new work just came in.
|
|
|
|
POOL.injector.push(task);
|
|
|
|
POOL.sleepers.notify_one();
|
|
|
|
POOL.sleepers.notify_one();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Main loop running a worker thread.
|
|
|
|
/// Main loop running a worker thread.
|
|
|
|
fn main_loop(local: Worker<Runnable>) {
|
|
|
|
fn main_loop() {
|
|
|
|
// Initialize the local task queue.
|
|
|
|
/// Number of yields when no runnable task is found.
|
|
|
|
QUEUE.with(|queue| unsafe { *queue.get() = Some(local) });
|
|
|
|
const YIELDS: u32 = 3;
|
|
|
|
|
|
|
|
/// Number of short sleeps when no runnable task in found.
|
|
|
|
|
|
|
|
const SLEEPS: u32 = 1;
|
|
|
|
|
|
|
|
|
|
|
|
// The number of times the thread didn't find work in a row.
|
|
|
|
// The number of times the thread didn't find work in a row.
|
|
|
|
let mut step = 0;
|
|
|
|
let mut fails = 0;
|
|
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
loop {
|
|
|
|
// Try to find a runnable task.
|
|
|
|
// Try to find a runnable task.
|
|
|
|
match find_runnable() {
|
|
|
|
match find_runnable() {
|
|
|
|
Some(task) => {
|
|
|
|
Some(task) => {
|
|
|
|
// Found. Now run the task.
|
|
|
|
fails = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Run the found task.
|
|
|
|
task.run();
|
|
|
|
task.run();
|
|
|
|
step = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
None => {
|
|
|
|
|
|
|
|
fails += 1;
|
|
|
|
|
|
|
|
|
|
|
|
// Yield the current thread or put it to sleep.
|
|
|
|
// Yield the current thread or put it to sleep.
|
|
|
|
match step {
|
|
|
|
if fails <= YIELDS {
|
|
|
|
0..=2 => {
|
|
|
|
|
|
|
|
thread::yield_now();
|
|
|
|
thread::yield_now();
|
|
|
|
step += 1;
|
|
|
|
} else if fails <= YIELDS + SLEEPS {
|
|
|
|
}
|
|
|
|
|
|
|
|
3 => {
|
|
|
|
|
|
|
|
thread::sleep(Duration::from_micros(10));
|
|
|
|
thread::sleep(Duration::from_micros(10));
|
|
|
|
step += 1;
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
|
|
|
|
POOL.sleepers.wait();
|
|
|
|
POOL.sleepers.wait();
|
|
|
|
step = 0;
|
|
|
|
fails = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -106,29 +132,42 @@ fn main_loop(local: Worker<Runnable>) {
|
|
|
|
|
|
|
|
|
|
|
|
/// Find the next runnable task.
|
|
|
|
/// Find the next runnable task.
|
|
|
|
fn find_runnable() -> Option<Runnable> {
|
|
|
|
fn find_runnable() -> Option<Runnable> {
|
|
|
|
let pool = &*POOL;
|
|
|
|
/// Maximum number of times the slot can be used in a row.
|
|
|
|
|
|
|
|
const SLOT_LIMIT: u32 = 16;
|
|
|
|
QUEUE.with(|queue| {
|
|
|
|
|
|
|
|
let local = unsafe { (*queue.get()).as_ref().unwrap() };
|
|
|
|
PROCESSOR.with(|proc| {
|
|
|
|
|
|
|
|
let proc = proc.get().unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Try taking a task from the slot.
|
|
|
|
|
|
|
|
let runs = proc.slot_runs.get();
|
|
|
|
|
|
|
|
if runs < SLOT_LIMIT {
|
|
|
|
|
|
|
|
if let Some(task) = proc.slot.take() {
|
|
|
|
|
|
|
|
proc.slot_runs.set(runs + 1);
|
|
|
|
|
|
|
|
return Some(task);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
proc.slot_runs.set(0);
|
|
|
|
|
|
|
|
|
|
|
|
// Pop a task from the local queue, if not empty.
|
|
|
|
// Pop a task from the local queue, if not empty.
|
|
|
|
local.pop().or_else(|| {
|
|
|
|
proc.worker.pop().or_else(|| {
|
|
|
|
// Otherwise, we need to look for a task elsewhere.
|
|
|
|
// Otherwise, we need to look for a task elsewhere.
|
|
|
|
iter::repeat_with(|| {
|
|
|
|
iter::repeat_with(|| {
|
|
|
|
// Try stealing a batch of tasks from the global queue.
|
|
|
|
// Try stealing a batch of tasks from the global queue.
|
|
|
|
pool.injector
|
|
|
|
POOL.injector
|
|
|
|
.steal_batch_and_pop(&local)
|
|
|
|
.steal_batch_and_pop(&proc.worker)
|
|
|
|
// Or try stealing a batch of tasks from one of the other threads.
|
|
|
|
// Or try stealing a batch of tasks from one of the other threads.
|
|
|
|
.or_else(|| {
|
|
|
|
.or_else(|| {
|
|
|
|
// First, pick a random starting point in the list of local queues.
|
|
|
|
// First, pick a random starting point in the list of local queues.
|
|
|
|
let len = pool.stealers.len();
|
|
|
|
let len = POOL.stealers.len();
|
|
|
|
let start = random(len as u32) as usize;
|
|
|
|
let start = random(len as u32) as usize;
|
|
|
|
|
|
|
|
|
|
|
|
// Try stealing a batch of tasks from each local queue starting from the
|
|
|
|
// Try stealing a batch of tasks from each local queue starting from the
|
|
|
|
// chosen point.
|
|
|
|
// chosen point.
|
|
|
|
let (l, r) = pool.stealers.split_at(start);
|
|
|
|
let (l, r) = POOL.stealers.split_at(start);
|
|
|
|
let rotated = r.iter().chain(l.iter());
|
|
|
|
let stealers = r.iter().chain(l.iter());
|
|
|
|
rotated.map(|s| s.steal_batch_and_pop(&local)).collect()
|
|
|
|
stealers
|
|
|
|
|
|
|
|
.map(|s| s.steal_batch_and_pop(&proc.worker))
|
|
|
|
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
// Loop while no task was stolen and any steal operation needs to be retried.
|
|
|
|
// Loop while no task was stolen and any steal operation needs to be retried.
|
|
|
|