| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
| 6 #if defined(TARGET_OS_ANDROID) | 6 #if defined(TARGET_OS_ANDROID) |
| 7 | 7 |
| 8 #include "bin/eventhandler.h" | 8 #include "bin/eventhandler.h" |
| 9 | 9 |
| 10 #include <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 } | 67 } |
| 68 | 68 |
| 69 | 69 |
| 70 // Register the file descriptor for a SocketData structure with epoll | 70 // Register the file descriptor for a SocketData structure with epoll |
| 71 // if events are requested. | 71 // if events are requested. |
| 72 static void UpdateEpollInstance(intptr_t epoll_fd_, SocketData* sd) { | 72 static void UpdateEpollInstance(intptr_t epoll_fd_, SocketData* sd) { |
| 73 struct epoll_event event; | 73 struct epoll_event event; |
| 74 event.events = sd->GetPollEvents(); | 74 event.events = sd->GetPollEvents(); |
| 75 event.data.ptr = sd; | 75 event.data.ptr = sd; |
| 76 if (sd->port() != 0 && event.events != 0) { | 76 if (sd->port() != 0 && event.events != 0) { |
| 77 // Only report events once and wait for them to be re-enabled after the | |
| 78 // event has been handled by the Dart code. | |
| 79 event.events |= EPOLLONESHOT; | |
| 80 int status = 0; | 77 int status = 0; |
| 81 if (sd->tracked_by_epoll()) { | 78 if (sd->tracked_by_epoll()) { |
| 82 status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_, | 79 status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_, |
| 83 EPOLL_CTL_MOD, | 80 EPOLL_CTL_MOD, |
| 84 sd->fd(), | 81 sd->fd(), |
| 85 &event)); | 82 &event)); |
| 86 } else { | 83 } else { |
| 87 status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_, | 84 status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_, |
| 88 EPOLL_CTL_ADD, | 85 EPOLL_CTL_ADD, |
| 89 sd->fd(), | 86 sd->fd(), |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 | 331 |
| 335 void EventHandlerImplementation::HandleEvents(struct epoll_event* events, | 332 void EventHandlerImplementation::HandleEvents(struct epoll_event* events, |
| 336 int size) { | 333 int size) { |
| 337 bool interrupt_seen = false; | 334 bool interrupt_seen = false; |
| 338 for (int i = 0; i < size; i++) { | 335 for (int i = 0; i < size; i++) { |
| 339 if (events[i].data.ptr == NULL) { | 336 if (events[i].data.ptr == NULL) { |
| 340 interrupt_seen = true; | 337 interrupt_seen = true; |
| 341 } else { | 338 } else { |
| 342 SocketData* sd = reinterpret_cast<SocketData*>(events[i].data.ptr); | 339 SocketData* sd = reinterpret_cast<SocketData*>(events[i].data.ptr); |
| 343 intptr_t event_mask = GetPollEvents(events[i].events, sd); | 340 intptr_t event_mask = GetPollEvents(events[i].events, sd); |
| 344 if (event_mask == 0) { | 341 if (event_mask != 0) { |
| 345 // Event not handled, re-add to epoll. | 342 // Unregister events for the file descriptor. Events will be |
| 346 UpdateEpollInstance(epoll_fd_, sd); | 343 // registered again when the current event has been handled in |
| 347 } else { | 344 // Dart code. |
| 345 RemoveFromEpollInstance(epoll_fd_, sd); |
| 348 Dart_Port port = sd->port(); | 346 Dart_Port port = sd->port(); |
| 349 ASSERT(port != 0); | 347 ASSERT(port != 0); |
| 350 DartUtils::PostInt32(port, event_mask); | 348 DartUtils::PostInt32(port, event_mask); |
| 351 } | 349 } |
| 352 } | 350 } |
| 353 } | 351 } |
| 354 if (interrupt_seen) { | 352 if (interrupt_seen) { |
| 355 // Handle after socket events, so we avoid closing a socket before we handle | 353 // Handle after socket events, so we avoid closing a socket before we handle |
| 356 // the current events. | 354 // the current events. |
| 357 HandleInterruptFd(); | 355 HandleInterruptFd(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 if (millis > kMaxInt32) millis = kMaxInt32; | 391 if (millis > kMaxInt32) millis = kMaxInt32; |
| 394 intptr_t result = TEMP_FAILURE_RETRY(epoll_wait(handler->epoll_fd_, | 392 intptr_t result = TEMP_FAILURE_RETRY(epoll_wait(handler->epoll_fd_, |
| 395 events, | 393 events, |
| 396 kMaxEvents, | 394 kMaxEvents, |
| 397 millis)); | 395 millis)); |
| 398 ASSERT(EAGAIN == EWOULDBLOCK); | 396 ASSERT(EAGAIN == EWOULDBLOCK); |
| 399 if (result == -1) { | 397 if (result == -1) { |
| 400 if (errno != EWOULDBLOCK) { | 398 if (errno != EWOULDBLOCK) { |
| 401 perror("Poll failed"); | 399 perror("Poll failed"); |
| 402 } | 400 } |
| 403 } else if (result == 0) { | 401 } else { |
| 404 handler->HandleTimeout(); | 402 handler->HandleTimeout(); |
| 405 } else { | |
| 406 handler->HandleEvents(events, result); | 403 handler->HandleEvents(events, result); |
| 407 } | 404 } |
| 408 } | 405 } |
| 409 } | 406 } |
| 410 | 407 |
| 411 | 408 |
| 412 void EventHandlerImplementation::Start(EventHandler* handler) { | 409 void EventHandlerImplementation::Start(EventHandler* handler) { |
| 413 int result = dart::Thread::Start(&EventHandlerImplementation::Poll, | 410 int result = dart::Thread::Start(&EventHandlerImplementation::Poll, |
| 414 reinterpret_cast<uword>(handler)); | 411 reinterpret_cast<uword>(handler)); |
| 415 if (result != 0) { | 412 if (result != 0) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 438 | 435 |
| 439 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { | 436 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { |
| 440 // The hashmap does not support keys with value 0. | 437 // The hashmap does not support keys with value 0. |
| 441 return dart::Utils::WordHash(fd + 1); | 438 return dart::Utils::WordHash(fd + 1); |
| 442 } | 439 } |
| 443 | 440 |
| 444 } // namespace bin | 441 } // namespace bin |
| 445 } // namespace dart | 442 } // namespace dart |
| 446 | 443 |
| 447 #endif // defined(TARGET_OS_ANDROID) | 444 #endif // defined(TARGET_OS_ANDROID) |
| OLD | NEW |