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