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 | 9 |
9 #include "include/dart_api.h" | 10 #include "include/dart_api.h" |
10 | 11 |
11 | 12 |
12 namespace dart { | 13 namespace dart { |
13 namespace bin { | 14 namespace bin { |
14 | 15 |
15 static const int kNativeEventHandlerFieldIndex = 0; | 16 static const int kNativeEventHandlerFieldIndex = 0; |
16 static const intptr_t kTimerId = -1; | 17 static const intptr_t kTimerId = -1; |
17 static const intptr_t kInvalidId = -2; | 18 static const intptr_t kInvalidId = -2; |
18 | 19 |
| 20 static EventHandler* event_handler = NULL; |
| 21 static dart::Mutex* mutex_ = new dart::Mutex(); |
| 22 |
| 23 |
| 24 void TimeoutQueue::UpdateTimeout(Dart_Port port, int64_t timeout) { |
| 25 // Find port if present. |
| 26 Timeout* last = NULL; |
| 27 Timeout* current = timeouts_; |
| 28 while (current != NULL) { |
| 29 if (current->port() == port) { |
| 30 // Found. |
| 31 if (timeout < 0) { |
| 32 // Remove from list and delete existing. |
| 33 if (last != NULL) { |
| 34 last->set_next(current->next()); |
| 35 } else { |
| 36 timeouts_ = current->next(); |
| 37 } |
| 38 delete current; |
| 39 } else { |
| 40 // Update timeout. |
| 41 current->set_timeout(timeout); |
| 42 } |
| 43 break; |
| 44 } |
| 45 last = current; |
| 46 current = current->next(); |
| 47 } |
| 48 if (current == NULL && timeout >= 0) { |
| 49 // Not found, create a new. |
| 50 timeouts_ = new Timeout(port, timeout, timeouts_); |
| 51 } |
| 52 // Clear and find next timeout. |
| 53 next_timeout_ = NULL; |
| 54 current = timeouts_; |
| 55 while (current != NULL) { |
| 56 if (next_timeout_ == NULL || |
| 57 current->timeout() < next_timeout_->timeout()) { |
| 58 next_timeout_ = current; |
| 59 } |
| 60 current = current->next(); |
| 61 } |
| 62 } |
| 63 |
| 64 |
19 /* | 65 /* |
20 * Returns the reference of the EventHandler stored in the native field. | 66 * Returns the reference of the EventHandler stored in the native field. |
21 */ | 67 */ |
22 static EventHandler* GetEventHandler(Dart_Handle handle) { | 68 static EventHandler* GetEventHandler(Dart_Handle handle) { |
23 intptr_t value = 0; | |
24 Dart_Handle result = Dart_GetNativeInstanceField( | |
25 handle, kNativeEventHandlerFieldIndex, &value); | |
26 if (Dart_IsError(result)) { | |
27 Dart_PropagateError(result); | |
28 } | |
29 EventHandler* event_handler = reinterpret_cast<EventHandler*>(value); | |
30 ASSERT(event_handler != NULL); | 69 ASSERT(event_handler != NULL); |
31 return event_handler; | 70 return event_handler; |
32 } | 71 } |
33 | 72 |
34 | 73 void EventHandler::Stop() { |
35 /* | 74 if (event_handler == NULL) return; |
36 * Sets the reference of the EventHandler in the native field. | 75 event_handler->Shutdown(); |
37 */ | |
38 static void SetEventHandler(Dart_Handle handle, EventHandler* event_handler) { | |
39 Dart_SetNativeInstanceField(handle, | |
40 kNativeEventHandlerFieldIndex, | |
41 reinterpret_cast<intptr_t>(event_handler)); | |
42 } | 76 } |
43 | 77 |
44 | 78 |
45 /* | 79 /* |
46 * Starts the EventHandler thread and stores its reference in the dart | 80 * Starts the EventHandler thread and stores its reference in the dart |
47 * EventHandler object. args[0] holds the reference to the dart EventHandler | 81 * EventHandler object. args[0] holds the reference to the dart EventHandler |
48 * object. | 82 * object. |
49 */ | 83 */ |
50 void FUNCTION_NAME(EventHandler_Start)(Dart_NativeArguments args) { | 84 void FUNCTION_NAME(EventHandler_Start)(Dart_NativeArguments args) { |
51 Dart_EnterScope(); | 85 MutexLocker locker(mutex_); |
52 Dart_Handle handle = Dart_GetNativeArgument(args, 0); | 86 if (event_handler != NULL) return; |
53 EventHandler* event_handler = EventHandler::Start(); | 87 event_handler = EventHandler::Start(); |
54 SetEventHandler(handle, event_handler); | |
55 Dart_ExitScope(); | |
56 } | 88 } |
57 | 89 |
58 | 90 |
59 /* | 91 /* |
60 * Send data to the EventHandler thread to register for a given instance | 92 * Send data to the EventHandler thread to register for a given instance |
61 * args[1] a ReceivePort args[2] with a notification event args[3]. args[0] | 93 * args[1] a ReceivePort args[2] with a notification event args[3]. args[0] |
62 * holds the reference to the dart EventHandler object. | 94 * holds the reference to the dart EventHandler object. |
63 */ | 95 */ |
64 void FUNCTION_NAME(EventHandler_SendData)(Dart_NativeArguments args) { | 96 void FUNCTION_NAME(EventHandler_SendData)(Dart_NativeArguments args) { |
65 Dart_EnterScope(); | 97 Dart_EnterScope(); |
66 Dart_Handle handle = Dart_GetNativeArgument(args, 0); | 98 Dart_Handle handle = Dart_GetNativeArgument(args, 0); |
67 EventHandler* event_handler = GetEventHandler(handle); | 99 EventHandler* event_handler = GetEventHandler(handle); |
68 Dart_Handle sender = Dart_GetNativeArgument(args, 1); | 100 Dart_Handle sender = Dart_GetNativeArgument(args, 1); |
69 intptr_t id = kInvalidId; | 101 intptr_t id = kInvalidId; |
70 if (Dart_IsNull(sender)) { | 102 if (Dart_IsNull(sender)) { |
71 id = kTimerId; | 103 id = kTimerId; |
72 } else { | 104 } else { |
73 Socket::GetSocketIdNativeField(sender, &id); | 105 Socket::GetSocketIdNativeField(sender, &id); |
74 } | 106 } |
75 handle = Dart_GetNativeArgument(args, 2); | 107 handle = Dart_GetNativeArgument(args, 2); |
76 Dart_Port dart_port = | 108 Dart_Port dart_port = |
77 DartUtils::GetIntegerField(handle, DartUtils::kIdFieldName); | 109 DartUtils::GetIntegerField(handle, DartUtils::kIdFieldName); |
78 int64_t data = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 110 int64_t data = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
79 event_handler->SendData(id, dart_port, data); | 111 event_handler->SendData(id, dart_port, data); |
80 Dart_ExitScope(); | 112 Dart_ExitScope(); |
81 } | 113 } |
82 | 114 |
83 } // namespace bin | 115 } // namespace bin |
84 } // namespace dart | 116 } // namespace dart |
OLD | NEW |