| 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/safepoint.h" | 10 #include "vm/safepoint.h" |
| 11 #include "vm/stack_frame.h" | 11 #include "vm/stack_frame.h" |
| 12 #include "vm/thread_pool.h" | 12 #include "vm/thread_pool.h" |
| 13 | 13 |
| 14 namespace dart { | 14 namespace dart { |
| 15 | 15 |
| 16 UNIT_TEST_CASE(Mutex) { | 16 VM_UNIT_TEST_CASE(Mutex) { |
| 17 // This unit test case needs a running isolate. | 17 // This unit test case needs a running isolate. |
| 18 Dart_CreateIsolate(NULL, NULL, bin::core_isolate_snapshot_data, | 18 Dart_CreateIsolate(NULL, NULL, bin::core_isolate_snapshot_data, |
| 19 bin::core_isolate_snapshot_instructions, NULL, NULL, NULL); | 19 bin::core_isolate_snapshot_instructions, NULL, NULL, NULL); |
| 20 | 20 |
| 21 Mutex* mutex = new Mutex(); | 21 Mutex* mutex = new Mutex(); |
| 22 mutex->Lock(); | 22 mutex->Lock(); |
| 23 EXPECT_EQ(false, mutex->TryLock()); | 23 EXPECT_EQ(false, mutex->TryLock()); |
| 24 mutex->Unlock(); | 24 mutex->Unlock(); |
| 25 EXPECT_EQ(true, mutex->TryLock()); | 25 EXPECT_EQ(true, mutex->TryLock()); |
| 26 mutex->Unlock(); | 26 mutex->Unlock(); |
| 27 { | 27 { |
| 28 MutexLocker ml(mutex); | 28 MutexLocker ml(mutex); |
| 29 EXPECT_EQ(false, mutex->TryLock()); | 29 EXPECT_EQ(false, mutex->TryLock()); |
| 30 } | 30 } |
| 31 // The isolate shutdown and the destruction of the mutex are out-of-order on | 31 // The isolate shutdown and the destruction of the mutex are out-of-order on |
| 32 // purpose. | 32 // purpose. |
| 33 Dart_ShutdownIsolate(); | 33 Dart_ShutdownIsolate(); |
| 34 delete mutex; | 34 delete mutex; |
| 35 } | 35 } |
| 36 | 36 |
| 37 | 37 |
| 38 UNIT_TEST_CASE(Monitor) { | 38 VM_UNIT_TEST_CASE(Monitor) { |
| 39 // This unit test case needs a running isolate. | 39 // This unit test case needs a running isolate. |
| 40 Dart_CreateIsolate(NULL, NULL, bin::core_isolate_snapshot_data, | 40 Dart_CreateIsolate(NULL, NULL, bin::core_isolate_snapshot_data, |
| 41 bin::core_isolate_snapshot_instructions, NULL, NULL, NULL); | 41 bin::core_isolate_snapshot_instructions, NULL, NULL, NULL); |
| 42 OSThread* thread = OSThread::Current(); | 42 OSThread* thread = OSThread::Current(); |
| 43 // Thread interrupter interferes with this test, disable interrupts. | 43 // Thread interrupter interferes with this test, disable interrupts. |
| 44 thread->DisableThreadInterrupts(); | 44 thread->DisableThreadInterrupts(); |
| 45 Monitor* monitor = new Monitor(); | 45 Monitor* monitor = new Monitor(); |
| 46 monitor->Enter(); | 46 monitor->Enter(); |
| 47 monitor->Exit(); | 47 monitor->Exit(); |
| 48 EXPECT_EQ(true, monitor->TryEnter()); | 48 EXPECT_EQ(true, monitor->TryEnter()); |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 } | 166 } |
| 167 | 167 |
| 168 private: | 168 private: |
| 169 Isolate* isolate_; | 169 Isolate* isolate_; |
| 170 Monitor* monitor_; | 170 Monitor* monitor_; |
| 171 bool* done_; | 171 bool* done_; |
| 172 intptr_t id_; | 172 intptr_t id_; |
| 173 }; | 173 }; |
| 174 | 174 |
| 175 | 175 |
| 176 VM_TEST_CASE(ManyTasksWithZones) { | 176 ISOLATE_UNIT_TEST_CASE(ManyTasksWithZones) { |
| 177 const int kTaskCount = 100; | 177 const int kTaskCount = 100; |
| 178 Monitor sync[kTaskCount]; | 178 Monitor sync[kTaskCount]; |
| 179 bool done[kTaskCount]; | 179 bool done[kTaskCount]; |
| 180 Isolate* isolate = Thread::Current()->isolate(); | 180 Isolate* isolate = Thread::Current()->isolate(); |
| 181 EXPECT(isolate->heap()->GrowthControlState()); | 181 EXPECT(isolate->heap()->GrowthControlState()); |
| 182 isolate->heap()->DisableGrowthControl(); | 182 isolate->heap()->DisableGrowthControl(); |
| 183 for (int i = 0; i < kTaskCount; i++) { | 183 for (int i = 0; i < kTaskCount; i++) { |
| 184 done[i] = false; | 184 done[i] = false; |
| 185 Dart::thread_pool()->Run( | 185 Dart::thread_pool()->Run( |
| 186 new TaskWithZoneAllocation(isolate, &sync[i], &done[i], i)); | 186 new TaskWithZoneAllocation(isolate, &sync[i], &done[i], i)); |
| (...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 EXPECT_EQ(SafepointTestTask::kTaskCount, exited); | 547 EXPECT_EQ(SafepointTestTask::kTaskCount, exited); |
| 548 } | 548 } |
| 549 } | 549 } |
| 550 | 550 |
| 551 | 551 |
| 552 // Test rendezvous of: | 552 // Test rendezvous of: |
| 553 // - helpers in VM code, and | 553 // - helpers in VM code, and |
| 554 // - main thread in VM code, | 554 // - main thread in VM code, |
| 555 // organized by | 555 // organized by |
| 556 // - helpers. | 556 // - helpers. |
| 557 VM_TEST_CASE(SafepointTestVM) { | 557 ISOLATE_UNIT_TEST_CASE(SafepointTestVM) { |
| 558 Isolate* isolate = thread->isolate(); | 558 Isolate* isolate = thread->isolate(); |
| 559 Monitor monitor; | 559 Monitor monitor; |
| 560 intptr_t expected_count = 0; | 560 intptr_t expected_count = 0; |
| 561 intptr_t total_done = 0; | 561 intptr_t total_done = 0; |
| 562 intptr_t exited = 0; | 562 intptr_t exited = 0; |
| 563 for (int i = 0; i < SafepointTestTask::kTaskCount; i++) { | 563 for (int i = 0; i < SafepointTestTask::kTaskCount; i++) { |
| 564 Dart::thread_pool()->Run(new SafepointTestTask( | 564 Dart::thread_pool()->Run(new SafepointTestTask( |
| 565 isolate, &monitor, &expected_count, &total_done, &exited)); | 565 isolate, &monitor, &expected_count, &total_done, &exited)); |
| 566 } | 566 } |
| 567 String& label = String::Handle(String::New("foo")); | 567 String& label = String::Handle(String::New("foo")); |
| 568 UserTag& tag = UserTag::Handle(UserTag::New(label)); | 568 UserTag& tag = UserTag::Handle(UserTag::New(label)); |
| 569 isolate->set_current_tag(tag); | 569 isolate->set_current_tag(tag); |
| 570 MonitorLocker ml(&monitor); | 570 MonitorLocker ml(&monitor); |
| 571 while (exited != SafepointTestTask::kTaskCount) { | 571 while (exited != SafepointTestTask::kTaskCount) { |
| 572 ml.WaitWithSafepointCheck(thread); | 572 ml.WaitWithSafepointCheck(thread); |
| 573 } | 573 } |
| 574 } | 574 } |
| 575 | 575 |
| 576 | 576 |
| 577 // Test case for recursive safepoint operations. | 577 // Test case for recursive safepoint operations. |
| 578 VM_TEST_CASE(RecursiveSafepointTest1) { | 578 ISOLATE_UNIT_TEST_CASE(RecursiveSafepointTest1) { |
| 579 intptr_t count = 0; | 579 intptr_t count = 0; |
| 580 { | 580 { |
| 581 SafepointOperationScope safepoint_scope(thread); | 581 SafepointOperationScope safepoint_scope(thread); |
| 582 count += 1; | 582 count += 1; |
| 583 { | 583 { |
| 584 SafepointOperationScope safepoint_scope(thread); | 584 SafepointOperationScope safepoint_scope(thread); |
| 585 count += 1; | 585 count += 1; |
| 586 { | 586 { |
| 587 SafepointOperationScope safepoint_scope(thread); | 587 SafepointOperationScope safepoint_scope(thread); |
| 588 count += 1; | 588 count += 1; |
| 589 } | 589 } |
| 590 } | 590 } |
| 591 } | 591 } |
| 592 EXPECT(count == 3); | 592 EXPECT(count == 3); |
| 593 } | 593 } |
| 594 | 594 |
| 595 | 595 |
| 596 VM_TEST_CASE(ThreadIterator_Count) { | 596 ISOLATE_UNIT_TEST_CASE(ThreadIterator_Count) { |
| 597 intptr_t thread_count_0 = 0; | 597 intptr_t thread_count_0 = 0; |
| 598 intptr_t thread_count_1 = 0; | 598 intptr_t thread_count_1 = 0; |
| 599 | 599 |
| 600 { | 600 { |
| 601 OSThreadIterator ti; | 601 OSThreadIterator ti; |
| 602 while (ti.HasNext()) { | 602 while (ti.HasNext()) { |
| 603 OSThread* thread = ti.Next(); | 603 OSThread* thread = ti.Next(); |
| 604 EXPECT(thread != NULL); | 604 EXPECT(thread != NULL); |
| 605 thread_count_0++; | 605 thread_count_0++; |
| 606 } | 606 } |
| 607 } | 607 } |
| 608 | 608 |
| 609 { | 609 { |
| 610 OSThreadIterator ti; | 610 OSThreadIterator ti; |
| 611 while (ti.HasNext()) { | 611 while (ti.HasNext()) { |
| 612 OSThread* thread = ti.Next(); | 612 OSThread* thread = ti.Next(); |
| 613 EXPECT(thread != NULL); | 613 EXPECT(thread != NULL); |
| 614 thread_count_1++; | 614 thread_count_1++; |
| 615 } | 615 } |
| 616 } | 616 } |
| 617 | 617 |
| 618 EXPECT(thread_count_0 > 0); | 618 EXPECT(thread_count_0 > 0); |
| 619 EXPECT(thread_count_1 > 0); | 619 EXPECT(thread_count_1 > 0); |
| 620 EXPECT(thread_count_0 >= thread_count_1); | 620 EXPECT(thread_count_0 >= thread_count_1); |
| 621 } | 621 } |
| 622 | 622 |
| 623 | 623 |
| 624 VM_TEST_CASE(ThreadIterator_FindSelf) { | 624 ISOLATE_UNIT_TEST_CASE(ThreadIterator_FindSelf) { |
| 625 OSThread* current = OSThread::Current(); | 625 OSThread* current = OSThread::Current(); |
| 626 EXPECT(OSThread::IsThreadInList(current->id())); | 626 EXPECT(OSThread::IsThreadInList(current->id())); |
| 627 } | 627 } |
| 628 | 628 |
| 629 | 629 |
| 630 struct ThreadIteratorTestParams { | 630 struct ThreadIteratorTestParams { |
| 631 ThreadId spawned_thread_id; | 631 ThreadId spawned_thread_id; |
| 632 ThreadJoinId spawned_thread_join_id; | 632 ThreadJoinId spawned_thread_join_id; |
| 633 Monitor* monitor; | 633 Monitor* monitor; |
| 634 }; | 634 }; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 675 delete params.monitor; | 675 delete params.monitor; |
| 676 } | 676 } |
| 677 | 677 |
| 678 | 678 |
| 679 // Test rendezvous of: | 679 // Test rendezvous of: |
| 680 // - helpers in VM code, and | 680 // - helpers in VM code, and |
| 681 // - main thread in VM code, | 681 // - main thread in VM code, |
| 682 // organized by | 682 // organized by |
| 683 // - main thread, and | 683 // - main thread, and |
| 684 // - helpers. | 684 // - helpers. |
| 685 VM_TEST_CASE(SafepointTestVM2) { | 685 ISOLATE_UNIT_TEST_CASE(SafepointTestVM2) { |
| 686 Isolate* isolate = thread->isolate(); | 686 Isolate* isolate = thread->isolate(); |
| 687 Monitor monitor; | 687 Monitor monitor; |
| 688 intptr_t expected_count = 0; | 688 intptr_t expected_count = 0; |
| 689 intptr_t total_done = 0; | 689 intptr_t total_done = 0; |
| 690 intptr_t exited = 0; | 690 intptr_t exited = 0; |
| 691 for (int i = 0; i < SafepointTestTask::kTaskCount; i++) { | 691 for (int i = 0; i < SafepointTestTask::kTaskCount; i++) { |
| 692 Dart::thread_pool()->Run(new SafepointTestTask( | 692 Dart::thread_pool()->Run(new SafepointTestTask( |
| 693 isolate, &monitor, &expected_count, &total_done, &exited)); | 693 isolate, &monitor, &expected_count, &total_done, &exited)); |
| 694 } | 694 } |
| 695 bool all_helpers = false; | 695 bool all_helpers = false; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 707 isolate->set_current_tag(tag); | 707 isolate->set_current_tag(tag); |
| 708 MonitorLocker ml(&monitor); | 708 MonitorLocker ml(&monitor); |
| 709 while (exited != SafepointTestTask::kTaskCount) { | 709 while (exited != SafepointTestTask::kTaskCount) { |
| 710 ml.WaitWithSafepointCheck(thread); | 710 ml.WaitWithSafepointCheck(thread); |
| 711 } | 711 } |
| 712 } | 712 } |
| 713 | 713 |
| 714 | 714 |
| 715 // Test recursive safepoint operation scopes with other threads trying | 715 // Test recursive safepoint operation scopes with other threads trying |
| 716 // to also start a safepoint operation scope. | 716 // to also start a safepoint operation scope. |
| 717 VM_TEST_CASE(RecursiveSafepointTest2) { | 717 ISOLATE_UNIT_TEST_CASE(RecursiveSafepointTest2) { |
| 718 Isolate* isolate = thread->isolate(); | 718 Isolate* isolate = thread->isolate(); |
| 719 Monitor monitor; | 719 Monitor monitor; |
| 720 intptr_t expected_count = 0; | 720 intptr_t expected_count = 0; |
| 721 intptr_t total_done = 0; | 721 intptr_t total_done = 0; |
| 722 intptr_t exited = 0; | 722 intptr_t exited = 0; |
| 723 for (int i = 0; i < SafepointTestTask::kTaskCount; i++) { | 723 for (int i = 0; i < SafepointTestTask::kTaskCount; i++) { |
| 724 Dart::thread_pool()->Run(new SafepointTestTask( | 724 Dart::thread_pool()->Run(new SafepointTestTask( |
| 725 isolate, &monitor, &expected_count, &total_done, &exited)); | 725 isolate, &monitor, &expected_count, &total_done, &exited)); |
| 726 } | 726 } |
| 727 bool all_helpers = false; | 727 bool all_helpers = false; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 778 } | 778 } |
| 779 } | 779 } |
| 780 | 780 |
| 781 private: | 781 private: |
| 782 Isolate* isolate_; | 782 Isolate* isolate_; |
| 783 Monitor* done_monitor_; | 783 Monitor* done_monitor_; |
| 784 bool* done_; | 784 bool* done_; |
| 785 }; | 785 }; |
| 786 | 786 |
| 787 | 787 |
| 788 VM_TEST_CASE(HelperAllocAndGC) { | 788 ISOLATE_UNIT_TEST_CASE(HelperAllocAndGC) { |
| 789 Monitor done_monitor; | 789 Monitor done_monitor; |
| 790 bool done = false; | 790 bool done = false; |
| 791 Isolate* isolate = thread->isolate(); | 791 Isolate* isolate = thread->isolate(); |
| 792 Dart::thread_pool()->Run(new AllocAndGCTask(isolate, &done_monitor, &done)); | 792 Dart::thread_pool()->Run(new AllocAndGCTask(isolate, &done_monitor, &done)); |
| 793 { | 793 { |
| 794 while (true) { | 794 while (true) { |
| 795 TransitionVMToBlocked transition(thread); | 795 TransitionVMToBlocked transition(thread); |
| 796 MonitorLocker ml(&done_monitor); | 796 MonitorLocker ml(&done_monitor); |
| 797 if (done) { | 797 if (done) { |
| 798 break; | 798 break; |
| 799 } | 799 } |
| 800 } | 800 } |
| 801 } | 801 } |
| 802 } | 802 } |
| 803 | 803 |
| 804 } // namespace dart | 804 } // namespace dart |
| OLD | NEW |