Skip to content

Commit

Permalink
Minor cleanups
Browse files Browse the repository at this point in the history
* queue: remove final unprotected.

* list: stricter send/sync bounds.

* lock/api: remove unnessecary API for LockGuard & test cleanup
  • Loading branch information
Lee-Janggun committed Jan 5, 2024
1 parent 417ba16 commit 41bb50d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 38 deletions.
28 changes: 3 additions & 25 deletions src/lock/api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::cell::UnsafeCell;
use core::mem::{self, ManuallyDrop};
use core::mem::ManuallyDrop;
use core::ops::{Deref, DerefMut};

/// Raw lock interface.
Expand Down Expand Up @@ -151,40 +151,18 @@ impl<L: RawLock, T> DerefMut for LockGuard<'_, L, T> {
}
}

impl<L: RawLock, T> LockGuard<'_, L, T> {
/// Transforms a lock guard to an address.
pub fn into_raw(self) -> usize {
let ret = self.lock as *const _ as usize;
mem::forget(self);
ret
}

/// # Safety
///
/// The given arguments should be the data of a forgotten lock guard.
pub unsafe fn from_raw(data: usize, token: L::Token) -> Self {
Self {
// SAFETY: data is from a `lock` that was forgotten.
lock: &*(data as *const _),
token: ManuallyDrop::new(token),
}
}
}

#[cfg(test)]
pub mod tests {

use std::thread::scope;

use super::{Lock, RawLock};
use std::thread::scope;

pub fn smoke<L: RawLock>() {
const LENGTH: usize = 1024;
let d = Lock::<L, Vec<usize>>::default();

scope(|s| {
let d = &d;
for i in 1..LENGTH {
let d = &d;
s.spawn(move || {
let mut d = d.lock();
d.push(i);
Expand Down
5 changes: 5 additions & 0 deletions src/lockfree/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ pub struct List<K, V> {
head: Atomic<Node<K, V>>,
}

// Unlike stack and queue, we need `K` and `V` to be `Sync` for the list to be `Sync`,
// as both `K` and `V` are accessed concurrently in `find` and `delete`, respectively.
unsafe impl<K: Sync, V: Sync> Sync for List<K, V> {}
unsafe impl<K: Send, V: Send> Send for List<K, V> {}

impl<K, V> Default for List<K, V>
where
K: Ord,
Expand Down
21 changes: 8 additions & 13 deletions src/lockfree/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use core::mem::{self, MaybeUninit};
use core::sync::atomic::Ordering::*;

use crossbeam_epoch::{unprotected, Atomic, Guard, Owned, Shared};
use crossbeam_epoch::{Atomic, Guard, Owned, Shared};
use crossbeam_utils::CachePadded;

/// Michael-Scott queue.
Expand Down Expand Up @@ -39,21 +39,16 @@ unsafe impl<T: Send> Send for Queue<T> {}

impl<T> Default for Queue<T> {
fn default() -> Self {
let q = Self {
head: CachePadded::new(Atomic::null()),
tail: CachePadded::new(Atomic::null()),
};

// SAFETY: We are creating a new queue, hence have sole ownership of it.
let sentinel = Owned::new(Node {
let sentinel = Box::into_raw(Box::new(Node {
data: MaybeUninit::uninit(),
next: Atomic::null(),
})
.into_shared(unsafe { unprotected() });
}))
.cast_const();

q.head.store(sentinel, Relaxed);
q.tail.store(sentinel, Relaxed);
q
Self {
head: CachePadded::new(sentinel.into()),
tail: CachePadded::new(sentinel.into()),
}
}
}

Expand Down

0 comments on commit 41bb50d

Please sign in to comment.