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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 | 68 |
69 #include "platform-posix.h" | 69 #include "platform-posix.h" |
70 #include "platform.h" | 70 #include "platform.h" |
71 #include "v8threads.h" | 71 #include "v8threads.h" |
72 #include "vm-state-inl.h" | 72 #include "vm-state-inl.h" |
73 | 73 |
74 | 74 |
75 namespace v8 { | 75 namespace v8 { |
76 namespace internal { | 76 namespace internal { |
77 | 77 |
78 // 0 is never a valid thread id on Linux since tids and pids share a | |
79 // name space and pid 0 is reserved (see man 2 kill). | |
80 static const pthread_t kNoThread = (pthread_t) 0; | |
81 | |
82 | 78 |
83 double ceiling(double x) { | 79 double ceiling(double x) { |
84 return ceil(x); | 80 return ceil(x); |
85 } | 81 } |
86 | 82 |
87 | 83 |
88 static Mutex* limit_mutex = NULL; | 84 static Mutex* limit_mutex = NULL; |
89 | 85 |
90 | 86 |
91 uint64_t OS::CpuFeaturesImpliedByPlatform() { | 87 uint64_t OS::CpuFeaturesImpliedByPlatform() { |
(...skipping 612 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
704 bool VirtualMemory::ReleaseRegion(void* base, size_t size) { | 700 bool VirtualMemory::ReleaseRegion(void* base, size_t size) { |
705 return munmap(base, size) == 0; | 701 return munmap(base, size) == 0; |
706 } | 702 } |
707 | 703 |
708 | 704 |
709 bool VirtualMemory::HasLazyCommits() { | 705 bool VirtualMemory::HasLazyCommits() { |
710 return true; | 706 return true; |
711 } | 707 } |
712 | 708 |
713 | 709 |
714 class Thread::PlatformData : public Malloced { | |
715 public: | |
716 PlatformData() : thread_(kNoThread) {} | |
717 | |
718 pthread_t thread_; // Thread handle for pthread. | |
719 }; | |
720 | |
721 Thread::Thread(const Options& options) | |
722 : data_(new PlatformData()), | |
723 stack_size_(options.stack_size()), | |
724 start_semaphore_(NULL) { | |
725 set_name(options.name()); | |
726 } | |
727 | |
728 | |
729 Thread::~Thread() { | |
730 delete data_; | |
731 } | |
732 | |
733 | |
734 static void* ThreadEntry(void* arg) { | |
735 Thread* thread = reinterpret_cast<Thread*>(arg); | |
736 // This is also initialized by the first argument to pthread_create() but we | |
737 // don't know which thread will run first (the original thread or the new | |
738 // one) so we initialize it here too. | |
739 #ifdef PR_SET_NAME | |
740 prctl(PR_SET_NAME, | |
741 reinterpret_cast<unsigned long>(thread->name()), // NOLINT | |
742 0, 0, 0); | |
743 #endif | |
744 thread->data()->thread_ = pthread_self(); | |
745 ASSERT(thread->data()->thread_ != kNoThread); | |
746 thread->NotifyStartedAndRun(); | |
747 return NULL; | |
748 } | |
749 | |
750 | |
751 void Thread::set_name(const char* name) { | |
752 strncpy(name_, name, sizeof(name_)); | |
753 name_[sizeof(name_) - 1] = '\0'; | |
754 } | |
755 | |
756 | |
757 void Thread::Start() { | |
758 pthread_attr_t* attr_ptr = NULL; | |
759 #if defined(__native_client__) | |
760 // use default stack size. | |
761 #else | |
762 pthread_attr_t attr; | |
763 if (stack_size_ > 0) { | |
764 pthread_attr_init(&attr); | |
765 pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_)); | |
766 attr_ptr = &attr; | |
767 } | |
768 #endif | |
769 int result = pthread_create(&data_->thread_, attr_ptr, ThreadEntry, this); | |
770 CHECK_EQ(0, result); | |
771 ASSERT(data_->thread_ != kNoThread); | |
772 } | |
773 | |
774 | |
775 void Thread::Join() { | |
776 pthread_join(data_->thread_, NULL); | |
777 } | |
778 | |
779 | |
780 Thread::LocalStorageKey Thread::CreateThreadLocalKey() { | |
781 pthread_key_t key; | |
782 int result = pthread_key_create(&key, NULL); | |
783 USE(result); | |
784 ASSERT(result == 0); | |
785 return static_cast<LocalStorageKey>(key); | |
786 } | |
787 | |
788 | |
789 void Thread::DeleteThreadLocalKey(LocalStorageKey key) { | |
790 pthread_key_t pthread_key = static_cast<pthread_key_t>(key); | |
791 int result = pthread_key_delete(pthread_key); | |
792 USE(result); | |
793 ASSERT(result == 0); | |
794 } | |
795 | |
796 | |
797 void* Thread::GetThreadLocal(LocalStorageKey key) { | |
798 pthread_key_t pthread_key = static_cast<pthread_key_t>(key); | |
799 return pthread_getspecific(pthread_key); | |
800 } | |
801 | |
802 | |
803 void Thread::SetThreadLocal(LocalStorageKey key, void* value) { | |
804 pthread_key_t pthread_key = static_cast<pthread_key_t>(key); | |
805 pthread_setspecific(pthread_key, value); | |
806 } | |
807 | |
808 | |
809 class LinuxSemaphore : public Semaphore { | 710 class LinuxSemaphore : public Semaphore { |
810 public: | 711 public: |
811 explicit LinuxSemaphore(int count) { sem_init(&sem_, 0, count); } | 712 explicit LinuxSemaphore(int count) { sem_init(&sem_, 0, count); } |
812 virtual ~LinuxSemaphore() { sem_destroy(&sem_); } | 713 virtual ~LinuxSemaphore() { sem_destroy(&sem_); } |
813 | 714 |
814 virtual void Wait(); | 715 virtual void Wait(); |
815 virtual bool Wait(int timeout); | 716 virtual bool Wait(int timeout); |
816 virtual void Signal() { sem_post(&sem_); } | 717 virtual void Signal() { sem_post(&sem_); } |
817 private: | 718 private: |
818 sem_t sem_; | 719 sem_t sem_; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
883 limit_mutex = CreateMutex(); | 784 limit_mutex = CreateMutex(); |
884 } | 785 } |
885 | 786 |
886 | 787 |
887 void OS::TearDown() { | 788 void OS::TearDown() { |
888 delete limit_mutex; | 789 delete limit_mutex; |
889 } | 790 } |
890 | 791 |
891 | 792 |
892 } } // namespace v8::internal | 793 } } // namespace v8::internal |
OLD | NEW |