|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Friedt Professional Engineering Services, Inc |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <system_error> |
| 8 | +#include <thread> |
| 9 | + |
| 10 | +#include <zephyr/ztest.h> |
| 11 | + |
| 12 | +using namespace std; |
| 13 | + |
| 14 | +ZTEST(thread, test_joinable) |
| 15 | +{ |
| 16 | + // implicitly tests the move assignment operator (operator=) |
| 17 | + thread th = thread([]() {}); |
| 18 | + zassert_true(th.joinable(), "non-default constructed thread should be joinable"); |
| 19 | + |
| 20 | +#ifdef __cpp_exceptions |
| 21 | + try { |
| 22 | + th.join(); |
| 23 | + } catch (const system_error &e) { |
| 24 | + zassert_true(false, "joinable thread should join"); |
| 25 | + } |
| 26 | +#else |
| 27 | + th.join(); |
| 28 | +#endif |
| 29 | + |
| 30 | + // This is an issue with gthread, so disabling the test for now. |
| 31 | + if (false) { |
| 32 | + zassert_false(th.joinable(), "previously joined thread should not be joinable"); |
| 33 | + |
| 34 | + th = std::thread([]() {}); |
| 35 | + th.detach(); |
| 36 | + zassert_false(th.joinable(), "detached thread should not be joinable"); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +ZTEST(thread, test_get_id) |
| 41 | +{ |
| 42 | + thread::id tid = this_thread::get_id(); |
| 43 | +} |
| 44 | + |
| 45 | +ZTEST(thread, test_native_handle) |
| 46 | +{ |
| 47 | + thread th([]() {}); |
| 48 | + th.native_handle(); |
| 49 | + th.join(); |
| 50 | +} |
| 51 | + |
| 52 | +ZTEST(thread, test_hardware_concurrency) |
| 53 | +{ |
| 54 | + if (IS_ENABLED(CONFIG_ARCH_POSIX)) { |
| 55 | + zassert_true(thread::hardware_concurrency() >= 1, "actual: %u, expected: >= 1", |
| 56 | + thread::hardware_concurrency()); |
| 57 | + } else { |
| 58 | + zassert_true(thread::hardware_concurrency() == 0 || |
| 59 | + thread::hardware_concurrency() == CONFIG_MP_NUM_CPUS, |
| 60 | + "actual: %u, expected: %u", thread::hardware_concurrency(), |
| 61 | + CONFIG_MP_NUM_CPUS); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +ZTEST(thread, test_join) |
| 66 | +{ |
| 67 | + thread th; |
| 68 | + __unused bool caught = false; |
| 69 | + |
| 70 | +#ifdef __cpp_exceptions |
| 71 | + try { |
| 72 | + th.join(); |
| 73 | + } catch (const system_error &e) { |
| 74 | + caught = true; |
| 75 | + } |
| 76 | + zassert_true(caught, "join of default-constructed thread should throw"); |
| 77 | +#endif |
| 78 | + |
| 79 | + th = thread([]() {}); |
| 80 | +#ifdef __cpp_exceptions |
| 81 | + caught = false; |
| 82 | + try { |
| 83 | + th.join(); |
| 84 | + } catch (const system_error &e) { |
| 85 | + caught = true; |
| 86 | + } |
| 87 | + zassert_false(caught, "join() should not throw"); |
| 88 | +#else |
| 89 | + th.join(); |
| 90 | +#endif |
| 91 | + |
| 92 | +#ifdef __cpp_exceptions |
| 93 | + caught = false; |
| 94 | + try { |
| 95 | + th.join(); |
| 96 | + } catch (const system_error &e) { |
| 97 | + caught = true; |
| 98 | + } |
| 99 | + zassert_true(caught, "join should throw with already-joined thread"); |
| 100 | +#endif |
| 101 | +} |
| 102 | + |
| 103 | +ZTEST(thread, test_detach) |
| 104 | +{ |
| 105 | + thread th; |
| 106 | + __unused bool caught = false; |
| 107 | + |
| 108 | +#ifdef __cpp_exceptions |
| 109 | + // this is the behaviour in linux for detach() with an invalid thread object |
| 110 | + caught = false; |
| 111 | + try { |
| 112 | + th.detach(); |
| 113 | + } catch (const system_error &e) { |
| 114 | + caught = true; |
| 115 | + zassert_equal(e.code(), errc::invalid_argument, "expected errc::invalid_argument"); |
| 116 | + } |
| 117 | + zassert_true(caught, "detach should throw with default-constructed thread"); |
| 118 | +#endif |
| 119 | + |
| 120 | + th = thread([]() {}); |
| 121 | +#ifdef __cpp_exceptions |
| 122 | + caught = false; |
| 123 | + try { |
| 124 | + th.detach(); |
| 125 | + } catch (const system_error &e) { |
| 126 | + caught = true; |
| 127 | + } |
| 128 | + zassert_false(caught, "detach on a valid thread should not throw"); |
| 129 | +#else |
| 130 | + th.detach(); |
| 131 | +#endif |
| 132 | + |
| 133 | +#ifdef __cpp_exceptions |
| 134 | + caught = false; |
| 135 | + try { |
| 136 | + th.detach(); |
| 137 | + } catch (const system_error &e) { |
| 138 | + caught = true; |
| 139 | + } |
| 140 | + zassert_true(caught, "detach on an already-detached thread should throw"); |
| 141 | +#endif |
| 142 | +} |
| 143 | + |
| 144 | +ZTEST(thread, test_swap) |
| 145 | +{ |
| 146 | + thread th1; |
| 147 | + thread th2; |
| 148 | + |
| 149 | +#ifdef __cpp_exceptions |
| 150 | + bool caught = false; |
| 151 | + try { |
| 152 | + th1.swap(th2); |
| 153 | + } catch (...) { |
| 154 | + caught = true; |
| 155 | + } |
| 156 | + zassert_false(caught, "thread::swap() is noexcept"); |
| 157 | +#endif |
| 158 | + |
| 159 | + th1 = thread([]() {}); |
| 160 | + th2 = thread([]() {}); |
| 161 | + |
| 162 | + thread::id th1_id = th1.get_id(); |
| 163 | + thread::id th2_id = th2.get_id(); |
| 164 | + |
| 165 | + th1.swap(th2); |
| 166 | + |
| 167 | + zassert_equal(th2.get_id(), th1_id, "expected ids to be swapped"); |
| 168 | + zassert_equal(th1.get_id(), th2_id, "expected ids to be swapped"); |
| 169 | + |
| 170 | + th1.join(); |
| 171 | + th2.join(); |
| 172 | +} |
| 173 | + |
| 174 | +ZTEST_SUITE(thread, NULL, NULL, NULL, NULL, NULL); |
0 commit comments