1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <comphelper/threadpool.hxx>
#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/plugin/TestPlugIn.h>
#include <tools/time.hxx>
#include <osl/thread.hxx>
#include <stdlib.h>
#include <atomic>
#include <cstddef>
#include <thread>
#include <mutex>
class ThreadPoolTest : public CppUnit::TestFixture
{
public:
void testPreferredConcurrency();
void testWorkerUsage();
void testTasksInThreads();
void testNoThreads();
void testDedicatedPool();
CPPUNIT_TEST_SUITE(ThreadPoolTest);
CPPUNIT_TEST(testPreferredConcurrency);
CPPUNIT_TEST(testWorkerUsage);
CPPUNIT_TEST(testTasksInThreads);
CPPUNIT_TEST(testNoThreads);
CPPUNIT_TEST(testDedicatedPool);
CPPUNIT_TEST_SUITE_END();
};
void ThreadPoolTest::testPreferredConcurrency()
{
// Check default.
auto nThreads = comphelper::ThreadPool::getPreferredConcurrency();
std::size_t nExpected = 4; // UTs are capped to 4.
CPPUNIT_ASSERT_MESSAGE("Expected no more than 4 threads", nExpected >= nThreads);
#ifndef _WIN32
// The result should be cached, so this should change anything.
nThreads = std::thread::hardware_concurrency() * 2;
setenv("MAX_CONCURRENCY", std::to_string(nThreads).c_str(), true);
nThreads = comphelper::ThreadPool::getPreferredConcurrency();
CPPUNIT_ASSERT_MESSAGE("Expected no more than hardware threads",
nThreads <= std::thread::hardware_concurrency());
// Revert and check. Again, nothing should change.
unsetenv("MAX_CONCURRENCY");
nThreads = comphelper::ThreadPool::getPreferredConcurrency();
CPPUNIT_ASSERT_MESSAGE("Expected no more than 4 threads", nExpected >= nThreads);
#endif
}
namespace
{
class UsageTask : public comphelper::ThreadTask
{
public:
UsageTask(const std::shared_ptr<comphelper::ThreadTaskTag>& pTag)
: ThreadTask(pTag)
{
}
virtual void doWork()
{
++count;
mutex.lock();
mutex.unlock();
}
static inline std::atomic<int> count = 0;
static inline std::mutex mutex;
};
} // namespace
void ThreadPoolTest::testWorkerUsage()
{
// Create tasks for each available worker. Lock a shared mutex before that to make all
// tasks block on it. And check that all workers have started, i.e. that the full
// thread pool capacity is used.
comphelper::ThreadPool& rSharedPool = comphelper::ThreadPool::getSharedOptimalPool();
std::shared_ptr<comphelper::ThreadTaskTag> pTag = comphelper::ThreadPool::createThreadTaskTag();
UsageTask::mutex.lock();
for (int i = 0; i < rSharedPool.getWorkerCount(); ++i)
{
rSharedPool.pushTask(std::make_unique<UsageTask>(pTag));
osl::Thread::wait(std::chrono::milliseconds(10)); // give it a time to start
}
sal_uInt64 startTicks = tools::Time::GetSystemTicks();
while (UsageTask::count != rSharedPool.getWorkerCount())
{
// Wait at most 5 seconds, that should do even on slow systems.
CPPUNIT_ASSERT_MESSAGE("Thread pool does not use all worker threads.",
startTicks + 5000 > tools::Time::GetSystemTicks());
osl::Thread::wait(std::chrono::milliseconds(10));
}
UsageTask::mutex.unlock();
rSharedPool.waitUntilDone(pTag);
}
namespace
{
class CheckThreadTask : public comphelper::ThreadTask
{
oslThreadIdentifier mThreadId;
bool mCheckEqual;
public:
CheckThreadTask(oslThreadIdentifier threadId, bool checkEqual,
const std::shared_ptr<comphelper::ThreadTaskTag>& pTag)
: ThreadTask(pTag)
, mThreadId(threadId)
, mCheckEqual(checkEqual)
{
}
virtual void doWork()
{
CPPUNIT_ASSERT(mCheckEqual ? osl::Thread::getCurrentIdentifier() == mThreadId
: osl::Thread::getCurrentIdentifier() != mThreadId);
}
};
} // namespace
void ThreadPoolTest::testTasksInThreads()
{
// Check that all tasks are run in worker threads, not this thread.
comphelper::ThreadPool& pool = comphelper::ThreadPool::getSharedOptimalPool();
std::shared_ptr<comphelper::ThreadTaskTag> pTag = comphelper::ThreadPool::createThreadTaskTag();
for (int i = 0; i < 8; ++i)
pool.pushTask(
std::make_unique<CheckThreadTask>(osl::Thread::getCurrentIdentifier(), false, pTag));
pool.waitUntilDone(pTag);
}
void ThreadPoolTest::testNoThreads()
{
// No worker threads, tasks will be run in this thread.
comphelper::ThreadPool pool(0);
std::shared_ptr<comphelper::ThreadTaskTag> pTag = comphelper::ThreadPool::createThreadTaskTag();
for (int i = 0; i < 8; ++i)
pool.pushTask(
std::make_unique<CheckThreadTask>(osl::Thread::getCurrentIdentifier(), true, pTag));
pool.waitUntilDone(pTag);
}
void ThreadPoolTest::testDedicatedPool()
{
// Test that a separate thread pool works. The tasks themselves do not matter.
comphelper::ThreadPool pool(4);
std::shared_ptr<comphelper::ThreadTaskTag> pTag = comphelper::ThreadPool::createThreadTaskTag();
for (int i = 0; i < 8; ++i)
pool.pushTask(
std::make_unique<CheckThreadTask>(osl::Thread::getCurrentIdentifier(), false, pTag));
pool.waitUntilDone(pTag);
}
CPPUNIT_TEST_SUITE_REGISTRATION(ThreadPoolTest);
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|