| 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 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) |
| 6 | 6 |
| 7 #include "bin/eventhandler.h" | 7 #include "bin/eventhandler.h" |
| 8 | 8 |
| 9 #include "bin/builtin.h" | 9 #include "bin/builtin.h" |
| 10 #include "bin/dartutils.h" | 10 #include "bin/dartutils.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 current = timeouts_; | 50 current = timeouts_; |
| 51 while (current != NULL) { | 51 while (current != NULL) { |
| 52 if ((next_timeout_ == NULL) || | 52 if ((next_timeout_ == NULL) || |
| 53 (current->timeout() < next_timeout_->timeout())) { | 53 (current->timeout() < next_timeout_->timeout())) { |
| 54 next_timeout_ = current; | 54 next_timeout_ = current; |
| 55 } | 55 } |
| 56 current = current->next(); | 56 current = current->next(); |
| 57 } | 57 } |
| 58 } | 58 } |
| 59 | 59 |
| 60 | |
| 61 static EventHandler* event_handler = NULL; | 60 static EventHandler* event_handler = NULL; |
| 62 static Monitor* shutdown_monitor = NULL; | 61 static Monitor* shutdown_monitor = NULL; |
| 63 | 62 |
| 64 | |
| 65 void EventHandler::Start() { | 63 void EventHandler::Start() { |
| 66 // Initialize global socket registry. | 64 // Initialize global socket registry. |
| 67 ListeningSocketRegistry::Initialize(); | 65 ListeningSocketRegistry::Initialize(); |
| 68 | 66 |
| 69 ASSERT(event_handler == NULL); | 67 ASSERT(event_handler == NULL); |
| 70 shutdown_monitor = new Monitor(); | 68 shutdown_monitor = new Monitor(); |
| 71 event_handler = new EventHandler(); | 69 event_handler = new EventHandler(); |
| 72 event_handler->delegate_.Start(event_handler); | 70 event_handler->delegate_.Start(event_handler); |
| 73 } | 71 } |
| 74 | 72 |
| 75 | |
| 76 void EventHandler::NotifyShutdownDone() { | 73 void EventHandler::NotifyShutdownDone() { |
| 77 MonitorLocker ml(shutdown_monitor); | 74 MonitorLocker ml(shutdown_monitor); |
| 78 ml.Notify(); | 75 ml.Notify(); |
| 79 } | 76 } |
| 80 | 77 |
| 81 | |
| 82 void EventHandler::Stop() { | 78 void EventHandler::Stop() { |
| 83 if (event_handler == NULL) { | 79 if (event_handler == NULL) { |
| 84 return; | 80 return; |
| 85 } | 81 } |
| 86 | 82 |
| 87 // Wait until it has stopped. | 83 // Wait until it has stopped. |
| 88 { | 84 { |
| 89 MonitorLocker ml(shutdown_monitor); | 85 MonitorLocker ml(shutdown_monitor); |
| 90 | 86 |
| 91 // Signal to event handler that we want it to stop. | 87 // Signal to event handler that we want it to stop. |
| 92 event_handler->delegate_.Shutdown(); | 88 event_handler->delegate_.Shutdown(); |
| 93 ml.Wait(Monitor::kNoTimeout); | 89 ml.Wait(Monitor::kNoTimeout); |
| 94 } | 90 } |
| 95 | 91 |
| 96 // Cleanup | 92 // Cleanup |
| 97 delete event_handler; | 93 delete event_handler; |
| 98 event_handler = NULL; | 94 event_handler = NULL; |
| 99 delete shutdown_monitor; | 95 delete shutdown_monitor; |
| 100 shutdown_monitor = NULL; | 96 shutdown_monitor = NULL; |
| 101 | 97 |
| 102 // Destroy the global socket registry. | 98 // Destroy the global socket registry. |
| 103 ListeningSocketRegistry::Cleanup(); | 99 ListeningSocketRegistry::Cleanup(); |
| 104 } | 100 } |
| 105 | 101 |
| 106 | |
| 107 EventHandlerImplementation* EventHandler::delegate() { | 102 EventHandlerImplementation* EventHandler::delegate() { |
| 108 if (event_handler == NULL) { | 103 if (event_handler == NULL) { |
| 109 return NULL; | 104 return NULL; |
| 110 } | 105 } |
| 111 return &event_handler->delegate_; | 106 return &event_handler->delegate_; |
| 112 } | 107 } |
| 113 | 108 |
| 114 | |
| 115 void EventHandler::SendFromNative(intptr_t id, Dart_Port port, int64_t data) { | 109 void EventHandler::SendFromNative(intptr_t id, Dart_Port port, int64_t data) { |
| 116 event_handler->SendData(id, port, data); | 110 event_handler->SendData(id, port, data); |
| 117 } | 111 } |
| 118 | 112 |
| 119 | |
| 120 /* | 113 /* |
| 121 * Send data to the EventHandler thread to register for a given instance | 114 * Send data to the EventHandler thread to register for a given instance |
| 122 * args[0] a ReceivePort args[1] with a notification event args[2]. | 115 * args[0] a ReceivePort args[1] with a notification event args[2]. |
| 123 */ | 116 */ |
| 124 void FUNCTION_NAME(EventHandler_SendData)(Dart_NativeArguments args) { | 117 void FUNCTION_NAME(EventHandler_SendData)(Dart_NativeArguments args) { |
| 125 // Get the id out of the send port. If the handle is not a send port | 118 // Get the id out of the send port. If the handle is not a send port |
| 126 // we will get an error and propagate that out. | 119 // we will get an error and propagate that out. |
| 127 Dart_Handle handle = Dart_GetNativeArgument(args, 1); | 120 Dart_Handle handle = Dart_GetNativeArgument(args, 1); |
| 128 Dart_Port dart_port; | 121 Dart_Port dart_port; |
| 129 handle = Dart_SendPortGetId(handle, &dart_port); | 122 handle = Dart_SendPortGetId(handle, &dart_port); |
| 130 if (Dart_IsError(handle)) { | 123 if (Dart_IsError(handle)) { |
| 131 Dart_PropagateError(handle); | 124 Dart_PropagateError(handle); |
| 132 UNREACHABLE(); | 125 UNREACHABLE(); |
| 133 } | 126 } |
| 134 Dart_Handle sender = Dart_GetNativeArgument(args, 0); | 127 Dart_Handle sender = Dart_GetNativeArgument(args, 0); |
| 135 intptr_t id; | 128 intptr_t id; |
| 136 if (Dart_IsNull(sender)) { | 129 if (Dart_IsNull(sender)) { |
| 137 id = kTimerId; | 130 id = kTimerId; |
| 138 } else { | 131 } else { |
| 139 Socket* socket = Socket::GetSocketIdNativeField(sender); | 132 Socket* socket = Socket::GetSocketIdNativeField(sender); |
| 140 ASSERT(dart_port != ILLEGAL_PORT); | 133 ASSERT(dart_port != ILLEGAL_PORT); |
| 141 socket->set_port(dart_port); | 134 socket->set_port(dart_port); |
| 142 socket->Retain(); // inc refcount before sending to the eventhandler. | 135 socket->Retain(); // inc refcount before sending to the eventhandler. |
| 143 id = reinterpret_cast<intptr_t>(socket); | 136 id = reinterpret_cast<intptr_t>(socket); |
| 144 } | 137 } |
| 145 int64_t data = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); | 138 int64_t data = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); |
| 146 event_handler->SendData(id, dart_port, data); | 139 event_handler->SendData(id, dart_port, data); |
| 147 } | 140 } |
| 148 | 141 |
| 149 | |
| 150 void FUNCTION_NAME(EventHandler_TimerMillisecondClock)( | 142 void FUNCTION_NAME(EventHandler_TimerMillisecondClock)( |
| 151 Dart_NativeArguments args) { | 143 Dart_NativeArguments args) { |
| 152 int64_t now = TimerUtils::GetCurrentMonotonicMillis(); | 144 int64_t now = TimerUtils::GetCurrentMonotonicMillis(); |
| 153 Dart_SetReturnValue(args, Dart_NewInteger(now)); | 145 Dart_SetReturnValue(args, Dart_NewInteger(now)); |
| 154 } | 146 } |
| 155 | 147 |
| 156 } // namespace bin | 148 } // namespace bin |
| 157 } // namespace dart | 149 } // namespace dart |
| 158 | 150 |
| 159 #endif // !defined(DART_IO_DISABLED) | 151 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |