Repository navigation
Expand file tree
/
Copy pathpoller.cpp
More file actions
227 lines (203 loc) · 6.21 KB
/
Copy pathpoller.cpp
File metadata and controls
227 lines (203 loc) · 6.21 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright (C) 2026 Shitty team
* MIT licensed
* See the file LICENSE.MIT for the full license.
*/
#include "poller.h"
#include "composer.h"
#include "listener.h"
#include <std/lib/vector.h>
#include <std/mem/obj_pool.h>
#include <std/sym/i_map.h>
#include <std/sys/crt.h>
#include <climits>
#include <poll.h>
using namespace stl;
namespace {
struct ArmedFD {
int fd;
int mode;
};
struct PollerImpl final: public Poller {
explicit PollerImpl(Composer& composer);
void arm(int fd, int mode) override;
void disarm(int fd) override;
void timeout(u64 microseconds) override;
void deadline(u64 monotonicMicroseconds) override;
int poll(struct pollfd* sourceFDs, size_t sourceCount, double* timeout) override;
void dispatch() override;
static short toNative(int mode);
static int fromNative(short events);
static int toMilliseconds(double seconds);
Composer& composer;
IntMap<ArmedFD> armed;
Vector<struct pollfd> waiting;
Vector<struct pollfd> pendingSource;
Vector<FDReady> ready;
u64 minDeadline = 0;
bool sourcePending = false;
bool timeoutReady = false;
};
}
PollerImpl::PollerImpl(Composer& composer_)
: composer(composer_)
, armed(ObjPool::create(composer.pool))
{
}
void PollerImpl::arm(int fd, int mode) {
armed[fd] = {fd, mode};
}
void PollerImpl::disarm(int fd) {
armed.erase(fd);
}
void PollerImpl::timeout(u64 microseconds) {
deadline(monotonicNowUs() + microseconds);
}
void PollerImpl::deadline(u64 monotonicMicroseconds) {
if (monotonicMicroseconds == 0) {
monotonicMicroseconds = monotonicNowUs();
}
if (minDeadline == 0 || monotonicMicroseconds < minDeadline) {
minDeadline = monotonicMicroseconds;
}
}
short PollerImpl::toNative(int mode) {
short events = 0;
if (mode & PollRead) {
events |= POLLIN;
}
if (mode & PollWrite) {
events |= POLLOUT;
}
return events;
}
int PollerImpl::fromNative(short events) {
int mode = 0;
if (events & POLLIN) {
mode |= PollRead;
}
if (events & POLLOUT) {
mode |= PollWrite;
}
if (events & (POLLERR | POLLNVAL)) {
mode |= PollError;
}
if (events & POLLHUP) {
mode |= PollHangup;
}
return mode;
}
int PollerImpl::toMilliseconds(double seconds) {
if (seconds <= 0.0) {
return 0;
}
if (seconds >= INT_MAX / 1000.0) {
return INT_MAX;
}
return (int)(seconds * 1000.0 + 0.999);
}
int PollerImpl::poll(struct pollfd* sourceFDs, size_t sourceCount, double* timeout) {
if (sourcePending) {
int result = 0;
for (size_t index = 0; index != sourceCount; ++index) {
sourceFDs[index].revents = pendingSource[index].revents;
result += sourceFDs[index].revents != 0;
}
pendingSource.clear();
sourcePending = false;
return result;
}
if (!ready.empty() || timeoutReady) {
for (size_t index = 0; index != sourceCount; ++index) {
sourceFDs[index].revents = 0;
}
if (timeout != nullptr) {
*timeout = 0.0;
}
return 0;
}
waiting.clear();
if (sourceCount != 0) {
waiting.append(sourceFDs, sourceCount);
}
armed.visit([this](const ArmedFD& current) {
waiting.pushBack({
.fd = current.fd,
.events = toNative(current.mode),
.revents = 0,
});
});
const u64 started = monotonicNowUs();
double waitSeconds = timeout == nullptr ? 0.0 : *timeout;
bool waitForever = timeout == nullptr;
if (minDeadline != 0) {
const double untilDeadline = minDeadline > started ? (minDeadline - started) / 1'000'000.0 : 0.0;
if (waitForever || untilDeadline < waitSeconds) {
waitSeconds = untilDeadline;
waitForever = false;
}
}
const int milliseconds = waitForever ? -1 : toMilliseconds(waitSeconds);
const int result = ::poll(waiting.mutData(), waiting.length(), milliseconds);
if (timeout != nullptr) {
const double elapsed = (monotonicNowUs() - started) / 1'000'000.0;
*timeout = elapsed < *timeout ? *timeout - elapsed : 0.0;
}
for (size_t index = 0; index != sourceCount; ++index) {
sourceFDs[index].revents = waiting[index].revents;
}
bool externalReady = false;
if (result > 0) {
for (size_t index = sourceCount; index != waiting.length(); ++index) {
const struct pollfd& source = waiting[index];
if (source.revents == 0) {
continue;
}
externalReady = true;
FDReady ready{
.fd = source.fd,
.what = fromNative(source.revents),
};
// GLFW calls us with a prepared Wayland read. Dispatching here
// would deadlock any listener that enters another Wayland queue.
this->ready.pushBack(ready);
}
}
if (minDeadline != 0 && monotonicNowUs() >= minDeadline) {
minDeadline = 0;
timeoutReady = true;
}
if (externalReady) {
pendingSource.clear();
if (sourceCount != 0) {
pendingSource.append(sourceFDs, sourceCount);
}
sourcePending = true;
for (size_t index = 0; index != sourceCount; ++index) {
sourceFDs[index].revents = 0;
}
}
return result;
}
void PollerImpl::dispatch() {
for (const FDReady& event : ready) {
for (IntrusiveNode* node = composer.onFDReady.mutFront(); node != composer.onFDReady.mutEnd();) {
Listener* const listener = static_cast<Listener*>(node);
node = node->next;
listener->onListen((void*)(&event));
}
}
ready.clear();
if (!timeoutReady) {
return;
}
timeoutReady = false;
for (IntrusiveNode* node = composer.onTimeout.mutFront(); node != composer.onTimeout.mutEnd();) {
Listener* const listener = static_cast<Listener*>(node);
node = node->next;
listener->onListen();
}
}
Poller* Poller::create(Composer& composer) {
return composer.pool->make<PollerImpl>(composer);
}