Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
Closes #975
  • Loading branch information
Lee-Janggun committed Dec 31, 2024
1 parent 5f1f3fd commit 3cafc79
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 15 deletions.
9 changes: 6 additions & 3 deletions homework/src/boc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ pub unsafe trait CownPtrs {
}

unsafe impl CownPtrs for () {
type CownRefs<'l> = ()
type CownRefs<'l>
= ()
where
Self: 'l;

Expand All @@ -253,7 +254,8 @@ unsafe impl CownPtrs for () {
}

unsafe impl<T: Send + 'static, Ts: CownPtrs> CownPtrs for (CownPtr<T>, Ts) {
type CownRefs<'l> = (&'l mut T, Ts::CownRefs<'l>)
type CownRefs<'l>
= (&'l mut T, Ts::CownRefs<'l>)
where
Self: 'l;

Expand All @@ -270,7 +272,8 @@ unsafe impl<T: Send + 'static, Ts: CownPtrs> CownPtrs for (CownPtr<T>, Ts) {
}

unsafe impl<T: Send + 'static> CownPtrs for Vec<CownPtr<T>> {
type CownRefs<'l> = Vec<&'l mut T>
type CownRefs<'l>
= Vec<&'l mut T>
where
Self: 'l;

Expand Down
2 changes: 0 additions & 2 deletions homework/src/elim_stack/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,13 @@ pub struct ElimStack<T, S: Stack<T>> {
// - 2: pop request
// - 3: request acknowledged
pub(crate) slots: [Atomic<S::PushReq>; ELIM_SIZE],
_marker: PhantomData<T>,
}

impl<T, S: Stack<T>> Default for ElimStack<T, S> {
fn default() -> Self {
Self {
inner: Default::default(),
slots: Default::default(),
_marker: PhantomData,
}
}
}
5 changes: 1 addition & 4 deletions homework/src/hazard_pointer/hazard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ impl Shield {
/// means that this shield is validated.
pub fn try_protect<T>(&self, pointer: *mut T, src: &AtomicPtr<T>) -> Result<(), *mut T> {
self.set(pointer);
Self::validate(pointer, src).map_err(|new| {
self.clear();
new
})
Self::validate(pointer, src).inspect_err(|_| self.clear())
}

/// Get a protected pointer from `src`.
Expand Down
5 changes: 3 additions & 2 deletions homework/src/list_set/optimistic_fine_grained.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::cmp::Ordering::*;
use std::mem::{self, ManuallyDrop};
use std::sync::atomic::Ordering;

use crate::ConcurrentSet;
use crossbeam_epoch::{pin, Atomic, Guard, Owned, Shared};
use cs431::lock::seqlock::{ReadGuard, SeqLock};

use crate::ConcurrentSet;

#[derive(Debug)]
struct Node<T> {
data: T,
Expand Down Expand Up @@ -92,7 +93,7 @@ pub struct Iter<'g, T> {
impl<T> OptimisticFineGrainedListSet<T> {
/// An iterator visiting all elements. `next()` returns `Some(Err(()))` when validation fails.
/// In that case, the user must restart the iteration.
pub fn iter<'g>(&'g self, guard: &'g Guard) -> Iter<'_, T> {
pub fn iter<'g>(&'g self, guard: &'g Guard) -> Iter<'g, T> {
Iter {
cursor: ManuallyDrop::new(self.head(guard)),
guard,
Expand Down
5 changes: 4 additions & 1 deletion homework/tests/growable_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<V> ConcurrentMap<u32, V> for ArrayMap<V> {
return Err(());
}
match slot.compare_exchange(curr, Shared::null(), AcqRel, Acquire, guard) {
Ok(_) => Ok(unsafe { curr.deref() }.deref()),
Ok(_) => Ok(unsafe { curr.deref() }),
Err(_) => Err(()), // already removed
}
}
Expand Down Expand Up @@ -132,8 +132,11 @@ mod stack {
pub(super) unsafe fn push_node<'g>(&self, n: Shared<'g, Node<T>>, guard: &'g Guard) {
let mut head = self.head.load(Relaxed, guard);
loop {
// SAEFTY: as n is pused only once, and after the push, n is not used again, we are
// the unique accessor of `n.next`. Hence non-atomic write is safe.
unsafe { *n.deref().next.get() = head.as_raw() };

// TODO: Relaxed fine here? Might need release so that it syncs with `drop`?
match self.head.compare_exchange(head, n, Relaxed, Relaxed, guard) {
Ok(_) => break,
Err(e) => head = e.current,
Expand Down
9 changes: 6 additions & 3 deletions src/lock/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use core::ops::{Deref, DerefMut};
pub unsafe trait RawLock: Default + Send + Sync {
/// Raw lock's token type.
///
/// Send + Sync is needed to make LockGuard Send + Sync.
type Token: Send + Sync;
/// We don't enforce Send + Sync, as some locks may not satisfy it. Nessecary bounds will be
/// auto-derived.
type Token;

/// Acquires the raw lock.
fn lock(&self) -> Self::Token;
Expand Down Expand Up @@ -49,6 +50,8 @@ pub struct Lock<L: RawLock, T> {
}

// Send is automatically implemented for Lock.

// SATEFY: threads can only access `&mut T` via the lock, and `L` is `Sync`.
unsafe impl<L: RawLock, T: Send> Sync for Lock<L, T> {}

impl<L: RawLock, T: Default> Default for Lock<L, T>
Expand Down Expand Up @@ -99,7 +102,7 @@ impl<L: RawTryLock, T> Lock<L, T> {
}

/// A guard that holds the lock and dereferences the inner value.
// Send/Sync are automatically implemented.
// `Send` and `Sync` are automatically derived.
#[derive(Debug)]
pub struct LockGuard<'s, L: RawLock, T> {
lock: &'s Lock<L, T>,
Expand Down

0 comments on commit 3cafc79

Please sign in to comment.