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// bool request_stop() noexcept;
14
15#include <cassert>
16#include <chrono>
17#include <concepts>
18#include <optional>
19#include <stop_token>
20#include <type_traits>
21
22#include "make_test_thread.h"
23#include "test_macros.h"
24
25template <class T>
26concept IsRequestStopNoexcept = requires(T& t) {
27 { t.request_stop() } noexcept;
28};
29
30static_assert(IsRequestStopNoexcept<std::stop_source>);
31
32int main(int, char**) {
33 // If *this does not have ownership of a stop state, returns false
34 {
35 std::stop_source ss{std::nostopstate};
36 auto ret = ss.request_stop();
37 assert(!ret);
38 assert(!ss.stop_requested());
39 }
40
41 // Otherwise, atomically determines whether the owned stop state has received
42 // a stop request, and if not, makes a stop request
43 {
44 std::stop_source ss;
45
46 auto ret = ss.request_stop();
47 assert(ret);
48 assert(ss.stop_requested());
49 }
50
51 // already requested
52 {
53 std::stop_source ss;
54 ss.request_stop();
55 assert(ss.stop_requested());
56
57 auto ret = ss.request_stop();
58 assert(!ret);
59 assert(ss.stop_requested());
60 }
61
62 // If the request was made, the callbacks registered by
63 // associated stop_callback objects are synchronously called.
64 {
65 std::stop_source ss;
66 auto st = ss.get_token();
67
68 bool cb1Called = false;
69 bool cb2Called = false;
70 std::stop_callback sc1(st, [&] { cb1Called = true; });
71 std::stop_callback sc2(st, [&] { cb2Called = true; });
72
73 ss.request_stop();
74 assert(cb1Called);
75 assert(cb2Called);
76 }
77
78 return 0;
79}
80

source code of libcxx/test/std/thread/thread.stoptoken/stopsource/request_stop.pass.cpp