From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id E57793857C5E; Wed, 11 Nov 2020 11:13:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E57793857C5E Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r11-4909] libstdc++: Use helper type for checking thread ID X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: ca813880dcaae71f664d8f386b1a584cfefbbd4b X-Git-Newrev: 43f9e5aff06f1ca2296fdbd3141fe90ec0be1912 Message-Id: <20201111111342.E57793857C5E@sourceware.org> Date: Wed, 11 Nov 2020 11:13:42 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 Nov 2020 11:13:43 -0000 https://gcc.gnu.org/g:43f9e5aff06f1ca2296fdbd3141fe90ec0be1912 commit r11-4909-g43f9e5aff06f1ca2296fdbd3141fe90ec0be1912 Author: Jonathan Wakely Date: Wed Nov 11 09:28:50 2020 +0000 libstdc++: Use helper type for checking thread ID This encapsulates the storing and checking of the thread ID into a class type, so that the macro _GLIBCXX_HAS_GTHREADS is only checked in one place. The code doing the checks just calls member functions of the new type, without caring whether that really does any work or not. libstdc++-v3/ChangeLog: * include/std/stop_token (_Stop_state_t::_M_requester): Define new struct with members to store and check the thread ID. (_Stop_state_t::_M_request_stop()): Use _M_requester._M_set(). (_Stop_state_t::_M_remove_callback(_Stop_cb*)): Use _M_requester._M_is_current_thread(). Diff: --- libstdc++-v3/include/std/stop_token | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/libstdc++-v3/include/std/stop_token b/libstdc++-v3/include/std/stop_token index ccec6fab15c..7cd01c9713e 100644 --- a/libstdc++-v3/include/std/stop_token +++ b/libstdc++-v3/include/std/stop_token @@ -162,9 +162,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION std::atomic _M_owners{1}; std::atomic _M_value{_S_ssrc_counter_inc}; _Stop_cb* _M_head = nullptr; + struct + { #ifdef _GLIBCXX_HAS_GTHREADS - __gthread_t _M_requester; + __gthread_t _M_id; + void _M_set() { _M_id = __gthread_self(); } + bool _M_is_current_thread() const + { return __gthread_equal(_M_id, __gthread_self()); } +#else + void _M_set() { } + constexpr bool _M_is_current_thread() const { return true; } #endif + } _M_requester; _Stop_state_t() = default; @@ -237,9 +246,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } while (!_M_try_lock_and_stop(__old)); -#ifdef _GLIBCXX_HAS_GTHREADS - _M_requester = __gthread_self(); -#endif + _M_requester._M_set(); while (_M_head) { @@ -343,18 +350,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // Callback is not in the list, so must have been removed by a call to // _M_request_stop. -#ifdef _GLIBCXX_HAS_GTHREADS // Despite appearances there is no data race on _M_requester. The only // write to it happens before the callback is removed from the list, // and removing it from the list happens before this read. - if (!__gthread_equal(_M_requester, __gthread_self())) + if (!_M_requester._M_is_current_thread()) { // Synchronize with completion of callback. __cb->_M_done.acquire(); // Safe for ~stop_callback to destroy *__cb now. return; } -#endif + if (__cb->_M_destroyed) *__cb->_M_destroyed = true; }