gcc/libstdc++-v3/include/std/latch
Thomas Rodgers 6591e42269 Add feature test macro for atomic<T>::wait
Adds __cpp_lib_atomic_wait feature test macro which was overlooked in
the initial commit of this feature. Replaces uses of
_GLIBCXX_HAVE_ATOMIC_WAIT.

libstdc++-v3/ChangeLog:

	* include/bits/atomic_base.h: Replace usage of
	_GLIBCXX_HAVE_ATOMIC_WAIT with __cpp_lib_atomic_wait.
	* include/bits/atomic_timed_wait.h: Likewise.
	* include/bits/atomic_wait.h: Define __cpp_lib_atomic_wait
	feature test macro.
	* include/bits/semaphore_base.h: Replace usage of
	_GLIBCXX_HAVE_ATOMIC_WAIT with __cpp_lib_atomic_wait.
	* include/std/atomic: Likewise.
	* include/std/latch: Likewise.
	* include/std/semaphore: Likewise.
	* include/std/version: Define __cpp_lib_atomic wait
	feature test macro and replace usage of
	_GLIBCXX_HAVE_ATOMIC_WAIT.
	* testsuite/29_atomics/atomic/wait_notify/1.cc: New test.
	* testsuite/29_atomics/atomic/wait_notify/2.cc: Likewise.
2020-12-01 15:43:17 -08:00

94 lines
2.6 KiB
C++

// <latch> -*- C++ -*-
// Copyright (C) 2020 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file include/latch
* This is a Standard C++ Library header.
*/
#ifndef _GLIBCXX_LATCH
#define _GLIBCXX_LATCH 1
#pragma GCC system_header
#if __cplusplus > 201703L
#include <bits/atomic_base.h>
#include <ext/numeric_traits.h>
#if __cpp_lib_atomic_wait
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
#define __cpp_lib_latch 201907L
class latch
{
public:
static constexpr ptrdiff_t
max() noexcept
{ return __gnu_cxx::__int_traits<ptrdiff_t>::__max; }
constexpr explicit latch(ptrdiff_t __expected) noexcept
: _M_a(__expected) { }
~latch() = default;
latch(const latch&) = delete;
latch& operator=(const latch&) = delete;
_GLIBCXX_ALWAYS_INLINE void
count_down(ptrdiff_t __update = 1)
{
auto const __old = __atomic_impl::fetch_sub(&_M_a,
__update, memory_order::release);
if (__old == __update)
__atomic_impl::notify_all(&_M_a);
}
_GLIBCXX_ALWAYS_INLINE bool
try_wait() const noexcept
{ return __atomic_impl::load(&_M_a, memory_order::acquire) == 0; }
_GLIBCXX_ALWAYS_INLINE void
wait() const noexcept
{
auto const __old = __atomic_impl::load(&_M_a, memory_order::acquire);
std::__atomic_wait(&_M_a, __old, [this] { return this->try_wait(); });
}
_GLIBCXX_ALWAYS_INLINE void
arrive_and_wait(ptrdiff_t __update = 1) noexcept
{
count_down(__update);
wait();
}
private:
alignas(__alignof__(ptrdiff_t)) ptrdiff_t _M_a;
};
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace
#endif // __cpp_lib_atomic_wait
#endif // __cplusplus > 201703L
#endif // _GLIBCXX_LATCH