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 "bin/eventhandler.h" | 5 #include "bin/eventhandler.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <pthread.h> | 8 #include <pthread.h> |
9 #include <stdio.h> | 9 #include <stdio.h> |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 delete sd; | 225 delete sd; |
226 } else { | 226 } else { |
227 // Setup events to wait for. | 227 // Setup events to wait for. |
228 sd->SetPortAndMask(msg.dart_port, msg.data); | 228 sd->SetPortAndMask(msg.dart_port, msg.data); |
229 UpdateKqueue(kqueue_fd_, sd); | 229 UpdateKqueue(kqueue_fd_, sd); |
230 } | 230 } |
231 } | 231 } |
232 } | 232 } |
233 } | 233 } |
234 | 234 |
235 | |
236 #ifdef DEBUG_KQUEUE | 235 #ifdef DEBUG_KQUEUE |
237 static void PrintEventMask(intptr_t fd, struct kevent* event) { | 236 static void PrintEventMask(intptr_t fd, struct kevent* event) { |
238 printf("%d ", fd); | 237 printf("%d ", static_cast<int>(fd)); |
239 if (event->filter == EVFILT_READ) printf("EVFILT_READ "); | 238 if (event->filter == EVFILT_READ) printf("EVFILT_READ "); |
240 if (event->filter == EVFILT_WRITE) printf("EVFILT_WRITE "); | 239 if (event->filter == EVFILT_WRITE) printf("EVFILT_WRITE "); |
241 printf("flags: %x: ", event->flags); | 240 printf("flags: %x: ", event->flags); |
242 if ((event->flags & EV_EOF) != 0) printf("EV_EOF "); | 241 if ((event->flags & EV_EOF) != 0) printf("EV_EOF "); |
243 if ((event->flags & EV_ERROR) != 0) printf("EV_ERROR "); | 242 if ((event->flags & EV_ERROR) != 0) printf("EV_ERROR "); |
244 printf("(available %d) ", FDUtils::AvailableBytes(fd)); | 243 printf("- fflags: %d ", event->fflags); |
| 244 printf("(available %d) ", static_cast<int>(FDUtils::AvailableBytes(fd))); |
245 printf("\n"); | 245 printf("\n"); |
246 } | 246 } |
247 #endif | 247 #endif |
248 | 248 |
249 | 249 |
250 intptr_t EventHandlerImplementation::GetEvents(struct kevent* event, | 250 intptr_t EventHandlerImplementation::GetEvents(struct kevent* event, |
251 SocketData* sd) { | 251 SocketData* sd) { |
252 #ifdef DEBUG_KQUEUE | 252 #ifdef DEBUG_KQUEUE |
253 PrintEventMask(sd->fd(), event); | 253 PrintEventMask(sd->fd(), event); |
254 #endif | 254 #endif |
255 intptr_t event_mask = 0; | 255 intptr_t event_mask = 0; |
256 if (sd->IsListeningSocket()) { | 256 if (sd->IsListeningSocket()) { |
257 // On a listening socket the READ event means that there are | 257 // On a listening socket the READ event means that there are |
258 // connections ready to be accepted. | 258 // connections ready to be accepted. |
259 if (event->filter == EVFILT_READ) { | 259 if (event->filter == EVFILT_READ) { |
260 if ((event->flags & EV_EOF) != 0) event_mask |= (1 << kCloseEvent); | 260 if ((event->flags & EV_EOF) != 0) { |
261 if ((event->flags & EV_ERROR) != 0) event_mask |= (1 << kErrorEvent); | 261 if (event->fflags != 0) { |
| 262 event_mask |= (1 << kErrorEvent); |
| 263 } else { |
| 264 event_mask |= (1 << kCloseEvent); |
| 265 } |
| 266 } |
262 if (event_mask == 0) event_mask |= (1 << kInEvent); | 267 if (event_mask == 0) event_mask |= (1 << kInEvent); |
263 } | 268 } |
264 } else { | 269 } else { |
265 // Prioritize data events over close and error events. | 270 // Prioritize data events over close and error events. |
266 if (event->filter == EVFILT_READ) { | 271 if (event->filter == EVFILT_READ) { |
267 if (FDUtils::AvailableBytes(sd->fd()) != 0) { | 272 if (FDUtils::AvailableBytes(sd->fd()) != 0) { |
268 event_mask = (1 << kInEvent); | 273 event_mask = (1 << kInEvent); |
269 } else if ((event->flags & EV_EOF) != 0) { | 274 } else if ((event->flags & EV_EOF) != 0) { |
270 event_mask = (1 << kCloseEvent); | 275 if (event->fflags != 0) { |
| 276 event_mask |= (1 << kErrorEvent); |
| 277 } else { |
| 278 event_mask |= (1 << kCloseEvent); |
| 279 } |
271 sd->MarkClosedRead(); | 280 sd->MarkClosedRead(); |
272 } else if ((event->flags & EV_ERROR) != 0) { | |
273 event_mask = (1 << kErrorEvent); | |
274 } | 281 } |
275 } | 282 } |
276 | 283 |
277 if (event->filter == EVFILT_WRITE) { | 284 if (event->filter == EVFILT_WRITE) { |
278 if ((event->flags & EV_ERROR) != 0) { | 285 if ((event->flags & EV_EOF) != 0) { |
279 event_mask = (1 << kErrorEvent); | 286 if (event->fflags != 0) { |
280 sd->MarkClosedWrite(); | 287 event_mask |= (1 << kErrorEvent); |
281 } else if ((event->flags & EV_EOF) != 0) { | 288 } else { |
| 289 event_mask |= (1 << kCloseEvent); |
| 290 } |
282 // If the receiver closed for reading, close for writing, | 291 // If the receiver closed for reading, close for writing, |
283 // update the registration with kqueue, and do not report a | 292 // update the registration with kqueue, and do not report a |
284 // write event. | 293 // write event. |
285 sd->MarkClosedWrite(); | 294 sd->MarkClosedWrite(); |
286 UpdateKqueue(kqueue_fd_, sd); | 295 UpdateKqueue(kqueue_fd_, sd); |
287 } else { | 296 } else { |
288 event_mask |= (1 << kOutEvent); | 297 event_mask |= (1 << kOutEvent); |
289 } | 298 } |
290 } | 299 } |
291 } | 300 } |
292 | 301 |
293 return event_mask; | 302 return event_mask; |
294 } | 303 } |
295 | 304 |
296 | 305 |
297 void EventHandlerImplementation::HandleEvents(struct kevent* events, | 306 void EventHandlerImplementation::HandleEvents(struct kevent* events, |
298 int size) { | 307 int size) { |
299 for (int i = 0; i < size; i++) { | 308 for (int i = 0; i < size; i++) { |
| 309 // If flag EV_ERROR is set it indicates an error in kevent processing. |
| 310 if ((events[i].flags & EV_ERROR) != 0) { |
| 311 FATAL1("kevent failed %s\n", strerror(events[i].data)); |
| 312 } |
300 if (events[i].udata != NULL) { | 313 if (events[i].udata != NULL) { |
301 SocketData* sd = reinterpret_cast<SocketData*>(events[i].udata); | 314 SocketData* sd = reinterpret_cast<SocketData*>(events[i].udata); |
302 intptr_t event_mask = GetEvents(events + i, sd); | 315 intptr_t event_mask = GetEvents(events + i, sd); |
303 if (event_mask != 0) { | 316 if (event_mask != 0) { |
304 // Unregister events for the file descriptor. Events will be | 317 // Unregister events for the file descriptor. Events will be |
305 // registered again when the current event has been handled in | 318 // registered again when the current event has been handled in |
306 // Dart code. | 319 // Dart code. |
307 RemoveFromKqueue(kqueue_fd_, sd); | 320 RemoveFromKqueue(kqueue_fd_, sd); |
308 Dart_Port port = sd->port(); | 321 Dart_Port port = sd->port(); |
309 ASSERT(port != 0); | 322 ASSERT(port != 0); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 ts.tv_nsec = (millis - (ts.tv_sec * 1000)) * 1000000; | 366 ts.tv_nsec = (millis - (ts.tv_sec * 1000)) * 1000000; |
354 timeout = &ts; | 367 timeout = &ts; |
355 } | 368 } |
356 intptr_t result = TEMP_FAILURE_RETRY(kevent(handler->kqueue_fd_, | 369 intptr_t result = TEMP_FAILURE_RETRY(kevent(handler->kqueue_fd_, |
357 NULL, | 370 NULL, |
358 0, | 371 0, |
359 events, | 372 events, |
360 kMaxEvents, | 373 kMaxEvents, |
361 timeout)); | 374 timeout)); |
362 if (result == -1) { | 375 if (result == -1) { |
363 perror("kevent failed"); | 376 FATAL1("kevent failed %s\n", strerror(errno)); |
364 FATAL("kevent failed\n"); | |
365 } else { | 377 } else { |
366 handler->HandleTimeout(); | 378 handler->HandleTimeout(); |
367 handler->HandleEvents(events, result); | 379 handler->HandleEvents(events, result); |
368 } | 380 } |
369 } | 381 } |
370 } | 382 } |
371 | 383 |
372 | 384 |
373 void EventHandlerImplementation::StartEventHandler() { | 385 void EventHandlerImplementation::StartEventHandler() { |
374 int result = | 386 int result = |
(...skipping 15 matching lines...) Expand all Loading... |
390 void* EventHandlerImplementation::GetHashmapKeyFromFd(intptr_t fd) { | 402 void* EventHandlerImplementation::GetHashmapKeyFromFd(intptr_t fd) { |
391 // The hashmap does not support keys with value 0. | 403 // The hashmap does not support keys with value 0. |
392 return reinterpret_cast<void*>(fd + 1); | 404 return reinterpret_cast<void*>(fd + 1); |
393 } | 405 } |
394 | 406 |
395 | 407 |
396 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { | 408 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { |
397 // The hashmap does not support keys with value 0. | 409 // The hashmap does not support keys with value 0. |
398 return dart::Utils::WordHash(fd + 1); | 410 return dart::Utils::WordHash(fd + 1); |
399 } | 411 } |
OLD | NEW |