From 49300991f77e63886d6e246575af4e23fca7e0aa Mon Sep 17 00:00:00 2001 From: Maurice Date: Sun, 15 Nov 2026 22:58:16 +0700 Subject: [PATCH 1/3] Use os_sync_wait_on_address for SHM condition semaphores on macOS Boost.Interprocess has no process-shared unnamed semaphores on macOS, so interprocess_semaphore falls back to a spin emulation that sleeps between polls. Every SHM notification on macOS was delivered with up to a scheduler tick of delay (about 3 ms average), and idle listener threads kept waking. Add RobustInterprocessSemaphore, used by RobustInterprocessCondition on macOS only. It keeps the 32-bit counter layout of Boost's spin_semaphore, blocks with os_sync_wait_on_address(OS_SYNC_WAIT_ON_ADDRESS_SHARED) on macOS 14.4+, and falls back to Boost's spin_wait on older systems. Each kernel wait is capped at 1 ms so posts from processes using the old spin_semaphore, which don't wake waiters, are still seen. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Maurice --- .../RobustInterprocessCondition.hpp | 10 ++ .../RobustInterprocessSemaphore.hpp | 142 ++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 src/cpp/utils/shared_memory/RobustInterprocessSemaphore.hpp diff --git a/src/cpp/utils/shared_memory/RobustInterprocessCondition.hpp b/src/cpp/utils/shared_memory/RobustInterprocessCondition.hpp index df9a7c68210..81bf97a42a2 100644 --- a/src/cpp/utils/shared_memory/RobustInterprocessCondition.hpp +++ b/src/cpp/utils/shared_memory/RobustInterprocessCondition.hpp @@ -22,6 +22,10 @@ #include "BoostAtExitRegistry.hpp" +#ifdef __APPLE__ +#include "RobustInterprocessSemaphore.hpp" +#endif // __APPLE__ + namespace eprosima { namespace fastdds { namespace rtps { @@ -209,7 +213,13 @@ class RobustInterprocessCondition struct SemaphoreNode { +#ifdef __APPLE__ + // Boost cannot use process-shared POSIX semaphores on macOS and falls back to a + // sleep-polling emulation, adding milliseconds of latency to every SHM notification. + RobustInterprocessSemaphore sem {0}; +#else bi::interprocess_semaphore sem {0}; +#endif // __APPLE__ uint32_t next; uint32_t prev; }; diff --git a/src/cpp/utils/shared_memory/RobustInterprocessSemaphore.hpp b/src/cpp/utils/shared_memory/RobustInterprocessSemaphore.hpp new file mode 100644 index 00000000000..ebb93928843 --- /dev/null +++ b/src/cpp/utils/shared_memory/RobustInterprocessSemaphore.hpp @@ -0,0 +1,142 @@ +// Copyright 2026 Proyectos y Sistemas de Mantenimiento SL (eProsima). +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _FASTDDS_SHAREDMEM_ROBUST_INTERPROCESS_SEMAPHORE_ +#define _FASTDDS_SHAREDMEM_ROBUST_INTERPROCESS_SEMAPHORE_ + +#ifdef __APPLE__ + +#include +#include + +#include +#include +#include + +#include +#include + +namespace eprosima { +namespace fastdds { +namespace rtps { + +/** + * Process-shared counting semaphore for macOS. + * + * macOS has no process-shared unnamed POSIX semaphores, so boost::interprocess::interprocess_semaphore + * falls back to a spin semaphore that sleeps between polls. That adds milliseconds of latency to + * every shared-memory notification. This semaphore blocks in the kernel with + * os_sync_wait_on_address(OS_SYNC_WAIT_ON_ADDRESS_SHARED) (macOS 14.4+), and spins like Boost + * on older systems. + * + * It has the same layout as boost's spin_semaphore (a single 32-bit counter in shared memory), + * so processes that still use the spin semaphore interoperate with it. + */ +class RobustInterprocessSemaphore +{ +public: + + explicit RobustInterprocessSemaphore( + uint32_t initial_count) + : count_(initial_count) + { + } + + RobustInterprocessSemaphore( + const RobustInterprocessSemaphore&) = delete; + RobustInterprocessSemaphore& operator =( + const RobustInterprocessSemaphore&) = delete; + + void post() + { + count_.fetch_add(1, std::memory_order_release); + if (__builtin_available(macOS 14.4, *)) + { + os_sync_wake_by_address_any(&count_, sizeof(uint32_t), OS_SYNC_WAKE_BY_ADDRESS_SHARED); + } + } + + /** + * @return false if abs_time (UTC, as boost::get_system_time()) is reached, otherwise true. + */ + bool timed_wait( + const boost::posix_time::ptime& abs_time) + { + const bool infinite = abs_time.is_pos_infinity(); + boost::interprocess::spin_wait swait; + + while (!try_wait()) + { + uint64_t timeout_ns = 0; + if (!infinite) + { + const boost::posix_time::time_duration remaining = abs_time - boost::get_system_time(); + if (remaining.is_negative() || remaining.total_microseconds() == 0) + { + return try_wait(); + } + timeout_ns = static_cast(remaining.total_microseconds()) * 1000u; + } + + if (__builtin_available(macOS 14.4, *)) + { + // Sleeps only while the counter is still 0, so a post() between try_wait() and + // here is not lost. Each sleep is capped, because processes using an older Fast DDS + // post with boost's spin_semaphore, which increments the counter without waking us. + if (infinite || timeout_ns > LEGACY_POST_POLL_NS) + { + timeout_ns = LEGACY_POST_POLL_NS; + } + os_sync_wait_on_address_with_timeout(&count_, 0, sizeof(uint32_t), + OS_SYNC_WAIT_ON_ADDRESS_SHARED, OS_CLOCK_MACH_ABSOLUTE_TIME, timeout_ns); + } + else + { + swait.yield(); + } + } + + return true; + } + +private: + + //! Upper bound for one kernel wait, so posts from processes that don't wake us are still seen. + static constexpr uint64_t LEGACY_POST_POLL_NS = 1000000u; + + bool try_wait() + { + uint32_t current = count_.load(std::memory_order_acquire); + while (current != 0) + { + if (count_.compare_exchange_weak(current, current - 1, std::memory_order_acquire)) + { + return true; + } + } + return false; + } + + std::atomic count_; + static_assert(sizeof(std::atomic) == sizeof(uint32_t), "Must match boost spin_semaphore layout"); + static_assert(ATOMIC_INT_LOCK_FREE == 2, "Must be lock free to live in shared memory"); +}; + +} // namespace rtps +} // namespace fastdds +} // namespace eprosima + +#endif // __APPLE__ + +#endif // _FASTDDS_SHAREDMEM_ROBUST_INTERPROCESS_SEMAPHORE_ From eff5162a61ab7f47a199fc74cf8c4b7e20a90827 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 16 Nov 2026 17:33:57 +0700 Subject: [PATCH 2/3] Check RobustInterprocessSemaphore layout against interprocess_semaphore Compare size and alignment of the whole semaphore with boost::interprocess::interprocess_semaphore, instead of only comparing std::atomic with uint32_t. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Maurice --- .../utils/shared_memory/RobustInterprocessSemaphore.hpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cpp/utils/shared_memory/RobustInterprocessSemaphore.hpp b/src/cpp/utils/shared_memory/RobustInterprocessSemaphore.hpp index ebb93928843..54c5c3ea6cc 100644 --- a/src/cpp/utils/shared_memory/RobustInterprocessSemaphore.hpp +++ b/src/cpp/utils/shared_memory/RobustInterprocessSemaphore.hpp @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -129,10 +130,14 @@ class RobustInterprocessSemaphore } std::atomic count_; - static_assert(sizeof(std::atomic) == sizeof(uint32_t), "Must match boost spin_semaphore layout"); static_assert(ATOMIC_INT_LOCK_FREE == 2, "Must be lock free to live in shared memory"); }; +static_assert(sizeof(RobustInterprocessSemaphore) == sizeof(boost::interprocess::interprocess_semaphore), + "Must match boost::interprocess::interprocess_semaphore layout"); +static_assert(alignof(RobustInterprocessSemaphore) == alignof(boost::interprocess::interprocess_semaphore), + "Must match boost::interprocess::interprocess_semaphore layout"); + } // namespace rtps } // namespace fastdds } // namespace eprosima From 4a85ed40cabaf03248704bc2382d51b75c016c69 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 16 Nov 2026 17:40:46 +0700 Subject: [PATCH 3/3] Also check ATOMIC_LONG_LOCK_FREE in RobustInterprocessSemaphore Co-Authored-By: Claude Opus 5.5 Signed-off-by: Maurice --- src/cpp/utils/shared_memory/RobustInterprocessSemaphore.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cpp/utils/shared_memory/RobustInterprocessSemaphore.hpp b/src/cpp/utils/shared_memory/RobustInterprocessSemaphore.hpp index 54c5c3ea6cc..d0e4e96643b 100644 --- a/src/cpp/utils/shared_memory/RobustInterprocessSemaphore.hpp +++ b/src/cpp/utils/shared_memory/RobustInterprocessSemaphore.hpp @@ -131,6 +131,7 @@ class RobustInterprocessSemaphore std::atomic count_; static_assert(ATOMIC_INT_LOCK_FREE == 2, "Must be lock free to live in shared memory"); + static_assert(ATOMIC_LONG_LOCK_FREE == 2, "Must be lock free to live in shared memory"); }; static_assert(sizeof(RobustInterprocessSemaphore) == sizeof(boost::interprocess::interprocess_semaphore),