aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/vespa/messagebus/routablequeue.cpp
blob: 0e2c509d7a69fffe4d7ef08404269e593b5dcf58 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "routablequeue.h"

using namespace std::chrono;

namespace mbus {

RoutableQueue::RoutableQueue()
    : _lock(),
      _queue()
{ }

RoutableQueue::~RoutableQueue()
{
    while (_queue.size() > 0) {
        Routable *r = _queue.front();
        _queue.pop();
        delete r;
    }
}

uint32_t
RoutableQueue::size()
{
    std::lock_guard guard(_lock);
    return _queue.size();
}

void
RoutableQueue::enqueue(Routable::UP r)
{
    std::unique_lock guard(_lock);
    _queue.push(r.get());
    r.release();
    if (_queue.size() == 1) {
        guard.unlock();
        _cond.notify_all(); // support multiple readers
    }
}

Routable::UP
RoutableQueue::dequeue(duration timeout)
{
    steady_clock::time_point startTime = steady_clock::now();
    duration left = timeout;
    std::unique_lock guard(_lock);
    while (_queue.size() == 0 && left > duration::zero()) {
        if ((_cond.wait_for(guard, left) == std::cv_status::timeout) || (_queue.size() > 0)) {
            break;
        }
        duration elapsed = (steady_clock::now() - startTime);
        left = (elapsed > timeout) ? duration::zero() : timeout - elapsed;
    }
    if (_queue.size() == 0) {
        return Routable::UP();
    }
    Routable::UP ret(_queue.front());
    _queue.pop();
    return ret;
}

void
RoutableQueue::handleMessage(Message::UP msg)
{
    enqueue(std::move(msg));
}

void
RoutableQueue::handleReply(Reply::UP reply)
{
    enqueue(std::move(reply));
}

} // namespace mbus