mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-28 00:00:11 +02:00
threadpool: rename inner to something more descriptive (#768)
`inner` is a fitting name for variables of type named `Inner`, but in other cases I find them confusing - sometimes `inner` refers to a `Pool`, sometimes to a `Sender`. I renamed a bunch of variables named `inner` to be more descriptive. This PR is the first step in an effort of splitting https://github.com/tokio-rs/tokio/pull/722#issuecomment-439552671 into multiple PRs.
This commit is contained in:
@@ -270,8 +270,8 @@ impl Pool {
|
||||
///
|
||||
/// Called from either inside or outside of the scheduler. If currently on
|
||||
/// the scheduler, then a fast path is taken.
|
||||
pub fn submit(&self, task: Arc<Task>, inner: &Arc<Pool>) {
|
||||
debug_assert_eq!(*self, **inner);
|
||||
pub fn submit(&self, task: Arc<Task>, pool: &Arc<Pool>) {
|
||||
debug_assert_eq!(*self, **pool);
|
||||
|
||||
Worker::with_current(|worker| {
|
||||
if let Some(worker) = worker {
|
||||
@@ -283,18 +283,18 @@ impl Pool {
|
||||
// The second check handles the case where the current thread is
|
||||
// part of a different threadpool than the one being submitted
|
||||
// to.
|
||||
if !worker.is_blocking() && *self == *worker.inner {
|
||||
if !worker.is_blocking() && *self == *worker.pool {
|
||||
let idx = worker.id.0;
|
||||
|
||||
trace!(" -> submit internal; idx={}", idx);
|
||||
|
||||
worker.inner.workers[idx].submit_internal(task);
|
||||
worker.inner.signal_work(inner);
|
||||
worker.pool.workers[idx].submit_internal(task);
|
||||
worker.pool.signal_work(pool);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
self.submit_external(task, inner);
|
||||
self.submit_external(task, pool);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -302,8 +302,8 @@ impl Pool {
|
||||
///
|
||||
/// Called from outside of the scheduler, this function is how new tasks
|
||||
/// enter the system.
|
||||
pub fn submit_external(&self, task: Arc<Task>, inner: &Arc<Pool>) {
|
||||
debug_assert_eq!(*self, **inner);
|
||||
pub fn submit_external(&self, task: Arc<Task>, pool: &Arc<Pool>) {
|
||||
debug_assert_eq!(*self, **pool);
|
||||
|
||||
use worker::Lifecycle::Notified;
|
||||
|
||||
@@ -311,21 +311,21 @@ impl Pool {
|
||||
// sleeping tasks get woken up
|
||||
if let Some((idx, worker_state)) = self.sleep_stack.pop(&self.workers, Notified, false) {
|
||||
trace!("submit to existing worker; idx={}; state={:?}", idx, worker_state);
|
||||
self.submit_to_external(idx, task, worker_state, inner);
|
||||
self.submit_to_external(idx, task, worker_state, pool);
|
||||
return;
|
||||
}
|
||||
|
||||
// All workers are active, so pick a random worker and submit the
|
||||
// task to it.
|
||||
self.submit_to_random(task, inner);
|
||||
self.submit_to_random(task, pool);
|
||||
}
|
||||
|
||||
/// Submit a task to a random worker
|
||||
///
|
||||
/// Called from outside of the scheduler, this function is how new tasks
|
||||
/// enter the system.
|
||||
pub fn submit_to_random(&self, task: Arc<Task>, inner: &Arc<Pool>) {
|
||||
debug_assert_eq!(*self, **inner);
|
||||
pub fn submit_to_random(&self, task: Arc<Task>, pool: &Arc<Pool>) {
|
||||
debug_assert_eq!(*self, **pool);
|
||||
|
||||
let len = self.workers.len();
|
||||
let idx = self.rand_usize() % len;
|
||||
@@ -333,21 +333,21 @@ impl Pool {
|
||||
trace!(" -> submitting to random; idx={}", idx);
|
||||
|
||||
let state = self.workers[idx].load_state();
|
||||
self.submit_to_external(idx, task, state, inner);
|
||||
self.submit_to_external(idx, task, state, pool);
|
||||
}
|
||||
|
||||
fn submit_to_external(&self,
|
||||
idx: usize,
|
||||
task: Arc<Task>,
|
||||
state: worker::State,
|
||||
inner: &Arc<Pool>)
|
||||
pool: &Arc<Pool>)
|
||||
{
|
||||
debug_assert_eq!(*self, **inner);
|
||||
debug_assert_eq!(*self, **pool);
|
||||
|
||||
let entry = &self.workers[idx];
|
||||
|
||||
if !entry.submit_external(task, state) {
|
||||
self.spawn_thread(WorkerId::new(idx), inner);
|
||||
self.spawn_thread(WorkerId::new(idx), pool);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -360,14 +360,14 @@ impl Pool {
|
||||
self.backup_stack.push(&self.backup, backup_id)
|
||||
}
|
||||
|
||||
pub fn notify_blocking_task(&self, inner: &Arc<Pool>) {
|
||||
debug_assert_eq!(*self, **inner);
|
||||
self.blocking.notify_task(&inner);
|
||||
pub fn notify_blocking_task(&self, pool: &Arc<Pool>) {
|
||||
debug_assert_eq!(*self, **pool);
|
||||
self.blocking.notify_task(&pool);
|
||||
}
|
||||
|
||||
/// Provision a thread to run a worker
|
||||
pub fn spawn_thread(&self, id: WorkerId, inner: &Arc<Pool>) {
|
||||
debug_assert_eq!(*self, **inner);
|
||||
pub fn spawn_thread(&self, id: WorkerId, pool: &Arc<Pool>) {
|
||||
debug_assert_eq!(*self, **pool);
|
||||
|
||||
let backup_id = match self.backup_stack.pop(&self.backup, false) {
|
||||
Ok(Some(backup_id)) => backup_id,
|
||||
@@ -392,31 +392,31 @@ impl Pool {
|
||||
|
||||
let mut th = thread::Builder::new();
|
||||
|
||||
if let Some(ref prefix) = inner.config.name_prefix {
|
||||
if let Some(ref prefix) = pool.config.name_prefix {
|
||||
th = th.name(format!("{}{}", prefix, backup_id.0));
|
||||
}
|
||||
|
||||
if let Some(stack) = inner.config.stack_size {
|
||||
if let Some(stack) = pool.config.stack_size {
|
||||
th = th.stack_size(stack);
|
||||
}
|
||||
|
||||
let inner = inner.clone();
|
||||
let pool = pool.clone();
|
||||
|
||||
let res = th.spawn(move || {
|
||||
if let Some(ref f) = inner.config.after_start {
|
||||
if let Some(ref f) = pool.config.after_start {
|
||||
f();
|
||||
}
|
||||
|
||||
let mut worker_id = id;
|
||||
|
||||
inner.backup[backup_id.0].start(&worker_id);
|
||||
pool.backup[backup_id.0].start(&worker_id);
|
||||
|
||||
loop {
|
||||
// The backup token should be in the running state.
|
||||
debug_assert!(inner.backup[backup_id.0].is_running());
|
||||
debug_assert!(pool.backup[backup_id.0].is_running());
|
||||
|
||||
// TODO: Avoid always cloning
|
||||
let worker = Worker::new(worker_id, backup_id, inner.clone());
|
||||
let worker = Worker::new(worker_id, backup_id, pool.clone());
|
||||
|
||||
// Run the worker. If the worker transitioned to a "blocking"
|
||||
// state, then `is_blocking` will be true.
|
||||
@@ -425,14 +425,14 @@ impl Pool {
|
||||
break;
|
||||
}
|
||||
|
||||
debug_assert!(!inner.backup[backup_id.0].is_pushed());
|
||||
debug_assert!(!pool.backup[backup_id.0].is_pushed());
|
||||
|
||||
// Push the thread back onto the backup stack. This makes it
|
||||
// available for future handoffs.
|
||||
//
|
||||
// This **must** happen before notifying the task.
|
||||
let res = inner.backup_stack
|
||||
.push(&inner.backup, backup_id);
|
||||
let res = pool.backup_stack
|
||||
.push(&pool.backup, backup_id);
|
||||
|
||||
if res.is_err() {
|
||||
// The pool is being shutdown.
|
||||
@@ -441,17 +441,17 @@ impl Pool {
|
||||
|
||||
// The task switched the current thread to blocking mode.
|
||||
// Now that the blocking task completed, any tasks
|
||||
inner.notify_blocking_task(&inner);
|
||||
pool.notify_blocking_task(&pool);
|
||||
|
||||
debug_assert!(inner.backup[backup_id.0].is_running());
|
||||
debug_assert!(pool.backup[backup_id.0].is_running());
|
||||
|
||||
// Wait for a handoff
|
||||
let handoff = inner.backup[backup_id.0]
|
||||
.wait_for_handoff(inner.config.keep_alive);
|
||||
let handoff = pool.backup[backup_id.0]
|
||||
.wait_for_handoff(pool.config.keep_alive);
|
||||
|
||||
match handoff {
|
||||
Handoff::Worker(id) => {
|
||||
debug_assert!(inner.backup[backup_id.0].is_running());
|
||||
debug_assert!(pool.backup[backup_id.0].is_running());
|
||||
worker_id = id;
|
||||
}
|
||||
Handoff::Idle | Handoff::Terminated => {
|
||||
@@ -460,11 +460,11 @@ impl Pool {
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ref f) = inner.config.before_stop {
|
||||
if let Some(ref f) = pool.config.before_stop {
|
||||
f();
|
||||
}
|
||||
|
||||
inner.thread_stopped();
|
||||
pool.thread_stopped();
|
||||
});
|
||||
|
||||
if let Err(e) = res {
|
||||
@@ -474,8 +474,8 @@ impl Pool {
|
||||
|
||||
/// If there are any other workers currently relaxing, signal them that work
|
||||
/// is available so that they can try to find more work to process.
|
||||
pub fn signal_work(&self, inner: &Arc<Pool>) {
|
||||
debug_assert_eq!(*self, **inner);
|
||||
pub fn signal_work(&self, pool: &Arc<Pool>) {
|
||||
debug_assert_eq!(*self, **pool);
|
||||
|
||||
use worker::Lifecycle::*;
|
||||
|
||||
@@ -509,7 +509,7 @@ impl Pool {
|
||||
}
|
||||
Shutdown => {
|
||||
trace!("signal_work -- spawn; idx={}", idx);
|
||||
self.spawn_thread(WorkerId(idx), inner);
|
||||
self.spawn_thread(WorkerId(idx), pool);
|
||||
}
|
||||
Running | Notified | Signaled => {
|
||||
// The workers are already active. No need to wake them up.
|
||||
|
||||
Reference in New Issue
Block a user