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 565 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
576 } | 576 } |
577 set_name(options.name()); | 577 set_name(options.name()); |
578 } | 578 } |
579 | 579 |
580 | 580 |
581 Thread::~Thread() { | 581 Thread::~Thread() { |
582 delete data_; | 582 delete data_; |
583 } | 583 } |
584 | 584 |
585 | 585 |
| 586 // Synchronizes thread creation |
| 587 static Mutex thread_creation_mutex_; |
| 588 |
| 589 |
586 static void SetThreadName(const char* name) { | 590 static void SetThreadName(const char* name) { |
587 #if V8_OS_DRAGONFLYBSD || V8_OS_FREEBSD || V8_OS_OPENBSD | 591 #if V8_OS_DRAGONFLYBSD || V8_OS_FREEBSD || V8_OS_OPENBSD |
588 pthread_set_name_np(pthread_self(), name); | 592 pthread_set_name_np(pthread_self(), name); |
589 #elif V8_OS_NETBSD | 593 #elif V8_OS_NETBSD |
590 STATIC_ASSERT(Thread::kMaxThreadNameLength <= PTHREAD_MAX_NAMELEN_NP); | 594 STATIC_ASSERT(Thread::kMaxThreadNameLength <= PTHREAD_MAX_NAMELEN_NP); |
591 pthread_setname_np(pthread_self(), "%s", name); | 595 pthread_setname_np(pthread_self(), "%s", name); |
592 #elif V8_OS_MACOSX | 596 #elif V8_OS_MACOSX |
593 // pthread_setname_np is only available in 10.6 or later, so test | 597 // pthread_setname_np is only available in 10.6 or later, so test |
594 // for it at runtime. | 598 // for it at runtime. |
595 int (*dynamic_pthread_setname_np)(const char*); | 599 int (*dynamic_pthread_setname_np)(const char*); |
596 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = | 600 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = |
597 dlsym(RTLD_DEFAULT, "pthread_setname_np"); | 601 dlsym(RTLD_DEFAULT, "pthread_setname_np"); |
598 if (dynamic_pthread_setname_np == NULL) | 602 if (dynamic_pthread_setname_np == NULL) |
599 return; | 603 return; |
600 | 604 |
601 // Mac OS X does not expose the length limit of the name, so hardcode it. | 605 // Mac OS X does not expose the length limit of the name, so hardcode it. |
602 static const int kMaxNameLength = 63; | 606 static const int kMaxNameLength = 63; |
603 STATIC_ASSERT(Thread::kMaxThreadNameLength <= kMaxNameLength); | 607 STATIC_ASSERT(Thread::kMaxThreadNameLength <= kMaxNameLength); |
604 dynamic_pthread_setname_np(name); | 608 dynamic_pthread_setname_np(name); |
605 #elif defined(PR_SET_NAME) | 609 #elif defined(PR_SET_NAME) |
606 prctl(PR_SET_NAME, | 610 prctl(PR_SET_NAME, |
607 reinterpret_cast<unsigned long>(name), // NOLINT | 611 reinterpret_cast<unsigned long>(name), // NOLINT |
608 0, 0, 0); | 612 0, 0, 0); |
609 #endif | 613 #endif |
610 } | 614 } |
611 | 615 |
612 | 616 |
613 static void* ThreadEntry(void* arg) { | 617 static void* ThreadEntry(void* arg) { |
614 Thread* thread = reinterpret_cast<Thread*>(arg); | 618 Thread* thread = reinterpret_cast<Thread*>(arg); |
615 // This is also initialized by the first argument to pthread_create() but we | 619 // We take the lock here to make sure that pthread_create finished first since |
616 // don't know which thread will run first (the original thread or the new | 620 // we don't know which thread will run first (the original thread or the new |
617 // one) so we initialize it here too. | 621 // one). |
618 thread->data()->thread_ = pthread_self(); | 622 { LockGuard<Mutex> lock_guard(&thread_creation_mutex_); } |
619 SetThreadName(thread->name()); | 623 SetThreadName(thread->name()); |
620 ASSERT(thread->data()->thread_ != kNoThread); | 624 ASSERT(thread->data()->thread_ != kNoThread); |
621 thread->NotifyStartedAndRun(); | 625 thread->NotifyStartedAndRun(); |
622 return NULL; | 626 return NULL; |
623 } | 627 } |
624 | 628 |
625 | 629 |
626 void Thread::set_name(const char* name) { | 630 void Thread::set_name(const char* name) { |
627 strncpy(name_, name, sizeof(name_)); | 631 strncpy(name_, name, sizeof(name_)); |
628 name_[sizeof(name_) - 1] = '\0'; | 632 name_[sizeof(name_) - 1] = '\0'; |
629 } | 633 } |
630 | 634 |
631 | 635 |
632 void Thread::Start() { | 636 void Thread::Start() { |
633 int result; | 637 int result; |
634 pthread_attr_t attr; | 638 pthread_attr_t attr; |
635 memset(&attr, 0, sizeof(attr)); | 639 memset(&attr, 0, sizeof(attr)); |
636 result = pthread_attr_init(&attr); | 640 result = pthread_attr_init(&attr); |
637 ASSERT_EQ(0, result); | 641 ASSERT_EQ(0, result); |
638 // Native client uses default stack size. | 642 // Native client uses default stack size. |
639 #if !V8_OS_NACL | 643 #if !V8_OS_NACL |
640 if (stack_size_ > 0) { | 644 if (stack_size_ > 0) { |
641 result = pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_)); | 645 result = pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_)); |
642 ASSERT_EQ(0, result); | 646 ASSERT_EQ(0, result); |
643 } | 647 } |
644 #endif | 648 #endif |
645 result = pthread_create(&data_->thread_, &attr, ThreadEntry, this); | 649 { |
| 650 LockGuard<Mutex> lock_guard(&thread_creation_mutex_); |
| 651 result = pthread_create(&data_->thread_, &attr, ThreadEntry, this); |
| 652 } |
646 ASSERT_EQ(0, result); | 653 ASSERT_EQ(0, result); |
647 result = pthread_attr_destroy(&attr); | 654 result = pthread_attr_destroy(&attr); |
648 ASSERT_EQ(0, result); | 655 ASSERT_EQ(0, result); |
649 ASSERT(data_->thread_ != kNoThread); | 656 ASSERT(data_->thread_ != kNoThread); |
650 USE(result); | 657 USE(result); |
651 } | 658 } |
652 | 659 |
653 | 660 |
654 void Thread::Join() { | 661 void Thread::Join() { |
655 pthread_join(data_->thread_, NULL); | 662 pthread_join(data_->thread_, NULL); |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
781 | 788 |
782 void Thread::SetThreadLocal(LocalStorageKey key, void* value) { | 789 void Thread::SetThreadLocal(LocalStorageKey key, void* value) { |
783 pthread_key_t pthread_key = LocalKeyToPthreadKey(key); | 790 pthread_key_t pthread_key = LocalKeyToPthreadKey(key); |
784 int result = pthread_setspecific(pthread_key, value); | 791 int result = pthread_setspecific(pthread_key, value); |
785 ASSERT_EQ(0, result); | 792 ASSERT_EQ(0, result); |
786 USE(result); | 793 USE(result); |
787 } | 794 } |
788 | 795 |
789 | 796 |
790 } } // namespace v8::internal | 797 } } // namespace v8::internal |
OLD | NEW |