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" |
| 6 |
5 #include <errno.h> | 7 #include <errno.h> |
6 #include <poll.h> | 8 #include <poll.h> |
7 #include <pthread.h> | 9 #include <pthread.h> |
8 #include <stdio.h> | 10 #include <stdio.h> |
9 #include <string.h> | 11 #include <string.h> |
10 #include <sys/time.h> | 12 #include <sys/time.h> |
11 #include <unistd.h> | 13 #include <unistd.h> |
12 | 14 |
13 #include "bin/eventhandler.h" | |
14 #include "bin/fdutils.h" | 15 #include "bin/fdutils.h" |
15 #include "bin/hashmap.h" | 16 #include "bin/hashmap.h" |
16 #include "platform/utils.h" | 17 #include "platform/utils.h" |
17 | 18 |
18 | 19 |
19 int64_t GetCurrentTimeMilliseconds() { | 20 int64_t GetCurrentTimeMilliseconds() { |
20 struct timeval tv; | 21 struct timeval tv; |
21 if (gettimeofday(&tv, NULL) < 0) { | 22 if (gettimeofday(&tv, NULL) < 0) { |
22 UNREACHABLE(); | 23 UNREACHABLE(); |
23 return 0; | 24 return 0; |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 intptr_t millis = timeout_ - GetCurrentTimeMilliseconds(); | 328 intptr_t millis = timeout_ - GetCurrentTimeMilliseconds(); |
328 if (millis <= 0) { | 329 if (millis <= 0) { |
329 Dart_PostIntArray(timeout_port_, 0, NULL); | 330 Dart_PostIntArray(timeout_port_, 0, NULL); |
330 timeout_ = kInfinityTimeout; | 331 timeout_ = kInfinityTimeout; |
331 timeout_port_ = 0; | 332 timeout_port_ = 0; |
332 } | 333 } |
333 } | 334 } |
334 } | 335 } |
335 | 336 |
336 | 337 |
337 void* EventHandlerImplementation::Poll(void* args) { | 338 void EventHandlerImplementation::Poll(uword args) { |
338 intptr_t pollfds_size; | 339 intptr_t pollfds_size; |
339 struct pollfd* pollfds; | 340 struct pollfd* pollfds; |
340 EventHandlerImplementation* handler = | 341 EventHandlerImplementation* handler = |
341 reinterpret_cast<EventHandlerImplementation*>(args); | 342 reinterpret_cast<EventHandlerImplementation*>(args); |
| 343 ASSERT(handler != NULL); |
342 while (1) { | 344 while (1) { |
343 pollfds = handler->GetPollFds(&pollfds_size); | 345 pollfds = handler->GetPollFds(&pollfds_size); |
344 intptr_t millis = handler->GetTimeout(); | 346 intptr_t millis = handler->GetTimeout(); |
345 intptr_t result = TEMP_FAILURE_RETRY(poll(pollfds, pollfds_size, millis)); | 347 intptr_t result = TEMP_FAILURE_RETRY(poll(pollfds, pollfds_size, millis)); |
346 ASSERT(EAGAIN == EWOULDBLOCK); | 348 ASSERT(EAGAIN == EWOULDBLOCK); |
347 if (result == -1) { | 349 if (result == -1) { |
348 if (errno != EWOULDBLOCK) { | 350 if (errno != EWOULDBLOCK) { |
349 perror("Poll failed"); | 351 perror("Poll failed"); |
350 } | 352 } |
351 } else { | 353 } else { |
352 handler->HandleTimeout(); | 354 handler->HandleTimeout(); |
353 handler->HandleEvents(pollfds, pollfds_size, result); | 355 handler->HandleEvents(pollfds, pollfds_size, result); |
354 } | 356 } |
355 free(pollfds); | 357 free(pollfds); |
356 } | 358 } |
357 return NULL; | |
358 } | 359 } |
359 | 360 |
360 | 361 |
361 void EventHandlerImplementation::StartEventHandler() { | 362 void EventHandlerImplementation::StartEventHandler() { |
362 pthread_t handler_thread; | 363 int result = dart::Thread::Start(&EventHandlerImplementation::Poll, |
363 int result = pthread_create(&handler_thread, | 364 reinterpret_cast<uword>(this)); |
364 NULL, | |
365 &EventHandlerImplementation::Poll, | |
366 this); | |
367 if (result != 0) { | 365 if (result != 0) { |
368 FATAL("Create start event handler thread"); | 366 FATAL1("Failed to start event handler thread %d", result); |
369 } | 367 } |
370 } | 368 } |
371 | 369 |
372 | 370 |
373 void EventHandlerImplementation::SendData(intptr_t id, | 371 void EventHandlerImplementation::SendData(intptr_t id, |
374 Dart_Port dart_port, | 372 Dart_Port dart_port, |
375 intptr_t data) { | 373 intptr_t data) { |
376 WakeupHandler(id, dart_port, data); | 374 WakeupHandler(id, dart_port, data); |
377 } | 375 } |
378 | 376 |
379 | 377 |
380 void* EventHandlerImplementation::GetHashmapKeyFromFd(intptr_t fd) { | 378 void* EventHandlerImplementation::GetHashmapKeyFromFd(intptr_t fd) { |
381 // The hashmap does not support keys with value 0. | 379 // The hashmap does not support keys with value 0. |
382 return reinterpret_cast<void*>(fd + 1); | 380 return reinterpret_cast<void*>(fd + 1); |
383 } | 381 } |
384 | 382 |
385 | 383 |
386 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { | 384 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { |
387 // The hashmap does not support keys with value 0. | 385 // The hashmap does not support keys with value 0. |
388 return dart::Utils::WordHash(fd + 1); | 386 return dart::Utils::WordHash(fd + 1); |
389 } | 387 } |
OLD | NEW |