diff --git a/include/etl/semaphore/semaphore_std.h b/include/etl/semaphore/semaphore_std.h index 74aaf2e5..505bb060 100644 --- a/include/etl/semaphore/semaphore_std.h +++ b/include/etl/semaphore/semaphore_std.h @@ -30,8 +30,10 @@ SOFTWARE. #define ETL_SEMAPHORE_STD_INCLUDED #include "../platform.h" -#include "../limits.h" +#include "../chrono.h" +#include +#include #include namespace etl @@ -40,9 +42,79 @@ namespace etl ///\ingroup semaphore ///\brief These semaphore classes are implemented using std::counting_semaphore and std::binary_semaphore. //*************************************************************************** - template ::max()> - using counting_semaphore = std::counting_semaphore; - using binary_semaphore = std::binary_semaphore; + + namespace private_semaphore_std + { + template + struct CompatClock + { + using rep = Clock::rep; + using period = std::ratio; + using duration = std::chrono::duration; + using time_point = std::chrono::time_point; + static const bool is_steady = Clock::is_steady; + static time_point now() + { + return time_point{duration{Clock::now().time_since_epoch().count()}}; + } + }; + } // namespace private_semaphore_std + + template ::max()> + class counting_semaphore + { + public: + + explicit counting_semaphore(ptrdiff_t desired) + : sem(desired) + { + } + + void acquire() + { + return sem.acquire(); + } + + bool try_acquire() + { + return sem.try_acquire(); + } + + template + bool try_acquire_for(const etl::chrono::duration& rel_time) + { + std::chrono::duration> duration(rel_time.count()); + + return sem.try_acquire_for(duration); + } + + template + bool try_acquire_until(const etl::chrono::time_point& abs_time) + { + using CompatDuration = std::chrono::duration>; + + std::chrono::time_point, CompatDuration> time_point{ + CompatDuration(abs_time.time_since_epoch().count())}; + + return sem.try_acquire_until(time_point); + } + + void release(ptrdiff_t update = 1) + { + return sem.release(update); + } + + static constexpr ptrdiff_t max() noexcept + { + return LeastMaxValue; + } + + private: + + std::counting_semaphore sem; + }; + + using binary_semaphore = counting_semaphore<1>; } // namespace etl #endif