Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
EventFd.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2021 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_IO_EVENTFD_HPP
18#define TOOLBOX_IO_EVENTFD_HPP
19
20#include <toolbox/io/File.hpp>
21
22#include <sys/eventfd.h>
23
24namespace toolbox {
25namespace os {
26
28inline FileHandle eventfd(unsigned intval, int flags, std::error_code& ec) noexcept
29{
30 const auto fd = ::eventfd(intval, flags);
31 if (fd < 0) {
33 }
34 return fd;
35}
36
38inline FileHandle eventfd(unsigned intval, int flags)
39{
40 const auto fd = ::eventfd(intval, flags);
41 if (fd < 0) {
42 throw std::system_error{make_error(errno), "eventfd"};
43 }
44 return fd;
45}
46
47} // namespace os
48inline namespace io {
49
50class EventFd {
51 public:
52 EventFd(unsigned intval, int flags)
53 : fh_{os::eventfd(intval, flags)}
54 {
55 }
56 ~EventFd() = default;
57
58 // Copy.
59 EventFd(const EventFd&) = delete;
60 EventFd& operator=(const EventFd&) = delete;
61
62 // Move.
65
66 int fd() const noexcept { return fh_.get(); }
67 std::int64_t read()
68 {
69 union {
70 char buf[sizeof(std::int64_t)];
71 std::int64_t val;
72 } u;
73 os::read(*fh_, u.buf, sizeof(u.buf));
74 return u.val;
75 }
76 void write(std::int64_t val, std::error_code& ec) noexcept
77 {
78 // Adds the 8-byte integer value supplied in its buffer to the counter.
79 union {
80 char buf[sizeof(std::int64_t)];
81 std::int64_t val;
82 } u;
83 u.val = val;
84 os::write(*fh_, u.buf, sizeof(u.buf), ec);
85 }
86 void write(std::int64_t val)
87 {
88 // Adds the 8-byte integer value supplied in its buffer to the counter.
89 union {
90 char buf[sizeof(std::int64_t)];
91 std::int64_t val;
92 } u;
93 u.val = val;
94 os::write(*fh_, u.buf, sizeof(u.buf));
95 }
96
97 private:
98 FileHandle fh_;
99};
100
101} // namespace io
102} // namespace toolbox
103
104#endif // TOOLBOX_IO_EVENTFD_HPP
constexpr Id get() const noexcept
Definition Handle.hpp:67
void write(std::int64_t val, std::error_code &ec) noexcept
Definition EventFd.hpp:76
std::int64_t read()
Definition EventFd.hpp:67
EventFd(const EventFd &)=delete
int fd() const noexcept
Definition EventFd.hpp:66
EventFd(unsigned intval, int flags)
Definition EventFd.hpp:52
EventFd(EventFd &&) noexcept=default
EventFd & operator=(const EventFd &)=delete
void write(std::int64_t val)
Definition EventFd.hpp:86
ssize_t read(int fd, void *buf, std::size_t len, std::error_code &ec) noexcept
Read from a file descriptor.
Definition File.hpp:146
ssize_t write(int fd, const void *buf, std::size_t len, std::error_code &ec) noexcept
Write to a file descriptor.
Definition File.hpp:178
FileHandle eventfd(unsigned intval, int flags, std::error_code &ec) noexcept
Create a file descriptor for event notification.
Definition EventFd.hpp:28
std::error_code make_error(int err) noexcept
Definition Error.hpp:25
constexpr auto bind() noexcept
Definition Slot.hpp:92