Repository navigation
Use os_sync_wait_on_address for SHM condition semaphores on macOS #6632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mini-1235
wants to merge
3
commits into
eProsima:master
Choose a base branch
from
mini-1235:fix/macos-shm-condition-wait-on-address
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
src/cpp/utils/shared_memory/RobustInterprocessSemaphore.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| // 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 <atomic> | ||
| #include <cstdint> | ||
|
|
||
| #include <boost/date_time/posix_time/posix_time_types.hpp> | ||
| #include <boost/interprocess/sync/interprocess_semaphore.hpp> | ||
| #include <boost/interprocess/sync/spin/wait.hpp> | ||
| #include <boost/thread/thread_time.hpp> | ||
|
|
||
| #include <os/clock.h> | ||
| #include <os/os_sync_wait_on_address.h> | ||
|
|
||
| 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<uint64_t>(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<uint32_t> 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), | ||
| "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 | ||
|
|
||
| #endif // __APPLE__ | ||
|
|
||
| #endif // _FASTDDS_SHAREDMEM_ROBUST_INTERPROCESS_SEMAPHORE_ | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.