| 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" |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 thread_count_1++; | 428 thread_count_1++; |
| 429 } | 429 } |
| 430 } | 430 } |
| 431 | 431 |
| 432 EXPECT(thread_count_0 > 0); | 432 EXPECT(thread_count_0 > 0); |
| 433 EXPECT(thread_count_1 > 0); | 433 EXPECT(thread_count_1 > 0); |
| 434 EXPECT(thread_count_0 >= thread_count_1); | 434 EXPECT(thread_count_0 >= thread_count_1); |
| 435 } | 435 } |
| 436 | 436 |
| 437 | 437 |
| 438 static bool ThreadInList(Thread* thread) { |
| 439 ThreadIterator it; |
| 440 while (it.HasNext()) { |
| 441 Thread* t = it.Next(); |
| 442 if (t == thread) { |
| 443 return true; |
| 444 } |
| 445 } |
| 446 return false; |
| 447 } |
| 448 |
| 449 |
| 438 TEST_CASE(ThreadIterator_FindSelf) { | 450 TEST_CASE(ThreadIterator_FindSelf) { |
| 439 Thread* current = Thread::Current(); | 451 Thread* current = Thread::Current(); |
| 440 | 452 EXPECT(ThreadInList(current)); |
| 441 bool found_self = false; | |
| 442 | |
| 443 { | |
| 444 ThreadIterator ti; | |
| 445 while (ti.HasNext()) { | |
| 446 Thread* thread = ti.Next(); | |
| 447 EXPECT(thread != NULL); | |
| 448 if (thread == current) { | |
| 449 found_self = true; | |
| 450 break; | |
| 451 } | |
| 452 } | |
| 453 } | |
| 454 | |
| 455 EXPECT(found_self); | |
| 456 } | 453 } |
| 457 | 454 |
| 458 | 455 |
| 459 struct ThreadIteratorTestParams { | 456 struct ThreadIteratorTestParams { |
| 460 Isolate* isolate; | 457 Isolate* isolate; |
| 461 Thread* spawned_thread; | 458 Thread* spawned_thread; |
| 462 ThreadJoinId spawned_thread_join_id; | 459 ThreadJoinId spawned_thread_join_id; |
| 463 Monitor* monitor; | 460 Monitor* monitor; |
| 464 }; | 461 }; |
| 465 | 462 |
| 466 | 463 |
| 467 void ThreadIteratorTestMain(uword parameter) { | 464 void ThreadIteratorTestMain(uword parameter) { |
| 468 Thread::EnsureInit(); | 465 Thread::EnsureInit(); |
| 469 ThreadIteratorTestParams* params = | 466 ThreadIteratorTestParams* params = |
| 470 reinterpret_cast<ThreadIteratorTestParams*>(parameter); | 467 reinterpret_cast<ThreadIteratorTestParams*>(parameter); |
| 471 Isolate* isolate = params->isolate; | 468 Isolate* isolate = params->isolate; |
| 472 ASSERT(isolate != NULL); | 469 EXPECT(isolate != NULL); |
| 470 Thread* thread = Thread::Current(); |
| 471 EXPECT(thread != NULL); |
| 473 | 472 |
| 474 Thread::EnterIsolateAsHelper(isolate); | 473 MonitorLocker ml(params->monitor); |
| 475 Thread* thread = Thread::Current(); | |
| 476 ASSERT(thread != NULL); | |
| 477 | |
| 478 params->spawned_thread = thread; | 474 params->spawned_thread = thread; |
| 479 params->spawned_thread_join_id = OSThread::GetCurrentThreadJoinId(); | 475 params->spawned_thread_join_id = OSThread::GetCurrentThreadJoinId(); |
| 480 | 476 EXPECT(params->spawned_thread_join_id != OSThread::kInvalidThreadJoinId); |
| 481 { | 477 EXPECT(ThreadInList(thread)); |
| 482 MonitorLocker ml(params->monitor); | 478 ml.Notify(); |
| 483 ml.Notify(); | |
| 484 } | |
| 485 | |
| 486 { | |
| 487 bool found_self = false; | |
| 488 ThreadIterator it; | |
| 489 while (it.HasNext()) { | |
| 490 Thread* t = it.Next(); | |
| 491 if (t == thread) { | |
| 492 found_self = true; | |
| 493 break; | |
| 494 } | |
| 495 } | |
| 496 EXPECT(found_self); | |
| 497 } | |
| 498 | |
| 499 Thread::ExitIsolateAsHelper(); | |
| 500 } | 479 } |
| 501 | 480 |
| 502 | 481 |
| 503 TEST_CASE(ThreadIterator_AddFindRemove) { | 482 TEST_CASE(ThreadIterator_AddFindRemove) { |
| 504 Isolate* isolate = thread->isolate(); | 483 Isolate* isolate = thread->isolate(); |
| 505 ThreadIteratorTestParams params; | 484 ThreadIteratorTestParams params; |
| 506 params.isolate = isolate; | 485 params.isolate = isolate; |
| 507 params.spawned_thread = NULL; | 486 params.spawned_thread = NULL; |
| 508 params.spawned_thread_join_id = OSThread::kInvalidThreadJoinId; | 487 params.spawned_thread_join_id = OSThread::kInvalidThreadJoinId; |
| 509 params.monitor = new Monitor(); | 488 params.monitor = new Monitor(); |
| 510 | 489 |
| 511 { | 490 { |
| 512 MonitorLocker ml(params.monitor); | 491 MonitorLocker ml(params.monitor); |
| 513 EXPECT(params.spawned_thread_join_id == OSThread::kInvalidThreadJoinId); | 492 EXPECT(params.spawned_thread_join_id == OSThread::kInvalidThreadJoinId); |
| 514 EXPECT(params.spawned_thread == NULL); | 493 EXPECT(params.spawned_thread == NULL); |
| 515 // Spawn thread and wait to receive the thread join id. | 494 // Spawn thread and wait to receive the thread join id. |
| 516 OSThread::Start(ThreadIteratorTestMain, reinterpret_cast<uword>(¶ms)); | 495 OSThread::Start(ThreadIteratorTestMain, reinterpret_cast<uword>(¶ms)); |
| 517 while (params.spawned_thread_join_id == OSThread::kInvalidThreadJoinId) { | 496 while (params.spawned_thread_join_id == OSThread::kInvalidThreadJoinId) { |
| 518 ml.Wait(); | 497 ml.Wait(); |
| 519 } | 498 } |
| 520 EXPECT(params.spawned_thread_join_id != OSThread::kInvalidThreadJoinId); | 499 EXPECT(params.spawned_thread_join_id != OSThread::kInvalidThreadJoinId); |
| 521 EXPECT(params.spawned_thread != NULL); | 500 EXPECT(params.spawned_thread != NULL); |
| 522 // Join thread. | 501 // Join thread. |
| 523 OSThread::Join(params.spawned_thread_join_id); | 502 OSThread::Join(params.spawned_thread_join_id); |
| 524 } | 503 } |
| 525 | 504 |
| 526 ThreadIterator it; | 505 for (intptr_t i = 0; i < 10; i++) { |
| 527 bool found_spawned_thread = false; | 506 // Sleep for 10 milliseconds. |
| 528 while (it.HasNext()) { | 507 OS::Sleep(10); |
| 529 Thread* t = it.Next(); | 508 if (!ThreadInList(params.spawned_thread)) { |
| 530 if (t == params.spawned_thread) { | |
| 531 found_spawned_thread = true; | |
| 532 break; | 509 break; |
| 533 } | 510 } |
| 534 } | 511 } |
| 535 | 512 |
| 536 EXPECT(!found_spawned_thread); | 513 EXPECT(!ThreadInList(params.spawned_thread)) |
| 514 |
| 515 delete params.monitor; |
| 537 } | 516 } |
| 538 | 517 |
| 539 | 518 |
| 540 // Test rendezvous of: | 519 // Test rendezvous of: |
| 541 // - helpers in VM code, and | 520 // - helpers in VM code, and |
| 542 // - main thread in VM code, | 521 // - main thread in VM code, |
| 543 // organized by | 522 // organized by |
| 544 // - main thread, and | 523 // - main thread, and |
| 545 // - helpers. | 524 // - helpers. |
| 546 TEST_CASE(SafepointTestVM2) { | 525 TEST_CASE(SafepointTestVM2) { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 630 isolate->thread_registry()->CheckSafepoint(); | 609 isolate->thread_registry()->CheckSafepoint(); |
| 631 MonitorLocker ml(&done_monitor); | 610 MonitorLocker ml(&done_monitor); |
| 632 if (done) { | 611 if (done) { |
| 633 break; | 612 break; |
| 634 } | 613 } |
| 635 } | 614 } |
| 636 } | 615 } |
| 637 } | 616 } |
| 638 | 617 |
| 639 } // namespace dart | 618 } // namespace dart |
| OLD | NEW |