| 1 | // Copyright (c) 2018-2022, The Monero Project |
| 2 | |
| 3 | // |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without modification, are |
| 7 | // permitted provided that the following conditions are met: |
| 8 | // |
| 9 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 10 | // conditions and the following disclaimer. |
| 11 | // |
| 12 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 13 | // of conditions and the following disclaimer in the documentation and/or other |
| 14 | // materials provided with the distribution. |
| 15 | // |
| 16 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 17 | // used to endorse or promote products derived from this software without specific |
| 18 | // prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 21 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 23 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 27 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 28 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #include <atomic> |
| 31 | #include "gtest/gtest.h" |
| 32 | #include "misc_language.h" |
| 33 | #include "common/threadpool.h" |
| 34 | |
| 35 | TEST(threadpool, wait_nothing) |
| 36 | { |
| 37 | std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests()); |
| 38 | tools::threadpool::waiter waiter(*tpool);; |
| 39 | waiter.wait(); |
| 40 | } |
| 41 | |
| 42 | TEST(threadpool, wait_waits) |
| 43 | { |
| 44 | std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests()); |
| 45 | tools::threadpool::waiter waiter(*tpool); |
| 46 | std::atomic<bool> b(false); |
| 47 | tpool->submit(&waiter, [&b](){ epee::misc_utils::sleep_no_w(1000); b = true; }); |
| 48 | ASSERT_FALSE(b); |
| 49 | waiter.wait(); |
| 50 | ASSERT_TRUE(b); |
| 51 | } |
| 52 | |
| 53 | TEST(threadpool, one_thread) |
| 54 | { |
| 55 | std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(1)); |
| 56 | tools::threadpool::waiter waiter(*tpool); |
| 57 | |
| 58 | std::atomic<unsigned int> counter(0); |
| 59 | for (size_t n = 0; n < 4096; ++n) |
| 60 | { |
| 61 | tpool->submit(&waiter, [&counter](){++counter;}); |
| 62 | } |
| 63 | waiter.wait(); |
| 64 | ASSERT_EQ(counter, 4096); |
| 65 | } |
| 66 | |
| 67 | TEST(threadpool, many_threads) |
| 68 | { |
| 69 | std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(256)); |
| 70 | tools::threadpool::waiter waiter(*tpool); |
| 71 | |
| 72 | std::atomic<unsigned int> counter(0); |
| 73 | for (size_t n = 0; n < 4096; ++n) |
| 74 | { |
| 75 | tpool->submit(&waiter, [&counter](){++counter;}); |
| 76 | } |
| 77 | waiter.wait(); |
| 78 | ASSERT_EQ(counter, 4096); |
| 79 | } |
| 80 | |
| 81 | static uint64_t fibonacci(std::shared_ptr<tools::threadpool> tpool, uint64_t n) |
| 82 | { |
| 83 | if (n <= 1) |
| 84 | return n; |
| 85 | uint64_t f1, f2; |
| 86 | tools::threadpool::waiter waiter(*tpool); |
| 87 | tpool->submit(&waiter, [&tpool, &f1, n](){ f1 = fibonacci(tpool, n-1); }); |
| 88 | tpool->submit(&waiter, [&tpool, &f2, n](){ f2 = fibonacci(tpool, n-2); }); |
| 89 | waiter.wait(); |
| 90 | return f1 + f2; |
| 91 | } |
| 92 | |
| 93 | TEST(threadpool, reentrency) |
| 94 | { |
| 95 | std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(4)); |
| 96 | tools::threadpool::waiter waiter(*tpool); |
| 97 | |
| 98 | uint64_t f = fibonacci(tpool, 13); |
| 99 | waiter.wait(); |
| 100 | ASSERT_EQ(f, 233); |
| 101 | } |
| 102 | |
| 103 | TEST(threadpool, reentrancy) |
| 104 | { |
| 105 | std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(4)); |
| 106 | tools::threadpool::waiter waiter(*tpool); |
| 107 | |
| 108 | uint64_t f = fibonacci(tpool, 13); |
| 109 | waiter.wait(); |
| 110 | ASSERT_EQ(f, 233); |
| 111 | } |
| 112 | |
| 113 | TEST(threadpool, leaf_throws) |
| 114 | { |
| 115 | std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests()); |
| 116 | tools::threadpool::waiter waiter(*tpool); |
| 117 | |
| 118 | bool thrown = false, executed = false; |
| 119 | tpool->submit(&waiter, [&](){ |
| 120 | try { tpool->submit(&waiter, [&](){ executed = true; }); } |
| 121 | catch(const std::exception &e) { thrown = true; } |
| 122 | }, true); |
| 123 | waiter.wait(); |
| 124 | ASSERT_TRUE(thrown); |
| 125 | ASSERT_FALSE(executed); |
| 126 | } |
| 127 | |
| 128 | TEST(threadpool, leaf_reentrancy) |
| 129 | { |
| 130 | std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(4)); |
| 131 | tools::threadpool::waiter waiter(*tpool); |
| 132 | |
| 133 | std::atomic<int> counter(0); |
| 134 | for (int i = 0; i < 1000; ++i) |
| 135 | { |
| 136 | tpool->submit(&waiter, [&](){ |
| 137 | tools::threadpool::waiter waiter(*tpool); |
| 138 | for (int j = 0; j < 500; ++j) |
| 139 | { |
| 140 | tpool->submit(&waiter, [&](){ ++counter; }, true); |
| 141 | } |
| 142 | waiter.wait(); |
| 143 | }); |
| 144 | } |
| 145 | waiter.wait(); |
| 146 | ASSERT_EQ(counter, 500000); |
| 147 | } |