| 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/socket.h" | 7 #include "bin/socket.h" |
| 8 #include "bin/thread.h" | 8 #include "bin/thread.h" |
| 9 | 9 |
| 10 #include "include/dart_api.h" | 10 #include "include/dart_api.h" |
| 11 | 11 |
| 12 | 12 |
| 13 namespace dart { | 13 namespace dart { |
| 14 namespace bin { | 14 namespace bin { |
| 15 | 15 |
| 16 static const int kNativeEventHandlerFieldIndex = 0; | |
| 17 static const intptr_t kTimerId = -1; | 16 static const intptr_t kTimerId = -1; |
| 18 static const intptr_t kInvalidId = -2; | 17 static const intptr_t kInvalidId = -2; |
| 19 | 18 |
| 20 static EventHandler* event_handler = NULL; | 19 static EventHandler* event_handler = NULL; |
| 20 // TODO(ajohnsen): Consider removing mutex_ if we can enforce an invariant |
| 21 // that eventhandler is kept alive untill all isolates are closed. |
| 21 static dart::Mutex* mutex_ = new dart::Mutex(); | 22 static dart::Mutex* mutex_ = new dart::Mutex(); |
| 22 | 23 |
| 23 | 24 |
| 24 void TimeoutQueue::UpdateTimeout(Dart_Port port, int64_t timeout) { | 25 void TimeoutQueue::UpdateTimeout(Dart_Port port, int64_t timeout) { |
| 25 // Find port if present. | 26 // Find port if present. |
| 26 Timeout* last = NULL; | 27 Timeout* last = NULL; |
| 27 Timeout* current = timeouts_; | 28 Timeout* current = timeouts_; |
| 28 while (current != NULL) { | 29 while (current != NULL) { |
| 29 if (current->port() == port) { | 30 if (current->port() == port) { |
| 30 // Found. | 31 // Found. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 55 while (current != NULL) { | 56 while (current != NULL) { |
| 56 if (next_timeout_ == NULL || | 57 if (next_timeout_ == NULL || |
| 57 current->timeout() < next_timeout_->timeout()) { | 58 current->timeout() < next_timeout_->timeout()) { |
| 58 next_timeout_ = current; | 59 next_timeout_ = current; |
| 59 } | 60 } |
| 60 current = current->next(); | 61 current = current->next(); |
| 61 } | 62 } |
| 62 } | 63 } |
| 63 | 64 |
| 64 | 65 |
| 65 /* | |
| 66 * Returns the reference of the EventHandler stored in the native field. | |
| 67 */ | |
| 68 static EventHandler* GetEventHandler(Dart_Handle handle) { | |
| 69 ASSERT(event_handler != NULL); | |
| 70 return event_handler; | |
| 71 } | |
| 72 | |
| 73 void EventHandler::Stop() { | 66 void EventHandler::Stop() { |
| 67 MutexLocker locker(mutex_); |
| 74 if (event_handler == NULL) return; | 68 if (event_handler == NULL) return; |
| 75 event_handler->Shutdown(); | 69 event_handler->Shutdown(); |
| 70 event_handler = NULL; |
| 76 } | 71 } |
| 77 | 72 |
| 78 | 73 |
| 79 /* | 74 /* |
| 80 * Starts the EventHandler thread and stores its reference in the dart | 75 * Starts the EventHandler thread and stores its reference in the dart |
| 81 * EventHandler object. args[0] holds the reference to the dart EventHandler | 76 * EventHandler object. args[0] holds the reference to the dart EventHandler |
| 82 * object. | 77 * object. |
| 83 */ | 78 */ |
| 84 void FUNCTION_NAME(EventHandler_Start)(Dart_NativeArguments args) { | 79 void FUNCTION_NAME(EventHandler_Start)(Dart_NativeArguments args) { |
| 85 MutexLocker locker(mutex_); | 80 MutexLocker locker(mutex_); |
| 86 if (event_handler != NULL) return; | 81 if (event_handler != NULL) return; |
| 87 event_handler = EventHandler::Start(); | 82 event_handler = EventHandler::Start(); |
| 88 } | 83 } |
| 89 | 84 |
| 90 | 85 |
| 91 /* | 86 /* |
| 92 * Send data to the EventHandler thread to register for a given instance | 87 * Send data to the EventHandler thread to register for a given instance |
| 93 * args[1] a ReceivePort args[2] with a notification event args[3]. args[0] | 88 * args[1] a ReceivePort args[2] with a notification event args[3]. args[0] |
| 94 * holds the reference to the dart EventHandler object. | 89 * holds the reference to the dart EventHandler object. |
| 95 */ | 90 */ |
| 96 void FUNCTION_NAME(EventHandler_SendData)(Dart_NativeArguments args) { | 91 void FUNCTION_NAME(EventHandler_SendData)(Dart_NativeArguments args) { |
| 97 Dart_EnterScope(); | 92 Dart_EnterScope(); |
| 98 Dart_Handle handle = Dart_GetNativeArgument(args, 0); | |
| 99 EventHandler* event_handler = GetEventHandler(handle); | |
| 100 Dart_Handle sender = Dart_GetNativeArgument(args, 1); | 93 Dart_Handle sender = Dart_GetNativeArgument(args, 1); |
| 101 intptr_t id = kInvalidId; | 94 intptr_t id = kInvalidId; |
| 102 if (Dart_IsNull(sender)) { | 95 if (Dart_IsNull(sender)) { |
| 103 id = kTimerId; | 96 id = kTimerId; |
| 104 } else { | 97 } else { |
| 105 Socket::GetSocketIdNativeField(sender, &id); | 98 Socket::GetSocketIdNativeField(sender, &id); |
| 106 } | 99 } |
| 107 handle = Dart_GetNativeArgument(args, 2); | 100 Dart_Handle handle = Dart_GetNativeArgument(args, 2); |
| 108 Dart_Port dart_port = | 101 Dart_Port dart_port = |
| 109 DartUtils::GetIntegerField(handle, DartUtils::kIdFieldName); | 102 DartUtils::GetIntegerField(handle, DartUtils::kIdFieldName); |
| 110 int64_t data = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 103 int64_t data = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
| 111 event_handler->SendData(id, dart_port, data); | 104 { |
| 105 MutexLocker locker(mutex_); |
| 106 // Only send if the event_handler is not NULL. This means that the handler |
| 107 // shut down, and a message is send later on. |
| 108 if (event_handler != NULL) { |
| 109 event_handler->SendData(id, dart_port, data); |
| 110 } |
| 111 } |
| 112 Dart_ExitScope(); | 112 Dart_ExitScope(); |
| 113 } | 113 } |
| 114 | 114 |
| 115 } // namespace bin | 115 } // namespace bin |
| 116 } // namespace dart | 116 } // namespace dart |
| OLD | NEW |