Skip to content

Commit d08f631

Browse files
committed
Merge lock and lockfree crates
1 parent 5399541 commit d08f631

21 files changed

+100
-128
lines changed

lock/.gitignore .gitignore

File renamed without changes.

lockfree/Cargo.toml Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "lockfree"
2+
name = "kaist-cs431"
33
version = "0.1.0"
44
authors = ["Jeehoon Kang <jeehoon.kang@kaist.ac.kr>"]
55
edition = "2021"

lock/Cargo.toml

-8
This file was deleted.

lock/src/lib.rs

-18
This file was deleted.

lockfree/.gitignore

-2
This file was deleted.

lockfree/Cargo.lock

-68
This file was deleted.

lockfree/src/lib.rs

-18
This file was deleted.

src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//! KAIST CS431: Concurrent Programming.
2+
3+
#![warn(missing_docs)]
4+
#![warn(missing_debug_implementations)]
5+
#![allow(clippy::result_unit_err)]
6+
7+
pub mod lock;
8+
pub mod lockfree;

lock/src/lock.rs src/lock/api.rs

+18
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,31 @@ use core::marker::PhantomData;
33
use core::mem;
44
use core::ops::{Deref, DerefMut};
55

6+
/// Raw lock interface.
67
pub trait RawLock: Default + Send + Sync {
8+
/// Raw lock's token type.
79
type Token: Clone;
810

11+
/// Acquires the raw lock.
912
fn lock(&self) -> Self::Token;
1013

14+
/// Releases the raw lock.
15+
///
1116
/// # Safety
1217
///
1318
/// `unlock()` should be called with the token given by the corresponding `lock()`.
1419
unsafe fn unlock(&self, token: Self::Token);
1520
}
1621

22+
/// Raw lock interface for the try_lock API.
1723
pub trait RawTryLock: RawLock {
24+
/// Tries to acquire the raw lock.
1825
fn try_lock(&self) -> Result<Self::Token, ()>;
1926
}
2027

