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 | 16 |
16 | 17 |
17 int64_t GetCurrentTimeMilliseconds() { | 18 int64_t GetCurrentTimeMilliseconds() { |
18 struct timeval tv; | 19 struct timeval tv; |
19 if (gettimeofday(&tv, NULL) < 0) { | 20 if (gettimeofday(&tv, NULL) < 0) { |
20 UNREACHABLE(); | 21 UNREACHABLE(); |
21 return 0; | 22 return 0; |
22 } | 23 } |
23 return ((static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec) / 1000; | 24 return ((static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec) / 1000; |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 intptr_t millis = timeout_ - GetCurrentTimeMilliseconds(); | 333 intptr_t millis = timeout_ - GetCurrentTimeMilliseconds(); |
333 if (millis <= 0) { | 334 if (millis <= 0) { |
334 Dart_PostIntArray(timeout_port_, 0, NULL); | 335 Dart_PostIntArray(timeout_port_, 0, NULL); |
335 timeout_ = kInfinityTimeout; | 336 timeout_ = kInfinityTimeout; |
336 timeout_port_ = 0; | 337 timeout_port_ = 0; |
337 } | 338 } |
338 } | 339 } |
339 } | 340 } |
340 | 341 |
341 | 342 |
342 void* EventHandlerImplementation::Poll(void* args) { | 343 void EventHandlerImplementation::Poll(uword args) { |
343 intptr_t pollfds_size; | 344 intptr_t pollfds_size; |
344 struct pollfd* pollfds; | 345 struct pollfd* pollfds; |
345 EventHandlerImplementation* handler = | 346 EventHandlerImplementation* handler = |
346 reinterpret_cast<EventHandlerImplementation*>(args); | 347 reinterpret_cast<EventHandlerImplementation*>(args); |
347 while (1) { | 348 while (1) { |
348 pollfds = handler->GetPollFds(&pollfds_size); | 349 pollfds = handler->GetPollFds(&pollfds_size); |
349 intptr_t millis = handler->GetTimeout(); | 350 intptr_t millis = handler->GetTimeout(); |
350 intptr_t result = TEMP_FAILURE_RETRY(poll(pollfds, pollfds_size, millis)); | 351 intptr_t result = TEMP_FAILURE_RETRY(poll(pollfds, pollfds_size, millis)); |
351 ASSERT(EAGAIN == EWOULDBLOCK); | 352 ASSERT(EAGAIN == EWOULDBLOCK); |
352 if (result == -1) { | 353 if (result == -1) { |
353 if (errno != EWOULDBLOCK) { | 354 if (errno != EWOULDBLOCK) { |
354 perror("Poll failed"); | 355 perror("Poll failed"); |
355 } | 356 } |
356 } else { | 357 } else { |
357 handler->HandleTimeout(); | 358 handler->HandleTimeout(); |
358 handler->HandleEvents(pollfds, pollfds_size, result); | 359 handler->HandleEvents(pollfds, pollfds_size, result); |
359 } | 360 } |
360 free(pollfds); | 361 free(pollfds); |
361 } | 362 } |
362 return NULL; | |
363 } | 363 } |
364 | 364 |
365 | 365 |
366 void EventHandlerImplementation::StartEventHandler() { | 366 void EventHandlerImplementation::StartEventHandler() { |
367 pthread_t handler_thread; | 367 dart::Thread::Start(&EventHandlerImplementation::Poll, |
368 int result = pthread_create(&handler_thread, | 368 reinterpret_cast<uword>(this)); |
369 NULL, | |
370 &EventHandlerImplementation::Poll, | |
371 this); | |
372 if (result != 0) { | |
373 FATAL("Create start event handler thread"); | |
374 } | |
375 } | 369 } |
376 | 370 |
377 | 371 |
378 void EventHandlerImplementation::SendData(intptr_t id, | 372 void EventHandlerImplementation::SendData(intptr_t id, |
379 Dart_Port dart_port, | 373 Dart_Port dart_port, |
380 intptr_t data) { | 374 intptr_t data) { |
381 WakeupHandler(id, dart_port, data); | 375 WakeupHandler(id, dart_port, data); |
382 } | 376 } |
OLD | NEW |