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 "platform/assert.h" | 5 #include "platform/assert.h" |
6 #include "vm/isolate.h" | 6 #include "vm/isolate.h" |
7 #include "vm/lockers.h" | 7 #include "vm/lockers.h" |
8 #include "vm/unit_test.h" | 8 #include "vm/unit_test.h" |
9 #include "vm/profiler.h" | 9 #include "vm/profiler.h" |
10 #include "vm/thread_pool.h" | 10 #include "vm/thread_pool.h" |
11 #include "vm/thread_registry.h" | 11 #include "vm/thread_registry.h" |
12 | 12 |
13 namespace dart { | 13 namespace dart { |
14 | 14 |
15 UNIT_TEST_CASE(Mutex) { | 15 UNIT_TEST_CASE(Mutex) { |
16 // This unit test case needs a running isolate. | 16 // This unit test case needs a running isolate. |
17 Isolate::Flags vm_flags; | 17 Dart_CreateIsolate( |
18 Dart_IsolateFlags api_flags; | 18 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); |
19 vm_flags.CopyTo(&api_flags); | |
20 Isolate* isolate = Isolate::Init(NULL, api_flags); | |
21 | 19 |
22 Mutex* mutex = new Mutex(); | 20 Mutex* mutex = new Mutex(); |
23 mutex->Lock(); | 21 mutex->Lock(); |
24 EXPECT_EQ(false, mutex->TryLock()); | 22 EXPECT_EQ(false, mutex->TryLock()); |
25 mutex->Unlock(); | 23 mutex->Unlock(); |
26 EXPECT_EQ(true, mutex->TryLock()); | 24 EXPECT_EQ(true, mutex->TryLock()); |
27 mutex->Unlock(); | 25 mutex->Unlock(); |
28 { | 26 { |
29 MutexLocker ml(mutex); | 27 MutexLocker ml(mutex); |
30 EXPECT_EQ(false, mutex->TryLock()); | 28 EXPECT_EQ(false, mutex->TryLock()); |
31 } | 29 } |
32 // The isolate shutdown and the destruction of the mutex are out-of-order on | 30 // The isolate shutdown and the destruction of the mutex are out-of-order on |
33 // purpose. | 31 // purpose. |
34 isolate->Shutdown(); | 32 Dart_ShutdownIsolate(); |
35 delete isolate; | |
36 delete mutex; | 33 delete mutex; |
37 } | 34 } |
38 | 35 |
39 | 36 |
40 UNIT_TEST_CASE(Monitor) { | 37 UNIT_TEST_CASE(Monitor) { |
41 // This unit test case needs a running isolate. | 38 // This unit test case needs a running isolate. |
42 Isolate::Flags vm_flags; | 39 Dart_CreateIsolate( |
43 Dart_IsolateFlags api_flags; | 40 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); |
44 vm_flags.CopyTo(&api_flags); | 41 Isolate* isolate = Isolate::Current(); |
45 Isolate* isolate = Isolate::Init(NULL, api_flags); | |
46 // Thread interrupter interferes with this test, disable interrupts. | 42 // Thread interrupter interferes with this test, disable interrupts. |
47 isolate->set_thread_state(NULL); | 43 isolate->set_thread_state(NULL); |
48 Profiler::EndExecution(isolate); | 44 Profiler::EndExecution(isolate); |
49 Monitor* monitor = new Monitor(); | 45 Monitor* monitor = new Monitor(); |
50 monitor->Enter(); | 46 monitor->Enter(); |
51 monitor->Exit(); | 47 monitor->Exit(); |
52 | 48 |
53 const int kNumAttempts = 5; | 49 const int kNumAttempts = 5; |
54 int attempts = 0; | 50 int attempts = 0; |
55 while (attempts < kNumAttempts) { | 51 while (attempts < kNumAttempts) { |
(...skipping 16 matching lines...) Expand all Loading... |
72 break; | 68 break; |
73 } | 69 } |
74 | 70 |
75 // Record the attempt. | 71 // Record the attempt. |
76 attempts++; | 72 attempts++; |
77 } | 73 } |
78 EXPECT_LT(attempts, kNumAttempts); | 74 EXPECT_LT(attempts, kNumAttempts); |
79 | 75 |
80 // The isolate shutdown and the destruction of the mutex are out-of-order on | 76 // The isolate shutdown and the destruction of the mutex are out-of-order on |
81 // purpose. | 77 // purpose. |
82 isolate->Shutdown(); | 78 Dart_ShutdownIsolate(); |
83 delete isolate; | |
84 delete monitor; | 79 delete monitor; |
85 } | 80 } |
86 | 81 |
87 | 82 |
88 class ObjectCounter : public ObjectPointerVisitor { | 83 class ObjectCounter : public ObjectPointerVisitor { |
89 public: | 84 public: |
90 explicit ObjectCounter(Isolate* isolate, const Object* obj) | 85 explicit ObjectCounter(Isolate* isolate, const Object* obj) |
91 : ObjectPointerVisitor(isolate), obj_(obj), count_(0) { } | 86 : ObjectPointerVisitor(isolate), obj_(obj), count_(0) { } |
92 | 87 |
93 virtual void VisitPointers(RawObject** first, RawObject** last) { | 88 virtual void VisitPointers(RawObject** first, RawObject** last) { |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 EXPECT(bar.Equals("bar")); | 198 EXPECT(bar.Equals("bar")); |
204 } | 199 } |
205 } | 200 } |
206 | 201 |
207 | 202 |
208 TEST_CASE(ThreadRegistry) { | 203 TEST_CASE(ThreadRegistry) { |
209 Isolate* orig = Thread::Current()->isolate(); | 204 Isolate* orig = Thread::Current()->isolate(); |
210 Zone* orig_zone = Thread::Current()->zone(); | 205 Zone* orig_zone = Thread::Current()->zone(); |
211 char* orig_str = orig_zone->PrintToString("foo"); | 206 char* orig_str = orig_zone->PrintToString("foo"); |
212 Thread::ExitIsolate(); | 207 Thread::ExitIsolate(); |
213 Isolate::Flags vm_flags; | |
214 Dart_IsolateFlags api_flags; | |
215 vm_flags.CopyTo(&api_flags); | |
216 Isolate* isos[2]; | |
217 // Create and enter a new isolate. | 208 // Create and enter a new isolate. |
218 isos[0] = Isolate::Init(NULL, api_flags); | 209 Dart_CreateIsolate( |
| 210 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); |
219 Zone* zone0 = Thread::Current()->zone(); | 211 Zone* zone0 = Thread::Current()->zone(); |
220 EXPECT(zone0 != orig_zone); | 212 EXPECT(zone0 != orig_zone); |
221 isos[0]->Shutdown(); | 213 Dart_ShutdownIsolate(); |
222 Thread::ExitIsolate(); | |
223 // Create and enter yet another isolate. | 214 // Create and enter yet another isolate. |
224 isos[1] = Isolate::Init(NULL, api_flags); | 215 Dart_CreateIsolate( |
| 216 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); |
225 { | 217 { |
226 // Create a stack resource this time, and exercise it. | 218 // Create a stack resource this time, and exercise it. |
227 StackZone stack_zone(Thread::Current()); | 219 StackZone stack_zone(Thread::Current()); |
228 Zone* zone1 = Thread::Current()->zone(); | 220 Zone* zone1 = Thread::Current()->zone(); |
229 EXPECT(zone1 != zone0); | 221 EXPECT(zone1 != zone0); |
230 EXPECT(zone1 != orig_zone); | 222 EXPECT(zone1 != orig_zone); |
231 } | 223 } |
232 isos[1]->Shutdown(); | 224 Dart_ShutdownIsolate(); |
233 Thread::ExitIsolate(); | |
234 Thread::EnterIsolate(orig); | 225 Thread::EnterIsolate(orig); |
235 // Original zone should be preserved. | 226 // Original zone should be preserved. |
236 EXPECT_EQ(orig_zone, Thread::Current()->zone()); | 227 EXPECT_EQ(orig_zone, Thread::Current()->zone()); |
237 EXPECT_STREQ("foo", orig_str); | 228 EXPECT_STREQ("foo", orig_str); |
238 delete isos[0]; | |
239 delete isos[1]; | |
240 } | 229 } |
241 | 230 |
242 | 231 |
243 // A helper thread that alternatingly cooperates and organizes | 232 // A helper thread that alternatingly cooperates and organizes |
244 // safepoint rendezvous. At rendezvous, it explicitly visits the | 233 // safepoint rendezvous. At rendezvous, it explicitly visits the |
245 // stacks looking for a specific marker (Smi) to verify that the expected | 234 // stacks looking for a specific marker (Smi) to verify that the expected |
246 // number threads are actually visited. The task is "done" when it has | 235 // number threads are actually visited. The task is "done" when it has |
247 // successfully made all other tasks and the main thread rendezvous (may | 236 // successfully made all other tasks and the main thread rendezvous (may |
248 // not happen in the first rendezvous, since tasks are still starting up). | 237 // not happen in the first rendezvous, since tasks are still starting up). |
249 class SafepointTestTask : public ThreadPool::Task { | 238 class SafepointTestTask : public ThreadPool::Task { |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 while (true) { | 431 while (true) { |
443 isolate->thread_registry()->CheckSafepoint(); | 432 isolate->thread_registry()->CheckSafepoint(); |
444 MutexLocker ml(&mutex); | 433 MutexLocker ml(&mutex); |
445 if (exited == SafepointTestTask::kTaskCount) { | 434 if (exited == SafepointTestTask::kTaskCount) { |
446 break; | 435 break; |
447 } | 436 } |
448 } | 437 } |
449 } | 438 } |
450 | 439 |
451 } // namespace dart | 440 } // namespace dart |
OLD | NEW |