Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Side by Side Diff: runtime/bin/eventhandler_android.cc

Issue 77983002: Only read from interrupt_fd when data is available. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/eventhandler_android.h ('k') | runtime/bin/eventhandler_linux.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 FDUtils::WriteToBlocking(interrupt_fds_[1], &msg, kInterruptMessageSize); 168 FDUtils::WriteToBlocking(interrupt_fds_[1], &msg, kInterruptMessageSize);
169 if (result != kInterruptMessageSize) { 169 if (result != kInterruptMessageSize) {
170 if (result == -1) { 170 if (result == -1) {
171 perror("Interrupt message failure:"); 171 perror("Interrupt message failure:");
172 } 172 }
173 FATAL1("Interrupt message failure. Wrote %d bytes.", result); 173 FATAL1("Interrupt message failure. Wrote %d bytes.", result);
174 } 174 }
175 } 175 }
176 176
177 177
178 bool EventHandlerImplementation::GetInterruptMessage(InterruptMessage* msg) {
179 char* dst = reinterpret_cast<char*>(msg);
180 int total_read = 0;
181 int bytes_read =
182 TEMP_FAILURE_RETRY(read(interrupt_fds_[0], dst, kInterruptMessageSize));
183 if (bytes_read < 0) {
184 return false;
185 }
186 total_read = bytes_read;
187 while (total_read < kInterruptMessageSize) {
188 bytes_read = TEMP_FAILURE_RETRY(read(interrupt_fds_[0],
189 dst + total_read,
190 kInterruptMessageSize - total_read));
191 if (bytes_read > 0) {
192 total_read = total_read + bytes_read;
193 }
194 }
195 return (total_read == kInterruptMessageSize) ? true : false;
196 }
197
198 void EventHandlerImplementation::HandleInterruptFd() { 178 void EventHandlerImplementation::HandleInterruptFd() {
199 InterruptMessage msg; 179 InterruptMessage msg;
200 while (GetInterruptMessage(&msg)) { 180 intptr_t available = FDUtils::AvailableBytes(interrupt_fds_[0]);
181 for (int i = 0;
182 i + kInterruptMessageSize <= available;
183 i += kInterruptMessageSize) {
184 VOID_TEMP_FAILURE_RETRY(read(interrupt_fds_[0],
185 reinterpret_cast<char*>(&msg),
186 kInterruptMessageSize));
Søren Gjesse 2013/11/20 09:46:09 We could even consider reading more than one at th
Anders Johnsen 2013/11/20 10:26:39 There should rarely be more than one at a time. I'
201 if (msg.id == kTimerId) { 187 if (msg.id == kTimerId) {
202 timeout_queue_.UpdateTimeout(msg.dart_port, msg.data); 188 timeout_queue_.UpdateTimeout(msg.dart_port, msg.data);
203 } else if (msg.id == kShutdownId) { 189 } else if (msg.id == kShutdownId) {
204 shutdown_ = true; 190 shutdown_ = true;
205 } else { 191 } else {
206 SocketData* sd = GetSocketData(msg.id); 192 SocketData* sd = GetSocketData(msg.id);
207 if ((msg.data & (1 << kShutdownReadCommand)) != 0) { 193 if ((msg.data & (1 << kShutdownReadCommand)) != 0) {
208 ASSERT(msg.data == (1 << kShutdownReadCommand)); 194 ASSERT(msg.data == (1 << kShutdownReadCommand));
209 // Close the socket for reading. 195 // Close the socket for reading.
210 sd->ShutdownRead(); 196 sd->ShutdownRead();
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 } 327 }
342 } 328 }
343 } 329 }
344 330
345 return event_mask; 331 return event_mask;
346 } 332 }
347 333
348 334
349 void EventHandlerImplementation::HandleEvents(struct epoll_event* events, 335 void EventHandlerImplementation::HandleEvents(struct epoll_event* events,
350 int size) { 336 int size) {
337 bool interrupt_seen = false;
351 for (int i = 0; i < size; i++) { 338 for (int i = 0; i < size; i++) {
352 if (events[i].data.ptr != NULL) { 339 if (events[i].data.ptr == NULL) {
340 interrupt_seen = true;
341 } else {
353 SocketData* sd = reinterpret_cast<SocketData*>(events[i].data.ptr); 342 SocketData* sd = reinterpret_cast<SocketData*>(events[i].data.ptr);
354 intptr_t event_mask = GetPollEvents(events[i].events, sd); 343 intptr_t event_mask = GetPollEvents(events[i].events, sd);
355 if (event_mask != 0) { 344 if (event_mask != 0) {
356 // Unregister events for the file descriptor. Events will be 345 // Unregister events for the file descriptor. Events will be
357 // registered again when the current event has been handled in 346 // registered again when the current event has been handled in
358 // Dart code. 347 // Dart code.
359 RemoveFromEpollInstance(epoll_fd_, sd); 348 RemoveFromEpollInstance(epoll_fd_, sd);
360 Dart_Port port = sd->port(); 349 Dart_Port port = sd->port();
361 ASSERT(port != 0); 350 ASSERT(port != 0);
362 DartUtils::PostInt32(port, event_mask); 351 DartUtils::PostInt32(port, event_mask);
363 } 352 }
364 } 353 }
365 } 354 }
366 HandleInterruptFd(); 355 if (interrupt_seen) {
356 // Handle after socket events, so we avoid closing a socket before we handle
357 // the current events.
358 HandleInterruptFd();
359 }
367 } 360 }
368 361
369 362
370 int64_t EventHandlerImplementation::GetTimeout() { 363 int64_t EventHandlerImplementation::GetTimeout() {
371 if (!timeout_queue_.HasTimeout()) { 364 if (!timeout_queue_.HasTimeout()) {
372 return kInfinityTimeout; 365 return kInfinityTimeout;
373 } 366 }
374 int64_t millis = timeout_queue_.CurrentTimeout() - 367 int64_t millis = timeout_queue_.CurrentTimeout() -
375 TimerUtils::GetCurrentTimeMilliseconds(); 368 TimerUtils::GetCurrentTimeMilliseconds();
376 return (millis < 0) ? 0 : millis; 369 return (millis < 0) ? 0 : millis;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 438
446 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { 439 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) {
447 // The hashmap does not support keys with value 0. 440 // The hashmap does not support keys with value 0.
448 return dart::Utils::WordHash(fd + 1); 441 return dart::Utils::WordHash(fd + 1);
449 } 442 }
450 443
451 } // namespace bin 444 } // namespace bin
452 } // namespace dart 445 } // namespace dart
453 446
454 #endif // defined(TARGET_OS_ANDROID) 447 #endif // defined(TARGET_OS_ANDROID)
OLDNEW
« no previous file with comments | « runtime/bin/eventhandler_android.h ('k') | runtime/bin/eventhandler_linux.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698