#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2023 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License #ifndef NUCLEX_SUPPORT_PLATFORM_LINUXFUTEXAPI_H #define NUCLEX_SUPPORT_PLATFORM_LINUXFUTEXAPI_H #include "Nuclex/Support/Config.h" #if defined(NUCLEX_SUPPORT_LINUX) #include // std::string #include // std::uint8_t and std::size_t #include // for ::timespec namespace Nuclex { namespace Support { namespace Platform { // ------------------------------------------------------------------------------------------- // /// Wraps the Linux futex synchronization API class LinuxFutexApi { // ----------------------------------------------------------------------------------------- // // These are all for "private" futexes. Which means we hint to the Linux kernel // that the futex is private to the calling process (i.e. not in shared memory) // and certain assumptions and optimizations for that special case can be made. // ----------------------------------------------------------------------------------------- // #pragma region enum WaitResult /// Reasons for why has returned public: enum WaitResult { /// The wait was cancelled because the timeout was reached> TimedOut = -1, /// The wait was interrupted for some reason Interrupted = 0, /// Either the monitored value changed or we woke spuriously ValueChanged = 1 // We can distinguish between ValueChanged and ManualWake, but we don't need it }; #pragma endregion // enum WaitResult /// Waits for a private futex variable to change its value /// Futex word that will be watched for changed /// /// Value the futex word is expected to have, the method will return immediately /// when the watched futex word has a different value. /// /// /// The reason why the wait method has returned. This method will never report back /// as a reason because it does not time out. /// public: static WaitResult PrivateFutexWait( const volatile std::uint32_t &futexWord, std::uint32_t comparisonValue ); /// Waits for a private futex variable to change its value /// Futex word that will be watched for changed /// /// Value the futex word is expected to have, the method will return immediately /// when the watched futex word has a different value. /// /// /// Maximum amount of time to wait before returning even when the value doesn't change /// /// The reason why the wait method has returned public: static WaitResult PrivateFutexWait( const volatile std::uint32_t &futexWord, std::uint32_t comparisonValue, const ::timespec &patience ); /// Wakes a single thread waiting for a futex word to change /// Futex word that is being watched by threads public: static void PrivateFutexWakeSingle( const volatile std::uint32_t &futexWord ); /// Wakes all threads waiting for a futex word to change /// Futex word that is being watched by threads public: static void PrivateFutexWakeAll( const volatile std::uint32_t &futexWord ); }; // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Support::Platform #endif // defined(NUCLEX_SUPPORT_LINUX) #endif // NUCLEX_SUPPORT_PLATFORM_LINUXFUTEXAPI_H