Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5ff7b40

Browse files
committedJul 15, 2024
std: Unsafe-wrap std::sync
1 parent e8fa3ef commit 5ff7b40

File tree

8 files changed

+54
-41
lines changed

8 files changed

+54
-41
lines changed
 

‎std/src/sync/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@
157157
//! [`RwLock`]: crate::sync::RwLock
158158
159159
#![stable(feature = "rust1", since = "1.0.0")]
160-
#![allow(unsafe_op_in_unsafe_fn)]
161160

162161
#[stable(feature = "rust1", since = "1.0.0")]
163162
pub use alloc_crate::sync::{Arc, Weak};

‎std/src/sync/mpmc/array.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,12 @@ impl<T> Channel<T> {
200200
return Err(msg);
201201
}
202202

203-
let slot: &Slot<T> = &*(token.array.slot as *const Slot<T>);
204-
205203
// Write the message into the slot and update the stamp.
206-
slot.msg.get().write(MaybeUninit::new(msg));
207-
slot.stamp.store(token.array.stamp, Ordering::Release);
204+
unsafe {
205+
let slot: &Slot<T> = &*(token.array.slot as *const Slot<T>);
206+
slot.msg.get().write(MaybeUninit::new(msg));
207+
slot.stamp.store(token.array.stamp, Ordering::Release);
208+
}
208209

209210
// Wake a sleeping receiver.
210211
self.receivers.notify();
@@ -291,11 +292,14 @@ impl<T> Channel<T> {
291292
return Err(());
292293
}
293294

294-
let slot: &Slot<T> = &*(token.array.slot as *const Slot<T>);
295-
296295
// Read the message from the slot and update the stamp.
297-
let msg = slot.msg.get().read().assume_init();
298-
slot.stamp.store(token.array.stamp, Ordering::Release);
296+
let msg = unsafe {
297+
let slot: &Slot<T> = &*(token.array.slot as *const Slot<T>);
298+
299+
let msg = slot.msg.get().read().assume_init();
300+
slot.stamp.store(token.array.stamp, Ordering::Release);
301+
msg
302+
};
299303

300304
// Wake a sleeping sender.
301305
self.senders.notify();
@@ -471,7 +475,7 @@ impl<T> Channel<T> {
471475
false
472476
};
473477

474-
self.discard_all_messages(tail);
478+
unsafe { self.discard_all_messages(tail) };
475479
disconnected
476480
}
477481

‎std/src/sync/mpmc/counter.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<C> Sender<C> {
6363
disconnect(&self.counter().chan);
6464

6565
if self.counter().destroy.swap(true, Ordering::AcqRel) {
66-
drop(Box::from_raw(self.counter));
66+
drop(unsafe { Box::from_raw(self.counter) });
6767
}
6868
}
6969
}
@@ -116,7 +116,7 @@ impl<C> Receiver<C> {
116116
disconnect(&self.counter().chan);
117117

118118
if self.counter().destroy.swap(true, Ordering::AcqRel) {
119-
drop(Box::from_raw(self.counter));
119+
drop(unsafe { Box::from_raw(self.counter) });
120120
}
121121
}
122122
}

‎std/src/sync/mpmc/list.rs

+21-17
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl<T> Block<T> {
9191
// It is not necessary to set the `DESTROY` bit in the last slot because that slot has
9292
// begun destruction of the block.
9393
for i in start..BLOCK_CAP - 1 {
94-
let slot = (*this).slots.get_unchecked(i);
94+
let slot = unsafe { (*this).slots.get_unchecked(i) };
9595

9696
// Mark the `DESTROY` bit if a thread is still using the slot.
9797
if slot.state.load(Ordering::Acquire) & READ == 0
@@ -103,7 +103,7 @@ impl<T> Block<T> {
103103
}
104104

105105
// No thread is using the block, now it is safe to destroy it.
106-
drop(Box::from_raw(this));
106+
drop(unsafe { Box::from_raw(this) });
107107
}
108108
}
109109

