Skip to content
Snippets Groups Projects
Commit e73b891e authored by Jeremy Soller's avatar Jeremy Soller
Browse files

Merge branch 'unsafe-blocks-in-sync' into 'master'

use unsafe blocks in `sync/`

See merge request redox-os/relibc!505
parents d6ef87ad 7df5b16f
No related branches found
No related tags found
1 merge request!505use unsafe blocks in `sync/`
Pipeline #16159 passed
// TODO: set this for entire crate when possible
#![deny(unsafe_op_in_unsafe_fn)]
pub mod barrier; pub mod barrier;
pub mod cond; pub mod cond;
// TODO: Merge with pthread_mutex // TODO: Merge with pthread_mutex
...@@ -95,14 +98,14 @@ impl FutexAtomicTy for AtomicI32 { ...@@ -95,14 +98,14 @@ impl FutexAtomicTy for AtomicI32 {
pub unsafe fn futex_wake_ptr(ptr: *mut impl FutexTy, n: i32) -> usize { pub unsafe fn futex_wake_ptr(ptr: *mut impl FutexTy, n: i32) -> usize {
// TODO: unwrap_unchecked? // TODO: unwrap_unchecked?
Sys::futex_wake(ptr.cast(), n as u32).unwrap() as usize unsafe { Sys::futex_wake(ptr.cast(), n as u32) }.unwrap() as usize
} }
pub unsafe fn futex_wait_ptr<T: FutexTy>( pub unsafe fn futex_wait_ptr<T: FutexTy>(
ptr: *mut T, ptr: *mut T,
value: T, value: T,
deadline_opt: Option<&timespec>, deadline_opt: Option<&timespec>,
) -> FutexWaitResult { ) -> FutexWaitResult {
match Sys::futex_wait(ptr.cast(), value.conv(), deadline_opt) { match unsafe { Sys::futex_wait(ptr.cast(), value.conv(), deadline_opt) } {
Ok(()) => FutexWaitResult::Waited, Ok(()) => FutexWaitResult::Waited,
Err(Errno(EAGAIN)) => FutexWaitResult::Stale, Err(Errno(EAGAIN)) => FutexWaitResult::Stale,
Err(Errno(ETIMEDOUT)) if deadline_opt.is_some() => FutexWaitResult::TimedOut, Err(Errno(ETIMEDOUT)) if deadline_opt.is_some() => FutexWaitResult::TimedOut,
......
...@@ -76,8 +76,8 @@ impl<T> Mutex<T> { ...@@ -76,8 +76,8 @@ impl<T> Mutex<T> {
/// on failure. You should probably not worry about this, it's used for /// on failure. You should probably not worry about this, it's used for
/// internal optimizations. /// internal optimizations.
pub unsafe fn manual_try_lock(&self) -> Result<&mut T, c_int> { pub unsafe fn manual_try_lock(&self) -> Result<&mut T, c_int> {
if manual_try_lock_generic(&self.lock) { if unsafe { manual_try_lock_generic(&self.lock) } {
Ok(&mut *self.content.get()) Ok(unsafe { &mut *self.content.get() })
} else { } else {
Err(0) Err(0)
} }
...@@ -86,12 +86,12 @@ impl<T> Mutex<T> { ...@@ -86,12 +86,12 @@ impl<T> Mutex<T> {
/// your responsibility to unlock it after usage. Mostly useful for FFI: /// your responsibility to unlock it after usage. Mostly useful for FFI:
/// Prefer normal .lock() where possible. /// Prefer normal .lock() where possible.
pub unsafe fn manual_lock(&self) -> &mut T { pub unsafe fn manual_lock(&self) -> &mut T {
manual_lock_generic(&self.lock); unsafe { manual_lock_generic(&self.lock) };
&mut *self.content.get() unsafe { &mut *self.content.get() }
} }
/// Unlock the mutex, if it's locked. /// Unlock the mutex, if it's locked.
pub unsafe fn manual_unlock(&self) { pub unsafe fn manual_unlock(&self) {
manual_unlock_generic(&self.lock) unsafe { manual_unlock_generic(&self.lock) }
} }
pub fn as_ptr(&self) -> *mut T { pub fn as_ptr(&self) -> *mut T {
self.content.get() self.content.get()
......
...@@ -28,7 +28,7 @@ impl<T> Waitval<T> { ...@@ -28,7 +28,7 @@ impl<T> Waitval<T> {
// SAFETY: Caller must ensure both (1) that the value has not yet been initialized, and (2) // SAFETY: Caller must ensure both (1) that the value has not yet been initialized, and (2)
// that this is never run by more than one thread simultaneously. // that this is never run by more than one thread simultaneously.
pub unsafe fn post(&self, value: T) { pub unsafe fn post(&self, value: T) {
self.value.get().write(MaybeUninit::new(value)); unsafe { self.value.get().write(MaybeUninit::new(value)) };
self.state.store(1, Ordering::Release); self.state.store(1, Ordering::Release);
crate::sync::futex_wake(&self.state, i32::MAX); crate::sync::futex_wake(&self.state, i32::MAX);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment