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_LINUX) | 6 #if defined(TARGET_OS_LINUX) |
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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 EventHandlerImplementation::EventHandlerImplementation() | 104 EventHandlerImplementation::EventHandlerImplementation() |
105 : socket_map_(&HashMap::SamePointerValue, 16) { | 105 : socket_map_(&HashMap::SamePointerValue, 16) { |
106 intptr_t result; | 106 intptr_t result; |
107 result = TEMP_FAILURE_RETRY(pipe(interrupt_fds_)); | 107 result = TEMP_FAILURE_RETRY(pipe(interrupt_fds_)); |
108 if (result != 0) { | 108 if (result != 0) { |
109 FATAL("Pipe creation failed"); | 109 FATAL("Pipe creation failed"); |
110 } | 110 } |
111 FDUtils::SetNonBlocking(interrupt_fds_[0]); | 111 FDUtils::SetNonBlocking(interrupt_fds_[0]); |
112 FDUtils::SetCloseOnExec(interrupt_fds_[0]); | 112 FDUtils::SetCloseOnExec(interrupt_fds_[0]); |
113 FDUtils::SetCloseOnExec(interrupt_fds_[1]); | 113 FDUtils::SetCloseOnExec(interrupt_fds_[1]); |
114 timeout_ = kInfinityTimeout; | |
115 timeout_port_ = 0; | |
116 shutdown_ = false; | 114 shutdown_ = false; |
117 // The initial size passed to epoll_create is ignore on newer (>= | 115 // The initial size passed to epoll_create is ignore on newer (>= |
118 // 2.6.8) Linux versions | 116 // 2.6.8) Linux versions |
119 static const int kEpollInitialSize = 64; | 117 static const int kEpollInitialSize = 64; |
120 epoll_fd_ = TEMP_FAILURE_RETRY(epoll_create(kEpollInitialSize)); | 118 epoll_fd_ = TEMP_FAILURE_RETRY(epoll_create(kEpollInitialSize)); |
121 if (epoll_fd_ == -1) { | 119 if (epoll_fd_ == -1) { |
122 FATAL("Failed creating epoll file descriptor"); | 120 FATAL("Failed creating epoll file descriptor"); |
123 } | 121 } |
124 FDUtils::SetCloseOnExec(epoll_fd_); | 122 FDUtils::SetCloseOnExec(epoll_fd_); |
125 // Register the interrupt_fd with the epoll instance. | 123 // Register the interrupt_fd with the epoll instance. |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 bytes_read = TEMP_FAILURE_RETRY(read(interrupt_fds_[0], | 189 bytes_read = TEMP_FAILURE_RETRY(read(interrupt_fds_[0], |
192 dst + total_read, | 190 dst + total_read, |
193 kInterruptMessageSize - total_read)); | 191 kInterruptMessageSize - total_read)); |
194 if (bytes_read > 0) { | 192 if (bytes_read > 0) { |
195 total_read = total_read + bytes_read; | 193 total_read = total_read + bytes_read; |
196 } | 194 } |
197 } | 195 } |
198 return (total_read == kInterruptMessageSize) ? true : false; | 196 return (total_read == kInterruptMessageSize) ? true : false; |
199 } | 197 } |
200 | 198 |
| 199 |
201 void EventHandlerImplementation::HandleInterruptFd() { | 200 void EventHandlerImplementation::HandleInterruptFd() { |
202 InterruptMessage msg; | 201 InterruptMessage msg; |
203 while (GetInterruptMessage(&msg)) { | 202 while (GetInterruptMessage(&msg)) { |
204 if (msg.id == kTimerId) { | 203 if (msg.id == kTimerId) { |
205 timeout_ = msg.data; | 204 timeout_queue_.UpdateTimeout(msg.dart_port, msg.data); |
206 timeout_port_ = msg.dart_port; | |
207 } else if (msg.id == kShutdownId) { | 205 } else if (msg.id == kShutdownId) { |
208 shutdown_ = true; | 206 shutdown_ = true; |
209 } else { | 207 } else { |
210 SocketData* sd = GetSocketData(msg.id); | 208 SocketData* sd = GetSocketData(msg.id); |
211 if ((msg.data & (1 << kShutdownReadCommand)) != 0) { | 209 if ((msg.data & (1 << kShutdownReadCommand)) != 0) { |
212 ASSERT(msg.data == (1 << kShutdownReadCommand)); | 210 ASSERT(msg.data == (1 << kShutdownReadCommand)); |
213 // Close the socket for reading. | 211 // Close the socket for reading. |
214 sd->ShutdownRead(); | 212 sd->ShutdownRead(); |
215 UpdateEpollInstance(epoll_fd_, sd); | 213 UpdateEpollInstance(epoll_fd_, sd); |
216 } else if ((msg.data & (1 << kShutdownWriteCommand)) != 0) { | 214 } else if ((msg.data & (1 << kShutdownWriteCommand)) != 0) { |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 } | 356 } |
359 } | 357 } |
360 } | 358 } |
361 // Handle after socket events, so we avoid closing a socket before we handle | 359 // Handle after socket events, so we avoid closing a socket before we handle |
362 // the current events. | 360 // the current events. |
363 HandleInterruptFd(); | 361 HandleInterruptFd(); |
364 } | 362 } |
365 | 363 |
366 | 364 |
367 int64_t EventHandlerImplementation::GetTimeout() { | 365 int64_t EventHandlerImplementation::GetTimeout() { |
368 if (timeout_ == kInfinityTimeout) { | 366 if (!timeout_queue_.HasTimeout()) { |
369 return kInfinityTimeout; | 367 return kInfinityTimeout; |
370 } | 368 } |
371 int64_t millis = timeout_ - TimerUtils::GetCurrentTimeMilliseconds(); | 369 int64_t millis = timeout_queue_.CurrentTimeout() - |
| 370 TimerUtils::GetCurrentTimeMilliseconds(); |
372 return (millis < 0) ? 0 : millis; | 371 return (millis < 0) ? 0 : millis; |
373 } | 372 } |
374 | 373 |
375 | 374 |
376 void EventHandlerImplementation::HandleTimeout() { | 375 void EventHandlerImplementation::HandleTimeout() { |
377 if (timeout_ != kInfinityTimeout) { | 376 if (timeout_queue_.HasTimeout()) { |
378 int64_t millis = timeout_ - TimerUtils::GetCurrentTimeMilliseconds(); | 377 int64_t millis = timeout_queue_.CurrentTimeout() - |
| 378 TimerUtils::GetCurrentTimeMilliseconds(); |
379 if (millis <= 0) { | 379 if (millis <= 0) { |
380 DartUtils::PostNull(timeout_port_); | 380 DartUtils::PostNull(timeout_queue_.CurrentPort()); |
381 timeout_ = kInfinityTimeout; | 381 // Remove current from queue. |
382 timeout_port_ = 0; | 382 timeout_queue_.RemoveCurrent(); |
383 } | 383 } |
384 } | 384 } |
385 } | 385 } |
386 | 386 |
387 | 387 |
388 void EventHandlerImplementation::Poll(uword args) { | 388 void EventHandlerImplementation::Poll(uword args) { |
389 static const intptr_t kMaxEvents = 16; | 389 static const intptr_t kMaxEvents = 16; |
390 struct epoll_event events[kMaxEvents]; | 390 struct epoll_event events[kMaxEvents]; |
391 EventHandler* handler = reinterpret_cast<EventHandler*>(args); | 391 EventHandler* handler = reinterpret_cast<EventHandler*>(args); |
392 EventHandlerImplementation* handler_impl = &handler->delegate_; | 392 EventHandlerImplementation* handler_impl = &handler->delegate_; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 | 442 |
443 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { | 443 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { |
444 // The hashmap does not support keys with value 0. | 444 // The hashmap does not support keys with value 0. |
445 return dart::Utils::WordHash(fd + 1); | 445 return dart::Utils::WordHash(fd + 1); |
446 } | 446 } |
447 | 447 |
448 } // namespace bin | 448 } // namespace bin |
449 } // namespace dart | 449 } // namespace dart |
450 | 450 |
451 #endif // defined(TARGET_OS_LINUX) | 451 #endif // defined(TARGET_OS_LINUX) |
OLD | NEW |