-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
eventhandler_macos.cc
499 lines (456 loc) · 16.2 KB
/
eventhandler_macos.cc
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "platform/globals.h"
#if defined(HOST_OS_MACOS)
#include "bin/eventhandler.h"
#include "bin/eventhandler_macos.h"
#include <errno.h> // NOLINT
#include <fcntl.h> // NOLINT
#include <pthread.h> // NOLINT
#include <stdio.h> // NOLINT
#include <string.h> // NOLINT
#include <sys/event.h> // NOLINT
#include <unistd.h> // NOLINT
#include "bin/dartutils.h"
#include "bin/fdutils.h"
#include "bin/lockers.h"
#include "bin/socket.h"
#include "bin/thread.h"
#include "bin/utils.h"
#include "platform/hashmap.h"
#include "platform/syslog.h"
#include "platform/utils.h"
namespace dart {
namespace bin {
bool DescriptorInfo::HasReadEvent() {
return (Mask() & (1 << kInEvent)) != 0;
}
bool DescriptorInfo::HasWriteEvent() {
return (Mask() & (1 << kOutEvent)) != 0;
}
// Unregister the file descriptor for a SocketData structure with kqueue.
static void RemoveFromKqueue(intptr_t kqueue_fd_, DescriptorInfo* di) {
if (!di->tracked_by_kqueue()) {
return;
}
static const intptr_t kMaxChanges = 2;
struct kevent events[kMaxChanges];
EV_SET(events, di->fd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
VOID_NO_RETRY_EXPECTED(kevent(kqueue_fd_, events, 1, NULL, 0, NULL));
EV_SET(events, di->fd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
VOID_NO_RETRY_EXPECTED(kevent(kqueue_fd_, events, 1, NULL, 0, NULL));
di->set_tracked_by_kqueue(false);
}
// Update the kqueue registration for SocketData structure to reflect
// the events currently of interest.
static void AddToKqueue(intptr_t kqueue_fd_, DescriptorInfo* di) {
ASSERT(!di->tracked_by_kqueue());
static const intptr_t kMaxChanges = 2;
intptr_t changes = 0;
struct kevent events[kMaxChanges];
int flags = EV_ADD;
if (!di->IsListeningSocket()) {
flags |= EV_CLEAR;
}
ASSERT(di->HasReadEvent() || di->HasWriteEvent());
// Register or unregister READ filter if needed.
if (di->HasReadEvent()) {
EV_SET(events + changes, di->fd(), EVFILT_READ, flags, 0, 0, di);
++changes;
}
// Register or unregister WRITE filter if needed.
if (di->HasWriteEvent()) {
EV_SET(events + changes, di->fd(), EVFILT_WRITE, flags, 0, 0, di);
++changes;
}
ASSERT(changes > 0);
ASSERT(changes <= kMaxChanges);
int status =
NO_RETRY_EXPECTED(kevent(kqueue_fd_, events, changes, NULL, 0, NULL));
if (status == -1) {
// TODO(dart:io): Verify that the dart end is handling this correctly.
// kQueue does not accept the file descriptor. It could be due to
// already closed file descriptor, or unuspported devices, such
// as /dev/null. In such case, mark the file descriptor as closed,
// so dart will handle it accordingly.
di->NotifyAllDartPorts(1 << kCloseEvent);
} else {
di->set_tracked_by_kqueue(true);
}
}
EventHandlerImplementation::EventHandlerImplementation()
: socket_map_(&SimpleHashMap::SamePointerValue, 16) {
intptr_t result;
result = NO_RETRY_EXPECTED(pipe(interrupt_fds_));
if (result != 0) {
FATAL("Pipe creation failed");
}
if (!FDUtils::SetNonBlocking(interrupt_fds_[0])) {
FATAL("Failed to set pipe fd non-blocking\n");
}
if (!FDUtils::SetCloseOnExec(interrupt_fds_[0])) {
FATAL("Failed to set pipe fd close on exec\n");
}
if (!FDUtils::SetCloseOnExec(interrupt_fds_[1])) {
FATAL("Failed to set pipe fd close on exec\n");
}
shutdown_ = false;
kqueue_fd_ = NO_RETRY_EXPECTED(kqueue());
if (kqueue_fd_ == -1) {
FATAL("Failed creating kqueue");
}
if (!FDUtils::SetCloseOnExec(kqueue_fd_)) {
FATAL("Failed to set kqueue fd close on exec\n");
}
// Register the interrupt_fd with the kqueue.
struct kevent event;
EV_SET(&event, interrupt_fds_[0], EVFILT_READ, EV_ADD, 0, 0, NULL);
int status = NO_RETRY_EXPECTED(kevent(kqueue_fd_, &event, 1, NULL, 0, NULL));
if (status == -1) {
const int kBufferSize = 1024;
char error_message[kBufferSize];
Utils::StrError(errno, error_message, kBufferSize);
FATAL1("Failed adding interrupt fd to kqueue: %s\n", error_message);
}
}
static void DeleteDescriptorInfo(void* info) {
DescriptorInfo* di = reinterpret_cast<DescriptorInfo*>(info);
di->Close();
delete di;
}
EventHandlerImplementation::~EventHandlerImplementation() {
socket_map_.Clear(DeleteDescriptorInfo);
close(kqueue_fd_);
close(interrupt_fds_[0]);
close(interrupt_fds_[1]);
}
void EventHandlerImplementation::UpdateKQueueInstance(intptr_t old_mask,
DescriptorInfo* di) {
intptr_t new_mask = di->Mask();
if (old_mask != 0 && new_mask == 0) {
RemoveFromKqueue(kqueue_fd_, di);
} else if ((old_mask == 0) && (new_mask != 0)) {
AddToKqueue(kqueue_fd_, di);
} else if ((old_mask != 0) && (new_mask != 0) && (old_mask != new_mask)) {
ASSERT(!di->IsListeningSocket());
RemoveFromKqueue(kqueue_fd_, di);
AddToKqueue(kqueue_fd_, di);
}
}
DescriptorInfo* EventHandlerImplementation::GetDescriptorInfo(
intptr_t fd,
bool is_listening) {
ASSERT(fd >= 0);
SimpleHashMap::Entry* entry = socket_map_.Lookup(
GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd), true);
ASSERT(entry != NULL);
DescriptorInfo* di = reinterpret_cast<DescriptorInfo*>(entry->value);
if (di == NULL) {
// If there is no data in the hash map for this file descriptor a
// new DescriptorInfo for the file descriptor is inserted.
if (is_listening) {
di = new DescriptorInfoMultiple(fd);
} else {
di = new DescriptorInfoSingle(fd);
}
entry->value = di;
}
ASSERT(fd == di->fd());
return di;
}
void EventHandlerImplementation::WakeupHandler(intptr_t id,
Dart_Port dart_port,
int64_t data) {
InterruptMessage msg;
msg.id = id;
msg.dart_port = dart_port;
msg.data = data;
// WriteToBlocking will write up to 512 bytes atomically, and since our msg
// is smaller than 512, we don't need a thread lock.
ASSERT(kInterruptMessageSize < PIPE_BUF);
intptr_t result =
FDUtils::WriteToBlocking(interrupt_fds_[1], &msg, kInterruptMessageSize);
if (result != kInterruptMessageSize) {
if (result == -1) {
perror("Interrupt message failure:");
}
FATAL1("Interrupt message failure. Wrote %" Pd " bytes.", result);
}
}
void EventHandlerImplementation::HandleInterruptFd() {
const intptr_t MAX_MESSAGES = kInterruptMessageSize;
InterruptMessage msg[MAX_MESSAGES];
ssize_t bytes = TEMP_FAILURE_RETRY(
read(interrupt_fds_[0], msg, MAX_MESSAGES * kInterruptMessageSize));
for (ssize_t i = 0; i < bytes / kInterruptMessageSize; i++) {
if (msg[i].id == kTimerId) {
timeout_queue_.UpdateTimeout(msg[i].dart_port, msg[i].data);
} else if (msg[i].id == kShutdownId) {
shutdown_ = true;
} else {
ASSERT((msg[i].data & COMMAND_MASK) != 0);
Socket* socket = reinterpret_cast<Socket*>(msg[i].id);
RefCntReleaseScope<Socket> rs(socket);
if (socket->fd() == -1) {
continue;
}
DescriptorInfo* di =
GetDescriptorInfo(socket->fd(), IS_LISTENING_SOCKET(msg[i].data));
if (IS_COMMAND(msg[i].data, kShutdownReadCommand)) {
ASSERT(!di->IsListeningSocket());
// Close the socket for reading.
VOID_NO_RETRY_EXPECTED(shutdown(di->fd(), SHUT_RD));
} else if (IS_COMMAND(msg[i].data, kShutdownWriteCommand)) {
ASSERT(!di->IsListeningSocket());
// Close the socket for writing.
VOID_NO_RETRY_EXPECTED(shutdown(di->fd(), SHUT_WR));
} else if (IS_COMMAND(msg[i].data, kCloseCommand)) {
// Close the socket and free system resources and move on to next
// message.
intptr_t old_mask = di->Mask();
Dart_Port port = msg[i].dart_port;
if (port != ILLEGAL_PORT) {
di->RemovePort(port);
}
intptr_t new_mask = di->Mask();
UpdateKQueueInstance(old_mask, di);
intptr_t fd = di->fd();
if (di->IsListeningSocket()) {
// We only close the socket file descriptor from the operating
// system if there are no other dart socket objects which
// are listening on the same (address, port) combination.
ListeningSocketRegistry* registry =
ListeningSocketRegistry::Instance();
MutexLocker locker(registry->mutex());
if (registry->CloseSafe(socket)) {
ASSERT(new_mask == 0);
socket_map_.Remove(GetHashmapKeyFromFd(fd),
GetHashmapHashFromFd(fd));
di->Close();
delete di;
}
socket->CloseFd();
} else {
ASSERT(new_mask == 0);
socket_map_.Remove(GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd));
di->Close();
delete di;
socket->CloseFd();
}
DartUtils::PostInt32(port, 1 << kDestroyedEvent);
} else if (IS_COMMAND(msg[i].data, kReturnTokenCommand)) {
intptr_t old_mask = di->Mask();
di->ReturnTokens(msg[i].dart_port, TOKEN_COUNT(msg[i].data));
UpdateKQueueInstance(old_mask, di);
} else if (IS_COMMAND(msg[i].data, kSetEventMaskCommand)) {
// `events` can only have kInEvent/kOutEvent flags set.
intptr_t events = msg[i].data & EVENT_MASK;
ASSERT(0 == (events & ~(1 << kInEvent | 1 << kOutEvent)));
intptr_t old_mask = di->Mask();
di->SetPortAndMask(msg[i].dart_port, msg[i].data & EVENT_MASK);
UpdateKQueueInstance(old_mask, di);
} else {
UNREACHABLE();
}
}
}
}
#ifdef DEBUG_KQUEUE
static void PrintEventMask(intptr_t fd, struct kevent* event) {
Syslog::Print("%d ", static_cast<int>(fd));
Syslog::Print("filter=0x%x:", event->filter);
if (event->filter == EVFILT_READ) {
Syslog::Print("EVFILT_READ ");
}
if (event->filter == EVFILT_WRITE) {
Syslog::Print("EVFILT_WRITE ");
}
Syslog::Print("flags: %x: ", event->flags);
if ((event->flags & EV_EOF) != 0) {
Syslog::Print("EV_EOF ");
}
if ((event->flags & EV_ERROR) != 0) {
Syslog::Print("EV_ERROR ");
}
if ((event->flags & EV_CLEAR) != 0) {
Syslog::Print("EV_CLEAR ");
}
if ((event->flags & EV_ADD) != 0) {
Syslog::Print("EV_ADD ");
}
if ((event->flags & EV_DELETE) != 0) {
Syslog::Print("EV_DELETE ");
}
Syslog::Print("- fflags: %d ", event->fflags);
Syslog::Print("- data: %ld ", event->data);
Syslog::Print("(available %d) ",
static_cast<int>(FDUtils::AvailableBytes(fd)));
Syslog::Print("\n");
}
#endif
intptr_t EventHandlerImplementation::GetEvents(struct kevent* event,
DescriptorInfo* di) {
#ifdef DEBUG_KQUEUE
PrintEventMask(di->fd(), event);
#endif
intptr_t event_mask = 0;
if (di->IsListeningSocket()) {
// On a listening socket the READ event means that there are
// connections ready to be accepted.
if (event->filter == EVFILT_READ) {
if ((event->flags & EV_EOF) != 0) {
if (event->fflags != 0) {
event_mask |= (1 << kErrorEvent);
} else {
event_mask |= (1 << kCloseEvent);
}
}
if (event_mask == 0) {
event_mask |= (1 << kInEvent);
}
} else {
UNREACHABLE();
}
} else {
// Prioritize data events over close and error events.
if (event->filter == EVFILT_READ) {
event_mask = (1 << kInEvent);
if ((event->flags & EV_EOF) != 0) {
if (event->fflags != 0) {
event_mask = (1 << kErrorEvent);
} else {
event_mask |= (1 << kCloseEvent);
}
}
} else if (event->filter == EVFILT_WRITE) {
event_mask |= (1 << kOutEvent);
if ((event->flags & EV_EOF) != 0) {
if (event->fflags != 0) {
event_mask = (1 << kErrorEvent);
}
}
} else {
UNREACHABLE();
}
}
return event_mask;
}
void EventHandlerImplementation::HandleEvents(struct kevent* events, int size) {
bool interrupt_seen = false;
for (int i = 0; i < size; i++) {
// If flag EV_ERROR is set it indicates an error in kevent processing.
if ((events[i].flags & EV_ERROR) != 0) {
const int kBufferSize = 1024;
char error_message[kBufferSize];
Utils::StrError(events[i].data, error_message, kBufferSize);
FATAL1("kevent failed %s\n", error_message);
}
if (events[i].udata == NULL) {
interrupt_seen = true;
} else {
DescriptorInfo* di = reinterpret_cast<DescriptorInfo*>(events[i].udata);
const intptr_t old_mask = di->Mask();
const intptr_t event_mask = GetEvents(events + i, di);
if ((event_mask & (1 << kErrorEvent)) != 0) {
di->NotifyAllDartPorts(event_mask);
UpdateKQueueInstance(old_mask, di);
} else if (event_mask != 0) {
Dart_Port port = di->NextNotifyDartPort(event_mask);
ASSERT(port != 0);
UpdateKQueueInstance(old_mask, di);
DartUtils::PostInt32(port, event_mask);
}
}
}
if (interrupt_seen) {
// Handle after socket events, so we avoid closing a socket before we handle
// the current events.
HandleInterruptFd();
}
}
int64_t EventHandlerImplementation::GetTimeout() {
if (!timeout_queue_.HasTimeout()) {
return kInfinityTimeout;
}
int64_t millis =
timeout_queue_.CurrentTimeout() - TimerUtils::GetCurrentMonotonicMillis();
return (millis < 0) ? 0 : millis;
}
void EventHandlerImplementation::HandleTimeout() {
if (timeout_queue_.HasTimeout()) {
int64_t millis = timeout_queue_.CurrentTimeout() -
TimerUtils::GetCurrentMonotonicMillis();
if (millis <= 0) {
DartUtils::PostNull(timeout_queue_.CurrentPort());
timeout_queue_.RemoveCurrent();
}
}
}
void EventHandlerImplementation::EventHandlerEntry(uword args) {
static const intptr_t kMaxEvents = 16;
struct kevent events[kMaxEvents];
EventHandler* handler = reinterpret_cast<EventHandler*>(args);
EventHandlerImplementation* handler_impl = &handler->delegate_;
ASSERT(handler_impl != NULL);
while (!handler_impl->shutdown_) {
int64_t millis = handler_impl->GetTimeout();
ASSERT(millis == kInfinityTimeout || millis >= 0);
if (millis > kMaxInt32) {
millis = kMaxInt32;
}
// NULL pointer timespec for infinite timeout.
ASSERT(kInfinityTimeout < 0);
struct timespec* timeout = NULL;
struct timespec ts;
if (millis >= 0) {
int32_t millis32 = static_cast<int32_t>(millis);
int32_t secs = millis32 / 1000;
ts.tv_sec = secs;
ts.tv_nsec = (millis32 - (secs * 1000)) * 1000000;
timeout = &ts;
}
// We have to use TEMP_FAILURE_RETRY for mac, as kevent can modify the
// current sigmask.
intptr_t result = TEMP_FAILURE_RETRY(
kevent(handler_impl->kqueue_fd_, NULL, 0, events, kMaxEvents, timeout));
if (result == -1) {
const int kBufferSize = 1024;
char error_message[kBufferSize];
Utils::StrError(errno, error_message, kBufferSize);
FATAL1("kevent failed %s\n", error_message);
} else {
handler_impl->HandleTimeout();
handler_impl->HandleEvents(events, result);
}
}
DEBUG_ASSERT(ReferenceCounted<Socket>::instances() == 0);
handler->NotifyShutdownDone();
}
void EventHandlerImplementation::Start(EventHandler* handler) {
int result = Thread::Start("dart:io EventHandler",
&EventHandlerImplementation::EventHandlerEntry,
reinterpret_cast<uword>(handler));
if (result != 0) {
FATAL1("Failed to start event handler thread %d", result);
}
}
void EventHandlerImplementation::Shutdown() {
SendData(kShutdownId, 0, 0);
}
void EventHandlerImplementation::SendData(intptr_t id,
Dart_Port dart_port,
int64_t data) {
WakeupHandler(id, dart_port, data);
}
void* EventHandlerImplementation::GetHashmapKeyFromFd(intptr_t fd) {
// The hashmap does not support keys with value 0.
return reinterpret_cast<void*>(fd + 1);
}
uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) {
// The hashmap does not support keys with value 0.
return dart::Utils::WordHash(fd + 1);
}
} // namespace bin
} // namespace dart
#endif // defined(HOST_OS_MACOS)