OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 547 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 | 558 |
559 | 559 |
560 // ---------------------------------------------------------------------------- | 560 // ---------------------------------------------------------------------------- |
561 // POSIX thread support. | 561 // POSIX thread support. |
562 // | 562 // |
563 | 563 |
564 class Thread::PlatformData : public Malloced { | 564 class Thread::PlatformData : public Malloced { |
565 public: | 565 public: |
566 PlatformData() : thread_(kNoThread) {} | 566 PlatformData() : thread_(kNoThread) {} |
567 pthread_t thread_; // Thread handle for pthread. | 567 pthread_t thread_; // Thread handle for pthread. |
| 568 // Synchronizes thread creation |
| 569 Mutex thread_creation_mutex_; |
568 }; | 570 }; |
569 | 571 |
570 Thread::Thread(const Options& options) | 572 Thread::Thread(const Options& options) |
571 : data_(new PlatformData), | 573 : data_(new PlatformData), |
572 stack_size_(options.stack_size()), | 574 stack_size_(options.stack_size()), |
573 start_semaphore_(NULL) { | 575 start_semaphore_(NULL) { |
574 if (stack_size_ > 0 && stack_size_ < PTHREAD_STACK_MIN) { | 576 if (stack_size_ > 0 && stack_size_ < PTHREAD_STACK_MIN) { |
575 stack_size_ = PTHREAD_STACK_MIN; | 577 stack_size_ = PTHREAD_STACK_MIN; |
576 } | 578 } |
577 set_name(options.name()); | 579 set_name(options.name()); |
578 } | 580 } |
579 | 581 |
580 | 582 |
581 Thread::~Thread() { | 583 Thread::~Thread() { |
582 delete data_; | 584 delete data_; |
583 } | 585 } |
584 | 586 |
585 | 587 |
586 // Synchronizes thread creation | |
587 static Mutex thread_creation_mutex_; | |
588 | |
589 | |
590 static void SetThreadName(const char* name) { | 588 static void SetThreadName(const char* name) { |
591 #if V8_OS_DRAGONFLYBSD || V8_OS_FREEBSD || V8_OS_OPENBSD | 589 #if V8_OS_DRAGONFLYBSD || V8_OS_FREEBSD || V8_OS_OPENBSD |
592 pthread_set_name_np(pthread_self(), name); | 590 pthread_set_name_np(pthread_self(), name); |
593 #elif V8_OS_NETBSD | 591 #elif V8_OS_NETBSD |
594 STATIC_ASSERT(Thread::kMaxThreadNameLength <= PTHREAD_MAX_NAMELEN_NP); | 592 STATIC_ASSERT(Thread::kMaxThreadNameLength <= PTHREAD_MAX_NAMELEN_NP); |
595 pthread_setname_np(pthread_self(), "%s", name); | 593 pthread_setname_np(pthread_self(), "%s", name); |
596 #elif V8_OS_MACOSX | 594 #elif V8_OS_MACOSX |
597 // pthread_setname_np is only available in 10.6 or later, so test | 595 // pthread_setname_np is only available in 10.6 or later, so test |
598 // for it at runtime. | 596 // for it at runtime. |
599 int (*dynamic_pthread_setname_np)(const char*); | 597 int (*dynamic_pthread_setname_np)(const char*); |
(...skipping 12 matching lines...) Expand all Loading... |
612 0, 0, 0); | 610 0, 0, 0); |
613 #endif | 611 #endif |
614 } | 612 } |
615 | 613 |
616 | 614 |
617 static void* ThreadEntry(void* arg) { | 615 static void* ThreadEntry(void* arg) { |
618 Thread* thread = reinterpret_cast<Thread*>(arg); | 616 Thread* thread = reinterpret_cast<Thread*>(arg); |
619 // We take the lock here to make sure that pthread_create finished first since | 617 // We take the lock here to make sure that pthread_create finished first since |
620 // we don't know which thread will run first (the original thread or the new | 618 // we don't know which thread will run first (the original thread or the new |
621 // one). | 619 // one). |
622 { LockGuard<Mutex> lock_guard(&thread_creation_mutex_); } | 620 { LockGuard<Mutex> lock_guard(&thread->data()->thread_creation_mutex_); } |
623 SetThreadName(thread->name()); | 621 SetThreadName(thread->name()); |
624 ASSERT(thread->data()->thread_ != kNoThread); | 622 ASSERT(thread->data()->thread_ != kNoThread); |
625 thread->NotifyStartedAndRun(); | 623 thread->NotifyStartedAndRun(); |
626 return NULL; | 624 return NULL; |
627 } | 625 } |
628 | 626 |
629 | 627 |
630 void Thread::set_name(const char* name) { | 628 void Thread::set_name(const char* name) { |
631 strncpy(name_, name, sizeof(name_)); | 629 strncpy(name_, name, sizeof(name_)); |
632 name_[sizeof(name_) - 1] = '\0'; | 630 name_[sizeof(name_) - 1] = '\0'; |
633 } | 631 } |
634 | 632 |
635 | 633 |
636 void Thread::Start() { | 634 void Thread::Start() { |
637 int result; | 635 int result; |
638 pthread_attr_t attr; | 636 pthread_attr_t attr; |
639 memset(&attr, 0, sizeof(attr)); | 637 memset(&attr, 0, sizeof(attr)); |
640 result = pthread_attr_init(&attr); | 638 result = pthread_attr_init(&attr); |
641 ASSERT_EQ(0, result); | 639 ASSERT_EQ(0, result); |
642 // Native client uses default stack size. | 640 // Native client uses default stack size. |
643 #if !V8_OS_NACL | 641 #if !V8_OS_NACL |
644 if (stack_size_ > 0) { | 642 if (stack_size_ > 0) { |
645 result = pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_)); | 643 result = pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_)); |
646 ASSERT_EQ(0, result); | 644 ASSERT_EQ(0, result); |
647 } | 645 } |
648 #endif | 646 #endif |
649 { | 647 { |
650 LockGuard<Mutex> lock_guard(&thread_creation_mutex_); | 648 LockGuard<Mutex> lock_guard(&data_->thread_creation_mutex_); |
651 result = pthread_create(&data_->thread_, &attr, ThreadEntry, this); | 649 result = pthread_create(&data_->thread_, &attr, ThreadEntry, this); |
652 } | 650 } |
653 ASSERT_EQ(0, result); | 651 ASSERT_EQ(0, result); |
654 result = pthread_attr_destroy(&attr); | 652 result = pthread_attr_destroy(&attr); |
655 ASSERT_EQ(0, result); | 653 ASSERT_EQ(0, result); |
656 ASSERT(data_->thread_ != kNoThread); | 654 ASSERT(data_->thread_ != kNoThread); |
657 USE(result); | 655 USE(result); |
658 } | 656 } |
659 | 657 |
660 | 658 |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
788 | 786 |
789 void Thread::SetThreadLocal(LocalStorageKey key, void* value) { | 787 void Thread::SetThreadLocal(LocalStorageKey key, void* value) { |
790 pthread_key_t pthread_key = LocalKeyToPthreadKey(key); | 788 pthread_key_t pthread_key = LocalKeyToPthreadKey(key); |
791 int result = pthread_setspecific(pthread_key, value); | 789 int result = pthread_setspecific(pthread_key, value); |
792 ASSERT_EQ(0, result); | 790 ASSERT_EQ(0, result); |
793 USE(result); | 791 USE(result); |
794 } | 792 } |
795 | 793 |
796 | 794 |
797 } } // namespace v8::internal | 795 } } // namespace v8::internal |
OLD | NEW |