w11 - cpp 0.794
Backend server for Rlink and w11
Loading...
Searching...
No Matches
RtimerFd.cpp
Go to the documentation of this file.
1// $Id: RtimerFd.cpp 1185 2019-07-12 17:29:12Z mueller $
2// SPDX-License-Identifier: GPL-3.0-or-later
3// Copyright 2013-2019 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4//
5// Revision History:
6// Date Rev Version Comment
7// 2019-06-08 1161 1.1 derive from Rfd, inherit IsOpen,Close,Fd
8// 2017-02-18 852 1.0 Initial version
9// 2013-01-11 473 0.1 First draft
10// ---------------------------------------------------------------------------
11
16#include <errno.h>
17#include <unistd.h>
18#include <sys/timerfd.h>
19
20#include "RtimerFd.hpp"
21
22#include "Rexception.hpp"
23
24using namespace std;
25
31// all method definitions in namespace Retro
32namespace Retro {
33
34//------------------------------------------+-----------------------------------
36
38 : RtimerFd("RtimerFd::")
39{}
40
41//------------------------------------------+-----------------------------------
43
44RtimerFd::RtimerFd(const char* cnam)
45 : Rfd(cnam)
46{}
47
48//------------------------------------------+-----------------------------------
50
51void RtimerFd::Open(clockid_t clkid)
52{
53 if (IsOpen())
54 throw Rexception(fCnam+"Open()", "bad state: already open");
55
56 fFd = ::timerfd_create(clkid, TFD_NONBLOCK);
57 if (!IsOpen())
58 throw Rexception(fCnam+"Open()", "timerfd_create() failed: ", errno);
59 return;
60}
61
62//------------------------------------------+-----------------------------------
64
66{
67 if (!IsOpen())
68 throw Rexception(fCnam+"SetRelative()", "bad state: not open");
69
70 if (dt.Sec() <= 0 || dt.NSec() <= 0)
71 throw Rexception(fCnam+"SetRelative()", "bad value: dt zero or negative ");
72
73 struct itimerspec itspec;
74 itspec.it_interval.tv_sec = 0;
75 itspec.it_interval.tv_nsec = 0;
76 itspec.it_value = dt.Timespec();
77
78 if (::timerfd_settime(fFd, 0, &itspec, nullptr) < 0)
79 throw Rexception(fCnam+"SetRelative()",
80 "timerfd_settime() failed: ", errno);
81 return;
82}
83
84//------------------------------------------+-----------------------------------
86
88{
89 if (!IsOpen())
90 throw Rexception(fCnam+"Cancel()", "bad state: not open");
91
92 struct itimerspec itspec;
93 itspec.it_interval.tv_sec = 0;
94 itspec.it_interval.tv_nsec = 0;
95 itspec.it_value.tv_sec = 0;
96 itspec.it_value.tv_nsec = 0;
97
98 // cancel running timers
99 if (::timerfd_settime(fFd, 0, &itspec, nullptr) < 0)
100 throw Rexception(fCnam+"Cancel()", "timerfd_settime() failed: ", errno);
101
102 // clear aready experied timers
103 uint64_t cnt;
104 int irc = ::read(fFd, &cnt, sizeof(cnt));
105 if (irc < 0 && errno != EAGAIN)
106 throw Rexception(fCnam+"Cancel()", "read() failed: ", errno);
107
108 return;
109}
110
111//------------------------------------------+-----------------------------------
113
115{
116 if (!IsOpen())
117 throw Rexception(fCnam+"Read()", "bad state: not open");
118
119 uint64_t cnt;
120 int irc = ::read(fFd, &cnt, sizeof(cnt));
121 if (irc < 0) {
122 if (errno == EAGAIN) return 0;
123 throw Rexception(fCnam+"Read()", "read() failed: ", errno);
124 }
125 return cnt;
126}
127
128} // end namespace Retro
FIXME_docs.
Definition: Rexception.hpp:29
FIXME_docs.
Definition: Rfd.hpp:25
int fFd
Definition: Rfd.hpp:48
bool IsOpen() const
FIXME_docs.
Definition: Rfd.ipp:28
std::string fCnam
Definition: Rfd.hpp:49
FIXME_docs.
Definition: Rtime.hpp:25
const struct timespec & Timespec() const
FIXME_docs.
Definition: Rtime.ipp:125
time_t Sec() const
FIXME_docs.
Definition: Rtime.ipp:109
long NSec() const
FIXME_docs.
Definition: Rtime.ipp:117
FIXME_docs.
Definition: RtimerFd.hpp:29
uint64_t Read()
FIXME_docs.
Definition: RtimerFd.cpp:114
void Cancel()
FIXME_docs.
Definition: RtimerFd.cpp:87
void Open(clockid_t clkid=CLOCK_MONOTONIC)
FIXME_docs.
Definition: RtimerFd.cpp:51
void SetRelative(const Rtime &dt)
FIXME_docs.
Definition: RtimerFd.cpp:65
RtimerFd()
FIXME_docs.
Definition: RtimerFd.cpp:37
Declaration of class ReventLoop.
Definition: ReventLoop.cpp:47