Skip to content

Commit

Permalink
std: clarify comments about initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
joboet authored and gitbot committed Feb 20, 2025
1 parent 95b00fe commit 9750511
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
12 changes: 6 additions & 6 deletions std/src/sys/pal/unix/sync/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ impl Condvar {
}

/// # Safety
/// `init` must have been called.
/// `init` must have been called on this instance.
#[inline]
pub unsafe fn notify_one(self: Pin<&Self>) {
let r = unsafe { libc::pthread_cond_signal(self.raw()) };
debug_assert_eq!(r, 0);
}

/// # Safety
/// `init` must have been called.
/// `init` must have been called on this instance.
#[inline]
pub unsafe fn notify_all(self: Pin<&Self>) {
let r = unsafe { libc::pthread_cond_broadcast(self.raw()) };
debug_assert_eq!(r, 0);
}

/// # Safety
/// * `init` must have been called.
/// * `init` must have been called on this instance.
/// * `mutex` must be locked by the current thread.
/// * This condition variable may only be used with the same mutex.
#[inline]
Expand All @@ -49,7 +49,7 @@ impl Condvar {
}

/// # Safety
/// * `init` must have been called.
/// * `init` must have been called on this instance.
/// * `mutex` must be locked by the current thread.
/// * This condition variable may only be used with the same mutex.
pub unsafe fn wait_timeout(&self, mutex: Pin<&Mutex>, dur: Duration) -> bool {
Expand Down Expand Up @@ -95,7 +95,7 @@ impl Condvar {
const CLOCK: libc::clockid_t = libc::CLOCK_MONOTONIC;

/// # Safety
/// May only be called once.
/// May only be called once per instance of `Self`.
pub unsafe fn init(self: Pin<&mut Self>) {
use crate::mem::MaybeUninit;

Expand Down Expand Up @@ -137,7 +137,7 @@ impl Condvar {
const CLOCK: libc::clockid_t = libc::CLOCK_REALTIME;

/// # Safety
/// May only be called once.
/// May only be called once per instance of `Self`.
pub unsafe fn init(self: Pin<&mut Self>) {
if cfg!(any(target_os = "espidf", target_os = "horizon", target_os = "teeos")) {
// NOTE: ESP-IDF's PTHREAD_COND_INITIALIZER support is not released yet
Expand Down
8 changes: 5 additions & 3 deletions std/src/sys/pal/unix/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl Mutex {
}

/// # Safety
/// Must only be called once.
/// May only be called once per instance of `Self`.
pub unsafe fn init(self: Pin<&mut Self>) {
// Issue #33770
//
Expand Down Expand Up @@ -58,7 +58,8 @@ impl Mutex {
}

/// # Safety
/// * If `init` was not called, reentrant locking causes undefined behaviour.
/// * If `init` was not called on this instance, reentrant locking causes
/// undefined behaviour.
/// * Destroying a locked mutex causes undefined behaviour.
pub unsafe fn lock(self: Pin<&Self>) {
#[cold]
Expand All @@ -82,7 +83,8 @@ impl Mutex {
}

/// # Safety
/// * If `init` was not called, reentrant locking causes undefined behaviour.
/// * If `init` was not called on this instance, reentrant locking causes
/// undefined behaviour.
/// * Destroying a locked mutex causes undefined behaviour.
pub unsafe fn try_lock(self: Pin<&Self>) -> bool {
unsafe { libc::pthread_mutex_trylock(self.raw()) == 0 }
Expand Down
2 changes: 1 addition & 1 deletion std/src/sys/sync/condvar/pthread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Condvar {
fn get(&self) -> Pin<&pal::Condvar> {
self.cvar.get_or_init(|| {
let mut cvar = Box::pin(pal::Condvar::new());
// SAFETY: we only call `init` once, namely here.
// SAFETY: we only call `init` once per `pal::Condvar`, namely here.
unsafe { cvar.as_mut().init() };
cvar
})
Expand Down
2 changes: 1 addition & 1 deletion std/src/sys/sync/mutex/pthread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Mutex {
// This is sound however, as it cannot have been locked.
self.pal.get_or_init(|| {
let mut pal = Box::pin(pal::Mutex::new());
// SAFETY: we only call `init` once, namely here.
// SAFETY: we only call `init` once per `pal::Mutex`, namely here.
unsafe { pal.as_mut().init() };
pal
})
Expand Down

0 comments on commit 9750511

Please sign in to comment.