aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/cxxsupp/libcxx/include/semaphore
diff options
context:
space:
mode:
authorarcadia-devtools <arcadia-devtools@yandex-team.ru>2022-02-10 17:53:52 +0300
committerarcadia-devtools <arcadia-devtools@yandex-team.ru>2022-02-10 17:53:52 +0300
commit5c64b97bb7e4034eff8833e4c367f61d34fcb4ee (patch)
tree7c5769528f2fcdaa5a718aa73e4aa64d50905269 /contrib/libs/cxxsupp/libcxx/include/semaphore
parent1b56f620ac98766b198121ca1b728e7e61efbb56 (diff)
downloadydb-5c64b97bb7e4034eff8833e4c367f61d34fcb4ee.tar.gz
intermediate changes
ref:4635f4dd763168c3fa295f87727595c785b4d5a4
Diffstat (limited to 'contrib/libs/cxxsupp/libcxx/include/semaphore')
-rw-r--r--contrib/libs/cxxsupp/libcxx/include/semaphore86
1 files changed, 16 insertions, 70 deletions
diff --git a/contrib/libs/cxxsupp/libcxx/include/semaphore b/contrib/libs/cxxsupp/libcxx/include/semaphore
index 4f9ecd0461..db03fb967e 100644
--- a/contrib/libs/cxxsupp/libcxx/include/semaphore
+++ b/contrib/libs/cxxsupp/libcxx/include/semaphore
@@ -67,10 +67,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
/*
-__atomic_semaphore_base is the general-case implementation, to be used for
-user-requested least-max values that exceed the OS implementation support
-(incl. when the OS has no support of its own) and for binary semaphores.
-
+__atomic_semaphore_base is the general-case implementation.
It is a typical Dijkstra semaphore algorithm over atomics, wait and notify
functions. It avoids contention against users' own use of those facilities.
@@ -108,81 +105,30 @@ public:
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
bool try_acquire_for(chrono::duration<Rep, Period> const& __rel_time)
{
- auto const __test_fn = [this]() -> bool {
- auto __old = __a.load(memory_order_acquire);
- while(1) {
- if (__old == 0)
- return false;
- if(__a.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed))
- return true;
- }
- };
+ if (__rel_time == chrono::duration<Rep, Period>::zero())
+ return try_acquire();
+ auto const __test_fn = [this]() { return try_acquire(); };
return __libcpp_thread_poll_with_backoff(__test_fn, __libcpp_timed_backoff_policy(), __rel_time);
}
-};
-
-#ifndef _LIBCPP_NO_NATIVE_SEMAPHORES
-
-/*
-
-__platform_semaphore_base a simple wrapper for the OS semaphore type. That
-is, every call is routed to the OS in the most direct manner possible.
-
-*/
-
-class __platform_semaphore_base
-{
- __libcpp_semaphore_t __semaphore;
-
-public:
- _LIBCPP_INLINE_VISIBILITY
- explicit __platform_semaphore_base(ptrdiff_t __count) :
- __semaphore()
- {
- __libcpp_semaphore_init(&__semaphore, __count);
- }
- _LIBCPP_INLINE_VISIBILITY
- ~__platform_semaphore_base() {
- __libcpp_semaphore_destroy(&__semaphore);
- }
- _LIBCPP_INLINE_VISIBILITY
- void release(ptrdiff_t __update)
- {
- for(; __update; --__update)
- __libcpp_semaphore_post(&__semaphore);
- }
- _LIBCPP_INLINE_VISIBILITY
- void acquire()
- {
- __libcpp_semaphore_wait(&__semaphore);
- }
- _LIBCPP_INLINE_VISIBILITY
- bool try_acquire_for(chrono::nanoseconds __rel_time)
+ _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
+ bool try_acquire()
{
- return __libcpp_semaphore_wait_timed(&__semaphore, __rel_time);
+ auto __old = __a.load(memory_order_acquire);
+ while (true) {
+ if (__old == 0)
+ return false;
+ if (__a.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed))
+ return true;
+ }
}
};
-template<ptrdiff_t __least_max_value>
-using __semaphore_base =
- typename conditional<(__least_max_value > 1 && __least_max_value <= _LIBCPP_SEMAPHORE_MAX),
- __platform_semaphore_base,
- __atomic_semaphore_base>::type;
-
-#else
-
-template<ptrdiff_t __least_max_value>
-using __semaphore_base =
- __atomic_semaphore_base;
-
#define _LIBCPP_SEMAPHORE_MAX (numeric_limits<ptrdiff_t>::max())
-#endif //_LIBCPP_NO_NATIVE_SEMAPHORES
-
template<ptrdiff_t __least_max_value = _LIBCPP_SEMAPHORE_MAX>
class counting_semaphore
{
- __semaphore_base<__least_max_value> __semaphore;
+ __atomic_semaphore_base __semaphore;
public:
static constexpr ptrdiff_t max() noexcept {
@@ -215,14 +161,14 @@ public:
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
bool try_acquire()
{
- return try_acquire_for(chrono::nanoseconds::zero());
+ return __semaphore.try_acquire();
}
template <class Clock, class Duration>
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
bool try_acquire_until(chrono::time_point<Clock, Duration> const& __abs_time)
{
auto const current = Clock::now();
- if(current >= __abs_time)
+ if (current >= __abs_time)
return try_acquire();
else
return try_acquire_for(__abs_time - current);