15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier //
9*a7f9895cSLouis Dionne // UNSUPPORTED: no-threads
105a83710eSEric Fiselier 
115a83710eSEric Fiselier // <condition_variable>
125a83710eSEric Fiselier 
135a83710eSEric Fiselier // class condition_variable_any;
145a83710eSEric Fiselier 
155a83710eSEric Fiselier // ~condition_variable_any();
165a83710eSEric Fiselier 
175a83710eSEric Fiselier #include <condition_variable>
185a83710eSEric Fiselier #include <mutex>
195a83710eSEric Fiselier #include <thread>
205a83710eSEric Fiselier #include <cassert>
215a83710eSEric Fiselier 
2256462801SLouis Dionne #include "make_test_thread.h"
237fc6a556SMarshall Clow #include "test_macros.h"
247fc6a556SMarshall Clow 
255a83710eSEric Fiselier std::condition_variable_any* cv;
265a83710eSEric Fiselier std::mutex m;
275a83710eSEric Fiselier 
285a83710eSEric Fiselier bool f_ready = false;
295a83710eSEric Fiselier bool g_ready = false;
305a83710eSEric Fiselier 
f()315a83710eSEric Fiselier void f()
325a83710eSEric Fiselier {
335a83710eSEric Fiselier     m.lock();
345a83710eSEric Fiselier     f_ready = true;
355a83710eSEric Fiselier     cv->notify_one();
365a83710eSEric Fiselier     delete cv;
375a83710eSEric Fiselier     m.unlock();
385a83710eSEric Fiselier }
395a83710eSEric Fiselier 
g()405a83710eSEric Fiselier void g()
415a83710eSEric Fiselier {
425a83710eSEric Fiselier     m.lock();
435a83710eSEric Fiselier     g_ready = true;
445a83710eSEric Fiselier     cv->notify_one();
455a83710eSEric Fiselier     while (!f_ready)
465a83710eSEric Fiselier         cv->wait(m);
475a83710eSEric Fiselier     m.unlock();
485a83710eSEric Fiselier }
495a83710eSEric Fiselier 
main(int,char **)502df59c50SJF Bastien int main(int, char**)
515a83710eSEric Fiselier {
525a83710eSEric Fiselier     cv = new std::condition_variable_any;
5356462801SLouis Dionne     std::thread th2 = support::make_test_thread(g);
545a83710eSEric Fiselier     m.lock();
555a83710eSEric Fiselier     while (!g_ready)
565a83710eSEric Fiselier         cv->wait(m);
575a83710eSEric Fiselier     m.unlock();
5856462801SLouis Dionne     std::thread th1 = support::make_test_thread(f);
595a83710eSEric Fiselier     th1.join();
605a83710eSEric Fiselier     th2.join();
612df59c50SJF Bastien 
622df59c50SJF Bastien   return 0;
635a83710eSEric Fiselier }
64