Skip to content

Commit 9ab9201

Browse files
committed
Move pthread locks to own module.
1 parent 8a34aaf commit 9ab9201

File tree

11 files changed

+69
-60
lines changed

11 files changed

+69
-60
lines changed

std/src/sys/unix/locks/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
mod pthread_condvar;
2+
mod pthread_mutex;
3+
mod pthread_remutex;
4+
mod pthread_rwlock;
5+
pub use pthread_condvar::{Condvar, MovableCondvar};
6+
pub use pthread_mutex::{MovableMutex, Mutex};
7+
pub use pthread_remutex::ReentrantMutex;
8+
pub use pthread_rwlock::{MovableRWLock, RWLock};

std/src/sys/unix/condvar.rs renamed to std/src/sys/unix/locks/pthread_condvar.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::cell::UnsafeCell;
2-
use crate::sys::mutex::{self, Mutex};
2+
use crate::sys::locks::{pthread_mutex, Mutex};
33
use crate::time::Duration;
44

55
pub struct Condvar {
@@ -79,7 +79,7 @@ impl Condvar {
7979

8080
#[inline]
8181
pub unsafe fn wait(&self, mutex: &Mutex) {
82-
let r = libc::pthread_cond_wait(self.inner.get(), mutex::raw(mutex));
82+
let r = libc::pthread_cond_wait(self.inner.get(), pthread_mutex::raw(mutex));
8383
debug_assert_eq!(r, 0);
8484
}
8585

@@ -111,7 +111,7 @@ impl Condvar {
111111
let timeout =
112112
sec.map(|s| libc::timespec { tv_sec: s, tv_nsec: nsec as _ }).unwrap_or(TIMESPEC_MAX);
113113

114-
let r = libc::pthread_cond_timedwait(self.inner.get(), mutex::raw(mutex), &timeout);
114+
let r = libc::pthread_cond_timedwait(self.inner.get(), pthread_mutex::raw(mutex), &timeout);
115115
assert!(r == libc::ETIMEDOUT || r == 0);
116116
r == 0
117117
}
@@ -169,7 +169,7 @@ impl Condvar {
169169
.unwrap_or(TIMESPEC_MAX);
170170

171171
// And wait!
172-
let r = libc::pthread_cond_timedwait(self.inner.get(), mutex::raw(mutex), &timeout);
172+
let r = libc::pthread_cond_timedwait(self.inner.get(), pthread_mutex::raw(mutex), &timeout);
173173
debug_assert!(r == libc::ETIMEDOUT || r == 0);
174174

175175
// ETIMEDOUT is not a totally reliable method of determining timeout due

std/src/sys/unix/mutex.rs renamed to std/src/sys/unix/locks/pthread_mutex.rs

+1-43
Original file line numberDiff line numberDiff line change
@@ -90,49 +90,7 @@ impl Mutex {
9090
}
9191
}
9292

93-
pub struct ReentrantMutex {
94-
inner: UnsafeCell<libc::pthread_mutex_t>,
95-
}
96-
97-
unsafe impl Send for ReentrantMutex {}
98-
unsafe impl Sync for ReentrantMutex {}
99-
100-
impl ReentrantMutex {
101-
pub const unsafe fn uninitialized() -> ReentrantMutex {
102-
ReentrantMutex { inner: UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER) }
103-
}
104-
105-
pub unsafe fn init(&self) {
106-
let mut attr = MaybeUninit::<libc::pthread_mutexattr_t>::uninit();
107-
cvt_nz(libc::pthread_mutexattr_init(attr.as_mut_ptr())).unwrap();
108-
let attr = PthreadMutexAttr(&mut attr);
109-
cvt_nz(libc::pthread_mutexattr_settype(attr.0.as_mut_ptr(), libc::PTHREAD_MUTEX_RECURSIVE))
110-
.unwrap();
111-
cvt_nz(libc::pthread_mutex_init(self.inner.get(), attr.0.as_ptr())).unwrap();
112-
}
113-
114-
pub unsafe fn lock(&self) {
115-
let result = libc::pthread_mutex_lock(self.inner.get());
116-
debug_assert_eq!(result, 0);
117-
}
118-
119-
#[inline]
120-
pub unsafe fn try_lock(&self) -> bool {
121-
libc::pthread_mutex_trylock(self.inner.get()) == 0
122-
}
123-
124-
pub unsafe fn unlock(&self) {
125-
let result = libc::pthread_mutex_unlock(self.inner.get());
126-
debug_assert_eq!(result, 0);
127-
}
128-
129-
pub unsafe fn destroy(&self) {
130-
let result = libc::pthread_mutex_destroy(self.inner.get());
131-
debug_assert_eq!(result, 0);
132-
}
133-
}
134-
135-
struct PthreadMutexAttr<'a>(&'a mut MaybeUninit<libc::pthread_mutexattr_t>);
93+
pub(super) struct PthreadMutexAttr<'a>(pub &'a mut MaybeUninit<libc::pthread_mutexattr_t>);
13694

