Skip to content

Commit

Permalink
optimize the implement of task slot
Browse files Browse the repository at this point in the history
Signed-off-by: glorv <[email protected]>
  • Loading branch information
glorv committed Dec 7, 2022
1 parent d7dc51b commit 5e35595
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/queue/priority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use std::{
sync::{
atomic::{AtomicBool, AtomicU64, Ordering},
atomic::{AtomicPtr, AtomicU64, Ordering},
Arc,
},
time::{Duration, Instant},
Expand Down Expand Up @@ -156,26 +156,22 @@ impl<T: TaskCell + Send + 'static> QueueCore<T> {
}
}

/// A holder to store task.
/// A holder to store task. The slot can be concurrently visit by multiple thread in the skip-list.
struct Slot<T> {
ptr: *mut T,
consumed: AtomicBool,
ptr: AtomicPtr<T>,
}

unsafe impl<T: Send> Send for Slot<T> {}
unsafe impl<T: Send> Sync for Slot<T> {}

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

fn take(&self) -> Option<T> {
if !self.consumed.swap(true, Ordering::SeqCst) {
unsafe { Some(*Box::from_raw(self.ptr)) }
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
}
Expand Down

0 comments on commit 5e35595

Please sign in to comment.