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); |
siva
2012/01/27 18:53:17
ASSERT(handler != NULL);
Søren Gjesse
2012/01/30 14:42:57
Done.
| |
342 while (1) { | 343 while (1) { |
343 pollfds = handler->GetPollFds(&pollfds_size); | 344 pollfds = handler->GetPollFds(&pollfds_size); |
344 intptr_t millis = handler->GetTimeout(); | 345 intptr_t millis = handler->GetTimeout(); |
345 intptr_t result = TEMP_FAILURE_RETRY(poll(pollfds, pollfds_size, millis)); | 346 intptr_t result = TEMP_FAILURE_RETRY(poll(pollfds, pollfds_size, millis)); |
346 ASSERT(EAGAIN == EWOULDBLOCK); | 347 ASSERT(EAGAIN == EWOULDBLOCK); |
347 if (result == -1) { | 348 if (result == -1) { |
348 if (errno != EWOULDBLOCK) { | 349 if (errno != EWOULDBLOCK) { |
349 perror("Poll failed"); | 350 perror("Poll failed"); |
350 } | 351 } |
351 } else { | 352 } else { |
352 handler->HandleTimeout(); | 353 handler->HandleTimeout(); |
353 handler->HandleEvents(pollfds, pollfds_size, result); | 354 handler->HandleEvents(pollfds, pollfds_size, result); |
354 } | 355 } |
355 free(pollfds); | 356 free(pollfds); |
356 } | 357 } |
357 return NULL; | |
358 } | 358 } |
359 | 359 |
360 | 360 |
361 void EventHandlerImplementation::StartEventHandler() { | 361 void EventHandlerImplementation::StartEventHandler() { |
362 pthread_t handler_thread; | 362 int result = dart::Thread::Start(&EventHandlerImplementation::Poll, |
363 int result = pthread_create(&handler_thread, | 363 reinterpret_cast<uword>(this)); |
364 NULL, | |
365 &EventHandlerImplementation::Poll, | |
366 this); | |
367 if (result != 0) { | 364 if (result != 0) { |
368 FATAL("Create start event handler thread"); | 365 FATAL1("Failed to start event handler thread %d", result); |
369 } | 366 } |
370 } | 367 } |
371 | 368 |
372 | 369 |
373 void EventHandlerImplementation::SendData(intptr_t id, | 370 void EventHandlerImplementation::SendData(intptr_t id, |
374 Dart_Port dart_port, | 371 Dart_Port dart_port, |
375 intptr_t data) { | 372 intptr_t data) { |
376 WakeupHandler(id, dart_port, data); | 373 WakeupHandler(id, dart_port, data); |
377 } | 374 } |
378 | 375 |
379 | 376 |
380 void* EventHandlerImplementation::GetHashmapKeyFromFd(intptr_t fd) { | 377 void* EventHandlerImplementation::GetHashmapKeyFromFd(intptr_t fd) { |
381 // The hashmap does not support keys with value 0. | 378 // The hashmap does not support keys with value 0. |
382 return reinterpret_cast<void*>(fd + 1); | 379 return reinterpret_cast<void*>(fd + 1); |
383 } | 380 } |
384 | 381 |
385 | 382 |
386 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { | 383 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { |
387 // The hashmap does not support keys with value 0. | 384 // The hashmap does not support keys with value 0. |
388 return dart::Utils::WordHash(fd + 1); | 385 return dart::Utils::WordHash(fd + 1); |
389 } | 386 } |
OLD | NEW |