28+
/// A type-safe lock.
2129
#[repr(C)]
30+
#[derive(Debug)]
2231
pub struct Lock<L: RawLock, T> {
2332
lock: L,
2433
data: UnsafeCell<T>,
@@ -28,17 +37,20 @@ unsafe impl<L: RawLock, T: Send> Send for Lock<L, T> {}
2837
unsafe impl<L: RawLock, T: Send> Sync for Lock<L, T> {}
2938

3039
impl<L: RawLock, T> Lock<L, T> {
40+
/// Creates a new lock.
3141
pub fn new(data: T) -> Self {
3242
Self {
3343
lock: L::default(),
3444
data: UnsafeCell::new(data),
3545
}
3646
}
3747

48+
/// Destroys the lock and retrieves the lock-protected value.
3849
pub fn into_inner(self) -> T {
3950
self.data.into_inner()
4051
}
4152

53+
/// Acquires the lock and dereferences the inner value.
4254
pub fn lock(&self) -> LockGuard<L, T> {
4355
let token = self.lock.lock();
4456
LockGuard {
@@ -50,6 +62,7 @@ impl<L: RawLock, T> Lock<L, T> {
5062
}
5163

5264
impl<L: RawTryLock, T> Lock<L, T> {
65+
/// Tries to acquire the lock and dereferences the inner value.
5366
pub fn try_lock(&self) -> Result<LockGuard<L, T>, ()> {
5467
self.lock.try_lock().map(|token| LockGuard {
5568
lock: self,
@@ -74,6 +87,7 @@ impl<L: RawLock, T> Lock<L, T> {
7487
&*self.data.get()
7588
}
7689

90+
/// Dereferences the inner value.
7791
pub fn get_mut(&mut self) -> &mut T {
7892
unsafe { &mut *self.data.get() }
7993
}
@@ -87,6 +101,8 @@ impl<L: RawLock, T> Lock<L, T> {
87101
}
88102
}
89103

104+
/// A guard that holds the lock and dereferences the inner value.
105+
#[derive(Debug)]
90106
pub struct LockGuard<'s, L: RawLock, T> {
91107
lock: &'s Lock<L, T>,
92108
token: L::Token,
@@ -97,6 +113,7 @@ unsafe impl<'s, L: RawLock, T: Send> Send for LockGuard<'s, L, T> {}
97113
unsafe impl<'s, L: RawLock, T: Sync> Sync for LockGuard<'s, L, T> {}
98114

99115
impl<'s, L: RawLock, T> LockGuard<'s, L, T> {
116+
/// Returns the address of the referenced lock.
100117
pub fn raw(&mut self) -> usize {
101118
self.lock as *const _ as usize
102119
}
@@ -123,6 +140,7 @@ impl<'s, L: RawLock, T> DerefMut for LockGuard<'s, L, T> {
123140
}
124141

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

lock/src/clhlock.rs src/lock/clhlock.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ struct Node {
88
locked: AtomicBool,
99
}
1010

11-
#[derive(Clone)]
11+
#[derive(Debug, Clone)]
1212
pub struct Token(*const CachePadded<Node>);
1313

14+
/// CLH lock.
15+
#[derive(Debug)]
1416
pub struct ClhLock {
1517
tail: AtomicPtr<CachePadded<Node>>,
1618
}
@@ -54,10 +56,11 @@ impl RawLock for ClhLock {
5456

5557
#[cfg(test)]
5658
mod tests {
57-
use crate::clhlock::ClhLock;
59+
use super::super::api;
60+
use super::ClhLock;
5861

5962
#[test]
6063
fn smoke() {
61-
crate::lock::tests::smoke::<ClhLock>();
64+
api::tests::smoke::<ClhLock>();
6265
}
6366
}

lock/src/mcslock.rs src/lock/mcslock.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ struct Node {
1010
next: AtomicPtr<CachePadded<Node>>,
1111
}
1212

13-
#[derive(Clone)]
13+
#[derive(Debug, Clone)]
1414
pub struct Token(*mut CachePadded<Node>);
1515

16+
/// An MCS lock.
17+
#[derive(Debug)]
1618
pub struct McsLock {
1719
tail: AtomicPtr<CachePadded<Node>>,
1820
}
@@ -82,10 +84,11 @@ impl RawLock for McsLock {
8284

8385
#[cfg(test)]
8486
mod tests {
85-
use crate::mcslock::McsLock;
87+
use super::super::api;
88+
use super::McsLock;
8689

8790
#[test]
8891
fn smoke() {
89-
crate::lock::tests::smoke::<McsLock>();
92+
api::tests::smoke::<McsLock>();
9093
}
9194
}

lock/src/mcsparkinglock.rs src/lock/mcsparkinglock.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ struct Node {
1414
_marker: PhantomData<*const ()>,
1515
}
1616

17-
#[derive(Clone)]
17+
#[derive(Debug, Clone)]
1818
pub struct Token(*mut CachePadded<Node>);
1919

20+
/// An MCS parking lock.
21+
#[derive(Debug)]
2022
pub struct McsParkingLock {
2123
tail: AtomicPtr<CachePadded<Node>>,
2224
}
@@ -89,10 +91,11 @@ impl RawLock for McsParkingLock {
8991

9092
#[cfg(test)]
9193
mod tests {
92-
use crate::mcsparkinglock::McsParkingLock;
94+
use super::super::api;
95+
use super::mcsparkinglock::McsParkingLock;
9396

9497
#[test]
9598
fn smoke() {
96-
crate::lock::tests::smoke::<McsParkingLock>();
99+
api::tests::smoke::<McsParkingLock>();
97100
}
98101
}

src/lock/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//! Locks.
2+
3+
mod api;
4+
mod clhlock;
5+
mod mcslock;
6+
mod mcsparkinglock;
7+
pub mod seqlock;
8+
mod spinlock;
9+
mod ticketlock;
10+
11+
pub use api::{Lock, LockGuard, RawLock, RawTryLock};
12+
pub use clhlock::ClhLock;
13+
pub use mcslock::McsLock;
14+
pub use mcsparkinglock::McsParkingLock;
15+
pub use spinlock::SpinLock;
16+
pub use ticketlock::TicketLock;

0 commit comments

Comments
 (0)