mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-26 00:00:16 +02:00
remove atomic-ness
This commit is contained in:
@@ -626,53 +626,29 @@ impl Core {
|
||||
.or_else(|| self.run_queue.pop())
|
||||
}
|
||||
|
||||
/// Function responsible for stealing tasks from another worker
|
||||
///
|
||||
/// Note: Only if less than half the workers are searching for tasks to steal
|
||||
/// a new worker will actually try to steal. The idea is to make sure not all
|
||||
/// workers will be trying to steal at the same time.
|
||||
fn steal_work(&mut self, worker: &Worker) -> Option<Notified> {
|
||||
const ATTEMPTS: usize = 4;
|
||||
|
||||
if !self.transition_to_searching(worker) {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Number of remotes
|
||||
let num = worker.handle.shared.remotes.len();
|
||||
// Start from a random worker
|
||||
let start = self.rand.fastrand_n(num as u32) as usize;
|
||||
|
||||
// Run this a few times
|
||||
for i in 0..ATTEMPTS {
|
||||
// Only try stealing the LIFO slot
|
||||
let steal_lifo = i == ATTEMPTS - 1;
|
||||
for i in 0..num {
|
||||
let i = (start + i) % num;
|
||||
|
||||
// Start from a random worker
|
||||
let start = self.rand.fastrand_n(num as u32) as usize;
|
||||
// Don't steal from ourself! We know we don't have work.
|
||||
if i == worker.index {
|
||||
continue;
|
||||
}
|
||||
|
||||
for i in 0..num {
|
||||
let i = (start + i) % num;
|
||||
|
||||
// Don't steal from ourself! We know we don't have work.
|
||||
if i == worker.index {
|
||||
continue;
|
||||
}
|
||||
|
||||
let target = &worker.handle.shared.remotes[i];
|
||||
if let Some(task) = target
|
||||
.steal
|
||||
.steal_into(&mut self.run_queue, &mut self.metrics)
|
||||
{
|
||||
return Some(task);
|
||||
}
|
||||
|
||||
if steal_lifo {
|
||||
// Try stealing from the LIFO slot
|
||||
if let Some(task) = target.lifo_slot.take_remote() {
|
||||
self.metrics.incr_steal_count(1);
|
||||
self.metrics.incr_steal_operations();
|
||||
return Some(task);
|
||||
}
|
||||
}
|
||||
let target = &worker.handle.shared.remotes[i];
|
||||
if let Some(task) = target
|
||||
.steal
|
||||
.steal_into(&mut self.run_queue, &mut self.metrics)
|
||||
{
|
||||
return Some(task);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,25 +5,33 @@ use crate::runtime::task::{Header, Notified};
|
||||
use std::marker::PhantomData;
|
||||
use std::ptr::{self, NonNull};
|
||||
|
||||
pub(crate) struct AtomicCell<S> {
|
||||
task: AtomicPtr<Header>,
|
||||
pub(crate) struct AtomicCell<S: 'static> {
|
||||
// task: AtomicPtr<Header>,
|
||||
task: std::cell::UnsafeCell<Option<Notified<S>>>,
|
||||
_p: PhantomData<S>,
|
||||
}
|
||||
|
||||
unsafe impl<S: 'static> Send for AtomicCell<S> {}
|
||||
unsafe impl<S: 'static> Sync for AtomicCell<S> {}
|
||||
|
||||
impl<S> AtomicCell<S> {
|
||||
pub(crate) fn new() -> AtomicCell<S> {
|
||||
AtomicCell {
|
||||
task: AtomicPtr::default(),
|
||||
// task: AtomicPtr::default(),
|
||||
task: Default::default(),
|
||||
_p: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Should be called from a local context
|
||||
pub(crate) fn is_some(&self) -> bool {
|
||||
!self.task.load(Acquire).is_null()
|
||||
// !self.task.load(Acquire).is_null()
|
||||
unsafe { (*self.task.get()).is_some() }
|
||||
}
|
||||
|
||||
pub(crate) fn take_local(&self) -> Option<Notified<S>> {
|
||||
unsafe { (*self.task.get()).take() }
|
||||
/*
|
||||
let ptr = self.task.load(Acquire);
|
||||
|
||||
if ptr.is_null() {
|
||||
@@ -39,9 +47,12 @@ impl<S> AtomicCell<S> {
|
||||
}
|
||||
|
||||
NonNull::new(ptr).map(|ptr| unsafe { Notified::from_raw(ptr) })
|
||||
*/
|
||||
}
|
||||
|
||||
pub(crate) fn swap_local(&self, task: Notified<S>) -> Option<Notified<S>> {
|
||||
std::mem::replace(unsafe { &mut (*self.task.get()) }, Some(task))
|
||||
/*
|
||||
let next = task.into_raw().as_ptr();
|
||||
let prev = self.task.load(Acquire);
|
||||
|
||||
@@ -66,8 +77,10 @@ impl<S> AtomicCell<S> {
|
||||
// this is the only thread that could set the cell to !null.
|
||||
self.task.store(next, Release);
|
||||
None
|
||||
*/
|
||||
}
|
||||
|
||||
/*
|
||||
pub(crate) fn take_remote(&self) -> Option<Notified<S>> {
|
||||
let task = self.task.load(Acquire);
|
||||
|
||||
@@ -87,4 +100,5 @@ impl<S> AtomicCell<S> {
|
||||
|
||||
return None;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user