Skip to content

Commit

Permalink
replace AtomicPtr with RefCell
Browse files Browse the repository at this point in the history
Signed-off-by: glorv <glorvs@163.com>
  • Loading branch information
glorv committed Dec 15, 2022
1 parent 4bde2f5 commit cea45e3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/queue/multilevel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ impl LevelManager {
std::cmp::min(total_tasks / level_0_tasks, LEVEL_MAX_QUEUE_MAX_STEAL_SIZE)
};
self.max_level_queue_steal_size
.store(new_steal_count as usize, SeqCst);
.store(new_steal_count, SeqCst);
for (i, c) in self.last_exec_tasks_per_level.iter().enumerate() {
c.set(cur_total_tasks_per_level[i]);
}
Expand Down Expand Up @@ -457,7 +457,7 @@ pub(crate) struct ElapsedTime(AtomicU64);

impl ElapsedTime {
pub(crate) fn as_duration(&self) -> Duration {
Duration::from_micros(self.0.load(Relaxed) as u64)
Duration::from_micros(self.0.load(Relaxed))
}

pub(super) fn inc_by(&self, t: Duration) {
Expand Down
30 changes: 12 additions & 18 deletions src/queue/priority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
//! information.

use std::{
cell::RefCell,
sync::{
atomic::{AtomicPtr, AtomicU64, Ordering},
atomic::{AtomicU64, Ordering},
Arc,
},
time::{Duration, Instant},
Expand Down Expand Up @@ -156,32 +157,25 @@ impl<T: TaskCell + Send + 'static> QueueCore<T> {
}
}

/// A holder to store task. We wrap the value in an atomic ptr because the return value of pop()
/// only provide readonly reference to this value, though in our can it's safe to just take it.
/// A holder to store task. Wrap the task in a RefCell becuase crossbeam-skip only provide
/// readonly acess to a popped Entry.
struct Slot<T> {
ptr: AtomicPtr<T>,
//ptr: AtomicPtr<T>,
value: RefCell<Option<T>>,
}

// It is safe here because the value is only visited by the thread which calls `pop()`.
unsafe impl<T: Send> Sync for Slot<T> {}

impl<T> Slot<T> {
fn new(value: T) -> Self {
Self {
ptr: AtomicPtr::new(Box::into_raw(Box::new(value))),
value: RefCell::new(Some(value)),
}
}

fn take(&self) -> Option<T> {
let raw_ptr = self.ptr.swap(std::ptr::null_mut(), Ordering::SeqCst);
if !raw_ptr.is_null() {
unsafe { Some(*Box::from_raw(raw_ptr)) }
} else {
None
}
}
}

impl<T> Drop for Slot<T> {
fn drop(&mut self) {
self.take();
self.value.take()
}
}

Expand Down Expand Up @@ -350,7 +344,7 @@ impl Builder {
.collect();

let injector = TaskInjector {
queue: queue,
queue,
task_manager: self.manager,
};

Expand Down

0 comments on commit cea45e3

Please sign in to comment.