| 1 | // { dg-do run } |
| 2 | // { dg-additional-options "-pthread" { target pthread } } |
| 3 | // { dg-require-effective-target c++11 } |
| 4 | // { dg-require-gthreads "" } |
| 5 | |
| 6 | // Copyright (C) 2014-2025 Free Software Foundation, Inc. |
| 7 | // |
| 8 | // This file is part of the GNU ISO C++ Library. This library is free |
| 9 | // software; you can redistribute it and/or modify it under the |
| 10 | // terms of the GNU General Public License as published by the |
| 11 | // Free Software Foundation; either version 3, or (at your option) |
| 12 | // any later version. |
| 13 | |
| 14 | // This library is distributed in the hope that it will be useful, |
| 15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | // GNU General Public License for more details. |
| 18 | |
| 19 | // You should have received a copy of the GNU General Public License along |
| 20 | // with this library; see the file COPYING3. If not see |
| 21 | // <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | // Test set_value_at_thread_exit error conditions |
| 24 | |
| 25 | #include <future> |
| 26 | #include <testsuite_hooks.h> |
| 27 | |
| 28 | void test01() |
| 29 | { |
| 30 | std::promise<int> p1; |
| 31 | p1.set_value(1); |
| 32 | try |
| 33 | { |
| 34 | p1.set_value_at_thread_exit(2); |
| 35 | VERIFY( false ); |
| 36 | } |
| 37 | catch (std::future_error& e) |
| 38 | { |
| 39 | VERIFY( e.code() == std::future_errc::promise_already_satisfied ); |
| 40 | } |
| 41 | try |
| 42 | { |
| 43 | p1.set_exception_at_thread_exit(std::make_exception_ptr(3)); |
| 44 | VERIFY( false ); |
| 45 | } |
| 46 | catch (std::future_error& e) |
| 47 | { |
| 48 | VERIFY( e.code() == std::future_errc::promise_already_satisfied ); |
| 49 | } |
| 50 | |
| 51 | std::promise<int> p2(std::move(p1)); |
| 52 | try |
| 53 | { |
| 54 | p1.set_value_at_thread_exit(2); |
| 55 | VERIFY( false ); |
| 56 | } |
| 57 | catch (std::future_error& e) |
| 58 | { |
| 59 | VERIFY( e.code() == std::future_errc::no_state ); |
| 60 | } |
| 61 | try |
| 62 | { |
| 63 | p1.set_exception_at_thread_exit(std::make_exception_ptr(3)); |
| 64 | VERIFY( false ); |
| 65 | } |
| 66 | catch (std::future_error& e) |
| 67 | { |
| 68 | VERIFY( e.code() == std::future_errc::no_state ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void test02() |
| 73 | { |
| 74 | std::promise<int&> p1; |
| 75 | int i = 1; |
| 76 | p1.set_value(i); |
| 77 | try |
| 78 | { |
| 79 | p1.set_value_at_thread_exit(i); |
| 80 | VERIFY( false ); |
| 81 | } |
| 82 | catch (std::future_error& e) |
| 83 | { |
| 84 | VERIFY( e.code() == std::future_errc::promise_already_satisfied ); |
| 85 | } |
| 86 | try |
| 87 | { |
| 88 | p1.set_exception_at_thread_exit(std::make_exception_ptr(3)); |
| 89 | VERIFY( false ); |
| 90 | } |
| 91 | catch (std::future_error& e) |
| 92 | { |
| 93 | VERIFY( e.code() == std::future_errc::promise_already_satisfied ); |
| 94 | } |
| 95 | |
| 96 | std::promise<int&> p2(std::move(p1)); |
| 97 | try |
| 98 | { |
| 99 | int i = 0; |
| 100 | p1.set_value_at_thread_exit(i); |
| 101 | VERIFY( false ); |
| 102 | } |
| 103 | catch (std::future_error& e) |
| 104 | { |
| 105 | VERIFY( e.code() == std::future_errc::no_state ); |
| 106 | } |
| 107 | try |
| 108 | { |
| 109 | p1.set_exception_at_thread_exit(std::make_exception_ptr(3)); |
| 110 | VERIFY( false ); |
| 111 | } |
| 112 | catch (std::future_error& e) |
| 113 | { |
| 114 | VERIFY( e.code() == std::future_errc::no_state ); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void test03() |
| 119 | { |
| 120 | std::promise<void> p1; |
| 121 | p1.set_value(); |
| 122 | try { |
| 123 | p1.set_value_at_thread_exit(); |
| 124 | VERIFY( false ); |
| 125 | } |
| 126 | catch (std::future_error& e) |
| 127 | { |
| 128 | VERIFY( e.code() == std::future_errc::promise_already_satisfied ); |
| 129 | } |
| 130 | try |
| 131 | { |
| 132 | p1.set_exception_at_thread_exit(std::make_exception_ptr(3)); |
| 133 | VERIFY( false ); |
| 134 | } |
| 135 | catch (std::future_error& e) |
| 136 | { |
| 137 | VERIFY( e.code() == std::future_errc::promise_already_satisfied ); |
| 138 | } |
| 139 | |
| 140 | std::promise<void> p2(std::move(p1)); |
| 141 | try { |
| 142 | p1.set_value_at_thread_exit(); |
| 143 | VERIFY( false ); |
| 144 | } |
| 145 | catch (std::future_error& e) |
| 146 | { |
| 147 | VERIFY( e.code() == std::future_errc::no_state ); |
| 148 | } |
| 149 | try |
| 150 | { |
| 151 | p1.set_exception_at_thread_exit(std::make_exception_ptr(3)); |
| 152 | VERIFY( false ); |
| 153 | } |
| 154 | catch (std::future_error& e) |
| 155 | { |
| 156 | VERIFY( e.code() == std::future_errc::no_state ); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | int main() |
| 161 | { |
| 162 | test01(); |
| 163 | test02(); |
| 164 | test03(); |
| 165 | } |