13795
impl Drop for PthreadMutexAttr<'_> {
13896
fn drop(&mut self) {
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use super::pthread_mutex::PthreadMutexAttr;
2+
use crate::cell::UnsafeCell;
3+
use crate::mem::MaybeUninit;
4+
use crate::sys::cvt_nz;
5+
6+
pub struct ReentrantMutex {
7+
inner: UnsafeCell<libc::pthread_mutex_t>,
8+
}
9+
10+
unsafe impl Send for ReentrantMutex {}
11+
unsafe impl Sync for ReentrantMutex {}
12+
13+
impl ReentrantMutex {
14+
pub const unsafe fn uninitialized() -> ReentrantMutex {
15+
ReentrantMutex { inner: UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER) }
16+
}
17+
18+
pub unsafe fn init(&self) {
19+
let mut attr = MaybeUninit::<libc::pthread_mutexattr_t>::uninit();
20+
cvt_nz(libc::pthread_mutexattr_init(attr.as_mut_ptr())).unwrap();
21+
let attr = PthreadMutexAttr(&mut attr);
22+
cvt_nz(libc::pthread_mutexattr_settype(attr.0.as_mut_ptr(), libc::PTHREAD_MUTEX_RECURSIVE))
23+
.unwrap();
24+
cvt_nz(libc::pthread_mutex_init(self.inner.get(), attr.0.as_ptr())).unwrap();
25+
}
26+
27+
pub unsafe fn lock(&self) {
28+
let result = libc::pthread_mutex_lock(self.inner.get());
29+
debug_assert_eq!(result, 0);
30+
}
31+
32+
#[inline]
33+
pub unsafe fn try_lock(&self) -> bool {
34+
libc::pthread_mutex_trylock(self.inner.get()) == 0
35+
}
36+
37+
pub unsafe fn unlock(&self) {
38+
let result = libc::pthread_mutex_unlock(self.inner.get());
39+
debug_assert_eq!(result, 0);
40+
}
41+
42+
pub unsafe fn destroy(&self) {
43+
let result = libc::pthread_mutex_destroy(self.inner.get());
44+
debug_assert_eq!(result, 0);
45+
}
46+
}
File renamed without changes.

std/src/sys/unix/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub mod android;
1414
pub mod args;
1515
#[path = "../unix/cmath.rs"]
1616
pub mod cmath;
17-
pub mod condvar;
1817
pub mod env;
1918
pub mod fd;
2019
pub mod fs;
@@ -24,8 +23,8 @@ pub mod io;
2423
pub mod kernel_copy;
2524
#[cfg(target_os = "l4re")]
2625
mod l4re;
26+
pub mod locks;
2727
pub mod memchr;
28-
pub mod mutex;
2928
#[cfg(not(target_os = "l4re"))]
3029
pub mod net;
3130
#[cfg(target_os = "l4re")]
@@ -36,7 +35,6 @@ pub mod path;
3635
pub mod pipe;
3736
pub mod process;
3837
pub mod rand;
39-
pub mod rwlock;
4038
pub mod stack_overflow;
4139
pub mod stdio;
4240
pub mod thread;

std/src/sys_common/condvar.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use crate::sys::condvar as imp;
2-
use crate::sys::mutex as mutex_imp;
1+
use crate::sys::locks as imp;
32
use crate::sys_common::mutex::MovableMutex;
43
use crate::time::Duration;
54

65
mod check;
76

8-
type CondvarCheck = <mutex_imp::MovableMutex as check::CondvarCheck>::Check;
7+
type CondvarCheck = <imp::MovableMutex as check::CondvarCheck>::Check;
98

109
/// An OS-based condition variable.
1110
pub struct Condvar {

std/src/sys_common/condvar/check.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::sync::atomic::{AtomicUsize, Ordering};
2-
use crate::sys::mutex as mutex_imp;
2+
use crate::sys::locks as imp;
33
use crate::sys_common::mutex::MovableMutex;
44

55
pub trait CondvarCheck {
@@ -8,7 +8,7 @@ pub trait CondvarCheck {
88

99
/// For boxed mutexes, a `Condvar` will check it's only ever used with the same
1010
/// mutex, based on its (stable) address.
11-
impl CondvarCheck for Box<mutex_imp::Mutex> {
11+
impl CondvarCheck for Box<imp::Mutex> {
1212
type Check = SameMutexCheck;
1313
}
1414

@@ -22,7 +22,7 @@ impl SameMutexCheck {
2222
Self { addr: AtomicUsize::new(0) }
2323
}
2424
pub fn verify(&self, mutex: &MovableMutex) {
25-
let addr = mutex.raw() as *const mutex_imp::Mutex as usize;
25+
let addr = mutex.raw() as *const imp::Mutex as usize;
2626
match self.addr.compare_exchange(0, addr, Ordering::SeqCst, Ordering::SeqCst) {
2727
Ok(_) => {} // Stored the address
2828
Err(n) if n == addr => {} // Lost a race to store the same address
@@ -33,7 +33,7 @@ impl SameMutexCheck {
3333

3434
/// Unboxed mutexes may move, so `Condvar` can not require its address to stay
3535
/// constant.
36-
impl CondvarCheck for mutex_imp::Mutex {
36+
impl CondvarCheck for imp::Mutex {
3737
type Check = NoCheck;
3838
}
3939

std/src/sys_common/mutex.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::sys::mutex as imp;
1+
use crate::sys::locks as imp;
22

33
/// An OS-based mutual exclusion lock, meant for use in static variables.
44
///

std/src/sys_common/remutex.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::marker::PhantomPinned;
55
use crate::ops::Deref;
66
use crate::panic::{RefUnwindSafe, UnwindSafe};
77
use crate::pin::Pin;
8-
use crate::sys::mutex as sys;
8+
use crate::sys::locks as sys;
99

1010
/// A re-entrant mutual exclusion
1111
///

std/src/sys_common/rwlock.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::sys::rwlock as imp;
1+
use crate::sys::locks as imp;
22

33
/// An OS-based reader-writer lock, meant for use in static variables.
44
///

0 commit comments

Comments
 (0)