aboutsummaryrefslogtreecommitdiffstats
path: root/vbench/src/vbench/core/dispatcher.hpp
blob: 572e9f3381de92208e07b57fbc9b03e063da14f8 (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
89
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/util/thread.h>
#include <vespa/vespalib/util/time.h>

namespace vbench {

template <typename T>
Dispatcher<T>::Dispatcher(Handler<T> &fallback)
    : _fallback(fallback),
      _lock(),
      _threads(),
      _closed(false)
{
}

template <typename T>
Dispatcher<T>::~Dispatcher() = default;

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) {
            std::this_thread::sleep_for(20ms);
        }
        {
            std::lock_guard guard(_lock);
            if (_threads.size() >= threads) {
                return true;
            }
        }
    }
    return false;
}

template <typename T>
void
Dispatcher<T>::close()
{
    std::vector<ThreadState*> threads;
    {
        std::lock_guard 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)
{
    std::unique_lock 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;
    {
        std::unique_lock guard(_lock);
        if (!_closed) {
            _threads.push_back(&state);
            guard.unlock();
            state.gate.await();
        }
    }
    return std::move(state.object);
}

} // namespace vbench