@@ -265,9 +265,11 @@ impl<T> Channel<T> {
265265
// Write the message into the slot.
266266
let block = token.list.block as *mut Block<T>;
267267
let offset = token.list.offset;
268-
let slot = (*block).slots.get_unchecked(offset);
269-
slot.msg.get().write(MaybeUninit::new(msg));
270-
slot.state.fetch_or(WRITE, Ordering::Release);
268+
unsafe {
269+
let slot = (*block).slots.get_unchecked(offset);
270+
slot.msg.get().write(MaybeUninit::new(msg));
271+
slot.state.fetch_or(WRITE, Ordering::Release);
272+
}
271273

272274
// Wake a sleeping receiver.
273275
self.receivers.notify();
@@ -369,19 +371,21 @@ impl<T> Channel<T> {
369371
// Read the message.
370372
let block = token.list.block as *mut Block<T>;
371373
let offset = token.list.offset;
372-
let slot = (*block).slots.get_unchecked(offset);
373-
slot.wait_write();
374-
let msg = slot.msg.get().read().assume_init();
375-
376-
// Destroy the block if we've reached the end, or if another thread wanted to destroy but
377-
// couldn't because we were busy reading from the slot.
378-
if offset + 1 == BLOCK_CAP {
379-
Block::destroy(block, 0);
380-
} else if slot.state.fetch_or(READ, Ordering::AcqRel) & DESTROY != 0 {
381-
Block::destroy(block, offset + 1);
382-
}
374+
unsafe {
375+
let slot = (*block).slots.get_unchecked(offset);
376+
slot.wait_write();
377+
let msg = slot.msg.get().read().assume_init();
378+
379+
// Destroy the block if we've reached the end, or if another thread wanted to destroy but
380+
// couldn't because we were busy reading from the slot.
381+
if offset + 1 == BLOCK_CAP {
382+
Block::destroy(block, 0);
383+
} else if slot.state.fetch_or(READ, Ordering::AcqRel) & DESTROY != 0 {
384+
Block::destroy(block, offset + 1);
385+
}
383386

384-
Ok(msg)
387+
Ok(msg)
388+
}
385389
}
386390

387391
/// Attempts to send a message into the channel.

‎std/src/sync/mpmc/zero.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ impl<T> Channel<T> {
103103
return Err(msg);
104104
}
105105

106-
let packet = &*(token.zero.0 as *const Packet<T>);
107-
packet.msg.get().write(Some(msg));
108-
packet.ready.store(true, Ordering::Release);
106+
unsafe {
107+
let packet = &*(token.zero.0 as *const Packet<T>);
108+
packet.msg.get().write(Some(msg));
109+
packet.ready.store(true, Ordering::Release);
110+
}
109111
Ok(())
110112
}
111113

@@ -116,22 +118,24 @@ impl<T> Channel<T> {
116118
return Err(());
117119
}
118120

119-
let packet = &*(token.zero.0 as *const Packet<T>);
121+
let packet = unsafe { &*(token.zero.0 as *const Packet<T>) };
120122

121123
if packet.on_stack {
122124
// The message has been in the packet from the beginning, so there is no need to wait
123125
// for it. However, after reading the message, we need to set `ready` to `true` in
124126
// order to signal that the packet can be destroyed.
125-
let msg = packet.msg.get().replace(None).unwrap();
127+
let msg = unsafe { packet.msg.get().replace(None) }.unwrap();
126128
packet.ready.store(true, Ordering::Release);
127129
Ok(msg)
128130
} else {
129131
// Wait until the message becomes available, then read it and destroy the
130132
// heap-allocated packet.
131133
packet.wait_ready();
132-
let msg = packet.msg.get().replace(None).unwrap();
133-
drop(Box::from_raw(token.zero.0 as *mut Packet<T>));
134-
Ok(msg)
134+
unsafe {
135+
let msg = packet.msg.get().replace(None).unwrap();
136+
drop(Box::from_raw(token.zero.0 as *mut Packet<T>));
137+
Ok(msg)
138+
}
135139
}
136140
}
137141

‎std/src/sync/once_lock.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ impl<T> OnceLock<T> {
502502
#[inline]
503503
unsafe fn get_unchecked(&self) -> &T {
504504
debug_assert!(self.is_initialized());
505-
(&*self.value.get()).assume_init_ref()
505+
unsafe { (&*self.value.get()).assume_init_ref() }
506506
}
507507

508508
/// # Safety
@@ -511,7 +511,7 @@ impl<T> OnceLock<T> {
511511
#[inline]
512512
unsafe fn get_unchecked_mut(&mut self) -> &mut T {
513513
debug_assert!(self.is_initialized());
514-
(&mut *self.value.get()).assume_init_mut()
514+
unsafe { (&mut *self.value.get()).assume_init_mut() }
515515
}
516516
}
517517

‎std/src/sync/reentrant_lock.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ impl<T: ?Sized> ReentrantLock<T> {
244244
}
245245

246246
unsafe fn increment_lock_count(&self) -> Option<()> {
247-
*self.lock_count.get() = (*self.lock_count.get()).checked_add(1)?;
247+
unsafe {
248+
*self.lock_count.get() = (*self.lock_count.get()).checked_add(1)?;
249+
}
248250
Some(())
249251
}
250252
}

‎std/src/sync/rwlock.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ impl<'rwlock, T: ?Sized> RwLockReadGuard<'rwlock, T> {
578578
// successfully called from the same thread before instantiating this object.
579579
unsafe fn new(lock: &'rwlock RwLock<T>) -> LockResult<RwLockReadGuard<'rwlock, T>> {
580580
poison::map_result(lock.poison.borrow(), |()| RwLockReadGuard {
581-
data: NonNull::new_unchecked(lock.data.get()),
581+
data: unsafe { NonNull::new_unchecked(lock.data.get()) },
582582
inner_lock: &lock.inner,
583583
})
584584
}

0 commit comments

Comments
 (0)
Failed to load comments.