83a1beee27
Add support for - * atomic_flag::wait/notify_one/notify_all * atomic::wait/notify_one/notify_all * counting_semaphore * binary_semaphore * latch libstdc++-v3/ChangeLog: * include/Makefile.am (bits_headers): Add new header. * include/Makefile.in: Regenerate. * include/bits/atomic_base.h (__atomic_flag::wait): Define. (__atomic_flag::notify_one): Likewise. (__atomic_flag::notify_all): Likewise. (__atomic_base<_Itp>::wait): Likewise. (__atomic_base<_Itp>::notify_one): Likewise. (__atomic_base<_Itp>::notify_all): Likewise. (__atomic_base<_Ptp*>::wait): Likewise. (__atomic_base<_Ptp*>::notify_one): Likewise. (__atomic_base<_Ptp*>::notify_all): Likewise. (__atomic_impl::wait): Likewise. (__atomic_impl::notify_one): Likewise. (__atomic_impl::notify_all): Likewise. (__atomic_float<_Fp>::wait): Likewise. (__atomic_float<_Fp>::notify_one): Likewise. (__atomic_float<_Fp>::notify_all): Likewise. (__atomic_ref<_Tp>::wait): Likewise. (__atomic_ref<_Tp>::notify_one): Likewise. (__atomic_ref<_Tp>::notify_all): Likewise. (atomic_wait<_Tp>): Likewise. (atomic_wait_explicit<_Tp>): Likewise. (atomic_notify_one<_Tp>): Likewise. (atomic_notify_all<_Tp>): Likewise. * include/bits/atomic_wait.h: New file. * include/bits/atomic_timed_wait.h: New file. * include/bits/semaphore_base.h: New file. * include/std/atomic (atomic<bool>::wait): Define. (atomic<bool>::wait_one): Likewise. (atomic<bool>::wait_all): Likewise. (atomic<_Tp>::wait): Likewise. (atomic<_Tp>::wait_one): Likewise. (atomic<_Tp>::wait_all): Likewise. (atomic<_Tp*>::wait): Likewise. (atomic<_Tp*>::wait_one): Likewise. (atomic<_Tp*>::wait_all): Likewise. * include/std/latch: New file. * include/std/semaphore: New file. * include/std/version: Add __cpp_lib_semaphore and __cpp_lib_latch defines. * testsuite/29_atomics/atomic/wait_notify/bool.cc: New test. * testsuite/29_atomics/atomic/wait_notify/pointers.cc: Likewise. * testsuite/29_atomics/atomic/wait_notify/generic.cc: Liekwise. * testsuite/29_atomics/atomic_flag/wait_notify/1.cc: Likewise. * testsuite/29_atomics/atomic_float/wait_notify.cc: Likewise. * testsuite/29_atomics/atomic_integral/wait_notify.cc: Likewise. * testsuite/29_atomics/atomic_ref/wait_notify.cc: Likewise. * testsuite/30_threads/semaphore/1.cc: New test. * testsuite/30_threads/semaphore/2.cc: Likewise. * testsuite/30_threads/semaphore/least_max_value_neg.cc: Likewise. * testsuite/30_threads/semaphore/try_acquire.cc: Likewise. * testsuite/30_threads/semaphore/try_acquire_for.cc: Likewise. * testsuite/30_threads/semaphore/try_acquire_posix.cc: Likewise. * testsuite/30_threads/semaphore/try_acquire_until.cc: Likewise. * testsuite/30_threads/latch/1.cc: New test. * testsuite/30_threads/latch/2.cc: New test. * testsuite/30_threads/latch/3.cc: New test. * testsuite/util/atomic/wait_notify_util.h: New File.
161 lines
3.6 KiB
C++
161 lines
3.6 KiB
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.
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
// with this library; see the file COPYING3. If not see
|
|
// <http://www.gnu.org/licenses/>.
|
|
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <condition_variable>
|
|
#include <concepts>
|
|
#include <mutex>
|
|
#include <thread>
|
|
|
|
#include <testsuite_hooks.h>
|
|
|
|
#include <iostream>
|
|
|
|
template<typename Tp>
|
|
Tp check_wait_notify(Tp val1, Tp val2)
|
|
requires std::equality_comparable<Tp>
|
|
{
|
|
using namespace std::literals::chrono_literals;
|
|
|
|
std::mutex m;
|
|
std::condition_variable cv;
|
|
|
|
std::atomic<Tp> a(val1);
|
|
std::thread t([&]
|
|
{
|
|
cv.notify_one();
|
|
a.wait(val1);
|
|
if (a.load() != val2)
|
|
a = val1;
|
|
});
|
|
std::unique_lock<std::mutex> l(m);
|
|
cv.wait(l);
|
|
std::this_thread::sleep_for(100ms);
|
|
a.store(val2);
|
|
a.notify_one();
|
|
t.join();
|
|
return a.load();
|
|
}
|
|
|
|
template<typename Tp>
|
|
Tp check_wait_notify(Tp val1, Tp val2)
|
|
{
|
|
using namespace std::literals::chrono_literals;
|
|
|
|
std::mutex m;
|
|
std::condition_variable cv;
|
|
|
|
std::atomic<Tp> a(val1);
|
|
std::thread t([&]
|
|
{
|
|
cv.notify_one();
|
|
a.wait(val1);
|
|
auto v = a.load();
|
|
// TODO this needs to zero padding bits when we can do that
|
|
if (__builtin_memcmp(&v, &val2, sizeof(Tp)) != 0)
|
|
a = val1;
|
|
});
|
|
std::unique_lock<std::mutex> l(m);
|
|
cv.wait(l);
|
|
std::this_thread::sleep_for(100ms);
|
|
a.store(val2);
|
|
a.notify_one();
|
|
t.join();
|
|
return a.load();
|
|
}
|
|
|
|
template<typename Tp>
|
|
Tp check_atomic_wait_notify(Tp val1, Tp val2)
|
|
requires std::equality_comparable<Tp>
|
|
{
|
|
using namespace std::literals::chrono_literals;
|
|
|
|
std::mutex m;
|
|
std::condition_variable cv;
|
|
|
|
std::atomic<Tp> a(val1);
|
|
std::thread t([&]
|
|
{
|
|
cv.notify_one();
|
|
std::atomic_wait(&a, val1);
|
|
if (a.load() != val2)
|
|
a = val1;
|
|
});
|
|
std::unique_lock<std::mutex> l(m);
|
|
cv.wait(l);
|
|
std::this_thread::sleep_for(100ms);
|
|
a.store(val2);
|
|
std::atomic_notify_one(&a);
|
|
t.join();
|
|
return a.load();
|
|
}
|
|
|
|
template<typename Tp>
|
|
Tp check_atomic_wait_notify(Tp val1, Tp val2)
|
|
{
|
|
using namespace std::literals::chrono_literals;
|
|
|
|
std::mutex m;
|
|
std::condition_variable cv;
|
|
|
|
std::atomic<Tp> a(val1);
|
|
std::thread t([&]
|
|
{
|
|
cv.notify_one();
|
|
std::atomic_wait(&a, val1);
|
|
auto v = a.load();
|
|
// TODO this needs to zero padding bits when we can do that
|
|
if (__builtin_memcmp(&v, &val2, sizeof(Tp)) != 0)
|
|
a = val1;
|
|
});
|
|
std::unique_lock<std::mutex> l(m);
|
|
cv.wait(l);
|
|
std::this_thread::sleep_for(100ms);
|
|
a.store(val2);
|
|
std::atomic_notify_one(&a);
|
|
t.join();
|
|
return a.load();
|
|
}
|
|
|
|
template<typename Tp>
|
|
struct check
|
|
{
|
|
check(Tp a = 0, Tp b = 42)
|
|
{
|
|
if constexpr (std::equality_comparable<Tp>)
|
|
{
|
|
VERIFY( check_wait_notify(a, b) == b);
|
|
VERIFY( check_atomic_wait_notify(a, b) == b);
|
|
}
|
|
else
|
|
{
|
|
{
|
|
// TODO this needs to zero padding bits when we can do that
|
|
auto v = check_wait_notify(a, b);
|
|
VERIFY( __builtin_memcmp(&v, &b, sizeof(Tp)) == 0 );
|
|
}
|
|
|
|
{
|
|
// TODO this needs to zero padding bits when we can do that
|
|
auto v = check_atomic_wait_notify(a, b);
|
|
VERIFY( __builtin_memcmp(&v, &b, sizeof(Tp)) == 0);
|
|
}
|
|
}
|
|
}
|
|
};
|