-
Notifications
You must be signed in to change notification settings - Fork 2
/
Event.h
38 lines (28 loc) · 783 Bytes
/
Event.h
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
// SPDX-FileCopyrightText: 2018 Lutz Freitag
// SPDX-License-Identifier: MIT
#pragma once
#include "FileDescriptor.h"
#include <stdexcept>
#include <sys/eventfd.h>
namespace simplyfile
{
struct Event : FileDescriptor {
Event(int flags=0, int initval=0) noexcept
: FileDescriptor(eventfd(initval, flags))
{}
Event(Event&&) noexcept = default;
Event& operator=(Event&&) noexcept = default;
uint64_t get() {
uint64_t val {0};
if (::eventfd_read(*this, &val) != 0) {
throw std::runtime_error("cannot read from eventfd");
}
return val;
}
void put(uint64_t val) {
if (::eventfd_write(*this, val) != 0) {
throw std::runtime_error("cannot write to eventfd");
}
}
};
}