Skip to content

Commit 390c4b9

Browse files
committed
sys/pal/unix/sync/mutex: Fix Mutex::new() on NuttX
PTHREAD_MUTEX_INITIALIZER is highly configurable on NuttX, making it difficult to use a hardcoded initializer from libc crate in Rust way. Instead, call pthread_mutex_init() to initialize the mutex with default attributes, ensuring consistent behavior with PTHREAD_MUTEX_INITIALIZER. * Added conditional compilation for NuttX target * Replaced PTHREAD_MUTEX_INITIALIZER with pthread_mutex_init() for NuttX * Ensured consistent mutex initialization behavior across platforms
1 parent 57a4736 commit 390c4b9

File tree

1 file changed

+16
-0
lines changed
  • library/std/src/sys/pal/unix/sync

1 file changed

+16
-0
lines changed

library/std/src/sys/pal/unix/sync/mutex.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,26 @@ pub struct Mutex {
99
}
1010

1111
impl Mutex {
12+
#[cfg(not(target_os = "nuttx"))]
1213
pub fn new() -> Mutex {
1314
Mutex { inner: UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER) }
1415
}
1516

17+
#[cfg(target_os = "nuttx")]
18+
pub fn new() -> Mutex {
19+
// PTHREAD_MUTEX_INITIALIZER is highly configurable on NuttX, so it's
20+
// hard to use a hardcoded initializer from libc.
21+
// Instead, call pthread_mutex_init() to initialize the mutex with the
22+
// default attributes to get the same behavior as PTHREAD_MUTEX_INITIALIZER.
23+
let mut mutex = Mutex { inner: UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER) };
24+
25+
unsafe {
26+
libc::pthread_mutex_init(mutex.raw(), core::ptr::null());
27+
}
28+
29+
mutex
30+
}
31+
1632
pub(super) fn raw(&self) -> *mut libc::pthread_mutex_t {
1733
self.inner.get()
1834
}

0 commit comments

Comments
 (0)