OLD | NEW |
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 #include "include/dart_native_api.h" | 6 #include "include/dart_native_api.h" |
7 | 7 |
8 #include "vm/unit_test.h" | 8 #include "vm/unit_test.h" |
9 | 9 |
10 // Custom Isolate Test. | 10 // Custom Isolate Test. |
11 // | 11 // |
12 // This mid-size test uses the Dart Embedding Api to create a custom | 12 // This mid-size test uses the Dart Embedding Api to create a custom |
13 // isolate abstraction. Instead of having a dedicated thread for each | 13 // isolate abstraction. Instead of having a dedicated thread for each |
14 // isolate, as is the case normally, this implementation shares a | 14 // isolate, as is the case normally, this implementation shares a |
15 // single thread among the isolates using an event queue. | 15 // single thread among the isolates using an event queue. |
16 | 16 |
17 namespace dart { | 17 namespace dart { |
18 | 18 |
19 static void native_echo(Dart_NativeArguments args); | 19 static void native_echo(Dart_NativeArguments args); |
20 static void CustomIsolateImpl_start(Dart_NativeArguments args); | 20 static void CustomIsolateImpl_start(Dart_NativeArguments args); |
21 static Dart_NativeFunction NativeLookup(Dart_Handle name, int argc); | 21 static Dart_NativeFunction NativeLookup(Dart_Handle name, int argc); |
22 | 22 |
23 | 23 |
24 static const char* kCustomIsolateScriptChars = | 24 static const char* kCustomIsolateScriptChars = |
25 "import 'dart:isolate';\n" | 25 "import 'dart:isolate';\n" |
26 "\n" | 26 "\n" |
27 "ReceivePort mainPort;\n" | 27 "RawReceivePort mainPort;\n" |
28 "\n" | 28 "\n" |
29 "echo(arg) native \"native_echo\";\n" | 29 "echo(arg) native \"native_echo\";\n" |
30 "\n" | 30 "\n" |
31 "class CustomIsolateImpl implements CustomIsolate {\n" | 31 "class CustomIsolateImpl implements CustomIsolate {\n" |
32 " CustomIsolateImpl(String entry) : _entry = entry{\n" | 32 " CustomIsolateImpl(String entry) : _entry = entry{\n" |
33 " echo('Constructing isolate');\n" | 33 " echo('Constructing isolate');\n" |
34 " }\n" | 34 " }\n" |
35 "\n" | 35 "\n" |
36 " SendPort spawn() {\n" | 36 " SendPort spawn() {\n" |
37 " return _start(_entry);\n" | 37 " return _start(_entry);\n" |
38 " }\n" | 38 " }\n" |
39 "\n" | 39 "\n" |
40 " static SendPort _start(entry)\n" | 40 " static SendPort _start(entry)\n" |
41 " native \"CustomIsolateImpl_start\";\n" | 41 " native \"CustomIsolateImpl_start\";\n" |
42 "\n" | 42 "\n" |
43 " String _entry;\n" | 43 " String _entry;\n" |
44 "}\n" | 44 "}\n" |
45 "\n" | 45 "\n" |
46 "abstract class CustomIsolate {\n" | 46 "abstract class CustomIsolate {\n" |
47 " factory CustomIsolate(String entry) = CustomIsolateImpl;\n" | 47 " factory CustomIsolate(String entry) = CustomIsolateImpl;\n" |
48 "\n" | 48 "\n" |
49 " SendPort spawn();\n" | 49 " SendPort spawn();\n" |
50 "}\n" | 50 "}\n" |
51 "\n" | 51 "\n" |
52 "isolateMain() {\n" | 52 "isolateMain() {\n" |
53 " echo('Running isolateMain');\n" | 53 " echo('Running isolateMain');\n" |
54 " mainPort.receive((message, SendPort replyTo) {\n" | 54 " mainPort.handler = (message) {\n" |
55 " echo('Received: $message');\n" | 55 " var data = message[0];\n" |
56 " replyTo.send((message + 1), null);\n" | 56 " var replyTo = message[1];\n" |
57 " });\n" | 57 " echo('Received: $data');\n" |
| 58 " replyTo.send(data + 1);\n" |
| 59 " };\n" |
58 "}\n" | 60 "}\n" |
59 "\n" | 61 "\n" |
60 "main() {\n" | 62 "main() {\n" |
61 " var isolate = new CustomIsolate(\"isolateMain\");\n" | 63 " var isolate = new CustomIsolate(\"isolateMain\");\n" |
62 " var receivePort = new ReceivePort();\n" | 64 " var receivePort = new RawReceivePort();\n" |
63 " SendPort port = isolate.spawn();\n" | 65 " SendPort port = isolate.spawn();\n" |
64 " port.send(42, receivePort.toSendPort());\n" | 66 " port.send([42, receivePort.sendPort]);\n" |
65 " receivePort.receive((message, _) {\n" | 67 " receivePort.handler = (message) {\n" |
66 " receivePort.close();\n" | 68 " receivePort.close();\n" |
67 " echo('Received: $message');\n" | 69 " echo('Received: $message');\n" |
68 " });\n" | 70 " };\n" |
69 " return 'success';\n" | 71 " return 'success';\n" |
70 "}\n"; | 72 "}\n"; |
71 | 73 |
72 | 74 |
73 // An entry in our event queue. | 75 // An entry in our event queue. |
74 class Event { | 76 class Event { |
75 protected: | 77 protected: |
76 explicit Event(Dart_Isolate isolate) : isolate_(isolate), next_(NULL) {} | 78 explicit Event(Dart_Isolate isolate) : isolate_(isolate), next_(NULL) {} |
77 | 79 |
78 public: | 80 public: |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 event = event_queue->Get(); | 333 event = event_queue->Get(); |
332 } | 334 } |
333 OS::Print("-- Finished event loop --\n"); | 335 OS::Print("-- Finished event loop --\n"); |
334 EXPECT_STREQ("Received: 43", saved_echo); | 336 EXPECT_STREQ("Received: 43", saved_echo); |
335 free(const_cast<char*>(saved_echo)); | 337 free(const_cast<char*>(saved_echo)); |
336 | 338 |
337 delete event_queue; | 339 delete event_queue; |
338 } | 340 } |
339 | 341 |
340 } // namespace dart | 342 } // namespace dart |
OLD | NEW |