| 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/dartutils.h" | 5 #include "bin/dartutils.h" |
| 6 #include "bin/eventhandler.h" | 6 #include "bin/eventhandler.h" |
| 7 #include "bin/lockers.h" | 7 #include "bin/lockers.h" |
| 8 #include "bin/socket.h" | 8 #include "bin/socket.h" |
| 9 #include "bin/thread.h" | 9 #include "bin/thread.h" |
| 10 | 10 |
| 11 #include "include/dart_api.h" | 11 #include "include/dart_api.h" |
| 12 | 12 |
| 13 | |
| 14 namespace dart { | 13 namespace dart { |
| 15 namespace bin { | 14 namespace bin { |
| 16 | 15 |
| 17 | |
| 18 void TimeoutQueue::UpdateTimeout(Dart_Port port, int64_t timeout) { | 16 void TimeoutQueue::UpdateTimeout(Dart_Port port, int64_t timeout) { |
| 19 // Find port if present. | 17 // Find port if present. |
| 20 Timeout* last = NULL; | 18 Timeout* last = NULL; |
| 21 Timeout* current = timeouts_; | 19 Timeout* current = timeouts_; |
| 22 while (current != NULL) { | 20 while (current != NULL) { |
| 23 if (current->port() == port) { | 21 if (current->port() == port) { |
| 24 // Found. | 22 // Found. |
| 25 if (timeout < 0) { | 23 if (timeout < 0) { |
| 26 // Remove from list and delete existing. | 24 // Remove from list and delete existing. |
| 27 if (last != NULL) { | 25 if (last != NULL) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 40 current = current->next(); | 38 current = current->next(); |
| 41 } | 39 } |
| 42 if (current == NULL && timeout >= 0) { | 40 if (current == NULL && timeout >= 0) { |
| 43 // Not found, create a new. | 41 // Not found, create a new. |
| 44 timeouts_ = new Timeout(port, timeout, timeouts_); | 42 timeouts_ = new Timeout(port, timeout, timeouts_); |
| 45 } | 43 } |
| 46 // Clear and find next timeout. | 44 // Clear and find next timeout. |
| 47 next_timeout_ = NULL; | 45 next_timeout_ = NULL; |
| 48 current = timeouts_; | 46 current = timeouts_; |
| 49 while (current != NULL) { | 47 while (current != NULL) { |
| 50 if (next_timeout_ == NULL || | 48 if ((next_timeout_ == NULL) || |
| 51 current->timeout() < next_timeout_->timeout()) { | 49 (current->timeout() < next_timeout_->timeout())) { |
| 52 next_timeout_ = current; | 50 next_timeout_ = current; |
| 53 } | 51 } |
| 54 current = current->next(); | 52 current = current->next(); |
| 55 } | 53 } |
| 56 } | 54 } |
| 57 | 55 |
| 58 | 56 |
| 59 static EventHandler* event_handler = NULL; | 57 static EventHandler* event_handler = NULL; |
| 60 static Monitor *shutdown_monitor = NULL; | 58 static Monitor *shutdown_monitor = NULL; |
| 61 | 59 |
| 62 | 60 |
| 63 void EventHandler::Start() { | 61 void EventHandler::Start() { |
| 64 // Initialize global socket registry. | 62 // Initialize global socket registry. |
| 65 ListeningSocketRegistry::Initialize(); | 63 ListeningSocketRegistry::Initialize(); |
| 66 | 64 |
| 67 ASSERT(event_handler == NULL); | 65 ASSERT(event_handler == NULL); |
| 68 shutdown_monitor = new Monitor(); | 66 shutdown_monitor = new Monitor(); |
| 69 event_handler = new EventHandler(); | 67 event_handler = new EventHandler(); |
| 70 event_handler->delegate_.Start(event_handler); | 68 event_handler->delegate_.Start(event_handler); |
| 71 } | 69 } |
| 72 | 70 |
| 73 | 71 |
| 74 void EventHandler::NotifyShutdownDone() { | 72 void EventHandler::NotifyShutdownDone() { |
| 75 MonitorLocker ml(shutdown_monitor); | 73 MonitorLocker ml(shutdown_monitor); |
| 76 ml.Notify(); | 74 ml.Notify(); |
| 77 } | 75 } |
| 78 | 76 |
| 79 | 77 |
| 80 void EventHandler::Stop() { | 78 void EventHandler::Stop() { |
| 81 if (event_handler == NULL) return; | 79 if (event_handler == NULL) { |
| 80 return; |
| 81 } |
| 82 | 82 |
| 83 // Wait until it has stopped. | 83 // Wait until it has stopped. |
| 84 { | 84 { |
| 85 MonitorLocker ml(shutdown_monitor); | 85 MonitorLocker ml(shutdown_monitor); |
| 86 | 86 |
| 87 // Signal to event handler that we want it to stop. | 87 // Signal to event handler that we want it to stop. |
| 88 event_handler->delegate_.Shutdown(); | 88 event_handler->delegate_.Shutdown(); |
| 89 ml.Wait(Monitor::kNoTimeout); | 89 ml.Wait(Monitor::kNoTimeout); |
| 90 } | 90 } |
| 91 | 91 |
| 92 // Cleanup | 92 // Cleanup |
| 93 delete event_handler; | 93 delete event_handler; |
| 94 event_handler = NULL; | 94 event_handler = NULL; |
| 95 delete shutdown_monitor; | 95 delete shutdown_monitor; |
| 96 shutdown_monitor = NULL; | 96 shutdown_monitor = NULL; |
| 97 | 97 |
| 98 // Destroy the global socket registry. | 98 // Destroy the global socket registry. |
| 99 ListeningSocketRegistry::Cleanup(); | 99 ListeningSocketRegistry::Cleanup(); |
| 100 } | 100 } |
| 101 | 101 |
| 102 | 102 |
| 103 EventHandlerImplementation* EventHandler::delegate() { | 103 EventHandlerImplementation* EventHandler::delegate() { |
| 104 if (event_handler == NULL) return NULL; | 104 if (event_handler == NULL) { |
| 105 return NULL; |
| 106 } |
| 105 return &event_handler->delegate_; | 107 return &event_handler->delegate_; |
| 106 } | 108 } |
| 107 | 109 |
| 108 | 110 |
| 109 /* | 111 /* |
| 110 * Send data to the EventHandler thread to register for a given instance | 112 * Send data to the EventHandler thread to register for a given instance |
| 111 * args[0] a ReceivePort args[1] with a notification event args[2]. | 113 * args[0] a ReceivePort args[1] with a notification event args[2]. |
| 112 */ | 114 */ |
| 113 void FUNCTION_NAME(EventHandler_SendData)(Dart_NativeArguments args) { | 115 void FUNCTION_NAME(EventHandler_SendData)(Dart_NativeArguments args) { |
| 114 Dart_Handle sender = Dart_GetNativeArgument(args, 0); | 116 Dart_Handle sender = Dart_GetNativeArgument(args, 0); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 133 | 135 |
| 134 | 136 |
| 135 void FUNCTION_NAME(EventHandler_TimerMillisecondClock)( | 137 void FUNCTION_NAME(EventHandler_TimerMillisecondClock)( |
| 136 Dart_NativeArguments args) { | 138 Dart_NativeArguments args) { |
| 137 int64_t now = TimerUtils::GetCurrentMonotonicMillis(); | 139 int64_t now = TimerUtils::GetCurrentMonotonicMillis(); |
| 138 Dart_SetReturnValue(args, Dart_NewInteger(now)); | 140 Dart_SetReturnValue(args, Dart_NewInteger(now)); |
| 139 } | 141 } |
| 140 | 142 |
| 141 } // namespace bin | 143 } // namespace bin |
| 142 } // namespace dart | 144 } // namespace dart |
| OLD | NEW |