| 1 | // Copyright (C) 2019-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-add-options libatomic } |
| 19 | // { dg-do run { target c++20 } } |
| 20 | |
| 21 | #include <stop_token> |
| 22 | #include <functional> |
| 23 | #include <testsuite_hooks.h> |
| 24 | |
| 25 | void |
| 26 | test01() |
| 27 | { |
| 28 | bool called = false; |
| 29 | std::function<void()> f = [&called]{ called = true; }; |
| 30 | std::stop_source ssrc; |
| 31 | ssrc.request_stop(); |
| 32 | std::stop_token tok = ssrc.get_token(); |
| 33 | std::stop_callback cb1(tok, f); |
| 34 | VERIFY( tok.stop_possible() ); |
| 35 | VERIFY( f != nullptr ); |
| 36 | VERIFY( called == true ); |
| 37 | |
| 38 | called = false; |
| 39 | std::stop_callback cb2(std::move(tok), f); |
| 40 | // when callback is executed immediately, no change in ownership: |
| 41 | VERIFY( tok.stop_possible() ); |
| 42 | VERIFY( f != nullptr ); |
| 43 | VERIFY( called == true ); |
| 44 | |
| 45 | std::stop_token sink(std::move(tok)); // leave tok empty |
| 46 | |
| 47 | called = false; |
| 48 | std::stop_callback cb3(tok, f); |
| 49 | VERIFY( f != nullptr ); |
| 50 | VERIFY( called == false ); |
| 51 | |
| 52 | called = false; |
| 53 | std::stop_callback cb4(std::move(tok), f); |
| 54 | VERIFY( f != nullptr ); |
| 55 | VERIFY( called == false ); |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | test02() |
| 60 | { |
| 61 | bool called = false; |
| 62 | std::function<void()> f0 = [&called]{ called = true; }; |
| 63 | std::function<void()> f = f0; |
| 64 | std::stop_source ssrc; |
| 65 | ssrc.request_stop(); |
| 66 | std::stop_token tok = ssrc.get_token(); |
| 67 | |
| 68 | std::stop_callback cb1(tok, std::move(f)); |
| 69 | VERIFY( tok.stop_possible() ); |
| 70 | VERIFY( f == nullptr ); |
| 71 | VERIFY( called == true ); |
| 72 | |
| 73 | called = false; |
| 74 | f = f0; |
| 75 | std::stop_callback cb2(std::move(tok), std::move(f)); |
| 76 | // when callback is executed immediately, no change in ownership: |
| 77 | VERIFY( tok.stop_possible() ); |
| 78 | VERIFY( f == nullptr ); |
| 79 | VERIFY( called == true ); |
| 80 | |
| 81 | std::stop_token sink(std::move(tok)); // leave tok empty |
| 82 | |
| 83 | called = false; |
| 84 | f = f0; |
| 85 | std::stop_callback cb3(tok, std::move(f)); |
| 86 | VERIFY( f == nullptr ); |
| 87 | VERIFY( called == false ); |
| 88 | |
| 89 | called = false; |
| 90 | f = f0; |
| 91 | std::stop_callback cb4(std::move(tok), std::move(f)); |
| 92 | VERIFY( f == nullptr ); |
| 93 | VERIFY( called == false ); |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | test03() |
| 98 | { |
| 99 | bool called[4] = { }; |
| 100 | std::stop_source ssrc; |
| 101 | std::stop_token tok = ssrc.get_token(); |
| 102 | std::stop_callback cb1(tok, [&]{ called[0] = true; }); |
| 103 | VERIFY( tok.stop_possible() ); |
| 104 | VERIFY( called[0] == false ); |
| 105 | |
| 106 | std::stop_callback cb2(std::move(tok), [&]{ called[1] = true; }); |
| 107 | VERIFY( !tok.stop_possible() ); |
| 108 | VERIFY( called[1] == false ); |
| 109 | |
| 110 | std::stop_callback cb3(tok, [&]{ called[2] = true; }); |
| 111 | VERIFY( called[2] == false ); |
| 112 | |
| 113 | std::stop_callback cb4(std::move(tok), [&]{ called[3] = true; }); |
| 114 | VERIFY( called[3] == false ); |
| 115 | |
| 116 | ssrc.request_stop(); |
| 117 | VERIFY( called[0] == true ); |
| 118 | VERIFY( called[1] == true ); |
| 119 | VERIFY( called[2] == false ); |
| 120 | VERIFY( called[3] == false ); |
| 121 | } |
| 122 | |
| 123 | int main() |
| 124 | { |
| 125 | test01(); |
| 126 | test02(); |
| 127 | test03(); |
| 128 | } |