xref: /llvm-project/libcxx/test/std/thread/thread.condition/thread.condition.condvarany/helpers.h (revision 85a8e5c3e0586e85a2fa3ff9cef12455bd039921)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef TEST_STD_THREAD_CONDITION_CONDVARANY_HELPERS_H
10 #define TEST_STD_THREAD_CONDITION_CONDVARANY_HELPERS_H
11 
12 #include <chrono>
13 #include <cassert>
14 
15 #include "test_macros.h"
16 
17 #if TEST_STD_VER >= 17
18 
19 // wait_for and wait_until function can exit via
20 // - predicate is true
21 // - timeout
22 // - stop_requested
23 // The return value only tells if the predicate is true
24 // when the function exits, but it does not tell whether
25 // it is timeout or stop_requested.
26 //
27 // ElapsedTimeCheck would fail the test if a test takes
28 // longer than a duration. This is useful because we can
29 // ensure that the wait_{for, until} function does not
30 // wait until the timeout
31 struct ElapsedTimeCheck {
ElapsedTimeCheckElapsedTimeCheck32   ElapsedTimeCheck(std::chrono::steady_clock::duration timeoutDuration)
33       : timeout_(std::chrono::steady_clock::now() + timeoutDuration) {}
34 
35   ElapsedTimeCheck(ElapsedTimeCheck&&)            = delete;
36   ElapsedTimeCheck& operator=(ElapsedTimeCheck&&) = delete;
37 
~ElapsedTimeCheckElapsedTimeCheck38   ~ElapsedTimeCheck() { assert(std::chrono::steady_clock::now() < timeout_); }
39 
40   std::chrono::time_point<std::chrono::steady_clock> timeout_;
41 };
42 
43 #endif
44 
45 #endif // TEST_STD_THREAD_CONDITION_CONDVARANY_HELPERS_H
46