aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/net/wakeup_pipe.cpp
blob: b8025bfcf9f229918dfd23696ee12c566d6dbf3f (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "wakeup_pipe.h"
#include "socket_utils.h"
#include <vespa/vespalib/util/require.h>
#include <unistd.h>

namespace vespalib {

WakeupPipe::WakeupPipe()
  : _reader(),
    _writer()
{
    int pipe[2];
    socketutils::nonblocking_pipe(pipe);
    _reader.reset(pipe[0]);
    _writer.reset(pipe[1]);
}

WakeupPipe::~WakeupPipe() = default;

void
WakeupPipe::write_token()
{
    char token = 'T';
    ssize_t res = _writer.write(&token, 1);
    if (res < 0) {
        res = -errno;
    }
    REQUIRE(res > 0 || res == -EAGAIN || res == -EWOULDBLOCK);
}

void
WakeupPipe::read_tokens()
{
    char token_trash[128];
    ssize_t res = _reader.read(token_trash, sizeof(token_trash));
    REQUIRE(res > 0);
}

}