Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions compiler/rustc_thread_pool/src/latch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,17 @@ impl Latch for CountLatch {
#[inline]
unsafe fn set(this: *const Self) {
if unsafe { (*this).counter.fetch_sub(1, Ordering::SeqCst) == 1 } {
// NOTE: Once we call `set` on the internal `latch`,
// SAFETY: Once we call `set` on the internal `latch`,
// the target may proceed and invalidate `this`!
match unsafe { &(*this).kind } {
Comment on lines +391 to 393
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A safety comment here should explain why it is safe to create a reference out of (*this).kind. That it does not seem to do that, may be the reason it is a NOTE.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, we added the NOTE in rayon before there was much consensus on proper SAFETY comments. It's definitely a safety-relevant comment, even if it's not directly justifying this unsafe.

(Personally, I would have used a single unsafe block around all of this, but this was also added after the port from rayon. Originally, it's still using the unsafe fn context for inner unsafe too.)

CountLatchKind::Stealing { latch, registry, worker_index } => {
let registry = Arc::clone(registry);
let worker_index = *worker_index;
// SAFETY: We don't use any references from `this` after this call.
if unsafe { CoreLatch::set(latch) } {
registry.notify_worker_latch_is_set(*worker_index);
// We **must not** access any part of `this` anymore, which
// is why we read and shadowed these fields beforehand.
registry.notify_worker_latch_is_set(worker_index);
}
}
CountLatchKind::Blocking { latch } => unsafe { LockLatch::set(latch) },
Expand Down
Loading