xref: /llvm-project/libcxx/test/std/thread/thread.stoptoken/stopsource/get_token.pass.cpp (revision 121ed5c1985356436d0040dbe81bca26992b1fae)
1*b77e50e6SHui //===----------------------------------------------------------------------===//
2*b77e50e6SHui //
3*b77e50e6SHui // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*b77e50e6SHui // See https://llvm.org/LICENSE.txt for license information.
5*b77e50e6SHui // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*b77e50e6SHui //
7*b77e50e6SHui //===----------------------------------------------------------------------===//
8*b77e50e6SHui //
9*b77e50e6SHui // UNSUPPORTED: no-threads
10*b77e50e6SHui // UNSUPPORTED: c++03, c++11, c++14, c++17
11*b77e50e6SHui // XFAIL: availability-synchronization_library-missing
12*b77e50e6SHui 
13*b77e50e6SHui // [[nodiscard]] stop_token get_token() const noexcept;
14*b77e50e6SHui 
15*b77e50e6SHui #include <cassert>
16*b77e50e6SHui #include <concepts>
17*b77e50e6SHui #include <stop_token>
18*b77e50e6SHui #include <type_traits>
19*b77e50e6SHui 
20*b77e50e6SHui #include "test_macros.h"
21*b77e50e6SHui 
22*b77e50e6SHui template <class T>
23*b77e50e6SHui concept IsGetTokenNoexcept = requires(const T& t) {
24*b77e50e6SHui   { t.get_token() } noexcept;
25*b77e50e6SHui };
26*b77e50e6SHui 
27*b77e50e6SHui static_assert(IsGetTokenNoexcept<std::stop_source>);
28*b77e50e6SHui 
29*b77e50e6SHui int main(int, char**) {
30*b77e50e6SHui   // no state
31*b77e50e6SHui   {
32*b77e50e6SHui     std::stop_source ss{std::nostopstate};
33*b77e50e6SHui     std::same_as<std::stop_token> decltype(auto) st = ss.get_token();
34*b77e50e6SHui     assert(!st.stop_possible());
35*b77e50e6SHui     assert(!st.stop_requested());
36*b77e50e6SHui   }
37*b77e50e6SHui 
38*b77e50e6SHui   // with state
39*b77e50e6SHui   {
40*b77e50e6SHui     std::stop_source ss;
41*b77e50e6SHui     std::same_as<std::stop_token> decltype(auto) st = ss.get_token();
42*b77e50e6SHui     assert(st.stop_possible());
43*b77e50e6SHui     assert(!st.stop_requested());
44*b77e50e6SHui 
45*b77e50e6SHui     ss.request_stop();
46*b77e50e6SHui     assert(st.stop_requested());
47*b77e50e6SHui   }
48*b77e50e6SHui 
49*b77e50e6SHui   return 0;
50*b77e50e6SHui }
51