| 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
 
 | //===----------------------------------------------------------------------===//
//
// 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 Predicate>
//   bool wait(Lock& lock, stop_token stoken, Predicate pred);
#include <atomic>
#include <cassert>
#include <concepts>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <shared_mutex>
#include <stop_token>
#include <thread>
#include "make_test_thread.h"
#include "test_macros.h"
template <class Mutex, class Lock>
void test() {
  // stop_requested before hand
  {
    std::stop_source ss;
    std::condition_variable_any cv;
    Mutex mutex;
    Lock lock{mutex};
    ss.request_stop();
    // [Note 1: The returned value indicates whether the predicate evaluated to true regardless of whether there was a stop request.]
    std::same_as<bool> auto r1 = cv.wait(lock, ss.get_token(), []() { return false; });
    assert(!r1);
    std::same_as<bool> auto r2 = cv.wait(lock, ss.get_token(), []() { return true; });
    assert(r2);
    // Postconditions: lock is locked by the calling thread.
    assert(lock.owns_lock());
  }
  // no stop request
  {
    std::stop_source ss;
    std::condition_variable_any cv;
    Mutex mutex;
    Lock lock{mutex};
    std::same_as<bool> auto r1 = cv.wait(lock, ss.get_token(), []() { return true; });
    assert(r1);
    bool flag   = false;
    auto thread = support::make_test_thread([&]() {
      std::this_thread::sleep_for(std::chrono::milliseconds(2));
      std::unique_lock<Mutex> lock2{mutex};
      flag = true;
      cv.notify_all();
    });
    std::same_as<bool> auto r2 = cv.wait(lock, ss.get_token(), [&]() { return flag; });
    assert(flag);
    assert(r2);
    thread.join();
    assert(lock.owns_lock());
  }
  // 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(std::chrono::milliseconds(2));
      }
    });
    std::same_as<bool> auto r = cv.wait(lock, ss.get_token(), [&]() {
      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(lock, st, [] { return false; });
          }
        });
      }
    private:
      std::mutex m_;
      std::condition_variable_any cv_;
      std::jthread thread_;
    };
    [[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();
    });
    std::same_as<bool> auto r = cv.wait(lock, ss.get_token(), [&]() {
      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 {
      cv.wait(lock, ss.get_token(), []() -> 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;
}
 |