-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathSelectScheduler.cpp
executable file
·136 lines (110 loc) · 3 KB
/
SelectScheduler.cpp
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
130
131
132
133
134
135
136
/**************************************************************
* Copyright (c) 2010-2013, Dynamic Network Services, Inc.
* Jake Montgomery ([email protected]) & Tom Daly ([email protected])
* Distributed under the FreeBSD License - see LICENSE
***************************************************************/
#include "config.h"
#ifndef USE_KEVENT_SCHEDULER
#include "common.h"
#include "SelectScheduler.h"
#include "utils.h"
#include <errno.h>
#include <string.h>
using namespace std;
SelectScheduler::SelectScheduler() : SchedulerBase(),
m_foundEvents(0),
m_nextCheckEvent(0)
{
}
SelectScheduler::~SelectScheduler()
{
}
bool SelectScheduler::waitForEvents(const struct timespec &timeout)
{
fd_set watchSet;
struct timeval tv;
int largestSocket = 0;
set<int>::iterator it;
timespecToTimeval(timeout, tv);
m_nextCheckEvent = 0;
/**
* Make the set of sockets to watch.
*/
FD_ZERO(&watchSet);
for (it = m_watchSockets.begin(); it != m_watchSockets.end(); it++)
{
FD_SET(*it, &watchSet);
largestSocket = max(largestSocket, *it);
}
m_foundEvents = ::select(largestSocket + 1, &watchSet, NULL, NULL, &tv);
if (m_foundEvents < 0)
{
m_foundEvents = 0;
gLog.LogError("select failed: %s", ErrnoToString());
}
else if (m_foundEvents == 0)
{
if (timeout.tv_nsec != 0 || timeout.tv_sec != 0)
gLog.Optional(Log::TimerDetail, "select timeout");
}
else
{
int actuallyFound = 0;
gLog.Optional(Log::TimerDetail, "select received %d events", m_foundEvents);
// Determine what the sockets that hit are
for (it = m_watchSockets.begin(); it != m_watchSockets.end(); it++)
{
if (FD_ISSET(*it, &watchSet))
{
if (!LogVerify(actuallyFound < (int)m_foundSockets.size()))
break;
m_foundSockets[actuallyFound++] = *it;
}
}
LogVerify(actuallyFound == m_foundEvents);
m_foundEvents = actuallyFound;
}
return m_foundEvents > 0;
}
int SelectScheduler::getNextSocketEvent()
{
if (!LogVerify(m_foundEvents <= int(m_foundSockets.size())))
m_foundEvents = m_foundSockets.size();
if (m_nextCheckEvent < m_foundEvents)
return m_foundSockets[m_nextCheckEvent++];
else
return -1;
}
bool SelectScheduler::watchSocket(int fd)
{
if (!LogVerify(fd != -1))
return false;
m_watchSockets.insert(fd);
resizeFoundSockets();
return true;
}
void SelectScheduler::unWatchSocket(int fd)
{
if (!LogVerify(fd != -1))
return;
LogVerify(1 == m_watchSockets.erase(fd));
resizeFoundSockets();
}
/**
* resizes m_foundSockets.
*
* Note that m_foundSockets is maintained a vector big enough to hold all
* events so that resizing would occur only when adding or removing sockets.
* This prevents exceptions being thrown during regular operation.
*
* @throw - May throw.
*
*/
void SelectScheduler::resizeFoundSockets()
{
// Can not resize if we may be using it.
if ((int)m_watchSockets.size() < m_foundEvents)
return;
m_foundSockets.resize(m_watchSockets.size());
}
#endif // !USE_KEVENT_SCHEDULER