Remove libc sem completely on non-linux (#11511)

As it's only used on Linux, we can cfg it out completely on other platforms. It also enables test_topic_monitor_torture on NetBSD & Cygwin.
This commit is contained in:
Yuyi Wang
2025-05-19 16:26:47 +08:00
committed by GitHub
parent 7fe34ea401
commit 01560bf195
2 changed files with 7 additions and 8 deletions

View File

@@ -36,9 +36,6 @@ fn test_topic_monitor() {
}
#[test]
// FIXME: Does not compile on NetBSD & Cygwin
// "`*mut sem` cannot be sent between threads safely"
#[cfg(not(any(target_os = "netbsd", cygwin)))]
#[serial]
fn test_topic_monitor_torture() {
let _cleanup = test_init();

View File

@@ -27,11 +27,12 @@
use crate::wutil::perror;
use nix::errno::Errno;
use nix::unistd;
use std::cell::{Cell, UnsafeCell};
use std::cell::Cell;
use std::os::fd::AsRawFd;
use std::pin::Pin;
use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::{Condvar, Mutex, MutexGuard};
#[cfg(target_os = "linux")]
use std::{cell::UnsafeCell, pin::Pin};
/// The list of topics which may be observed.
#[repr(u8)]
@@ -159,6 +160,7 @@ pub fn any_valid(&self) -> bool {
pub enum BinarySemaphore {
/// Initialized semaphore.
/// This is Box'd so it has a stable address.
#[cfg(target_os = "linux")]
Semaphore(Pin<Box<UnsafeCell<libc::sem_t>>>),
/// Pipes used to emulate a semaphore, if not initialized.
Pipes(AutoClosePipes),
@@ -198,6 +200,7 @@ pub fn new() -> BinarySemaphore {
pub fn post(&self) {
// Beware, we are in a signal handler.
match self {
#[cfg(target_os = "linux")]
Self::Semaphore(sem) => {
let res = unsafe { libc::sem_post(sem.get()) };
// sem_post is non-interruptible.
@@ -222,6 +225,7 @@ pub fn post(&self) {
/// This loops on EINTR.
pub fn wait(&self) {
match self {
#[cfg(target_os = "linux")]
Self::Semaphore(sem) => {
loop {
match unsafe { libc::sem_wait(sem.get()) } {
@@ -261,9 +265,7 @@ pub fn die(&self, msg: &str) {
}
}
// sem_destroy has been deprecated since macOS 10.10 but we only use it under Linux so silence the
// warning.
#[cfg_attr(apple, allow(deprecated))]
#[cfg(target_os = "linux")]
impl Drop for BinarySemaphore {
fn drop(&mut self) {
if let Self::Semaphore(sem) = self {