| 12
 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
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 
 | //===----------------------------------------------------------------------===//
//
// 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
// UNSUPPORTED: c++03, c++11, c++14, c++17
// XFAIL: availability-synchronization_library-missing
// <condition_variable>
// class condition_variable_any;
// template<class Lock, class Rep, class Period, class Predicate>
//   bool wait_for(Lock& lock, stop_token stoken,
//                 const chrono::duration<Rep, Period>& rel_time, Predicate pred);
#include <atomic>
#include <cassert>
#include <chrono>
#include <concepts>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <shared_mutex>
#include <stop_token>
#include <thread>
#include "helpers.h"
#include "make_test_thread.h"
#include "test_macros.h"
template <class Mutex, class Lock>
void test() {
  using namespace std::chrono_literals;
  // stop_requested before hand
  {
    std::stop_source ss;
    std::condition_variable_any cv;
    Mutex mutex;
    Lock lock{mutex};
    ss.request_stop();
    ElapsedTimeCheck check(1min);
    // [Note 4: The returned value indicates whether the predicate evaluated to true
    // regardless of whether the timeout was triggered or a stop request was made.]
    std::same_as<bool> auto r1 = cv.wait_for(lock, ss.get_token(), -1h, []() { return false; });
    assert(!r1);
    std::same_as<bool> auto r2 = cv.wait_for(lock, ss.get_token(), 1h, []() { return false; });
    assert(!r2);
    std::same_as<bool> auto r3 = cv.wait_for(lock, ss.get_token(), -1h, []() { return true; });
    assert(r3);
    std::same_as<bool> auto r4 = cv.wait_for(lock, ss.get_token(), 1h, []() { return true; });
    assert(r4);
    // Postconditions: lock is locked by the calling thread.
    assert(lock.owns_lock());
  }
  // no stop request, pred was true
  {
    std::stop_source ss;
    std::condition_variable_any cv;
    Mutex mutex;
    Lock lock{mutex};
    ElapsedTimeCheck check(1min);
    std::same_as<bool> auto r1 = cv.wait_for(lock, ss.get_token(), -1h, []() { return true; });
    assert(r1);
    std::same_as<bool> auto r2 = cv.wait_for(lock, ss.get_token(), 1h, []() { return true; });
    assert(r2);
  }
  // no stop request, pred was false, abs_time was in the past
  {
    std::stop_source ss;
    std::condition_variable_any cv;
    Mutex mutex;
    Lock lock{mutex};
    ElapsedTimeCheck check(1min);
    std::same_as<bool> auto r1 = cv.wait_for(lock, ss.get_token(), -1h, []() { return false; });
    assert(!r1);
  }
  // no stop request, pred was false until timeout
  {
    std::stop_source ss;
    std::condition_variable_any cv;
    Mutex mutex;
    Lock lock{mutex};
    auto old_time = std::chrono::steady_clock::now();
    std::same_as<bool> auto r1 = cv.wait_for(lock, ss.get_token(), 2ms, [&]() { return false; });
    assert((std::chrono::steady_clock::now() - old_time) >= 2ms);
    assert(!r1);
  }
  // no stop request, pred was false, changed to true before timeout
  {
    std::stop_source ss;
    std::condition_variable_any cv;
    Mutex mutex;
    Lock lock{mutex};
    bool flag   = false;
    auto thread = support::make_test_thread([&]() {
      std::this_thread::sleep_for(2ms);
      std::unique_lock<Mutex> lock2{mutex};
      flag = true;
      cv.notify_all();
    });
    ElapsedTimeCheck check(10min);
    std::same_as<bool> auto r1 = cv.wait_for(lock, ss.get_token(), 1h, [&]() { return flag; });
    assert(flag);
    assert(r1);
    thread.join();
  }
  // stop request comes while waiting
  {
    std::stop_source ss;
    std::condition_variable_any cv;
    Mutex mutex;
    Lock lock{mutex};
    std::atomic_bool start = false;
    std::atomic_bool done  = false;
    auto thread            = support::make_test_thread([&]() {
      start.wait(false);
      ss.request_stop();
      while (!done) {
        cv.notify_all();
        std::this_thread::sleep_for(2ms);
      }
    });
    ElapsedTimeCheck check(10min);
    std::same_as<bool> auto r = cv.wait_for(lock, ss.get_token(), 1h, [&]() {
      start.store(true);
      start.notify_all();
      return false;
    });
    assert(!r);
    done = true;
    thread.join();
    assert(lock.owns_lock());
  }
  // #76807 Hangs in std::condition_variable_any when used with std::stop_token
  {
    class MyThread {
    public:
      MyThread() {
        thread_ = support::make_test_jthread([this](std::stop_token st) {
          while (!st.stop_requested()) {
            std::unique_lock lock{m_};
            cv_.wait_for(lock, st, 1h, [] { return false; });
          }
        });
      }
    private:
      std::mutex m_;
      std::condition_variable_any cv_;
      std::jthread thread_;
    };
    ElapsedTimeCheck check(10min);
    [[maybe_unused]] MyThread my_thread;
  }
  // request_stop potentially in-between check and wait
  {
    std::stop_source ss;
    std::condition_variable_any cv;
    Mutex mutex;
    Lock lock{mutex};
    std::atomic_bool pred_started        = false;
    std::atomic_bool request_stop_called = false;
    auto thread                          = support::make_test_thread([&]() {
      pred_started.wait(false);
      ss.request_stop();
      request_stop_called.store(true);
      request_stop_called.notify_all();
    });
    ElapsedTimeCheck check(10min);
    std::same_as<bool> auto r = cv.wait_for(lock, ss.get_token(), 1h, [&]() {
      pred_started.store(true);
      pred_started.notify_all();
      request_stop_called.wait(false);
      return false;
    });
    assert(!r);
    thread.join();
    assert(lock.owns_lock());
  }
#if !defined(TEST_HAS_NO_EXCEPTIONS)
  // Throws: Any exception thrown by pred.
  {
    std::stop_source ss;
    std::condition_variable_any cv;
    Mutex mutex;
    Lock lock{mutex};
    try {
      ElapsedTimeCheck check(10min);
      cv.wait_for(lock, ss.get_token(), 1h, []() -> bool { throw 5; });
      assert(false);
    } catch (int i) {
      assert(i == 5);
    }
  }
#endif //!defined(TEST_HAS_NO_EXCEPTIONS)
}
int main(int, char**) {
  test<std::mutex, std::unique_lock<std::mutex>>();
  test<std::shared_mutex, std::shared_lock<std::shared_mutex>>();
  return 0;
}
 |