| 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 intptr_t kTimerId = -1; | 16 static const intptr_t kTimerId = -1; |
| 17 static const intptr_t kInvalidId = -2; | 17 static const intptr_t kInvalidId = -2; |
| 18 | 18 |
| 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. | |
| 22 static dart::Mutex* mutex_ = new dart::Mutex(); | |
| 23 | |
| 24 | |
| 25 void TimeoutQueue::UpdateTimeout(Dart_Port port, int64_t timeout) { | 19 void TimeoutQueue::UpdateTimeout(Dart_Port port, int64_t timeout) { |
| 26 // Find port if present. | 20 // Find port if present. |
| 27 Timeout* last = NULL; | 21 Timeout* last = NULL; |
| 28 Timeout* current = timeouts_; | 22 Timeout* current = timeouts_; |
| 29 while (current != NULL) { | 23 while (current != NULL) { |
| 30 if (current->port() == port) { | 24 if (current->port() == port) { |
| 31 // Found. | 25 // Found. |
| 32 if (timeout < 0) { | 26 if (timeout < 0) { |
| 33 // Remove from list and delete existing. | 27 // Remove from list and delete existing. |
| 34 if (last != NULL) { | 28 if (last != NULL) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 56 while (current != NULL) { | 50 while (current != NULL) { |
| 57 if (next_timeout_ == NULL || | 51 if (next_timeout_ == NULL || |
| 58 current->timeout() < next_timeout_->timeout()) { | 52 current->timeout() < next_timeout_->timeout()) { |
| 59 next_timeout_ = current; | 53 next_timeout_ = current; |
| 60 } | 54 } |
| 61 current = current->next(); | 55 current = current->next(); |
| 62 } | 56 } |
| 63 } | 57 } |
| 64 | 58 |
| 65 | 59 |
| 60 static EventHandler* event_handler = NULL; |
| 61 |
| 62 |
| 63 void EventHandler::Start() { |
| 64 ASSERT(event_handler == NULL); |
| 65 event_handler = new EventHandler(); |
| 66 event_handler->delegate_.Start(event_handler); |
| 67 } |
| 68 |
| 69 |
| 66 void EventHandler::Stop() { | 70 void EventHandler::Stop() { |
| 67 MutexLocker locker(mutex_); | |
| 68 if (event_handler == NULL) return; | 71 if (event_handler == NULL) return; |
| 69 event_handler->Shutdown(); | 72 event_handler->delegate_.Shutdown(); |
| 70 event_handler = NULL; | 73 event_handler = NULL; |
| 71 } | 74 } |
| 72 | 75 |
| 73 | 76 |
| 74 /* | 77 /* |
| 75 * Starts the EventHandler thread and stores its reference in the dart | |
| 76 * EventHandler object. args[0] holds the reference to the dart EventHandler | |
| 77 * object. | |
| 78 */ | |
| 79 void FUNCTION_NAME(EventHandler_Start)(Dart_NativeArguments args) { | |
| 80 MutexLocker locker(mutex_); | |
| 81 if (event_handler != NULL) return; | |
| 82 event_handler = EventHandler::Start(); | |
| 83 } | |
| 84 | |
| 85 | |
| 86 /* | |
| 87 * Send data to the EventHandler thread to register for a given instance | 78 * Send data to the EventHandler thread to register for a given instance |
| 88 * args[1] a ReceivePort args[2] with a notification event args[3]. args[0] | 79 * args[0] a ReceivePort args[1] with a notification event args[2]. |
| 89 * holds the reference to the dart EventHandler object. | |
| 90 */ | 80 */ |
| 91 void FUNCTION_NAME(EventHandler_SendData)(Dart_NativeArguments args) { | 81 void FUNCTION_NAME(EventHandler_SendData)(Dart_NativeArguments args) { |
| 92 Dart_Handle sender = Dart_GetNativeArgument(args, 1); | 82 Dart_Handle sender = Dart_GetNativeArgument(args, 0); |
| 93 intptr_t id = kInvalidId; | 83 intptr_t id = kInvalidId; |
| 94 if (Dart_IsNull(sender)) { | 84 if (Dart_IsNull(sender)) { |
| 95 id = kTimerId; | 85 id = kTimerId; |
| 96 } else { | 86 } else { |
| 97 Socket::GetSocketIdNativeField(sender, &id); | 87 Socket::GetSocketIdNativeField(sender, &id); |
| 98 } | 88 } |
| 99 Dart_Handle handle = Dart_GetNativeArgument(args, 2); | 89 Dart_Handle handle = Dart_GetNativeArgument(args, 1); |
| 100 Dart_Port dart_port = | 90 Dart_Port dart_port = |
| 101 DartUtils::GetIntegerField(handle, DartUtils::kIdFieldName); | 91 DartUtils::GetIntegerField(handle, DartUtils::kIdFieldName); |
| 102 int64_t data = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 92 int64_t data = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); |
| 103 { | 93 event_handler->SendData(id, dart_port, data); |
| 104 MutexLocker locker(mutex_); | |
| 105 // Only send if the event_handler is not NULL. This means that the handler | |
| 106 // shut down, and a message is send later on. | |
| 107 if (event_handler != NULL) { | |
| 108 event_handler->SendData(id, dart_port, data); | |
| 109 } | |
| 110 } | |
| 111 } | 94 } |
| 112 | 95 |
| 113 } // namespace bin | 96 } // namespace bin |
| 114 } // namespace dart | 97 } // namespace dart |
| OLD | NEW |