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