Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
TimerFd.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_TIMERFD_HPP
18#define TOOLBOX_IO_TIMERFD_HPP
19
20#include <toolbox/io/File.hpp>
21#include <toolbox/sys/Time.hpp>
22
23#include <sys/timerfd.h>
24
25namespace toolbox {
26namespace os {
27
29inline FileHandle timerfd_create(int clock_id, int flags, std::error_code& ec) noexcept
30{
31 const auto fd = ::timerfd_create(clock_id, flags);
32 if (fd < 0) {
34 }
35 return fd;
36}
37
40{
41 const auto fd = ::timerfd_create(clock_id, flags);
42 if (fd < 0) {
43 throw std::system_error{make_error(errno), "timerfd_create"};
44 }
45 return fd;
46}
47
50 std::error_code& ec) noexcept
51{
52 const auto ret = ::timerfd_settime(fd, flags, &new_value, &old_value);
53 if (ret < 0) {
55 }
56}
57
60{
61 const auto ret = ::timerfd_settime(fd, flags, &new_value, &old_value);
62 if (ret < 0) {
63 throw std::system_error{make_error(errno), "timerfd_settime"};
64 }
65}
66
68inline void timerfd_settime(int fd, int flags, const itimerspec& new_value,
69 std::error_code& ec) noexcept
70{
71 const auto ret = ::timerfd_settime(fd, flags, &new_value, nullptr);
72 if (ret < 0) {
74 }
75}
76
78inline void timerfd_settime(int fd, int flags, const itimerspec& new_value)
79{
80 const auto ret = ::timerfd_settime(fd, flags, &new_value, nullptr);
81 if (ret < 0) {
82 throw std::system_error{make_error(errno), "timerfd_settime"};
83 }
84}
85
87template <typename ClockT>
88inline void timerfd_settime(int fd, int flags, std::chrono::time_point<ClockT, Duration> expiry,
89 Duration interval, std::error_code& ec) noexcept
90{
92 {to_timespec(interval), to_timespec(expiry)}, ec);
93}
94
96template <typename ClockT>
97inline void timerfd_settime(int fd, int flags, std::chrono::time_point<ClockT, Duration> expiry,
98 Duration interval)
99{
101 {to_timespec(interval), to_timespec(expiry)});
102}
103
105template <typename ClockT>
106inline void timerfd_settime(int fd, int flags, std::chrono::time_point<ClockT, Duration> expiry,
107 std::error_code& ec) noexcept
108{
109 return timerfd_settime(fd, flags | TFD_TIMER_ABSTIME, {{}, to_timespec(expiry)}, ec);
110}
111
113template <typename ClockT>
114inline void timerfd_settime(int fd, int flags, std::chrono::time_point<ClockT, Duration> expiry)
115{
116 return timerfd_settime(fd, flags | TFD_TIMER_ABSTIME, {{}, to_timespec(expiry)});
117}
118
119} // namespace os
120inline namespace io {
121
122template <typename ClockT>
123class TimerFd {
124 public:
125 using Clock = ClockT;
126 using TimePoint = std::chrono::time_point<ClockT, Duration>;
127
128 explicit TimerFd(int flags)
129 : fh_{os::timerfd_create(Clock::Id, flags)}
130 {
131 }
132 ~TimerFd() = default;
133
134 // Copy.
135 TimerFd(const TimerFd&) = delete;
136 TimerFd& operator=(const TimerFd&) = delete;
137
138 // Move.
141
142 int fd() const noexcept { return fh_.get(); }
143
144 void set_time(int flags, TimePoint expiry, Duration interval, std::error_code& ec) noexcept
145 {
146 return os::timerfd_settime(*fh_, flags, expiry, interval, ec);
147 }
148 void set_time(int flags, TimePoint expiry, Duration interval)
149 {
150 return os::timerfd_settime(*fh_, flags, expiry, interval);
151 }
152 void set_time(int flags, TimePoint expiry, std::error_code& ec) noexcept
153 {
154 return os::timerfd_settime(*fh_, flags, expiry, ec);
155 }
156 void set_time(int flags, TimePoint expiry) { return os::timerfd_settime(*fh_, flags, expiry); }
157
158 private:
159 FileHandle fh_;
160};
161
162} // namespace io
163} // namespace toolbox
164
165#endif // TOOLBOX_IO_TIMERFD_HPP
constexpr Id get() const noexcept
Definition Handle.hpp:67
int fd() const noexcept
Definition TimerFd.hpp:142
void set_time(int flags, TimePoint expiry, Duration interval)
Definition TimerFd.hpp:148
TimerFd(TimerFd &&) noexcept=default
TimerFd(const TimerFd &)=delete
std::chrono::time_point< ClockT, Duration > TimePoint
Definition TimerFd.hpp:126
void set_time(int flags, TimePoint expiry, std::error_code &ec) noexcept
Definition TimerFd.hpp:152
void set_time(int flags, TimePoint expiry)
Definition TimerFd.hpp:156
TimerFd & operator=(const TimerFd &)=delete
void set_time(int flags, TimePoint expiry, Duration interval, std::error_code &ec) noexcept
Definition TimerFd.hpp:144
void timerfd_settime(int fd, int flags, const itimerspec &new_value, itimerspec &old_value, std::error_code &ec) noexcept
Arm or disarm timer.
Definition TimerFd.hpp:49
FileHandle timerfd_create(int clock_id, int flags, std::error_code &ec) noexcept
Create a file descriptor for timer notification.
Definition TimerFd.hpp:29
std::error_code make_error(int err) noexcept
Definition Error.hpp:25
constexpr timespec to_timespec(std::chrono::duration< RepT, PeriodT > d) noexcept
Definition Time.hpp:234
Nanos Duration
Definition Time.hpp:40
constexpr auto bind() noexcept
Definition Slot.hpp:92