[gcc r16-1103] libstdc++: Refactor __semaphore_base member functions
Jonathan Wakely
redi@gcc.gnu.org
Wed Jun 4 13:00:20 GMT 2025
https://gcc.gnu.org/g:5dc3c5c16480e0fac0df248639960cfa26d774a0
commit r16-1103-g5dc3c5c16480e0fac0df248639960cfa26d774a0
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Mon Jun 2 23:01:40 2025 +0100
libstdc++: Refactor __semaphore_base member functions
Replace the _S_get_current and _S_do_try_acquire static member functions
with non-static member functions _M_get_current and _M_do_try_acquire.
This means they don't need the address of _M_counter passed in.
libstdc++-v3/ChangeLog:
* include/bits/semaphore_base.h (_S_get_current): Replace with
non-static _M_get_current.
(_S_do_try_acquire): Replace with non-static _M_do_try_acquire.
Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
Diff:
---
libstdc++-v3/include/bits/semaphore_base.h | 41 ++++++++++++++++--------------
1 file changed, 22 insertions(+), 19 deletions(-)
diff --git a/libstdc++-v3/include/bits/semaphore_base.h b/libstdc++-v3/include/bits/semaphore_base.h
index 526da4d82b6a..3f7a33ccd51a 100644
--- a/libstdc++-v3/include/bits/semaphore_base.h
+++ b/libstdc++-v3/include/bits/semaphore_base.h
@@ -64,20 +64,23 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__semaphore_base(const __semaphore_base&) = delete;
__semaphore_base& operator=(const __semaphore_base&) = delete;
- static _GLIBCXX_ALWAYS_INLINE __count_type
- _S_get_current(__count_type* __counter) noexcept
+ // Load the current counter value.
+ _GLIBCXX_ALWAYS_INLINE __count_type
+ _M_get_current() const noexcept
+ { return __atomic_impl::load(&_M_counter, memory_order::acquire); }
+
+ // Try to acquire the semaphore (i.e. decrement the counter).
+ // Returns false if the current counter is zero, or if another thread
+ // decrements the value first. In the latter case, __cur is set to the
+ // new value.
+ _GLIBCXX_ALWAYS_INLINE bool
+ _M_do_try_acquire(__count_type& __cur) noexcept
{
- return __atomic_impl::load(__counter, memory_order::acquire);
- }
-
- static _GLIBCXX_ALWAYS_INLINE bool
- _S_do_try_acquire(__count_type* __counter, __count_type& __old) noexcept
- {
- if (__old == 0)
- return false;
+ if (__cur == 0)
+ return false; // Cannot decrement when it's already zero.
- return __atomic_impl::compare_exchange_strong(__counter,
- __old, __old - 1,
+ return __atomic_impl::compare_exchange_strong(&_M_counter,
+ __cur, __cur - 1,
memory_order::acquire,
memory_order::relaxed);
}
@@ -85,7 +88,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
void
_M_acquire() noexcept
{
- auto const __vfn = [this]{ return _S_get_current(&this->_M_counter); };
+ auto const __vfn = [this]{ return _M_get_current(); };
auto __val = __vfn();
auto const __pred = [&__val](__count_type __cur) {
if (__cur > 0)
@@ -95,7 +98,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
return false;
};
- while (!_S_do_try_acquire(&_M_counter, __val))
+ while (!_M_do_try_acquire(__val))
if (__val == 0)
std::__atomic_wait_address(&_M_counter, __pred, __vfn, true);
}
@@ -103,7 +106,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
bool
_M_try_acquire() noexcept
{
- // The fastest implementation of this function is just _S_do_try_acquire
+ // The fastest implementation of this function is just _M_do_try_acquire
// but that can fail under contention even when _M_count > 0.
// Using _M_try_acquire_for(0ns) will retry a few times in a loop.
return _M_try_acquire_for(__detail::__wait_clock_t::duration{});
@@ -113,7 +116,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
bool
_M_try_acquire_until(const chrono::time_point<_Clock, _Duration>& __atime) noexcept
{
- auto const __vfn = [this]{ return _S_get_current(&this->_M_counter); };
+ auto const __vfn = [this]{ return _M_get_current(); };
auto __val = __vfn();
auto const __pred = [&__val](__count_type __cur) {
if (__cur > 0)
@@ -123,7 +126,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
return false;
};
- while (!_S_do_try_acquire(&this->_M_counter, __val))
+ while (!_M_do_try_acquire(__val))
if (__val == 0)
{
if (!std::__atomic_wait_address_until(&_M_counter, __pred,
@@ -137,7 +140,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
bool
_M_try_acquire_for(const chrono::duration<_Rep, _Period>& __rtime) noexcept
{
- auto const __vfn = [this]{ return _S_get_current(&this->_M_counter); };
+ auto const __vfn = [this]{ return _M_get_current(); };
auto __val = __vfn();
auto const __pred = [&__val](__count_type __cur) {
if (__cur > 0)
@@ -147,7 +150,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
return false;
};
- while (!_S_do_try_acquire(&this->_M_counter, __val))
+ while (!_M_do_try_acquire(__val))
if (__val == 0)
{
if (!std::__atomic_wait_address_for(&_M_counter, __pred,
More information about the Libstdc++-cvs
mailing list