diff --git a/libcxx/include/__threading_support b/libcxx/include/__threading_support --- a/libcxx/include/__threading_support +++ b/libcxx/include/__threading_support @@ -29,16 +29,9 @@ # include <__external_threading> #elif !defined(_LIBCPP_HAS_NO_THREADS) -#if defined(__APPLE__) || defined(__MVS__) -# define _LIBCPP_NO_NATIVE_SEMAPHORES -#endif - #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD) # include # include -# ifndef _LIBCPP_NO_NATIVE_SEMAPHORES -# include -# endif #elif defined(_LIBCPP_HAS_THREAD_API_C11) # include #endif @@ -78,12 +71,6 @@ typedef pthread_cond_t __libcpp_condvar_t; #define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER -#ifndef _LIBCPP_NO_NATIVE_SEMAPHORES -// Semaphore -typedef sem_t __libcpp_semaphore_t; -# define _LIBCPP_SEMAPHORE_MAX SEM_VALUE_MAX -#endif - // Execute once typedef pthread_once_t __libcpp_exec_once_flag; #define _LIBCPP_EXEC_ONCE_INITIALIZER PTHREAD_ONCE_INIT @@ -149,12 +136,6 @@ typedef void* __libcpp_condvar_t; #define _LIBCPP_CONDVAR_INITIALIZER 0 -// Semaphore -typedef void* __libcpp_semaphore_t; -#if defined(_LIBCPP_HAS_THREAD_API_WIN32) -# define _LIBCPP_SEMAPHORE_MAX (::std::numeric_limits::max()) -#endif - // Execute Once typedef void* __libcpp_exec_once_flag; #define _LIBCPP_EXEC_ONCE_INITIALIZER 0 @@ -219,26 +200,6 @@ _LIBCPP_THREAD_ABI_VISIBILITY int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv); -#ifndef _LIBCPP_NO_NATIVE_SEMAPHORES - -// Semaphore -_LIBCPP_THREAD_ABI_VISIBILITY -bool __libcpp_semaphore_init(__libcpp_semaphore_t* __sem, int __init); - -_LIBCPP_THREAD_ABI_VISIBILITY -bool __libcpp_semaphore_destroy(__libcpp_semaphore_t* __sem); - -_LIBCPP_THREAD_ABI_VISIBILITY -bool __libcpp_semaphore_post(__libcpp_semaphore_t* __sem); - -_LIBCPP_THREAD_ABI_VISIBILITY -bool __libcpp_semaphore_wait(__libcpp_semaphore_t* __sem); - -_LIBCPP_THREAD_ABI_VISIBILITY -bool __libcpp_semaphore_wait_timed(__libcpp_semaphore_t* __sem, chrono::nanoseconds const& __ns); - -#endif // _LIBCPP_NO_NATIVE_SEMAPHORES - // Execute once _LIBCPP_THREAD_ABI_VISIBILITY int __libcpp_execute_once(__libcpp_exec_once_flag *flag, @@ -452,38 +413,6 @@ return pthread_cond_destroy(__cv); } -#ifndef _LIBCPP_NO_NATIVE_SEMAPHORES - -// Semaphore -bool __libcpp_semaphore_init(__libcpp_semaphore_t* __sem, int __init) -{ - return sem_init(__sem, 0, __init) == 0; -} - -bool __libcpp_semaphore_destroy(__libcpp_semaphore_t* __sem) -{ - return sem_destroy(__sem) == 0; -} - -bool __libcpp_semaphore_post(__libcpp_semaphore_t* __sem) -{ - return sem_post(__sem) == 0; -} - -bool __libcpp_semaphore_wait(__libcpp_semaphore_t* __sem) -{ - return sem_wait(__sem) == 0; -} - -bool __libcpp_semaphore_wait_timed(__libcpp_semaphore_t* __sem, chrono::nanoseconds const& __ns) -{ - auto const __abs_time = chrono::system_clock::now().time_since_epoch() + __ns; - __libcpp_timespec_t __ts = __thread_detail::__convert_to_timespec(__abs_time); - return sem_timedwait(__sem, &__ts) == 0; -} - -#endif //_LIBCPP_NO_NATIVE_SEMAPHORES - // Execute once int __libcpp_execute_once(__libcpp_exec_once_flag *flag, void (*init_routine)()) { diff --git a/libcxx/include/semaphore b/libcxx/include/semaphore --- a/libcxx/include/semaphore +++ b/libcxx/include/semaphore @@ -67,10 +67,7 @@ /* -__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. @@ -82,7 +79,7 @@ public: _LIBCPP_INLINE_VISIBILITY - __atomic_semaphore_base(ptrdiff_t __count) : __a(__count) + constexpr explicit __atomic_semaphore_base(ptrdiff_t __count) : __a(__count) { } _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY @@ -121,68 +118,12 @@ } }; -#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 - __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) - { - return __libcpp_semaphore_wait_timed(&__semaphore, __rel_time); - } -}; - -template -using __semaphore_base = - typename conditional<(__least_max_value > 1 && __least_max_value <= _LIBCPP_SEMAPHORE_MAX), - __platform_semaphore_base, - __atomic_semaphore_base>::type; - -#else - -template -using __semaphore_base = - __atomic_semaphore_base; - #define _LIBCPP_SEMAPHORE_MAX (numeric_limits::max()) -#endif //_LIBCPP_NO_NATIVE_SEMAPHORES - template class counting_semaphore { - __semaphore_base<__least_max_value> __semaphore; + __atomic_semaphore_base __semaphore; public: static constexpr ptrdiff_t max() noexcept { @@ -190,7 +131,7 @@ } _LIBCPP_INLINE_VISIBILITY - counting_semaphore(ptrdiff_t __count = 0) : __semaphore(__count) { } + constexpr explicit counting_semaphore(ptrdiff_t __count) : __semaphore(__count) { } ~counting_semaphore() = default; counting_semaphore(const counting_semaphore&) = delete; diff --git a/libcxx/src/support/win32/thread_win32.cpp b/libcxx/src/support/win32/thread_win32.cpp --- a/libcxx/src/support/win32/thread_win32.cpp +++ b/libcxx/src/support/win32/thread_win32.cpp @@ -39,9 +39,6 @@ static_assert(sizeof(__libcpp_tls_key) == sizeof(DWORD), ""); static_assert(alignof(__libcpp_tls_key) == alignof(DWORD), ""); -static_assert(sizeof(__libcpp_semaphore_t) == sizeof(HANDLE), ""); -static_assert(alignof(__libcpp_semaphore_t) == alignof(HANDLE), ""); - // Mutex int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m) { @@ -275,37 +272,4 @@ return 0; } -// Semaphores -bool __libcpp_semaphore_init(__libcpp_semaphore_t* __sem, int __init) -{ - *(PHANDLE)__sem = CreateSemaphoreEx(nullptr, __init, _LIBCPP_SEMAPHORE_MAX, - nullptr, 0, SEMAPHORE_ALL_ACCESS); - return *__sem != nullptr; -} - -bool __libcpp_semaphore_destroy(__libcpp_semaphore_t* __sem) -{ - CloseHandle(*(PHANDLE)__sem); - return true; -} - -bool __libcpp_semaphore_post(__libcpp_semaphore_t* __sem) -{ - return ReleaseSemaphore(*(PHANDLE)__sem, 1, nullptr); -} - -bool __libcpp_semaphore_wait(__libcpp_semaphore_t* __sem) -{ - return WaitForSingleObjectEx(*(PHANDLE)__sem, INFINITE, false) == - WAIT_OBJECT_0; -} - -bool __libcpp_semaphore_wait_timed(__libcpp_semaphore_t* __sem, - chrono::nanoseconds const& __ns) -{ - chrono::milliseconds __ms = chrono::ceil(__ns); - return WaitForSingleObjectEx(*(PHANDLE)__sem, __ms.count(), false) == - WAIT_OBJECT_0; -} - _LIBCPP_END_NAMESPACE_STD diff --git a/libcxx/test/std/thread/thread.semaphore/binary.pass.cpp b/libcxx/test/std/thread/thread.semaphore/binary.pass.cpp --- a/libcxx/test/std/thread/thread.semaphore/binary.pass.cpp +++ b/libcxx/test/std/thread/thread.semaphore/binary.pass.cpp @@ -18,10 +18,13 @@ #include #include #include +#include #include "make_test_thread.h" #include "test_macros.h" +static_assert(std::is_same>::value, ""); + int main(int, char**) { std::binary_semaphore s(1); diff --git a/libcxx/test/std/thread/thread.semaphore/ctor.compile.pass.cpp b/libcxx/test/std/thread/thread.semaphore/ctor.compile.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/thread/thread.semaphore/ctor.compile.pass.cpp @@ -0,0 +1,34 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads +// UNSUPPORTED: c++03, c++11 + +// This test requires the dylib support introduced in D68480, which shipped in +// macOS 11.0. +// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12|13|14|15}} + +// + +// constexpr explicit counting_semaphore(ptrdiff_t desired); + +#include +#include + +#include "test_macros.h" + +static_assert(!std::is_default_constructible::value, ""); +static_assert(!std::is_default_constructible>::value, ""); + +static_assert(!std::is_convertible::value, ""); +static_assert(!std::is_convertible>::value, ""); + +#if 0 // TODO FIXME: the ctor should be constexpr when TEST_STD_VER > 17 +constinit std::binary_semaphore bs(1); +constinit std::counting_semaphore cs(1); +#endif diff --git a/libcxx/test/std/thread/thread.semaphore/max.pass.cpp b/libcxx/test/std/thread/thread.semaphore/max.pass.cpp --- a/libcxx/test/std/thread/thread.semaphore/max.pass.cpp +++ b/libcxx/test/std/thread/thread.semaphore/max.pass.cpp @@ -18,10 +18,9 @@ int main(int, char**) { - static_assert(std::counting_semaphore<>::max() > 0, ""); + static_assert(std::counting_semaphore<>::max() >= 1, ""); static_assert(std::counting_semaphore<1>::max() >= 1, ""); - static_assert(std::counting_semaphore::max()>::max() >= 1, ""); - static_assert(std::counting_semaphore::max()>::max() >= 1, ""); - static_assert(std::counting_semaphore<1>::max() == std::binary_semaphore::max(), ""); + static_assert(std::counting_semaphore::max()>::max() >= std::numeric_limits::max(), ""); + static_assert(std::counting_semaphore::max()>::max() == std::numeric_limits::max(), ""); return 0; }