xref: /llvm-project/libcxx/test/std/thread/thread.stoptoken/stopsource/get_token.pass.cpp (revision 121ed5c1985356436d0040dbe81bca26992b1fae)
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 // UNSUPPORTED: no-threads
10 // UNSUPPORTED: c++03, c++11, c++14, c++17
11 // XFAIL: availability-synchronization_library-missing
12 
13 // [[nodiscard]] stop_token get_token() const noexcept;
14 
15 #include <cassert>
16 #include <concepts>
17 #include <stop_token>
18 #include <type_traits>
19 
20 #include "test_macros.h"
21 
22 template <class T>
23 concept IsGetTokenNoexcept = requires(const T& t) {
24   { t.get_token() } noexcept;
25 };
26 
27 static_assert(IsGetTokenNoexcept<std::stop_source>);
28 
29 int main(int, char**) {
30   // no state
31   {
32     std::stop_source ss{std::nostopstate};
33     std::same_as<std::stop_token> decltype(auto) st = ss.get_token();
34     assert(!st.stop_possible());
35     assert(!st.stop_requested());
36   }
37 
38   // with state
39   {
40     std::stop_source ss;
41     std::same_as<std::stop_token> decltype(auto) st = ss.get_token();
42     assert(st.stop_possible());
43     assert(!st.stop_requested());
44 
45     ss.request_stop();
46     assert(st.stop_requested());
47   }
48 
49   return 0;
50 }
51