forked from rofafor/vdr-plugin-satip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poller.c
122 lines (106 loc) · 3.07 KB
/
poller.c
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
/*
* poller.c: SAT>IP plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
*/
#define __STDC_FORMAT_MACROS // Required for format specifiers
#include <inttypes.h>
#include <sys/epoll.h>
#include "config.h"
#include "common.h"
#include "log.h"
#include "poller.h"
cSatipPoller *cSatipPoller::instanceS = NULL;
cSatipPoller *cSatipPoller::GetInstance(void)
{
if (!instanceS)
instanceS = new cSatipPoller();
return instanceS;
}
bool cSatipPoller::Initialize(void)
{
debug1("%s", __PRETTY_FUNCTION__);
if (instanceS)
instanceS->Activate();
return true;
}
void cSatipPoller::Destroy(void)
{
debug1("%s", __PRETTY_FUNCTION__);
if (instanceS)
instanceS->Deactivate();
}
cSatipPoller::cSatipPoller()
: cThread("SATIP poller"),
mutexM(),
fdM(epoll_create(eMaxFileDescriptors))
{
debug1("%s", __PRETTY_FUNCTION__);
}
cSatipPoller::~cSatipPoller()
{
debug1("%s", __PRETTY_FUNCTION__);
Deactivate();
cMutexLock MutexLock(&mutexM);
close(fdM);
// Free allocated memory
}
void cSatipPoller::Activate(void)
{
// Start the thread
Start();
}
void cSatipPoller::Deactivate(void)
{
debug1("%s", __PRETTY_FUNCTION__);
cMutexLock MutexLock(&mutexM);
if (Running())
Cancel(3);
}
void cSatipPoller::Action(void)
{
debug1("%s Entering", __PRETTY_FUNCTION__);
struct epoll_event events[eMaxFileDescriptors];
uint64_t maxElapsed = 0;
// Increase priority
SetPriority(-1);
// Do the thread loop
while (Running()) {
int nfds = epoll_wait(fdM, events, eMaxFileDescriptors, -1);
ERROR_IF_FUNC((nfds == -1 && errno != EINTR), "epoll_wait() failed", break, ;);
for (int i = 0; i < nfds; ++i) {
cSatipPollerIf* poll = reinterpret_cast<cSatipPollerIf *>(events[i].data.ptr);
if (poll) {
uint64_t elapsed;
cTimeMs processing(0);
poll->Process();
elapsed = processing.Elapsed();
if (elapsed > maxElapsed) {
maxElapsed = elapsed;
debug1("%s Processing %s took %" PRIu64 " ms", __PRETTY_FUNCTION__, *(poll->ToString()), maxElapsed);
}
}
}
}
debug1("%s Exiting", __PRETTY_FUNCTION__);
}
bool cSatipPoller::Register(cSatipPollerIf &pollerP)
{
debug1("%s fd=%d", __PRETTY_FUNCTION__, pollerP.GetFd());
cMutexLock MutexLock(&mutexM);
struct epoll_event ev;
ev.events = EPOLLIN | EPOLLET;
ev.data.ptr = &pollerP;
ERROR_IF_RET(epoll_ctl(fdM, EPOLL_CTL_ADD, pollerP.GetFd(), &ev) == -1, "epoll_ctl(EPOLL_CTL_ADD) failed", return false);
debug1("%s Added interface fd=%d", __PRETTY_FUNCTION__, pollerP.GetFd());
return true;
}
bool cSatipPoller::Unregister(cSatipPollerIf &pollerP)
{
debug1("%s fd=%d", __PRETTY_FUNCTION__, pollerP.GetFd());
cMutexLock MutexLock(&mutexM);
ERROR_IF_RET((epoll_ctl(fdM, EPOLL_CTL_DEL, pollerP.GetFd(), NULL) == -1), "epoll_ctl(EPOLL_CTL_DEL) failed", return false);
debug1("%s Removed interface fd=%d", __PRETTY_FUNCTION__, pollerP.GetFd());
return true;
}