| 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 EventHandlerImplementation::EventHandlerImplementation() | 97 EventHandlerImplementation::EventHandlerImplementation() |
| 98 : socket_map_(&HashMap::SamePointerValue, 16) { | 98 : socket_map_(&HashMap::SamePointerValue, 16) { |
| 99 intptr_t result; | 99 intptr_t result; |
| 100 result = TEMP_FAILURE_RETRY(pipe(interrupt_fds_)); | 100 result = TEMP_FAILURE_RETRY(pipe(interrupt_fds_)); |
| 101 if (result != 0) { | 101 if (result != 0) { |
| 102 FATAL("Pipe creation failed"); | 102 FATAL("Pipe creation failed"); |
| 103 } | 103 } |
| 104 FDUtils::SetNonBlocking(interrupt_fds_[0]); | 104 FDUtils::SetNonBlocking(interrupt_fds_[0]); |
| 105 FDUtils::SetCloseOnExec(interrupt_fds_[0]); | 105 FDUtils::SetCloseOnExec(interrupt_fds_[0]); |
| 106 FDUtils::SetCloseOnExec(interrupt_fds_[1]); | 106 FDUtils::SetCloseOnExec(interrupt_fds_[1]); |
| 107 timeout_ = kInfinityTimeout; | |
| 108 timeout_port_ = 0; | |
| 109 shutdown_ = false; | 107 shutdown_ = false; |
| 110 // The initial size passed to epoll_create is ignore on newer (>= | 108 // The initial size passed to epoll_create is ignore on newer (>= |
| 111 // 2.6.8) Linux versions | 109 // 2.6.8) Linux versions |
| 112 static const int kEpollInitialSize = 64; | 110 static const int kEpollInitialSize = 64; |
| 113 epoll_fd_ = TEMP_FAILURE_RETRY(epoll_create(kEpollInitialSize)); | 111 epoll_fd_ = TEMP_FAILURE_RETRY(epoll_create(kEpollInitialSize)); |
| 114 if (epoll_fd_ == -1) { | 112 if (epoll_fd_ == -1) { |
| 115 FATAL("Failed creating epoll file descriptor"); | 113 FATAL("Failed creating epoll file descriptor"); |
| 116 } | 114 } |
| 117 FDUtils::SetCloseOnExec(epoll_fd_); | 115 FDUtils::SetCloseOnExec(epoll_fd_); |
| 118 // Register the interrupt_fd with the epoll instance. | 116 // Register the interrupt_fd with the epoll instance. |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 total_read = total_read + bytes_read; | 185 total_read = total_read + bytes_read; |
| 188 } | 186 } |
| 189 } | 187 } |
| 190 return (total_read == kInterruptMessageSize) ? true : false; | 188 return (total_read == kInterruptMessageSize) ? true : false; |
| 191 } | 189 } |
| 192 | 190 |
| 193 void EventHandlerImplementation::HandleInterruptFd() { | 191 void EventHandlerImplementation::HandleInterruptFd() { |
| 194 InterruptMessage msg; | 192 InterruptMessage msg; |
| 195 while (GetInterruptMessage(&msg)) { | 193 while (GetInterruptMessage(&msg)) { |
| 196 if (msg.id == kTimerId) { | 194 if (msg.id == kTimerId) { |
| 197 timeout_ = msg.data; | 195 timeout_queue_.UpdateTimeout(msg.dart_port, msg.data); |
| 198 timeout_port_ = msg.dart_port; | |
| 199 } else if (msg.id == kShutdownId) { | 196 } else if (msg.id == kShutdownId) { |
| 200 shutdown_ = true; | 197 shutdown_ = true; |
| 201 } else { | 198 } else { |
| 202 SocketData* sd = GetSocketData(msg.id); | 199 SocketData* sd = GetSocketData(msg.id); |
| 203 if ((msg.data & (1 << kShutdownReadCommand)) != 0) { | 200 if ((msg.data & (1 << kShutdownReadCommand)) != 0) { |
| 204 ASSERT(msg.data == (1 << kShutdownReadCommand)); | 201 ASSERT(msg.data == (1 << kShutdownReadCommand)); |
| 205 // Close the socket for reading. | 202 // Close the socket for reading. |
| 206 sd->ShutdownRead(); | 203 sd->ShutdownRead(); |
| 207 UpdateEpollInstance(epoll_fd_, sd); | 204 UpdateEpollInstance(epoll_fd_, sd); |
| 208 } else if ((msg.data & (1 << kShutdownWriteCommand)) != 0) { | 205 } else if ((msg.data & (1 << kShutdownWriteCommand)) != 0) { |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 ASSERT(port != 0); | 350 ASSERT(port != 0); |
| 354 DartUtils::PostInt32(port, event_mask); | 351 DartUtils::PostInt32(port, event_mask); |
| 355 } | 352 } |
| 356 } | 353 } |
| 357 } | 354 } |
| 358 HandleInterruptFd(); | 355 HandleInterruptFd(); |
| 359 } | 356 } |
| 360 | 357 |
| 361 | 358 |
| 362 int64_t EventHandlerImplementation::GetTimeout() { | 359 int64_t EventHandlerImplementation::GetTimeout() { |
| 363 if (timeout_ == kInfinityTimeout) { | 360 if (!timeout_queue_.HasTimeout()) { |
| 364 return kInfinityTimeout; | 361 return kInfinityTimeout; |
| 365 } | 362 } |
| 366 int64_t millis = timeout_ - TimerUtils::GetCurrentTimeMilliseconds(); | 363 int64_t millis = timeout_queue_.CurrentTimeout() - |
| 364 TimerUtils::GetCurrentTimeMilliseconds(); |
| 367 return (millis < 0) ? 0 : millis; | 365 return (millis < 0) ? 0 : millis; |
| 368 } | 366 } |
| 369 | 367 |
| 370 | 368 |
| 371 void EventHandlerImplementation::HandleTimeout() { | 369 void EventHandlerImplementation::HandleTimeout() { |
| 372 if (timeout_ != kInfinityTimeout) { | 370 if (timeout_queue_.HasTimeout()) { |
| 373 int64_t millis = timeout_ - TimerUtils::GetCurrentTimeMilliseconds(); | 371 int64_t millis = timeout_queue_.CurrentTimeout() - |
| 372 TimerUtils::GetCurrentTimeMilliseconds(); |
| 374 if (millis <= 0) { | 373 if (millis <= 0) { |
| 375 DartUtils::PostNull(timeout_port_); | 374 DartUtils::PostNull(timeout_queue_.CurrentPort()); |
| 376 timeout_ = kInfinityTimeout; | 375 timeout_queue_.RemoveCurrent(); |
| 377 timeout_port_ = 0; | |
| 378 } | 376 } |
| 379 } | 377 } |
| 380 } | 378 } |
| 381 | 379 |
| 382 | 380 |
| 383 void EventHandlerImplementation::Poll(uword args) { | 381 void EventHandlerImplementation::Poll(uword args) { |
| 384 static const intptr_t kMaxEvents = 16; | 382 static const intptr_t kMaxEvents = 16; |
| 385 struct epoll_event events[kMaxEvents]; | 383 struct epoll_event events[kMaxEvents]; |
| 386 EventHandlerImplementation* handler = | 384 EventHandlerImplementation* handler = |
| 387 reinterpret_cast<EventHandlerImplementation*>(args); | 385 reinterpret_cast<EventHandlerImplementation*>(args); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 | 434 |
| 437 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { | 435 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { |
| 438 // The hashmap does not support keys with value 0. | 436 // The hashmap does not support keys with value 0. |
| 439 return dart::Utils::WordHash(fd + 1); | 437 return dart::Utils::WordHash(fd + 1); |
| 440 } | 438 } |
| 441 | 439 |
| 442 } // namespace bin | 440 } // namespace bin |
| 443 } // namespace dart | 441 } // namespace dart |
| 444 | 442 |
| 445 #endif // defined(TARGET_OS_ANDROID) | 443 #endif // defined(TARGET_OS_ANDROID) |
| OLD | NEW |