Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: runtime/vm/custom_isolate_test.cc

Issue 9182001: OOB messages and general message refactor. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 "include/dart_api.h" 5 #include "include/dart_api.h"
6 6
7 #include "vm/unit_test.h" 7 #include "vm/unit_test.h"
8 8
9 // Custom Isolate Test. 9 // Custom Isolate Test.
10 // 10 //
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 78
79 // An entry in our event queue. 79 // An entry in our event queue.
80 class Event { 80 class Event {
81 protected: 81 protected:
82 Event() : next_(NULL) {} 82 Event() : next_(NULL) {}
83 83
84 public: 84 public:
85 virtual ~Event() {} 85 virtual ~Event() {}
86 virtual void Process() = 0; 86 virtual void Process() = 0;
87 87
88 virtual bool IsShutdownEvent(Dart_Isolate isolate) { 88 virtual bool IsMessageEvent(Dart_Isolate isolate) {
Anton Muhin 2012/01/12 12:58:16 maybe now move this logic into StartEvent and add
turnidge 2012/01/12 19:01:55 I have added an isolate_ field to the base Event c
Anton Muhin 2012/01/13 14:26:53 Yes, thank you. On 2012/01/12 19:01:55, turnidge
89 return false;
90 }
91 virtual bool IsMessageEvent(Dart_Isolate isolate, Dart_Port port) {
92 return false; 89 return false;
93 } 90 }
94 91
95 private: 92 private:
96 friend class EventQueue; 93 friend class EventQueue;
97 Event* next_; 94 Event* next_;
98 }; 95 };
99 96
100 97
98 // A simple event queue for our test.
99 class EventQueue {
100 public:
101 EventQueue() {
102 head_ = NULL;
103 }
104
105 void Add(Event* event) {
106 if (head_ == NULL) {
107 head_ = event;
108 tail_ = event;
109 } else {
110 tail_->next_ = event;
111 tail_ = event;
112 }
113 }
114
115 Event* Get() {
116 if (head_ == NULL) {
117 return NULL;
118 }
119 Event* tmp = head_;
120 head_ = head_->next_;
121 if (head_ == NULL) {
122 tail_ = NULL;
Anton Muhin 2012/01/12 12:58:16 technically, you don't need this assignment, corre
turnidge 2012/01/12 19:01:55 Added a comment.
123 }
124
125 return tmp;
126 }
127
128 void Remove(Dart_Isolate isolate) {
Anton Muhin 2012/01/12 12:58:16 nit: longer name like RemoveEventsForIsolate may m
turnidge 2012/01/12 19:01:55 Done.
129 Event* cur = head_;
130 Event* prev = NULL;
131 while (cur != NULL) {
132 Event* next = cur->next_;
133 if (cur->IsMessageEvent(isolate)) {
134 // Remove matching event.
135 if (prev != NULL) {
136 prev->next_ = next;
137 } else {
138 head_ = next;
139 }
140 delete cur;
141 } else {
142 // Advance.
143 prev = cur;
144 }
145 cur = next;
146 }
147 tail_ = prev;
148 }
149
150 private:
151 Event* head_;
152 Event* tail_;
153 };
154 Event* current_event;
Anton Muhin 2012/01/12 12:58:16 it looks like you don't need current_event any mor
turnidge 2012/01/12 19:01:55 Thanks, removed.
155 EventQueue* event_queue;
156
157
101 // Start an isolate. 158 // Start an isolate.
102 class StartEvent : public Event { 159 class StartEvent : public Event {
103 public: 160 public:
104 StartEvent(Dart_Isolate isolate, const char* main) 161 StartEvent(Dart_Isolate isolate, const char* main)
105 : isolate_(isolate), main_(main) {} 162 : isolate_(isolate), main_(main) {}
106 163
107 virtual void Process(); 164 virtual void Process();
108 private: 165 private:
109 Dart_Isolate isolate_; 166 Dart_Isolate isolate_;
110 const char* main_; 167 const char* main_;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 NULL); 200 NULL);
144 EXPECT_VALID(result); 201 EXPECT_VALID(result);
145 free(const_cast<char*>(main_)); 202 free(const_cast<char*>(main_));
146 main_ = NULL; 203 main_ = NULL;
147 204
148 Dart_ExitScope(); 205 Dart_ExitScope();
149 Dart_ExitIsolate(); 206 Dart_ExitIsolate();
150 } 207 }
151 208
152 209
153 // Shutdown an isolate. 210 // Notify an isolate of a pending message.
154 class ShutdownEvent : public Event { 211 class MessageEvent : public Event {
155 public: 212 public:
156 explicit ShutdownEvent(Dart_Isolate isolate) : isolate_(isolate) {} 213 explicit MessageEvent(Dart_Isolate isolate) : isolate_(isolate) {}
157 214
158 virtual bool IsShutdownEvent(Dart_Isolate isolate) { 215 ~MessageEvent() {
216 }
217
218 virtual bool IsMessageEvent(Dart_Isolate isolate) {
159 return isolate == isolate_; 219 return isolate == isolate_;
160 } 220 }
161 221
162 virtual void Process(); 222 virtual void Process();
163 private: 223 private:
164 Dart_Isolate isolate_; 224 Dart_Isolate isolate_;
165 }; 225 };
166 226
167 227
168 void ShutdownEvent::Process() { 228 void MessageEvent::Process() {
169 OS::Print("<< ShutdownEvent with isolate(%p)--\n", isolate_); 229 OS::Print("$$ MessageEvent with isolate(%p)\n", isolate_);
170 Dart_EnterIsolate(isolate_); 230 Dart_EnterIsolate(isolate_);
171 Dart_ShutdownIsolate(); 231 Dart_EnterScope();
232
233 Dart_Handle result = Dart_HandleMessage();
234 EXPECT_VALID(result);
235
236 if (!Dart_HasLivePorts()) {
237 OS::Print("<< Shutting down isolate(%p)\n", isolate_);
238 event_queue->Remove(isolate_);
239 Dart_ShutdownIsolate();
240 } else {
241 Dart_ExitScope();
242 Dart_ExitIsolate();
243 }
244 ASSERT(Dart_CurrentIsolate() == NULL);
172 } 245 }
173 246
174 247
175 // Deliver a message to an isolate. 248 static void NotifyMessage(Dart_Isolate dest_isolate) {
176 class MessageEvent : public Event { 249 OS::Print("-- Notify isolate(%p) of pending message --\n", dest_isolate);
177 public:
178 MessageEvent(Dart_Isolate isolate, Dart_Port dest, Dart_Port reply,
179 Dart_Message msg)
180 : isolate_(isolate), dest_(dest), reply_(reply), msg_(msg) {}
181
182 ~MessageEvent() {
183 free(msg_);
184 msg_ = NULL;
185 }
186
187 virtual bool IsMessageEvent(Dart_Isolate isolate, Dart_Port port) {
188 return isolate == isolate_ && (port == kCloseAllPorts || port == dest_);
189 }
190
191 virtual void Process();
192 private:
193 Dart_Isolate isolate_;
194 Dart_Port dest_;
195 Dart_Port reply_;
196 Dart_Message msg_;
197 };
198
199
200 void MessageEvent::Process() {
201 OS::Print("$$ MessageEvent with dest port %lld--\n", dest_);
202 Dart_EnterIsolate(isolate_);
203 Dart_EnterScope();
204
205 Dart_Handle result = Dart_HandleMessage(dest_, reply_, msg_);
206 EXPECT_VALID(result);
207
208 Dart_ExitScope();
209 Dart_ExitIsolate();
210 }
211
212
213 // A simple event queue for our test.
214 class EventQueue {
215 public:
216 EventQueue() {
217 head_ = NULL;
218 }
219
220 void Add(Event* event) {
221 if (head_ == NULL) {
222 head_ = event;
223 tail_ = event;
224 } else {
225 tail_->next_ = event;
226 tail_ = event;
227 }
228 }
229
230 Event* Get() {
231 if (head_ == NULL) {
232 return NULL;
233 }
234 Event* tmp = head_;
235 head_ = head_->next_;
236 if (head_ == NULL) {
237 tail_ = NULL;
238 }
239
240 return tmp;
241 }
242
243 void ClosePort(Dart_Isolate isolate, Dart_Port port) {
244 Event* cur = head_;
245 Event* prev = NULL;
246 while (cur != NULL) {
247 Event* next = cur->next_;
248 if (cur->IsMessageEvent(isolate, port)) {
249 // Remove matching event.
250 if (prev != NULL) {
251 prev->next_ = next;
252 } else {
253 head_ = next;
254 }
255 delete cur;
256 } else {
257 // Advance.
258 prev = cur;
259 }
260 cur = next;
261 }
262 tail_ = prev;
263 }
264
265 private:
266 Event* head_;
267 Event* tail_;
268 };
269 EventQueue* event_queue;
270 Event* current_event;
271
272 static bool PostMessage(Dart_Isolate dest_isolate,
273 Dart_Port dest_port,
274 Dart_Port reply_port,
275 Dart_Message message) {
276 OS::Print("-- Posting message dest(%d) reply(%d) --\n",
277 dest_port, reply_port);
278 OS::Print("-- Adding MessageEvent to queue --\n"); 250 OS::Print("-- Adding MessageEvent to queue --\n");
279 event_queue->Add( 251 event_queue->Add(new MessageEvent(dest_isolate));
280 new MessageEvent(dest_isolate, dest_port, reply_port, message));
281 return true;
282 }
283
284
285 static void ClosePort(Dart_Isolate isolate,
286 Dart_Port port) {
287 OS::Print("-- Closing port (%lld) for isolate(%p) --\n",
288 port, isolate);
289
290 // Remove any pending events for the isolate/port.
291 event_queue->ClosePort(isolate, port);
292
293 Dart_Isolate current = Dart_CurrentIsolate();
294 if (current) {
295 Dart_ExitIsolate();
296 }
297 Dart_EnterIsolate(isolate);
298 if (!Dart_HasLivePorts() &&
299 (current_event == NULL || !current_event->IsShutdownEvent(isolate))) {
300 OS::Print("-- Adding ShutdownEvent to queue --\n");
301 event_queue->Add(new ShutdownEvent(isolate));
302 }
303 Dart_ExitIsolate();
304 if (current) {
305 Dart_EnterIsolate(current);
306 }
307 } 252 }
308 253
309 254
310 static Dart_NativeFunction NativeLookup(Dart_Handle name, int argc) { 255 static Dart_NativeFunction NativeLookup(Dart_Handle name, int argc) {
311 const char* name_str = NULL; 256 const char* name_str = NULL;
312 EXPECT(Dart_IsString(name)); 257 EXPECT(Dart_IsString(name));
313 EXPECT_VALID(Dart_StringToCString(name, &name_str)); 258 EXPECT_VALID(Dart_StringToCString(name, &name_str));
314 if (strcmp(name_str, "native_echo") == 0) { 259 if (strcmp(name_str, "native_echo") == 0) {
315 return &native_echo; 260 return &native_echo;
316 } else if (strcmp(name_str, "CustomIsolateImpl_start") == 0) { 261 } else if (strcmp(name_str, "CustomIsolateImpl_start") == 0) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 EXPECT_VALID(Dart_StringToCString(param, &isolate_main)); 296 EXPECT_VALID(Dart_StringToCString(param, &isolate_main));
352 isolate_main = strdup(isolate_main); 297 isolate_main = strdup(isolate_main);
353 298
354 // Save current isolate. 299 // Save current isolate.
355 Dart_Isolate saved_isolate = Dart_CurrentIsolate(); 300 Dart_Isolate saved_isolate = Dart_CurrentIsolate();
356 Dart_ExitIsolate(); 301 Dart_ExitIsolate();
357 302
358 // Create a new Dart_Isolate. 303 // Create a new Dart_Isolate.
359 Dart_Isolate new_isolate = TestCase::CreateTestIsolate(); 304 Dart_Isolate new_isolate = TestCase::CreateTestIsolate();
360 EXPECT(new_isolate != NULL); 305 EXPECT(new_isolate != NULL);
361 Dart_SetMessageCallbacks(&PostMessage, &ClosePort); 306 Dart_SetMessageNotifyCallback(&NotifyMessage);
362 Dart_Port new_port = Dart_GetMainPortId(); 307 Dart_Port new_port = Dart_GetMainPortId();
363 308
364 OS::Print("-- Adding StartEvent to queue --\n"); 309 OS::Print("-- Adding StartEvent to queue --\n");
365 event_queue->Add(new StartEvent(new_isolate, isolate_main)); 310 event_queue->Add(new StartEvent(new_isolate, isolate_main));
366 311
367 // Restore the original isolate. 312 // Restore the original isolate.
368 Dart_ExitIsolate(); 313 Dart_ExitIsolate();
369 Dart_EnterIsolate(saved_isolate); 314 Dart_EnterIsolate(saved_isolate);
370 Dart_EnterScope(); 315 Dart_EnterScope();
371 316
372 Dart_Handle send_port = Dart_NewSendPort(new_port); 317 Dart_Handle send_port = Dart_NewSendPort(new_port);
373 EXPECT_VALID(send_port); 318 EXPECT_VALID(send_port);
374 Dart_SetReturnValue(args, send_port); 319 Dart_SetReturnValue(args, send_port);
375 320
376 OS::Print("-- Exit: CustomIsolateImpl_start --\n"); 321 OS::Print("-- Exit: CustomIsolateImpl_start --\n");
377 Dart_ExitScope(); 322 Dart_ExitScope();
378 } 323 }
379 324
380 325
381 UNIT_TEST_CASE(CustomIsolates) { 326 UNIT_TEST_CASE(CustomIsolates) {
382 event_queue = new EventQueue(); 327 event_queue = new EventQueue();
383 current_event = NULL; 328 current_event = NULL;
384 329
385 Dart_Isolate dart_isolate = TestCase::CreateTestIsolate(); 330 Dart_Isolate dart_isolate = TestCase::CreateTestIsolate();
386 EXPECT(dart_isolate != NULL); 331 EXPECT(dart_isolate != NULL);
387 Dart_SetMessageCallbacks(&PostMessage, &ClosePort); 332 Dart_SetMessageNotifyCallback(&NotifyMessage);
388 Dart_EnterScope(); 333 Dart_EnterScope();
389 Dart_Handle result; 334 Dart_Handle result;
390 335
391 // Create a test library. 336 // Create a test library.
392 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, 337 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars,
393 NativeLookup); 338 NativeLookup);
394 EXPECT_VALID(lib); 339 EXPECT_VALID(lib);
395 340
396 // Run main. 341 // Run main.
397 result = Dart_InvokeStatic(lib, 342 result = Dart_InvokeStatic(lib,
(...skipping 22 matching lines...) Expand all
420 OS::Print("-- Finished event loop --\n"); 365 OS::Print("-- Finished event loop --\n");
421 EXPECT_STREQ("Received: 43", saved_echo); 366 EXPECT_STREQ("Received: 43", saved_echo);
422 free(const_cast<char*>(saved_echo)); 367 free(const_cast<char*>(saved_echo));
423 368
424 delete event_queue; 369 delete event_queue;
425 } 370 }
426 371
427 #endif // TARGET_ARCH_IA32. 372 #endif // TARGET_ARCH_IA32.
428 373
429 } // namespace dart 374 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698