| 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 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 while (true) { | 400 while (true) { |
| 401 isolate->thread_registry()->CheckSafepoint(); | 401 isolate->thread_registry()->CheckSafepoint(); |
| 402 MutexLocker ml(&mutex); | 402 MutexLocker ml(&mutex); |
| 403 if (exited == SafepointTestTask::kTaskCount) { | 403 if (exited == SafepointTestTask::kTaskCount) { |
| 404 break; | 404 break; |
| 405 } | 405 } |
| 406 } | 406 } |
| 407 } | 407 } |
| 408 | 408 |
| 409 | 409 |
| 410 TEST_CASE(ThreadIterator_Count) { |
| 411 intptr_t thread_count_0 = 0; |
| 412 intptr_t thread_count_1 = 0; |
| 413 |
| 414 { |
| 415 ThreadIterator ti; |
| 416 while (ti.HasNext()) { |
| 417 Thread* thread = ti.Next(); |
| 418 EXPECT(thread != NULL); |
| 419 thread_count_0++; |
| 420 } |
| 421 } |
| 422 |
| 423 { |
| 424 ThreadIterator ti; |
| 425 while (ti.HasNext()) { |
| 426 Thread* thread = ti.Next(); |
| 427 EXPECT(thread != NULL); |
| 428 thread_count_1++; |
| 429 } |
| 430 } |
| 431 |
| 432 EXPECT(thread_count_0 > 0); |
| 433 EXPECT(thread_count_1 > 0); |
| 434 EXPECT(thread_count_0 >= thread_count_1); |
| 435 } |
| 436 |
| 437 |
| 438 TEST_CASE(ThreadIterator_FindSelf) { |
| 439 Thread* current = Thread::Current(); |
| 440 |
| 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 } |
| 457 |
| 458 |
| 459 class ThreadIteratorTestTask : public ThreadPool::Task { |
| 460 public: |
| 461 ThreadIteratorTestTask(Isolate* isolate, |
| 462 Monitor* monitor, |
| 463 Thread** task_thread) |
| 464 : isolate_(isolate), |
| 465 monitor_(monitor), |
| 466 task_thread_(task_thread) {} |
| 467 |
| 468 virtual void Run() { |
| 469 Thread* thread = Thread::Current(); |
| 470 Thread::EnterIsolateAsHelper(isolate_); |
| 471 MonitorLocker ml(monitor_); |
| 472 |
| 473 { |
| 474 bool found_self = false; |
| 475 ThreadIterator it; |
| 476 while (it.HasNext()) { |
| 477 Thread* t = it.Next(); |
| 478 if (t == thread) { |
| 479 found_self = true; |
| 480 break; |
| 481 } |
| 482 } |
| 483 EXPECT(found_self); |
| 484 } |
| 485 |
| 486 Thread::ExitIsolateAsHelper(); |
| 487 *task_thread_ = thread; |
| 488 ml.Notify(); |
| 489 } |
| 490 |
| 491 private: |
| 492 Isolate* isolate_; |
| 493 Monitor* monitor_; |
| 494 Thread** task_thread_; |
| 495 }; |
| 496 |
| 497 |
| 498 TEST_CASE(ThreadIterator_AddFindRemove) { |
| 499 Thread* task_thread = NULL; |
| 500 Isolate* isolate = thread->isolate(); |
| 501 Monitor* monitor = new Monitor(); |
| 502 |
| 503 ThreadPool* thread_pool = new ThreadPool(); |
| 504 |
| 505 { |
| 506 MonitorLocker ml(monitor); |
| 507 EXPECT(task_thread == NULL); |
| 508 thread_pool->Run(new ThreadIteratorTestTask(isolate, |
| 509 monitor, |
| 510 &task_thread)); |
| 511 // Wait to be notified that the task is complete and we have a value in |
| 512 // task_thread. |
| 513 ml.Wait(); |
| 514 EXPECT(task_thread != NULL); |
| 515 } |
| 516 |
| 517 // Shutdown thread pool so we know that the task thread has completed. |
| 518 delete thread_pool; |
| 519 |
| 520 ThreadIterator it; |
| 521 bool found_task_thread = false; |
| 522 while (it.HasNext()) { |
| 523 Thread* t = it.Next(); |
| 524 if (t == task_thread) { |
| 525 found_task_thread = true; |
| 526 break; |
| 527 } |
| 528 } |
| 529 |
| 530 EXPECT(!found_task_thread); |
| 531 } |
| 532 |
| 533 |
| 410 // Test rendezvous of: | 534 // Test rendezvous of: |
| 411 // - helpers in VM code, and | 535 // - helpers in VM code, and |
| 412 // - main thread in VM code, | 536 // - main thread in VM code, |
| 413 // organized by | 537 // organized by |
| 414 // - main thread, and | 538 // - main thread, and |
| 415 // - helpers. | 539 // - helpers. |
| 416 TEST_CASE(SafepointTestVM2) { | 540 TEST_CASE(SafepointTestVM2) { |
| 417 Isolate* isolate = thread->isolate(); | 541 Isolate* isolate = thread->isolate(); |
| 418 Mutex mutex; | 542 Mutex mutex; |
| 419 intptr_t expected_count = 0; | 543 intptr_t expected_count = 0; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 500 isolate->thread_registry()->CheckSafepoint(); | 624 isolate->thread_registry()->CheckSafepoint(); |
| 501 MonitorLocker ml(&done_monitor); | 625 MonitorLocker ml(&done_monitor); |
| 502 if (done) { | 626 if (done) { |
| 503 break; | 627 break; |
| 504 } | 628 } |
| 505 } | 629 } |
| 506 } | 630 } |
| 507 } | 631 } |
| 508 | 632 |
| 509 } // namespace dart | 633 } // namespace dart |
| OLD | NEW |