summaryrefslogtreecommitdiffstats
path: root/fnet/src/vespa/fnet/connect_thread.cpp
blob: 8c1b38596deaeb71128a96b32203c16f2359a160 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "connect_thread.h"

namespace fnet {

void
ConnectThread::run()
{
    for (;;) {
        Guard guard(_lock);
        while (!_done && _queue.empty()) {
            _cond.wait(guard);
        }
        if (_done && _queue.empty()) {
            return;
        }
        assert(!_queue.empty());
        ExtConnectable *conn = _queue.front();
        _queue.pop();
        guard.unlock(); // UNLOCK
        conn->ext_connect();
    }
}

ConnectThread::ConnectThread()
    : _lock(),
      _cond(),
      _queue(),
      _done(false),
      _thread(&ConnectThread::run, this)
{
}

ConnectThread::~ConnectThread()
{
    {
        Guard guard(_lock);
        _done = true;
        _cond.notify_one();
    }
    _thread.join();
    assert(_queue.empty());
}

void
ConnectThread::connect_later(ExtConnectable *conn)
{
    Guard guard(_lock);   
    assert(!_done);
    _queue.push(conn);
    _cond.notify_one();
}

} // namespace fnet