1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: no-threads, c++03
// <condition_variable>
// class condition_variable_any;
// template <class Lock, class Clock, class Duration>
// cv_status
// wait_until(Lock& lock, const chrono::time_point<Clock, Duration>& abs_time);
#include <condition_variable>
#include <atomic>
#include <cassert>
#include <chrono>
#include <mutex>
#include <thread>
#include "make_test_thread.h"
#include "test_macros.h"
struct TestClock {
typedef std::chrono::milliseconds duration;
typedef duration::rep rep;
typedef duration::period period;
typedef std::chrono::time_point<TestClock> time_point;
static const bool is_steady = true;
static time_point now() {
using namespace std::chrono;
return time_point(duration_cast<duration>(steady_clock::now().time_since_epoch()));
}
};
template <class Mutex>
struct MyLock : std::unique_lock<Mutex> {
using std::unique_lock<Mutex>::unique_lock;
};
template <class Lock, class Clock>
void test() {
using Mutex = typename Lock::mutex_type;
// Test unblocking via a call to notify_one() in another thread.
//
// To test this, we set a very long timeout in wait_until() and we wait
// again in case we get awoken spuriously. Note that it can actually
// happen that we get awoken spuriously and fail to recognize it
// (making this test useless), but the likelihood should be small.
{
std::atomic<bool> ready(false);
std::atomic<bool> likely_spurious(true);
auto timeout = Clock::now() + std::chrono::seconds(3600);
std::condition_variable_any cv;
Mutex mutex;
std::thread t1 = support::make_test_thread([&] {
Lock lock(mutex);
ready = true;
do {
std::cv_status result = cv.wait_until(lock, timeout);
assert(result == std::cv_status::no_timeout);
} while (likely_spurious);
// This can technically fail if we have many spurious awakenings, but in practice the
// tolerance is so high that it shouldn't be a problem.
assert(Clock::now() < timeout);
});
std::thread t2 = support::make_test_thread([&] {
while (!ready) {
// spin
}
// Acquire the same mutex as t1. This blocks the condition variable inside its wait call
// so we can notify it while it is waiting.
Lock lock(mutex);
cv.notify_one();
likely_spurious = false;
lock.unlock();
});
t2.join();
t1.join();
}
// Test unblocking via a timeout.
//
// To test this, we create a thread that waits on a condition variable
// with a certain timeout, and we never awaken it. To guard against
// spurious wakeups, we wait again whenever we are awoken for a reason
// other than a timeout.
{
auto timeout = Clock::now() + std::chrono::milliseconds(250);
std::condition_variable_any cv;
Mutex mutex;
std::thread t1 = support::make_test_thread([&] {
Lock lock(mutex);
std::cv_status result;
do {
result = cv.wait_until(lock, timeout);
if (result == std::cv_status::timeout)
assert(Clock::now() >= timeout);
} while (result != std::cv_status::timeout);
});
t1.join();
}
}
int main(int, char**) {
test<std::unique_lock<std::mutex>, TestClock>();
test<std::unique_lock<std::mutex>, std::chrono::steady_clock>();
test<std::unique_lock<std::timed_mutex>, TestClock>();
test<std::unique_lock<std::timed_mutex>, std::chrono::steady_clock>();
test<MyLock<std::mutex>, TestClock>();
test<MyLock<std::mutex>, std::chrono::steady_clock>();
test<MyLock<std::timed_mutex>, TestClock>();
test<MyLock<std::timed_mutex>, std::chrono::steady_clock>();
return 0;
}
|