Skip to content

Commit c652691

Browse files
committed
replace AtomicPtr with RefCell
Signed-off-by: glorv <glorvs@163.com>
1 parent 4bde2f5 commit c652691

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

src/queue/multilevel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ impl LevelManager {
405405
std::cmp::min(total_tasks / level_0_tasks, LEVEL_MAX_QUEUE_MAX_STEAL_SIZE)
406406
};
407407
self.max_level_queue_steal_size
408-
.store(new_steal_count as usize, SeqCst);
408+
.store(new_steal_count, SeqCst);
409409
for (i, c) in self.last_exec_tasks_per_level.iter().enumerate() {
410410
c.set(cur_total_tasks_per_level[i]);
411411
}
@@ -457,7 +457,7 @@ pub(crate) struct ElapsedTime(AtomicU64);
457457

458458
impl ElapsedTime {
459459
pub(crate) fn as_duration(&self) -> Duration {
460-
Duration::from_micros(self.0.load(Relaxed) as u64)
460+
Duration::from_micros(self.0.load(Relaxed))
461461
}
462462

463463
pub(super) fn inc_by(&self, t: Duration) {

src/queue/priority.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
//! information.
1010
1111
use std::{
12+
cell::RefCell,
1213
sync::{
13-
atomic::{AtomicPtr, AtomicU64, Ordering},
14+
atomic::{AtomicU64, Ordering},
1415
Arc,
1516
},
1617
time::{Duration, Instant},
@@ -156,32 +157,24 @@ impl<T: TaskCell + Send + 'static> QueueCore<T> {
156157
}
157158
}
158159

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

166+
// It is safe here because the value is only visited by the thread which calls `pop()`.
167+
unsafe impl<T: Send> Sync for Slot<T> {}
168+
165169
impl<T> Slot<T> {
166170
fn new(value: T) -> Self {
167171
Self {
168-
ptr: AtomicPtr::new(Box::into_raw(Box::new(value))),
172+
cell: RefCell::new(Some(value)),
169173
}
170174
}
171175

172176
fn take(&self) -> Option<T> {
173-
let raw_ptr = self.ptr.swap(std::ptr::null_mut(), Ordering::SeqCst);
174-
if !raw_ptr.is_null() {
175-
unsafe { Some(*Box::from_raw(raw_ptr)) }
176-
} else {
177-
None
178-
}
179-
}
180-
}
181-
182-
impl<T> Drop for Slot<T> {
183-
fn drop(&mut self) {
184-
self.take();
177+
self.cell.take()
185178
}
186179
}
187180

@@ -350,7 +343,7 @@ impl Builder {
350343
.collect();
351344

352345
let injector = TaskInjector {
353-
queue: queue,
346+
queue,
354347
task_manager: self.manager,
355348
};
356349

0 commit comments

Comments
 (0)