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

Side by Side Diff: runtime/bin/eventhandler_linux.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
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_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 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 FDUtils::WriteToBlocking(interrupt_fds_[1], &msg, kInterruptMessageSize); 173 FDUtils::WriteToBlocking(interrupt_fds_[1], &msg, kInterruptMessageSize);
174 if (result != kInterruptMessageSize) { 174 if (result != kInterruptMessageSize) {
175 if (result == -1) { 175 if (result == -1) {
176 perror("Interrupt message failure:"); 176 perror("Interrupt message failure:");
177 } 177 }
178 FATAL1("Interrupt message failure. Wrote %" Pd " bytes.", result); 178 FATAL1("Interrupt message failure. Wrote %" Pd " bytes.", result);
179 } 179 }
180 } 180 }
181 181
182 182
183 bool EventHandlerImplementation::GetInterruptMessage(InterruptMessage* msg) {
184 char* dst = reinterpret_cast<char*>(msg);
185 int total_read = 0;
186 int bytes_read =
187 TEMP_FAILURE_RETRY(read(interrupt_fds_[0], dst, kInterruptMessageSize));
188 if (bytes_read < 0) {
189 return false;
190 }
191 total_read = bytes_read;
192 while (total_read < kInterruptMessageSize) {
193 bytes_read = TEMP_FAILURE_RETRY(read(interrupt_fds_[0],
194 dst + total_read,
195 kInterruptMessageSize - total_read));
196 if (bytes_read > 0) {
197 total_read = total_read + bytes_read;
198 }
199 }
200 return (total_read == kInterruptMessageSize) ? true : false;
201 }
202
203
204 void EventHandlerImplementation::HandleInterruptFd() { 183 void EventHandlerImplementation::HandleInterruptFd() {
205 InterruptMessage msg; 184 InterruptMessage msg;
206 while (GetInterruptMessage(&msg)) { 185 intptr_t available = FDUtils::AvailableBytes(interrupt_fds_[0]);
186 for (int i = 0;
187 i + kInterruptMessageSize <= available;
188 i += kInterruptMessageSize) {
189 VOID_TEMP_FAILURE_RETRY(read(interrupt_fds_[0],
190 reinterpret_cast<char*>(&msg),
191 kInterruptMessageSize));
207 if (msg.id == kTimerId) { 192 if (msg.id == kTimerId) {
208 timeout_queue_.UpdateTimeout(msg.dart_port, msg.data); 193 timeout_queue_.UpdateTimeout(msg.dart_port, msg.data);
209 } else if (msg.id == kShutdownId) { 194 } else if (msg.id == kShutdownId) {
210 shutdown_ = true; 195 shutdown_ = true;
211 } else { 196 } else {
212 SocketData* sd = GetSocketData(msg.id); 197 SocketData* sd = GetSocketData(msg.id);
213 if ((msg.data & (1 << kShutdownReadCommand)) != 0) { 198 if ((msg.data & (1 << kShutdownReadCommand)) != 0) {
214 ASSERT(msg.data == (1 << kShutdownReadCommand)); 199 ASSERT(msg.data == (1 << kShutdownReadCommand));
215 // Close the socket for reading. 200 // Close the socket for reading.
216 sd->ShutdownRead(); 201 sd->ShutdownRead();
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 } 327 }
343 } 328 }
344 } 329 }
345 330
346 return event_mask; 331 return event_mask;
347 } 332 }
348 333
349 334
350 void EventHandlerImplementation::HandleEvents(struct epoll_event* events, 335 void EventHandlerImplementation::HandleEvents(struct epoll_event* events,
351 int size) { 336 int size) {
337 bool interrupt_seen = false;
352 for (int i = 0; i < size; i++) { 338 for (int i = 0; i < size; i++) {
353 if (events[i].data.ptr != NULL) { 339 if (events[i].data.ptr == NULL) {
340 interrupt_seen = true;
341 } else {
354 SocketData* sd = reinterpret_cast<SocketData*>(events[i].data.ptr); 342 SocketData* sd = reinterpret_cast<SocketData*>(events[i].data.ptr);
355 intptr_t event_mask = GetPollEvents(events[i].events, sd); 343 intptr_t event_mask = GetPollEvents(events[i].events, sd);
356 if (event_mask != 0) { 344 if (event_mask != 0) {
357 // Unregister events for the file descriptor. Events will be 345 // Unregister events for the file descriptor. Events will be
358 // registered again when the current event has been handled in 346 // registered again when the current event has been handled in
359 // Dart code. 347 // Dart code.
360 RemoveFromEpollInstance(epoll_fd_, sd); 348 RemoveFromEpollInstance(epoll_fd_, sd);
361 Dart_Port port = sd->port(); 349 Dart_Port port = sd->port();
362 ASSERT(port != 0); 350 ASSERT(port != 0);
363 DartUtils::PostInt32(port, event_mask); 351 DartUtils::PostInt32(port, event_mask);
364 } 352 }
365 } 353 }
366 } 354 }
367 // Handle after socket events, so we avoid closing a socket before we handle 355 if (interrupt_seen) {
368 // the current events. 356 // Handle after socket events, so we avoid closing a socket before we handle
369 HandleInterruptFd(); 357 // the current events.
358 HandleInterruptFd();
359 }
370 } 360 }
371 361
372 362
373 int64_t EventHandlerImplementation::GetTimeout() { 363 int64_t EventHandlerImplementation::GetTimeout() {
374 if (!timeout_queue_.HasTimeout()) { 364 if (!timeout_queue_.HasTimeout()) {
375 return kInfinityTimeout; 365 return kInfinityTimeout;
376 } 366 }
377 int64_t millis = timeout_queue_.CurrentTimeout() - 367 int64_t millis = timeout_queue_.CurrentTimeout() -
378 TimerUtils::GetCurrentTimeMilliseconds(); 368 TimerUtils::GetCurrentTimeMilliseconds();
379 return (millis < 0) ? 0 : millis; 369 return (millis < 0) ? 0 : millis;
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 440
451 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { 441 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) {
452 // The hashmap does not support keys with value 0. 442 // The hashmap does not support keys with value 0.
453 return dart::Utils::WordHash(fd + 1); 443 return dart::Utils::WordHash(fd + 1);
454 } 444 }
455 445
456 } // namespace bin 446 } // namespace bin
457 } // namespace dart 447 } // namespace dart
458 448
459 #endif // defined(TARGET_OS_LINUX) 449 #endif // defined(TARGET_OS_LINUX)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698