Electroneum
Toggle main menu visibility
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
34
TEST
(threadpool, wait_nothing)
35
{
36
std::shared_ptr<tools::threadpool> tpool(
tools::threadpool::getNewForUnitTests
());
37
tools::threadpool::waiter
waiter;
38
waiter.
wait
(tpool.get());
39
}
40
41
TEST
(threadpool, wait_waits)
42
{
43
std::shared_ptr<tools::threadpool> tpool(
tools::threadpool::getNewForUnitTests
());
44
tools::threadpool::waiter
waiter;
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
52
TEST
(threadpool, one_thread)
53
{
54
std::shared_ptr<tools::threadpool> tpool(
tools::threadpool::getNewForUnitTests
(1));
55
tools::threadpool::waiter
waiter;
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
66
TEST
(threadpool, many_threads)
67
{
68
std::shared_ptr<tools::threadpool> tpool(
tools::threadpool::getNewForUnitTests
(256));
69
tools::threadpool::waiter
waiter;
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
80
static
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
;
85
tools::threadpool::waiter
waiter;
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
92
TEST
(threadpool, reentrency)
93
{
94
std::shared_ptr<tools::threadpool> tpool(
tools::threadpool::getNewForUnitTests
(4));
95
tools::threadpool::waiter
waiter;
96
97
uint64_t
f = fibonacci(tpool, 13);
98
waiter.
wait
(tpool.get());
99
ASSERT_EQ
(f, 233);
100
}
101
102
TEST
(threadpool, reentrancy)
103
{
104
std::shared_ptr<tools::threadpool> tpool(
tools::threadpool::getNewForUnitTests
(4));
105
tools::threadpool::waiter
waiter;
106
107
uint64_t
f = fibonacci(tpool, 13);
108
waiter.
wait
(tpool.get());
109
ASSERT_EQ
(f, 233);
110
}
111
112
TEST
(threadpool, leaf_throws)
113
{
114
std::shared_ptr<tools::threadpool> tpool(
tools::threadpool::getNewForUnitTests
());
115
tools::threadpool::waiter
waiter;
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
127
TEST
(threadpool, leaf_reentrancy)
128
{
129
std::shared_ptr<tools::threadpool> tpool(
tools::threadpool::getNewForUnitTests
(4));
130
tools::threadpool::waiter
waiter;
131
132
std::atomic<int> counter(0);
133
for
(
int
i = 0; i < 1000; ++i)
134
{
135
tpool->submit(&waiter, [&](){
136
tools::threadpool::waiter
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
}
tools::threadpool::waiter
Definition
threadpool.h:56
tools::threadpool::waiter::wait
void wait(threadpool *tpool)
Definition
threadpool.cpp:115
tools::threadpool::getNewForUnitTests
static threadpool * getNewForUnitTests(unsigned max_threads=0)
Definition
threadpool.h:50
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition
gtest.h:1956
ASSERT_FALSE
#define ASSERT_FALSE(condition)
Definition
gtest.h:1868
TEST
#define TEST(test_case_name, test_name)
Definition
gtest.h:2187
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition
gtest.h:1865
misc_language.h
epee::misc_utils::sleep_no_w
bool sleep_no_w(long ms)
Definition
misc_language.h:100
std::tr1::f2
const T1 const T2 & f2
Definition
gtest-tuple.h:688
std::tr1::f1
const T1 & f1
Definition
gtest-tuple.h:683
uint64_t
unsigned __int64 uint64_t
Definition
stdint.h:136
threadpool.h
tests
unit_tests
threadpool.cpp
Generated on
for Electroneum by
1.17.0