-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathRtimerFd.cpp
More file actions
129 lines (100 loc) · 3.27 KB
/
RtimerFd.cpp
File metadata and controls
129 lines (100 loc) · 3.27 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// $Id: RtimerFd.cpp 1376 2023-02-20 15:05:03Z mueller $
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright 2013-2023 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// Revision History:
// Date Rev Version Comment
// 2023-02-20 1376 1.1.1 BUGFIX: SetRelative(): correct is-positive check
// 2019-06-08 1161 1.1 derive from Rfd, inherit IsOpen,Close,Fd
// 2017-02-18 852 1.0 Initial version
// 2013-01-11 473 0.1 First draft
// ---------------------------------------------------------------------------
/*!
\brief Implemenation of class RtimerFd.
*/
#include <errno.h>
#include <unistd.h>
#include <sys/timerfd.h>
#include "RtimerFd.hpp"
#include "Rexception.hpp"
using namespace std;
/*!
\class Retro::RtimerFd
\brief FIXME_docs
*/
// all method definitions in namespace Retro
namespace Retro {
//------------------------------------------+-----------------------------------
//! FIXME_docs
RtimerFd::RtimerFd()
: RtimerFd("RtimerFd::")
{}
//------------------------------------------+-----------------------------------
//! FIXME_docs
RtimerFd::RtimerFd(const char* cnam)
: Rfd(cnam)
{}
//------------------------------------------+-----------------------------------
//! FIXME_docs
void RtimerFd::Open(clockid_t clkid)
{
if (IsOpen())
throw Rexception(fCnam+"Open()", "bad state: already open");
fFd = ::timerfd_create(clkid, TFD_NONBLOCK);
if (!IsOpen())
throw Rexception(fCnam+"Open()", "timerfd_create() failed: ", errno);
return;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
void RtimerFd::SetRelative(const Rtime& dt)
{
if (!IsOpen())
throw Rexception(fCnam+"SetRelative()", "bad state: not open");
if (!dt.IsPositive())
throw Rexception(fCnam+"SetRelative()", "bad value: dt zero or negative ");
struct itimerspec itspec;
itspec.it_interval.tv_sec = 0;
itspec.it_interval.tv_nsec = 0;
itspec.it_value = dt.Timespec();
if (::timerfd_settime(fFd, 0, &itspec, nullptr) < 0)
throw Rexception(fCnam+"SetRelative()",
"timerfd_settime() failed: ", errno);
return;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
void RtimerFd::Cancel()
{
if (!IsOpen())
throw Rexception(fCnam+"Cancel()", "bad state: not open");
struct itimerspec itspec;
itspec.it_interval.tv_sec = 0;
itspec.it_interval.tv_nsec = 0;
itspec.it_value.tv_sec = 0;
itspec.it_value.tv_nsec = 0;
// cancel running timers
if (::timerfd_settime(fFd, 0, &itspec, nullptr) < 0)
throw Rexception(fCnam+"Cancel()", "timerfd_settime() failed: ", errno);
// clear aready experied timers
uint64_t cnt;
int irc = ::read(fFd, &cnt, sizeof(cnt));
if (irc < 0 && errno != EAGAIN)
throw Rexception(fCnam+"Cancel()", "read() failed: ", errno);
return;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
uint64_t RtimerFd::Read()
{
if (!IsOpen())
throw Rexception(fCnam+"Read()", "bad state: not open");
uint64_t cnt;
int irc = ::read(fFd, &cnt, sizeof(cnt));
if (irc < 0) {
if (errno == EAGAIN) return 0;
throw Rexception(fCnam+"Read()", "read() failed: ", errno);
}
return cnt;
}
} // end namespace Retro