Debian Codegraph @

libcxx/test/std/thread/thread.stoptoken/stopsource/get_token.pass.cpp

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