| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) |
| 6 | 6 |
| 7 #include "platform/globals.h" | 7 #include "platform/globals.h" |
| 8 #if defined(TARGET_OS_FUCHSIA) | 8 #if defined(TARGET_OS_FUCHSIA) |
| 9 | 9 |
| 10 #include "bin/eventhandler.h" | 10 #include "bin/eventhandler.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #define LOG_INFO(msg, ...) Log::Print(msg, ##__VA_ARGS__) | 22 #define LOG_INFO(msg, ...) Log::Print(msg, ##__VA_ARGS__) |
| 23 #else | 23 #else |
| 24 #define LOG_ERR(msg, ...) | 24 #define LOG_ERR(msg, ...) |
| 25 #define LOG_INFO(msg, ...) | 25 #define LOG_INFO(msg, ...) |
| 26 #endif // defined(EVENTHANDLER_LOGGING) | 26 #endif // defined(EVENTHANDLER_LOGGING) |
| 27 | 27 |
| 28 namespace dart { | 28 namespace dart { |
| 29 namespace bin { | 29 namespace bin { |
| 30 | 30 |
| 31 MagentaWaitManyInfo::MagentaWaitManyInfo() | 31 MagentaWaitManyInfo::MagentaWaitManyInfo() |
| 32 : capacity_(kInitialCapacity), | 32 : capacity_(kInitialCapacity), size_(0) { |
| 33 size_(0) { | |
| 34 descriptor_infos_ = static_cast<DescriptorInfo**>( | 33 descriptor_infos_ = static_cast<DescriptorInfo**>( |
| 35 malloc(kInitialCapacity * sizeof(*descriptor_infos_))); | 34 malloc(kInitialCapacity * sizeof(*descriptor_infos_))); |
| 36 if (descriptor_infos_ == NULL) { | 35 if (descriptor_infos_ == NULL) { |
| 37 FATAL("Failed to allocate descriptor_infos array"); | 36 FATAL("Failed to allocate descriptor_infos array"); |
| 38 } | 37 } |
| 39 items_ = static_cast<mx_wait_item_t*>( | 38 items_ = |
| 40 malloc(kInitialCapacity * sizeof(*items_))); | 39 static_cast<mx_wait_item_t*>(malloc(kInitialCapacity * sizeof(*items_))); |
| 41 if (items_ == NULL) { | 40 if (items_ == NULL) { |
| 42 FATAL("Failed to allocate items array"); | 41 FATAL("Failed to allocate items array"); |
| 43 } | 42 } |
| 44 } | 43 } |
| 45 | 44 |
| 46 | 45 |
| 47 MagentaWaitManyInfo::~MagentaWaitManyInfo() { | 46 MagentaWaitManyInfo::~MagentaWaitManyInfo() { |
| 48 free(descriptor_infos_); | 47 free(descriptor_infos_); |
| 49 free(items_); | 48 free(items_); |
| 50 } | 49 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 realloc(descriptor_infos_, new_capacity * sizeof(*descriptor_infos_))); | 102 realloc(descriptor_infos_, new_capacity * sizeof(*descriptor_infos_))); |
| 104 if (descriptor_infos_ == NULL) { | 103 if (descriptor_infos_ == NULL) { |
| 105 FATAL("Failed to grow descriptor_infos array"); | 104 FATAL("Failed to grow descriptor_infos array"); |
| 106 } | 105 } |
| 107 items_ = static_cast<mx_wait_item_t*>( | 106 items_ = static_cast<mx_wait_item_t*>( |
| 108 realloc(items_, new_capacity * sizeof(*items_))); | 107 realloc(items_, new_capacity * sizeof(*items_))); |
| 109 if (items_ == NULL) { | 108 if (items_ == NULL) { |
| 110 FATAL("Failed to grow items array"); | 109 FATAL("Failed to grow items array"); |
| 111 } | 110 } |
| 112 capacity_ = new_capacity; | 111 capacity_ = new_capacity; |
| 113 LOG_INFO("GrowArraysIfNeeded(%ld), capacity = %ld\n", | 112 LOG_INFO("GrowArraysIfNeeded(%ld), capacity = %ld\n", desired_size, |
| 114 desired_size, capacity_); | 113 capacity_); |
| 115 } | 114 } |
| 116 | 115 |
| 117 | 116 |
| 118 EventHandlerImplementation::EventHandlerImplementation() { | 117 EventHandlerImplementation::EventHandlerImplementation() { |
| 119 mx_status_t status = mx_channel_create(0, &interrupt_handles_[0], | 118 mx_status_t status = |
| 120 &interrupt_handles_[1]); | 119 mx_channel_create(0, &interrupt_handles_[0], &interrupt_handles_[1]); |
| 121 if (status != NO_ERROR) { | 120 if (status != NO_ERROR) { |
| 122 FATAL1("mx_channel_create failed: %s\n", mx_status_get_string(status)); | 121 FATAL1("mx_channel_create failed: %s\n", mx_status_get_string(status)); |
| 123 } | 122 } |
| 124 shutdown_ = false; | 123 shutdown_ = false; |
| 125 info_.AddHandle(interrupt_handles_[0], | 124 info_.AddHandle(interrupt_handles_[0], |
| 126 MX_SIGNAL_READABLE | MX_SIGNAL_PEER_CLOSED, | 125 MX_SIGNAL_READABLE | MX_SIGNAL_PEER_CLOSED, NULL); |
| 127 NULL); | |
| 128 LOG_INFO("EventHandlerImplementation initialized\n"); | 126 LOG_INFO("EventHandlerImplementation initialized\n"); |
| 129 } | 127 } |
| 130 | 128 |
| 131 | 129 |
| 132 EventHandlerImplementation::~EventHandlerImplementation() { | 130 EventHandlerImplementation::~EventHandlerImplementation() { |
| 133 mx_status_t status = mx_handle_close(interrupt_handles_[0]); | 131 mx_status_t status = mx_handle_close(interrupt_handles_[0]); |
| 134 if (status != NO_ERROR) { | 132 if (status != NO_ERROR) { |
| 135 FATAL1("mx_handle_close failed: %s\n", mx_status_get_string(status)); | 133 FATAL1("mx_handle_close failed: %s\n", mx_status_get_string(status)); |
| 136 } | 134 } |
| 137 status = mx_handle_close(interrupt_handles_[1]); | 135 status = mx_handle_close(interrupt_handles_[1]); |
| 138 if (status != NO_ERROR) { | 136 if (status != NO_ERROR) { |
| 139 FATAL1("mx_handle_close failed: %s\n", mx_status_get_string(status)); | 137 FATAL1("mx_handle_close failed: %s\n", mx_status_get_string(status)); |
| 140 } | 138 } |
| 141 LOG_INFO("EventHandlerImplementation destroyed\n"); | 139 LOG_INFO("EventHandlerImplementation destroyed\n"); |
| 142 } | 140 } |
| 143 | 141 |
| 144 | 142 |
| 145 void EventHandlerImplementation::WakeupHandler(intptr_t id, | 143 void EventHandlerImplementation::WakeupHandler(intptr_t id, |
| 146 Dart_Port dart_port, | 144 Dart_Port dart_port, |
| 147 int64_t data) { | 145 int64_t data) { |
| 148 InterruptMessage msg; | 146 InterruptMessage msg; |
| 149 msg.id = id; | 147 msg.id = id; |
| 150 msg.dart_port = dart_port; | 148 msg.dart_port = dart_port; |
| 151 msg.data = data; | 149 msg.data = data; |
| 152 | 150 |
| 153 mx_status_t status = | 151 mx_status_t status = |
| 154 mx_channel_write(interrupt_handles_[1], 0, &msg, sizeof(msg), NULL, 0); | 152 mx_channel_write(interrupt_handles_[1], 0, &msg, sizeof(msg), NULL, 0); |
| 155 if (status != NO_ERROR) { | 153 if (status != NO_ERROR) { |
| 156 FATAL1("mx_channel_write failed: %s\n", mx_status_get_string(status)); | 154 FATAL1("mx_channel_write failed: %s\n", mx_status_get_string(status)); |
| 157 } | 155 } |
| 158 LOG_INFO("WakeupHandler(%ld, %ld, %lld)\n", id, dart_port, data); | 156 LOG_INFO("WakeupHandler(%ld, %ld, %lld)\n", id, dart_port, data); |
| 159 } | 157 } |
| 160 | 158 |
| 161 | 159 |
| 162 void EventHandlerImplementation::HandleInterruptFd() { | 160 void EventHandlerImplementation::HandleInterruptFd() { |
| 163 LOG_INFO("HandleInterruptFd entry\n"); | 161 LOG_INFO("HandleInterruptFd entry\n"); |
| 164 InterruptMessage msg; | 162 InterruptMessage msg; |
| 165 uint32_t bytes = kInterruptMessageSize; | 163 uint32_t bytes = kInterruptMessageSize; |
| 166 mx_status_t status; | 164 mx_status_t status; |
| 167 while (true) { | 165 while (true) { |
| 168 status = mx_channel_read( | 166 status = mx_channel_read(interrupt_handles_[0], 0, &msg, bytes, &bytes, |
| 169 interrupt_handles_[0], 0, &msg, bytes, &bytes, NULL, 0, NULL); | 167 NULL, 0, NULL); |
| 170 if (status != NO_ERROR) { | 168 if (status != NO_ERROR) { |
| 171 break; | 169 break; |
| 172 } | 170 } |
| 173 ASSERT(bytes == kInterruptMessageSize); | 171 ASSERT(bytes == kInterruptMessageSize); |
| 174 if (msg.id == kTimerId) { | 172 if (msg.id == kTimerId) { |
| 175 LOG_INFO("HandleInterruptFd read timer update\n"); | 173 LOG_INFO("HandleInterruptFd read timer update\n"); |
| 176 timeout_queue_.UpdateTimeout(msg.dart_port, msg.data); | 174 timeout_queue_.UpdateTimeout(msg.dart_port, msg.data); |
| 177 } else if (msg.id == kShutdownId) { | 175 } else if (msg.id == kShutdownId) { |
| 178 LOG_INFO("HandleInterruptFd read shutdown\n"); | 176 LOG_INFO("HandleInterruptFd read shutdown\n"); |
| 179 shutdown_ = true; | 177 shutdown_ = true; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 } else { | 213 } else { |
| 216 LOG_INFO("HandleEvents interrupt_handles_[0] not readable\n"); | 214 LOG_INFO("HandleEvents interrupt_handles_[0] not readable\n"); |
| 217 } | 215 } |
| 218 } | 216 } |
| 219 | 217 |
| 220 | 218 |
| 221 int64_t EventHandlerImplementation::GetTimeout() const { | 219 int64_t EventHandlerImplementation::GetTimeout() const { |
| 222 if (!timeout_queue_.HasTimeout()) { | 220 if (!timeout_queue_.HasTimeout()) { |
| 223 return kInfinityTimeout; | 221 return kInfinityTimeout; |
| 224 } | 222 } |
| 225 int64_t millis = timeout_queue_.CurrentTimeout() - | 223 int64_t millis = |
| 226 TimerUtils::GetCurrentMonotonicMillis(); | 224 timeout_queue_.CurrentTimeout() - TimerUtils::GetCurrentMonotonicMillis(); |
| 227 return (millis < 0) ? 0 : millis; | 225 return (millis < 0) ? 0 : millis; |
| 228 } | 226 } |
| 229 | 227 |
| 230 | 228 |
| 231 void EventHandlerImplementation::HandleTimeout() { | 229 void EventHandlerImplementation::HandleTimeout() { |
| 232 if (timeout_queue_.HasTimeout()) { | 230 if (timeout_queue_.HasTimeout()) { |
| 233 int64_t millis = timeout_queue_.CurrentTimeout() - | 231 int64_t millis = timeout_queue_.CurrentTimeout() - |
| 234 TimerUtils::GetCurrentMonotonicMillis(); | 232 TimerUtils::GetCurrentMonotonicMillis(); |
| 235 if (millis <= 0) { | 233 if (millis <= 0) { |
| 236 DartUtils::PostNull(timeout_queue_.CurrentPort()); | 234 DartUtils::PostNull(timeout_queue_.CurrentPort()); |
| 237 timeout_queue_.RemoveCurrent(); | 235 timeout_queue_.RemoveCurrent(); |
| 238 } | 236 } |
| 239 } | 237 } |
| 240 } | 238 } |
| 241 | 239 |
| 242 | 240 |
| 243 void EventHandlerImplementation::Poll(uword args) { | 241 void EventHandlerImplementation::Poll(uword args) { |
| 244 EventHandler* handler = reinterpret_cast<EventHandler*>(args); | 242 EventHandler* handler = reinterpret_cast<EventHandler*>(args); |
| 245 EventHandlerImplementation* handler_impl = &handler->delegate_; | 243 EventHandlerImplementation* handler_impl = &handler->delegate_; |
| 246 ASSERT(handler_impl != NULL); | 244 ASSERT(handler_impl != NULL); |
| 247 | 245 |
| 248 while (!handler_impl->shutdown_) { | 246 while (!handler_impl->shutdown_) { |
| 249 int64_t millis = handler_impl->GetTimeout(); | 247 int64_t millis = handler_impl->GetTimeout(); |
| 250 ASSERT((millis == kInfinityTimeout) || (millis >= 0)); | 248 ASSERT((millis == kInfinityTimeout) || (millis >= 0)); |
| 251 mx_time_t timeout = | 249 mx_time_t timeout = |
| 252 millis * kMicrosecondsPerMillisecond * kNanosecondsPerMicrosecond; | 250 millis * kMicrosecondsPerMillisecond * kNanosecondsPerMicrosecond; |
| 253 const MagentaWaitManyInfo& info = handler_impl->info(); | 251 const MagentaWaitManyInfo& info = handler_impl->info(); |
| 254 LOG_INFO("mx_handle_wait_many(%p, %ld, %lld)\n", | 252 LOG_INFO("mx_handle_wait_many(%p, %ld, %lld)\n", info.items(), info.size(), |
| 255 info.items(), info.size(), timeout); | 253 timeout); |
| 256 mx_status_t status = mx_handle_wait_many( | 254 mx_status_t status = |
| 257 info.items(), | 255 mx_handle_wait_many(info.items(), info.size(), timeout); |
| 258 info.size(), | |
| 259 timeout); | |
| 260 if ((status != NO_ERROR) && (status != ERR_TIMED_OUT)) { | 256 if ((status != NO_ERROR) && (status != ERR_TIMED_OUT)) { |
| 261 FATAL1("mx_handle_wait_many failed: %s\n", mx_status_get_string(status)); | 257 FATAL1("mx_handle_wait_many failed: %s\n", mx_status_get_string(status)); |
| 262 } else { | 258 } else { |
| 263 LOG_INFO("mx_handle_wait_many returned: %ld\n", status); | 259 LOG_INFO("mx_handle_wait_many returned: %ld\n", status); |
| 264 handler_impl->HandleTimeout(); | 260 handler_impl->HandleTimeout(); |
| 265 handler_impl->HandleEvents(); | 261 handler_impl->HandleEvents(); |
| 266 } | 262 } |
| 267 } | 263 } |
| 268 handler->NotifyShutdownDone(); | 264 handler->NotifyShutdownDone(); |
| 269 LOG_INFO("EventHandlerImplementation notifying about shutdown\n"); | 265 LOG_INFO("EventHandlerImplementation notifying about shutdown\n"); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 289 int64_t data) { | 285 int64_t data) { |
| 290 WakeupHandler(id, dart_port, data); | 286 WakeupHandler(id, dart_port, data); |
| 291 } | 287 } |
| 292 | 288 |
| 293 } // namespace bin | 289 } // namespace bin |
| 294 } // namespace dart | 290 } // namespace dart |
| 295 | 291 |
| 296 #endif // defined(TARGET_OS_FUCHSIA) | 292 #endif // defined(TARGET_OS_FUCHSIA) |
| 297 | 293 |
| 298 #endif // !defined(DART_IO_DISABLED) | 294 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |