blob: 9fd5c5dd52738811afb316ab6a0cc910fda9700b (
plain) (
blame)
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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/util/thread.h>
namespace vbench {
template <typename T>
Dispatcher<T>::Dispatcher(Handler<T> &fallback)
: _fallback(fallback),
_lock(),
_threads(),
_closed(false)
{
}
template <typename T>
Dispatcher<T>::~Dispatcher() {}
template <typename T>
bool
Dispatcher<T>::waitForThreads(size_t threads, size_t pollCnt) const
{
for (size_t i = 0; i < pollCnt; ++i) {
if (i != 0) {
vespalib::Thread::sleep(20);
}
{
vespalib::LockGuard guard(_lock);
if (_threads.size() >= threads) {
return true;
}
}
}
return false;
}
template <typename T>
void
Dispatcher<T>::close()
{
std::vector<ThreadState*> threads;
{
vespalib::LockGuard guard(_lock);
std::swap(_threads, threads);
_closed = true;
}
for (size_t i = 0; i < threads.size(); ++i) {
threads[i]->gate.countDown();
}
}
template <typename T>
void
Dispatcher<T>::handle(std::unique_ptr<T> obj)
{
vespalib::LockGuard guard(_lock);
if (!_threads.empty()) {
ThreadState *state = _threads.back();
_threads.pop_back();
guard.unlock();
state->object = std::move(obj);
state->gate.countDown();
} else {
bool closed = _closed;
guard.unlock();
if (!closed) {
_fallback.handle(std::move(obj));
}
}
}
template <typename T>
std::unique_ptr<T>
Dispatcher<T>::provide()
{
ThreadState state;
{
vespalib::LockGuard guard(_lock);
if (!_closed) {
_threads.push_back(&state);
guard.unlock();
state.gate.await();
}
}
return std::move(state.object);
}
} // namespace vbench
|