| 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 "platform/globals.h" | 5 #include "platform/globals.h" |
| 6 #if defined(TARGET_OS_MACOS) | 6 #if defined(TARGET_OS_MACOS) |
| 7 | 7 |
| 8 #include "bin/eventhandler.h" | 8 #include "bin/eventhandler.h" |
| 9 | 9 |
| 10 #include <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 struct kevent event; | 139 struct kevent event; |
| 140 EV_SET(&event, interrupt_fds_[0], EVFILT_READ, EV_ADD, 0, 0, NULL); | 140 EV_SET(&event, interrupt_fds_[0], EVFILT_READ, EV_ADD, 0, 0, NULL); |
| 141 int status = TEMP_FAILURE_RETRY(kevent(kqueue_fd_, &event, 1, NULL, 0, NULL)); | 141 int status = TEMP_FAILURE_RETRY(kevent(kqueue_fd_, &event, 1, NULL, 0, NULL)); |
| 142 if (status == -1) { | 142 if (status == -1) { |
| 143 FATAL1("Failed adding interrupt fd to kqueue: %s\n", strerror(errno)); | 143 FATAL1("Failed adding interrupt fd to kqueue: %s\n", strerror(errno)); |
| 144 } | 144 } |
| 145 } | 145 } |
| 146 | 146 |
| 147 | 147 |
| 148 EventHandlerImplementation::~EventHandlerImplementation() { | 148 EventHandlerImplementation::~EventHandlerImplementation() { |
| 149 VOID_TEMP_FAILURE_RETRY(close(kqueue_fd_)); |
| 149 VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[0])); | 150 VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[0])); |
| 150 VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[1])); | 151 VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[1])); |
| 151 } | 152 } |
| 152 | 153 |
| 153 | 154 |
| 154 SocketData* EventHandlerImplementation::GetSocketData(intptr_t fd) { | 155 SocketData* EventHandlerImplementation::GetSocketData(intptr_t fd) { |
| 155 ASSERT(fd >= 0); | 156 ASSERT(fd >= 0); |
| 156 HashMap::Entry* entry = socket_map_.Lookup( | 157 HashMap::Entry* entry = socket_map_.Lookup( |
| 157 GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd), true); | 158 GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd), true); |
| 158 ASSERT(entry != NULL); | 159 ASSERT(entry != NULL); |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 timeout_ = kInfinityTimeout; | 364 timeout_ = kInfinityTimeout; |
| 364 timeout_port_ = 0; | 365 timeout_port_ = 0; |
| 365 } | 366 } |
| 366 } | 367 } |
| 367 } | 368 } |
| 368 | 369 |
| 369 | 370 |
| 370 void EventHandlerImplementation::EventHandlerEntry(uword args) { | 371 void EventHandlerImplementation::EventHandlerEntry(uword args) { |
| 371 static const intptr_t kMaxEvents = 16; | 372 static const intptr_t kMaxEvents = 16; |
| 372 struct kevent events[kMaxEvents]; | 373 struct kevent events[kMaxEvents]; |
| 373 EventHandlerImplementation* handler = | 374 EventHandler* handler = reinterpret_cast<EventHandler*>(args); |
| 374 reinterpret_cast<EventHandlerImplementation*>(args); | 375 EventHandlerImplementation* handler_impl = &handler->delegate_; |
| 375 ASSERT(handler != NULL); | 376 ASSERT(handler_impl != NULL); |
| 376 while (!handler->shutdown_) { | 377 while (!handler_impl->shutdown_) { |
| 377 intptr_t millis = handler->GetTimeout(); | 378 intptr_t millis = handler_impl->GetTimeout(); |
| 378 // NULL pointer timespec for infinite timeout. | 379 // NULL pointer timespec for infinite timeout. |
| 379 ASSERT(kInfinityTimeout < 0); | 380 ASSERT(kInfinityTimeout < 0); |
| 380 struct timespec* timeout = NULL; | 381 struct timespec* timeout = NULL; |
| 381 struct timespec ts; | 382 struct timespec ts; |
| 382 if (millis >= 0) { | 383 if (millis >= 0) { |
| 383 ts.tv_sec = millis / 1000; | 384 ts.tv_sec = millis / 1000; |
| 384 ts.tv_nsec = (millis - (ts.tv_sec * 1000)) * 1000000; | 385 ts.tv_nsec = (millis - (ts.tv_sec * 1000)) * 1000000; |
| 385 timeout = &ts; | 386 timeout = &ts; |
| 386 } | 387 } |
| 387 intptr_t result = TEMP_FAILURE_RETRY(kevent(handler->kqueue_fd_, | 388 intptr_t result = TEMP_FAILURE_RETRY(kevent(handler_impl->kqueue_fd_, |
| 388 NULL, | 389 NULL, |
| 389 0, | 390 0, |
| 390 events, | 391 events, |
| 391 kMaxEvents, | 392 kMaxEvents, |
| 392 timeout)); | 393 timeout)); |
| 393 if (result == -1) { | 394 if (result == -1) { |
| 394 FATAL1("kevent failed %s\n", strerror(errno)); | 395 FATAL1("kevent failed %s\n", strerror(errno)); |
| 395 } else { | 396 } else { |
| 396 handler->HandleTimeout(); | 397 handler_impl->HandleTimeout(); |
| 397 handler->HandleEvents(events, result); | 398 handler_impl->HandleEvents(events, result); |
| 398 } | 399 } |
| 399 } | 400 } |
| 401 delete handler; |
| 400 } | 402 } |
| 401 | 403 |
| 402 | 404 |
| 403 void EventHandlerImplementation::Start() { | 405 void EventHandlerImplementation::Start(EventHandler* handler) { |
| 404 int result = | 406 int result = |
| 405 dart::Thread::Start(&EventHandlerImplementation::EventHandlerEntry, | 407 dart::Thread::Start(&EventHandlerImplementation::EventHandlerEntry, |
| 406 reinterpret_cast<uword>(this)); | 408 reinterpret_cast<uword>(handler)); |
| 407 if (result != 0) { | 409 if (result != 0) { |
| 408 FATAL1("Failed to start event handler thread %d", result); | 410 FATAL1("Failed to start event handler thread %d", result); |
| 409 } | 411 } |
| 410 } | 412 } |
| 411 | 413 |
| 412 | 414 |
| 413 void EventHandlerImplementation::Shutdown() { | 415 void EventHandlerImplementation::Shutdown() { |
| 414 SendData(kShutdownId, 0, 0); | 416 SendData(kShutdownId, 0, 0); |
| 415 } | 417 } |
| 416 | 418 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 427 return reinterpret_cast<void*>(fd + 1); | 429 return reinterpret_cast<void*>(fd + 1); |
| 428 } | 430 } |
| 429 | 431 |
| 430 | 432 |
| 431 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { | 433 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { |
| 432 // The hashmap does not support keys with value 0. | 434 // The hashmap does not support keys with value 0. |
| 433 return dart::Utils::WordHash(fd + 1); | 435 return dart::Utils::WordHash(fd + 1); |
| 434 } | 436 } |
| 435 | 437 |
| 436 #endif // defined(TARGET_OS_MACOS) | 438 #endif // defined(TARGET_OS_MACOS) |
| OLD | NEW |