Patch Detail
get:
Show a patch.
patch:
Update a patch.
put:
Update a patch.
GET /api/patches/2226290/?format=api
{ "id": 2226290, "url": "http://patchwork.ozlabs.org/api/patches/2226290/?format=api", "web_url": "http://patchwork.ozlabs.org/project/gcc/patch/bmm.hhubrmqub2.gcc.gcc-TEST.redi.31.1.15@forge-stage.sourceware.org/", "project": { "id": 17, "url": "http://patchwork.ozlabs.org/api/projects/17/?format=api", "name": "GNU Compiler Collection", "link_name": "gcc", "list_id": "gcc-patches.gcc.gnu.org", "list_email": "gcc-patches@gcc.gnu.org", "web_url": null, "scm_url": null, "webscm_url": null, "list_archive_url": "", "list_archive_url_format": "", "commit_url_format": "" }, "msgid": "<bmm.hhubrmqub2.gcc.gcc-TEST.redi.31.1.15@forge-stage.sourceware.org>", "list_archive_url": null, "date": "2026-04-22T10:44:30", "name": "[v1,15/16] libstdc++: Rewrite std::counting_semaphore base class [PR118494]", "commit_ref": null, "pull_url": null, "state": "new", "archived": false, "hash": "ecc8ecf232e0711fb5ba8cb3801a910b551f86c0", "submitter": { "id": 93210, "url": "http://patchwork.ozlabs.org/api/people/93210/?format=api", "name": "Jonathan Wakely via Sourceware Forge", "email": "forge-bot+redi@forge-stage.sourceware.org" }, "delegate": null, "mbox": "http://patchwork.ozlabs.org/project/gcc/patch/bmm.hhubrmqub2.gcc.gcc-TEST.redi.31.1.15@forge-stage.sourceware.org/mbox/", "series": [ { "id": 500987, "url": "http://patchwork.ozlabs.org/api/series/500987/?format=api", "web_url": "http://patchwork.ozlabs.org/project/gcc/list/?series=500987", "date": "2026-04-22T10:44:17", "name": "atomic wait/notify ABI stabilization", "version": 1, "mbox": "http://patchwork.ozlabs.org/series/500987/mbox/" } ], "comments": "http://patchwork.ozlabs.org/api/patches/2226290/comments/", "check": "pending", "checks": "http://patchwork.ozlabs.org/api/patches/2226290/checks/", "tags": {}, "related": [], "headers": { "Return-Path": "<gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org>", "X-Original-To": [ "incoming@patchwork.ozlabs.org", "gcc-patches@gcc.gnu.org" ], "Delivered-To": [ "patchwork-incoming@legolas.ozlabs.org", "gcc-patches@gcc.gnu.org" ], "Authentication-Results": [ "legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=2620:52:6:3111::32; helo=vm01.sourceware.org;\n envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org;\n receiver=patchwork.ozlabs.org)", "sourceware.org; dmarc=none (p=none dis=none)\n header.from=forge-stage.sourceware.org", "sourceware.org;\n spf=pass smtp.mailfrom=forge-stage.sourceware.org", "server2.sourceware.org;\n arc=none smtp.remote-ip=38.145.34.39" ], "Received": [ "from vm01.sourceware.org (vm01.sourceware.org\n [IPv6:2620:52:6:3111::32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g0xRw1Tbgz1yCv\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 22 Apr 2026 21:13:48 +1000 (AEST)", "from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id 2EA7242C251B\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 22 Apr 2026 11:13:46 +0000 (GMT)", "from forge-stage.sourceware.org (vm08.sourceware.org [38.145.34.39])\n by sourceware.org (Postfix) with ESMTPS id 84118407C151\n for <gcc-patches@gcc.gnu.org>; Wed, 22 Apr 2026 10:46:12 +0000 (GMT)", "from forge-stage.sourceware.org (localhost [IPv6:::1])\n (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256)\n (No client certificate requested)\n by forge-stage.sourceware.org (Postfix) with ESMTPS id 5042742BBE\n for <gcc-patches@gcc.gnu.org>; Wed, 22 Apr 2026 10:46:02 +0000 (UTC)" ], "DKIM-Filter": [ "OpenDKIM Filter v2.11.0 sourceware.org 2EA7242C251B", "OpenDKIM Filter v2.11.0 sourceware.org 84118407C151" ], "DMARC-Filter": "OpenDMARC Filter v1.4.2 sourceware.org 84118407C151", "ARC-Filter": "OpenARC Filter v1.0.0 sourceware.org 84118407C151", "ARC-Seal": "i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1776854772; cv=none;\n b=fCZsUGreQAMH1dMDxTd9/wbTFqzt+WtQ1NtLVrHVCMJ5GBPEA1U9mQS+Gk4LPDlN9uyO4j5SKvbQafOQI1x/IVCYx/vEtgBCcMkPKQwCPi5S/JxjGy3bmXkVrrpfiBGJRLYvuJIGyOACxVsCSrxpw9HNBrbzpp3nVKmKG2kL8Ak=", "ARC-Message-Signature": "i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1776854772; c=relaxed/simple;\n bh=XSn92vArzJTRUI5idq5v6jMBy3YL8snYE3hZ+OIq9Hg=;\n h=From:Date:Subject:To:Message-ID;\n b=ZTonDDvBNiRAYA03/DMesXHgkv21OxxhYJr/KO9bAPQH+XWdiJmk197aGG5LcE/ITO9DzfPSvTnfmWdcbAQjhIQTONPpNmi0f5fJwk/stOfsyDDcautFiGTCmMDPVpHycI34X+j3vDQHm2T0+E4UvkjJDNkVRCqa7kiLvtT8vu0=", "ARC-Authentication-Results": "i=1; server2.sourceware.org", "From": "Jonathan Wakely via Sourceware Forge\n <forge-bot+redi@forge-stage.sourceware.org>", "Date": "Wed, 22 Apr 2026 10:44:30 +0000", "Subject": "[PATCH v1 15/16] libstdc++: Rewrite std::counting_semaphore base\n class [PR118494]", "To": "gcc-patches mailing list <gcc-patches@gcc.gnu.org>", "Message-ID": "\n <bmm.hhubrmqub2.gcc.gcc-TEST.redi.31.1.15@forge-stage.sourceware.org>", "X-Mailer": "batrachomyomachia", "X-Pull-Request-Organization": "gcc", "X-Pull-Request-Repository": "gcc-TEST", "X-Pull-Request": "https://forge.sourceware.org/gcc/gcc-TEST/pulls/31", "References": "\n <bmm.hhubrmqub2.gcc.gcc-TEST.redi.31.1.0@forge-stage.sourceware.org>", "In-Reply-To": "\n <bmm.hhubrmqub2.gcc.gcc-TEST.redi.31.1.0@forge-stage.sourceware.org>", "X-Patch-URL": "\n https://forge.sourceware.org/redi/gcc/commit/f9aa995c6396db5ae7e1dab6e74a7b485b9f38e6", "X-BeenThere": "gcc-patches@gcc.gnu.org", "X-Mailman-Version": "2.1.30", "Precedence": "list", "List-Id": "Gcc-patches mailing list <gcc-patches.gcc.gnu.org>", "List-Unsubscribe": "<https://gcc.gnu.org/mailman/options/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe>", "List-Archive": "<https://gcc.gnu.org/pipermail/gcc-patches/>", "List-Post": "<mailto:gcc-patches@gcc.gnu.org>", "List-Help": "<mailto:gcc-patches-request@gcc.gnu.org?subject=help>", "List-Subscribe": "<https://gcc.gnu.org/mailman/listinfo/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe>", "Reply-To": "gcc-patches mailing list <gcc-patches@gcc.gnu.org>, redi@gcc.gnu.org", "Errors-To": "gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org" }, "content": "From: Jonathan Wakely <jwakely@redhat.com>\n\nRemove __platform_semaphore. Replace __atomic_semaphore with\n__semaphore_base<bool> and change its counter to be ptrdiff_t when the\ncount doesn't fit in __platform_wait_t (PR 118494).\n\nMake the std::counting_semaphore constructor constexpr to support\nconstant initialization (PR 110854).\n\nAdd precondition checks to the constructor and release member functions\n(PR 98749).\n\nlibstdc++-v3/ChangeLog:\n\n\tPR libstdc++/118494\n\tPR libstdc++/110854\n\tPR libstdc++/98749\n\t* include/bits/semaphore_base.h (__platform_semaphore): Remove.\n\t(__atomic_semaphore): Replace with __semaphore_base<bool> and\n\tmake type of _M_count depend on template parameter. Fix _S_max\n\tconstant to use correct type.\n\t(__semaphore_base::_M_try_acquire): Qualify to avoid ADL.\n\t(__semaphore_base::_M_release): Return old value. Remove FIXME\n\tcomment.\n\t(__semaphore_impl): Replace typedef with alias template.\n\t* include/std/semaphore (semaphore): Adjust type of _M_sem\n\tmember. Add constexpr to constructor. Add assertions to\n\t(semaphore::semaphore(ptrdiff_t)): Add constexpr. Add assertion\n\tfor precondition.\n\t(semaphore::release): Add assertion using value returned from\n\t_M_release.\n\t* testsuite/30_threads/semaphore/100806.cc: Increase template\n\targument for std::counting_semaphore, so constructor\n\tprecondition is met.\n\t* testsuite/30_threads/semaphore/cons.cc: New test.\n\t* testsuite/30_threads/semaphore/try_acquire_posix.cc: Remove.\n\t* testsuite/30_threads/semaphore/platform_try_acquire_for.cc:\n\tRemoved.\n---\n libstdc++-v3/include/bits/semaphore_base.h | 224 ++++--------------\n libstdc++-v3/include/std/semaphore | 32 ++-\n .../testsuite/30_threads/semaphore/100806.cc | 2 +-\n .../testsuite/30_threads/semaphore/cons.cc | 7 +\n .../semaphore/platform_try_acquire_for.cc | 9 -\n .../30_threads/semaphore/try_acquire_posix.cc | 153 ------------\n 6 files changed, 79 insertions(+), 348 deletions(-)\n create mode 100644 libstdc++-v3/testsuite/30_threads/semaphore/cons.cc\n delete mode 100644 libstdc++-v3/testsuite/30_threads/semaphore/platform_try_acquire_for.cc\n delete mode 100644 libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_posix.cc", "diff": "diff --git a/libstdc++-v3/include/bits/semaphore_base.h b/libstdc++-v3/include/bits/semaphore_base.h\nindex 444a1589fb5a..5b5a1c982317 100644\n--- a/libstdc++-v3/include/bits/semaphore_base.h\n+++ b/libstdc++-v3/include/bits/semaphore_base.h\n@@ -34,162 +34,44 @@\n #pragma GCC system_header\n #endif\n \n+#include <bits/version.h>\n+\n+#ifdef __glibcxx_semaphore // C++ >= 20 && hosted && atomic_wait\n #include <bits/atomic_base.h>\n #include <bits/chrono.h>\n-#if __glibcxx_atomic_wait\n #include <bits/atomic_timed_wait.h>\n #include <ext/numeric_traits.h>\n-#endif // __cpp_lib_atomic_wait\n-\n-#ifdef _GLIBCXX_HAVE_POSIX_SEMAPHORE\n-# include <cerrno>\t// errno, EINTR, EAGAIN etc.\n-# include <limits.h>\t// SEM_VALUE_MAX\n-# include <semaphore.h>\t// sem_t, sem_init, sem_wait, sem_post etc.\n-#elif defined(_GLIBCXX_USE_POSIX_SEMAPHORE)\n-# warning \"POSIX semaphore not available, ignoring _GLIBCXX_USE_POSIX_SEMAPHORE\"\n-# undef _GLIBCXX_USE_POSIX_SEMAPHORE\n-#endif\n \n namespace std _GLIBCXX_VISIBILITY(default)\n {\n _GLIBCXX_BEGIN_NAMESPACE_VERSION\n \n-#ifdef _GLIBCXX_HAVE_POSIX_SEMAPHORE\n- struct __platform_semaphore\n+ template<bool _Platform_wait>\n+ struct __semaphore_base\n {\n- using __clock_t = chrono::system_clock;\n-#ifdef SEM_VALUE_MAX\n- static constexpr ptrdiff_t _S_max = SEM_VALUE_MAX;\n-#else\n- static constexpr ptrdiff_t _S_max = _POSIX_SEM_VALUE_MAX;\n-#endif\n-\n- explicit __platform_semaphore(ptrdiff_t __count) noexcept\n- {\n- sem_init(&_M_semaphore, 0, __count);\n- }\n-\n- __platform_semaphore(const __platform_semaphore&) = delete;\n- __platform_semaphore& operator=(const __platform_semaphore&) = delete;\n-\n- ~__platform_semaphore()\n- { sem_destroy(&_M_semaphore); }\n-\n- _GLIBCXX_ALWAYS_INLINE void\n- _M_acquire() noexcept\n- {\n- while (sem_wait(&_M_semaphore))\n-\tif (errno != EINTR)\n-\t std::__terminate();\n- }\n-\n- _GLIBCXX_ALWAYS_INLINE bool\n- _M_try_acquire() noexcept\n- {\n- while (sem_trywait(&_M_semaphore))\n-\t{\n-\t if (errno == EAGAIN) // already locked\n-\t return false;\n-\t else if (errno != EINTR)\n-\t std::__terminate();\n-\t // else got EINTR so retry\n-\t}\n- return true;\n- }\n-\n- _GLIBCXX_ALWAYS_INLINE void\n- _M_release(ptrdiff_t __update) noexcept\n- {\n- for(; __update != 0; --__update)\n-\tif (sem_post(&_M_semaphore))\n-\t std::__terminate();\n- }\n-\n- bool\n- _M_try_acquire_until_impl(const chrono::time_point<__clock_t>& __atime)\n- noexcept\n- {\n- auto __s = chrono::time_point_cast<chrono::seconds>(__atime);\n- auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);\n+ using __count_type = __conditional_t<_Platform_wait,\n+\t\t\t\t\t __detail::__platform_wait_t,\n+\t\t\t\t\t ptrdiff_t>;\n \n- struct timespec __ts =\n- {\n-\tstatic_cast<std::time_t>(__s.time_since_epoch().count()),\n-\tstatic_cast<long>(__ns.count())\n- };\n+ static constexpr ptrdiff_t _S_max\n+ = __gnu_cxx::__int_traits<__count_type>::__max;\n \n- while (sem_timedwait(&_M_semaphore, &__ts))\n-\t{\n-\t if (errno == ETIMEDOUT)\n-\t return false;\n-\t else if (errno != EINTR)\n-\t std::__terminate();\n-\t}\n- return true;\n- }\n-\n- template<typename _Clock, typename _Duration>\n- bool\n- _M_try_acquire_until(const chrono::time_point<_Clock,\n-\t\t\t _Duration>& __atime) noexcept\n- {\n-\tif constexpr (std::is_same_v<__clock_t, _Clock>)\n-\t {\n-\t using _Dur = __clock_t::duration;\n-\t return _M_try_acquire_until_impl(chrono::ceil<_Dur>(__atime));\n-\t }\n-\telse\n-\t {\n-\t // TODO: if _Clock is monotonic_clock we could use\n-\t // sem_clockwait with CLOCK_MONOTONIC.\n+ constexpr explicit\n+ __semaphore_base(__count_type __count) noexcept\n+ : _M_counter(__count)\n+ { }\n \n-\t const typename _Clock::time_point __c_entry = _Clock::now();\n-\t const auto __s_entry = __clock_t::now();\n-\t const auto __delta = __atime - __c_entry;\n-\t const auto __s_atime = __s_entry + __delta;\n-\t if (_M_try_acquire_until_impl(__s_atime))\n-\t return true;\n+ __semaphore_base(const __semaphore_base&) = delete;\n+ __semaphore_base& operator=(const __semaphore_base&) = delete;\n \n-\t // We got a timeout when measured against __clock_t but\n-\t // we need to check against the caller-supplied clock\n-\t // to tell whether we should return a timeout.\n-\t return (_Clock::now() < __atime);\n-\t }\n- }\n-\n- template<typename _Rep, typename _Period>\n- _GLIBCXX_ALWAYS_INLINE bool\n- _M_try_acquire_for(const chrono::duration<_Rep, _Period>& __rtime)\n-\tnoexcept\n- { return _M_try_acquire_until(__clock_t::now() + __rtime); }\n-\n- private:\n- sem_t _M_semaphore;\n- };\n-#endif // _GLIBCXX_HAVE_POSIX_SEMAPHORE\n-\n-#if __glibcxx_atomic_wait\n- struct __atomic_semaphore\n- {\n- static constexpr ptrdiff_t _S_max = __gnu_cxx::__int_traits<int>::__max;\n- explicit __atomic_semaphore(__detail::__platform_wait_t __count) noexcept\n- : _M_counter(__count)\n- {\n- __glibcxx_assert(__count >= 0 && __count <= _S_max);\n- }\n-\n- __atomic_semaphore(const __atomic_semaphore&) = delete;\n- __atomic_semaphore& operator=(const __atomic_semaphore&) = delete;\n-\n- static _GLIBCXX_ALWAYS_INLINE __detail::__platform_wait_t\n- _S_get_current(__detail::__platform_wait_t* __counter) noexcept\n+ static _GLIBCXX_ALWAYS_INLINE __count_type\n+ _S_get_current(__count_type* __counter) noexcept\n {\n return __atomic_impl::load(__counter, memory_order::acquire);\n }\n \n static _GLIBCXX_ALWAYS_INLINE bool\n- _S_do_try_acquire(__detail::__platform_wait_t* __counter,\n-\t\t __detail::__platform_wait_t __old) noexcept\n+ _S_do_try_acquire(__count_type* __counter, __count_type __old) noexcept\n {\n if (__old == 0)\n \treturn false;\n@@ -204,8 +86,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION\n _M_acquire() noexcept\n {\n auto const __vfn = [this]{ return _S_get_current(&this->_M_counter); };\n- auto const __pred = [this](__detail::__platform_wait_t __cur)\n-\t{ return _S_do_try_acquire(&this->_M_counter, __cur); };\n+ auto const __pred = [this](__count_type __cur) {\n+\treturn _S_do_try_acquire(&this->_M_counter, __cur);\n+ };\n std::__atomic_wait_address(&_M_counter, __pred, __vfn, true);\n }\n \n@@ -213,23 +96,25 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION\n _M_try_acquire() noexcept\n {\n auto const __vfn = [this]{ return _S_get_current(&this->_M_counter); };\n- auto const __pred = [this](__detail::__platform_wait_t __cur)\n-\t{ return _S_do_try_acquire(&this->_M_counter, __cur); };\n- return __atomic_wait_address_for(&_M_counter, __pred, __vfn,\n-\t\t\t\t\t __detail::__wait_clock_t::duration(),\n-\t\t\t\t\t true);\n+ auto const __pred = [this](__count_type __cur) {\n+\treturn _S_do_try_acquire(&this->_M_counter, __cur);\n+ };\n+ using __detail::__wait_clock_t;\n+ return std::__atomic_wait_address_for(&_M_counter, __pred, __vfn,\n+\t\t\t\t\t __wait_clock_t::duration(),\n+\t\t\t\t\t true);\n }\n \n template<typename _Clock, typename _Duration>\n _GLIBCXX_ALWAYS_INLINE bool\n- _M_try_acquire_until(const chrono::time_point<_Clock,\n-\t\t\t _Duration>& __atime) noexcept\n+ _M_try_acquire_until(const chrono::time_point<_Clock, _Duration>& __atime) noexcept\n {\n \tauto const __vfn = [this]{ return _S_get_current(&this->_M_counter); };\n-\tauto const __pred = [this](__detail::__platform_wait_t __cur)\n-\t { return _S_do_try_acquire(&this->_M_counter, __cur); };\n-\treturn std::__atomic_wait_address_until(&_M_counter,\n-\t\t\t\t\t\t__pred, __vfn, __atime, true);\n+\tauto const __pred = [this](__count_type __cur) {\n+\t return _S_do_try_acquire(&this->_M_counter, __cur);\n+\t};\n+\treturn std::__atomic_wait_address_until(&_M_counter, __pred, __vfn,\n+\t\t\t\t\t\t__atime, true);\n }\n \n template<typename _Rep, typename _Period>\n@@ -237,39 +122,34 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION\n _M_try_acquire_for(const chrono::duration<_Rep, _Period>& __rtime) noexcept\n {\n \tauto const __vfn = [this]{ return _S_get_current(&this->_M_counter); };\n-\tauto const __pred = [this](__detail::__platform_wait_t __cur)\n-\t { return _S_do_try_acquire(&this->_M_counter, __cur); };\n-\treturn std::__atomic_wait_address_for(&_M_counter,\n-\t\t\t\t\t __pred, __vfn, __rtime, true);\n+\tauto const __pred = [this](__count_type __cur) {\n+\t return _S_do_try_acquire(&this->_M_counter, __cur);\n+\t};\n+\treturn std::__atomic_wait_address_for(&_M_counter, __pred, __vfn,\n+\t\t\t\t\t __rtime, true);\n }\n \n- _GLIBCXX_ALWAYS_INLINE void\n+ _GLIBCXX_ALWAYS_INLINE ptrdiff_t\n _M_release(ptrdiff_t __update) noexcept\n {\n- if (0 < __atomic_impl::fetch_add(&_M_counter, __update, memory_order_release))\n-\treturn;\n- if (__update > 1)\n-\t__atomic_notify_address(&_M_counter, true, true);\n- else\n+ auto __old = __atomic_impl::fetch_add(&_M_counter, __update,\n+\t\t\t\t\t memory_order::release);\n+ if (__old == 0 && __update > 0)\n \t__atomic_notify_address(&_M_counter, true, true);\n-// FIXME - Figure out why this does not wake a waiting thread\n-//\t__atomic_notify_address_bare(&_M_counter, false);\n+ return __old;\n }\n \n private:\n- alignas(__detail::__platform_wait_alignment)\n- __detail::__platform_wait_t _M_counter;\n+ alignas(_Platform_wait ? __detail::__platform_wait_alignment\n+\t\t\t : __alignof__(__count_type))\n+ __count_type _M_counter;\n };\n-#endif // __cpp_lib_atomic_wait\n \n-// Note: the _GLIBCXX_USE_POSIX_SEMAPHORE macro can be used to force the\n-// use of Posix semaphores (sem_t). Doing so however, alters the ABI.\n-#if defined __glibcxx_atomic_wait && !_GLIBCXX_USE_POSIX_SEMAPHORE\n- using __semaphore_impl = __atomic_semaphore;\n-#elif _GLIBCXX_HAVE_POSIX_SEMAPHORE\n- using __semaphore_impl = __platform_semaphore;\n-#endif\n+ template<ptrdiff_t _Max>\n+ using __semaphore_impl\n+ = __semaphore_base<(_Max <= __semaphore_base<true>::_S_max)>;\n \n _GLIBCXX_END_NAMESPACE_VERSION\n } // namespace std\n+#endif // __glibcxx_semaphore\n #endif // _GLIBCXX_SEMAPHORE_BASE_H\ndiff --git a/libstdc++-v3/include/std/semaphore b/libstdc++-v3/include/std/semaphore\nindex bec5ac301aa2..ca1bffe371a0 100644\n--- a/libstdc++-v3/include/std/semaphore\n+++ b/libstdc++-v3/include/std/semaphore\n@@ -35,29 +35,29 @@\n \n #include <bits/requires_hosted.h> // concurrency\n \n-#if __cplusplus > 201703L\n-#include <bits/semaphore_base.h>\n-\n #define __glibcxx_want_semaphore\n #include <bits/version.h>\n \n-#ifdef __cpp_lib_semaphore // C++ >= 20 && hosted && (atomic_wait || posix_sem)\n+#ifdef __cpp_lib_semaphore // C++ >= 20 && hosted && atomic_wait\n+#include <bits/semaphore_base.h>\n+\n namespace std _GLIBCXX_VISIBILITY(default)\n {\n _GLIBCXX_BEGIN_NAMESPACE_VERSION\n \n- template<ptrdiff_t __least_max_value = __semaphore_impl::_S_max>\n+ template<ptrdiff_t __least_max_value = __semaphore_base<true>::_S_max>\n class counting_semaphore\n {\n static_assert(__least_max_value >= 0);\n- static_assert(__least_max_value <= __semaphore_impl::_S_max);\n \n- __semaphore_impl _M_sem;\n+ using _Impl = __semaphore_impl<__least_max_value>;\n+ _Impl _M_sem;\n \n public:\n- explicit counting_semaphore(ptrdiff_t __desired) noexcept\n-\t: _M_sem(__desired)\n- { }\n+ constexpr explicit\n+ counting_semaphore(ptrdiff_t __desired) noexcept\n+ : _M_sem(__desired)\n+ { __glibcxx_assert(__desired >= 0 && __desired <= max()); }\n \n ~counting_semaphore() = default;\n \n@@ -69,8 +69,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION\n { return __least_max_value; }\n \n void\n- release(ptrdiff_t __update = 1) noexcept(noexcept(_M_sem._M_release(1)))\n- { _M_sem._M_release(__update); }\n+ release(ptrdiff_t __update = 1) noexcept\n+ {\n+\t[[maybe_unused]] ptrdiff_t __old = _M_sem._M_release(__update);\n+\t__glibcxx_assert(__update >= 0 && __update <= max() - __old);\n+ }\n \n void\n acquire() noexcept(noexcept(_M_sem._M_acquire()))\n@@ -91,10 +94,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION\n \t{ return _M_sem._M_try_acquire_until(__atime); }\n };\n \n+ /** @brief A binary semaphore\n+ *\n+ * @since C++20\n+ */\n using binary_semaphore = std::counting_semaphore<1>;\n \n _GLIBCXX_END_NAMESPACE_VERSION\n } // namespace\n-#endif // cpp_lib_atomic_wait || _GLIBCXX_HAVE_POSIX_SEMAPHORE\n #endif // __cpp_lib_semaphore\n #endif // _GLIBCXX_SEMAPHORE\ndiff --git a/libstdc++-v3/testsuite/30_threads/semaphore/100806.cc b/libstdc++-v3/testsuite/30_threads/semaphore/100806.cc\nindex c770f05be009..4b761cef2024 100644\n--- a/libstdc++-v3/testsuite/30_threads/semaphore/100806.cc\n+++ b/libstdc++-v3/testsuite/30_threads/semaphore/100806.cc\n@@ -12,7 +12,7 @@\n #include <chrono>\n #include <vector>\n \n-std::counting_semaphore<4> semaphore{6};\n+std::counting_semaphore<6> semaphore{6};\n \n std::mutex mtx;\n std::vector<std::string> results;\ndiff --git a/libstdc++-v3/testsuite/30_threads/semaphore/cons.cc b/libstdc++-v3/testsuite/30_threads/semaphore/cons.cc\nnew file mode 100644\nindex 000000000000..920f7423332b\n--- /dev/null\n+++ b/libstdc++-v3/testsuite/30_threads/semaphore/cons.cc\n@@ -0,0 +1,7 @@\n+// { dg-do compile { target c++20 } }\n+\n+#include <semaphore>\n+\n+// PR 110854 Constructor of std::counting_semaphore is not constexpr\n+constinit std::binary_semaphore b(0);\n+constinit std::counting_semaphore<5> c(2);\ndiff --git a/libstdc++-v3/testsuite/30_threads/semaphore/platform_try_acquire_for.cc b/libstdc++-v3/testsuite/30_threads/semaphore/platform_try_acquire_for.cc\ndeleted file mode 100644\nindex 6d90564ea8ac..000000000000\n--- a/libstdc++-v3/testsuite/30_threads/semaphore/platform_try_acquire_for.cc\n+++ /dev/null\n@@ -1,9 +0,0 @@\n-// { dg-options \"-D_GLIBCXX_USE_POSIX_SEMAPHORE\" }\n-// { dg-do run { target c++20 } }\n-// { dg-additional-options \"-pthread\" { target pthread } }\n-// { dg-require-gthreads \"\" }\n-// { dg-add-options libatomic }\n-\n-#include \"try_acquire_for.cc\"\n-\n-// { dg-prune-output \"ignoring _GLIBCXX_USE_POSIX_SEMAPHORE\" }\ndiff --git a/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_posix.cc b/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_posix.cc\ndeleted file mode 100644\nindex cf5745584c8d..000000000000\n--- a/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_posix.cc\n+++ /dev/null\n@@ -1,153 +0,0 @@\n-// Copyright (C) 2020-2025 Free Software Foundation, Inc.\n-//\n-// This file is part of the GNU ISO C++ Library. This library is free\n-// software; you can redistribute it and/or modify it under the\n-// terms of the GNU General Public License as published by the\n-// Free Software Foundation; either version 3, or (at your option)\n-// any later version.\n-\n-// This library is distributed in the hope that it will be useful,\n-// but WITHOUT ANY WARRANTY; without even the implied warranty of\n-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n-// GNU General Public License for more details.\n-\n-// You should have received a copy of the GNU General Public License along\n-// with this library; see the file COPYING3. If not see\n-// <http://www.gnu.org/licenses/>.\n-\n-// { dg-do run { target c++20 } }\n-// { dg-additional-options \"-pthread\" { target pthread } }\n-// { dg-require-gthreads \"\" }\n-// { dg-add-options libatomic }\n-\n-#include <semaphore>\n-#ifdef _GLIBCXX_HAVE_POSIX_SEMAPHORE\n-#include <chrono>\n-#include <thread>\n-#include <atomic>\n-#include <testsuite_hooks.h>\n-\n-void test01()\n-{\n- using namespace std::chrono_literals;\n- std::__platform_semaphore s(2);\n- s._M_acquire();\n-\n- auto const dur = 250ms;\n- {\n- auto const t0 = std::chrono::steady_clock::now();\n- VERIFY( s._M_try_acquire_for(dur) );\n- auto const diff = std::chrono::steady_clock::now() - t0;\n- VERIFY( diff < dur );\n- }\n-\n- {\n- auto const t0 = std::chrono::steady_clock::now();\n- VERIFY( !s._M_try_acquire_for(dur) );\n- auto const diff = std::chrono::steady_clock::now() - t0;\n- VERIFY( diff >= dur );\n- }\n-}\n-\n-void test02()\n-{\n- using namespace std::chrono_literals;\n- std::__platform_semaphore s(1);\n- std::atomic<int> a(0), b(0);\n- std::thread t([&] {\n- a.wait(0);\n- auto const dur = 250ms;\n- VERIFY( !s._M_try_acquire_for(dur) );\n- b++;\n- b.notify_one();\n-\n- a.wait(1);\n- VERIFY( s._M_try_acquire_for(dur) );\n- b++;\n- b.notify_one();\n- });\n- t.detach();\n-\n- s._M_acquire();\n- a++;\n- a.notify_one();\n- b.wait(0);\n- s._M_release(1);\n- a++;\n- a.notify_one();\n-\n- b.wait(1);\n-}\n-\n-void test03()\n-{\n- using namespace std::chrono_literals;\n- std::__platform_semaphore s(2);\n- s._M_acquire();\n-\n- auto const dur = 250ms;\n- {\n- auto const at = std::chrono::system_clock::now() + dur;\n- auto const t0 = std::chrono::steady_clock::now();\n- VERIFY( s._M_try_acquire_until(at) );\n- auto const diff = std::chrono::steady_clock::now() - t0;\n- VERIFY( diff < dur );\n- }\n-\n- {\n- auto const at = std::chrono::system_clock::now() + dur;\n- auto const t0 = std::chrono::steady_clock::now();\n- VERIFY( !s._M_try_acquire_until(at) );\n- auto const diff = std::chrono::steady_clock::now() - t0;\n- VERIFY( diff >= dur );\n- }\n-}\n-\n-void test04()\n-{\n- using namespace std::chrono_literals;\n- std::__platform_semaphore s(1);\n- std::atomic<int> a(0), b(0);\n- std::thread t([&] {\n- a.wait(0);\n- auto const dur = 250ms;\n- {\n- auto const at = std::chrono::system_clock::now() + dur;\n- VERIFY( !s._M_try_acquire_until(at) );\n-\n- b++;\n- b.notify_one();\n- }\n-\n- a.wait(1);\n- {\n- auto const at = std::chrono::system_clock::now() + dur;\n- VERIFY( s._M_try_acquire_until(at) );\n- }\n- b++;\n- b.notify_one();\n- });\n- t.detach();\n-\n- s._M_acquire();\n- a++;\n- a.notify_one();\n- b.wait(0);\n- s._M_release(1);\n- a++;\n- a.notify_one();\n-\n- b.wait(1);\n-}\n-#endif\n-\n-int main()\n-{\n-#ifdef _GLIBCXX_HAVE_POSIX_SEMAPHORE\n- test01();\n- test02();\n- test03();\n- test04();\n-#endif\n- return 0;\n-}\n", "prefixes": [ "v1", "15/16" ] }