Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(247)

Side by Side Diff: runtime/bin/eventhandler_linux.cc

Issue 9141005: Change the thread interface in runtime/platform and use it starting all threads (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebased to r3537 Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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);
siva 2012/01/27 18:53:17 ASSERT(handler != NULL);
Søren Gjesse 2012/01/30 14:42:57 Done.
344 while (1) { 345 while (1) {
345 pollfds = handler->GetPollFds(&pollfds_size); 346 pollfds = handler->GetPollFds(&pollfds_size);
346 intptr_t millis = handler->GetTimeout(); 347 intptr_t millis = handler->GetTimeout();
347 intptr_t result = TEMP_FAILURE_RETRY(poll(pollfds, pollfds_size, millis)); 348 intptr_t result = TEMP_FAILURE_RETRY(poll(pollfds, pollfds_size, millis));
348 ASSERT(EAGAIN == EWOULDBLOCK); 349 ASSERT(EAGAIN == EWOULDBLOCK);
349 if (result == -1) { 350 if (result == -1) {
350 if (errno != EWOULDBLOCK) { 351 if (errno != EWOULDBLOCK) {
351 perror("Poll failed"); 352 perror("Poll failed");
352 } 353 }
353 } else { 354 } else {
354 handler->HandleTimeout(); 355 handler->HandleTimeout();
355 handler->HandleEvents(pollfds, pollfds_size, result); 356 handler->HandleEvents(pollfds, pollfds_size, result);
356 } 357 }
357 free(pollfds); 358 free(pollfds);
358 } 359 }
359 return NULL;
360 } 360 }
361 361
362 362
363 void EventHandlerImplementation::StartEventHandler() { 363 void EventHandlerImplementation::StartEventHandler() {
364 pthread_t handler_thread; 364 int result = dart::Thread::Start(&EventHandlerImplementation::Poll,
365 int result = pthread_create(&handler_thread, 365 reinterpret_cast<uword>(this));
366 NULL,
367 &EventHandlerImplementation::Poll,
368 this);
369 if (result != 0) { 366 if (result != 0) {
370 FATAL("Create start event handler thread"); 367 FATAL1("Failed to start event handler thread %d", result);
371 } 368 }
372 } 369 }
373 370
374 371
375 void EventHandlerImplementation::SendData(intptr_t id, 372 void EventHandlerImplementation::SendData(intptr_t id,
376 Dart_Port dart_port, 373 Dart_Port dart_port,
377 intptr_t data) { 374 intptr_t data) {
378 WakeupHandler(id, dart_port, data); 375 WakeupHandler(id, dart_port, data);
379 } 376 }
380 377
381 378
382 void* EventHandlerImplementation::GetHashmapKeyFromFd(intptr_t fd) { 379 void* EventHandlerImplementation::GetHashmapKeyFromFd(intptr_t fd) {
383 // The hashmap does not support keys with value 0. 380 // The hashmap does not support keys with value 0.
384 return reinterpret_cast<void*>(fd + 1); 381 return reinterpret_cast<void*>(fd + 1);
385 } 382 }
386 383
387 384
388 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { 385 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) {
389 // The hashmap does not support keys with value 0. 386 // The hashmap does not support keys with value 0.
390 return dart::Utils::WordHash(fd + 1); 387 return dart::Utils::WordHash(fd + 1);
391 } 388 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698