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 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 intptr_t millis = timeout_ - GetCurrentTimeMilliseconds(); | 330 intptr_t millis = timeout_ - GetCurrentTimeMilliseconds(); |
330 if (millis <= 0) { | 331 if (millis <= 0) { |
331 Dart_PostIntArray(timeout_port_, 0, NULL); | 332 Dart_PostIntArray(timeout_port_, 0, NULL); |
332 timeout_ = kInfinityTimeout; | 333 timeout_ = kInfinityTimeout; |
333 timeout_port_ = 0; | 334 timeout_port_ = 0; |
334 } | 335 } |
335 } | 336 } |
336 } | 337 } |
337 | 338 |
338 | 339 |
339 void* EventHandlerImplementation::Poll(void* args) { | 340 void EventHandlerImplementation::Poll(uword args) { |
340 intptr_t pollfds_size; | 341 intptr_t pollfds_size; |
341 struct pollfd* pollfds; | 342 struct pollfd* pollfds; |
342 EventHandlerImplementation* handler = | 343 EventHandlerImplementation* handler = |
343 reinterpret_cast<EventHandlerImplementation*>(args); | 344 reinterpret_cast<EventHandlerImplementation*>(args); |
| 345 ASSERT(handler != NULL); |
344 while (1) { | 346 while (1) { |
345 pollfds = handler->GetPollFds(&pollfds_size); | 347 pollfds = handler->GetPollFds(&pollfds_size); |
346 intptr_t millis = handler->GetTimeout(); | 348 intptr_t millis = handler->GetTimeout(); |
347 intptr_t result = TEMP_FAILURE_RETRY(poll(pollfds, pollfds_size, millis)); | 349 intptr_t result = TEMP_FAILURE_RETRY(poll(pollfds, pollfds_size, millis)); |
348 ASSERT(EAGAIN == EWOULDBLOCK); | 350 ASSERT(EAGAIN == EWOULDBLOCK); |
349 if (result == -1) { | 351 if (result == -1) { |
350 if (errno != EWOULDBLOCK) { | 352 if (errno != EWOULDBLOCK) { |
351 perror("Poll failed"); | 353 perror("Poll failed"); |
352 } | 354 } |
353 } else { | 355 } else { |
354 handler->HandleTimeout(); | 356 handler->HandleTimeout(); |
355 handler->HandleEvents(pollfds, pollfds_size, result); | 357 handler->HandleEvents(pollfds, pollfds_size, result); |
356 } | 358 } |
357 free(pollfds); | 359 free(pollfds); |
358 } | 360 } |
359 return NULL; | |
360 } | 361 } |
361 | 362 |
362 | 363 |
363 void EventHandlerImplementation::StartEventHandler() { | 364 void EventHandlerImplementation::StartEventHandler() { |
364 pthread_t handler_thread; | 365 int result = dart::Thread::Start(&EventHandlerImplementation::Poll, |
365 int result = pthread_create(&handler_thread, | 366 reinterpret_cast<uword>(this)); |
366 NULL, | |
367 &EventHandlerImplementation::Poll, | |
368 this); | |
369 if (result != 0) { | 367 if (result != 0) { |
370 FATAL("Create start event handler thread"); | 368 FATAL1("Failed to start event handler thread %d", result); |
371 } | 369 } |
372 } | 370 } |
373 | 371 |
374 | 372 |
375 void EventHandlerImplementation::SendData(intptr_t id, | 373 void EventHandlerImplementation::SendData(intptr_t id, |
376 Dart_Port dart_port, | 374 Dart_Port dart_port, |
377 intptr_t data) { | 375 intptr_t data) { |
378 WakeupHandler(id, dart_port, data); | 376 WakeupHandler(id, dart_port, data); |
379 } | 377 } |
380 | 378 |
381 | 379 |
382 void* EventHandlerImplementation::GetHashmapKeyFromFd(intptr_t fd) { | 380 void* EventHandlerImplementation::GetHashmapKeyFromFd(intptr_t fd) { |
383 // The hashmap does not support keys with value 0. | 381 // The hashmap does not support keys with value 0. |
384 return reinterpret_cast<void*>(fd + 1); | 382 return reinterpret_cast<void*>(fd + 1); |
385 } | 383 } |
386 | 384 |
387 | 385 |
388 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { | 386 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { |
389 // The hashmap does not support keys with value 0. | 387 // The hashmap does not support keys with value 0. |
390 return dart::Utils::WordHash(fd + 1); | 388 return dart::Utils::WordHash(fd + 1); |
391 } | 389 } |
OLD | NEW |