OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 10 matching lines...) Expand all Loading... |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // Platform specific code for MacOS goes here. For the POSIX comaptible parts | 28 // Platform specific code for MacOS goes here. For the POSIX comaptible parts |
29 // the implementation is in platform-posix.cc. | 29 // the implementation is in platform-posix.cc. |
30 | 30 |
| 31 #include <dlfcn.h> |
| 32 #include <string> |
31 #include <unistd.h> | 33 #include <unistd.h> |
32 #include <sys/mman.h> | 34 #include <sys/mman.h> |
33 #include <mach/mach_init.h> | 35 #include <mach/mach_init.h> |
34 #include <mach-o/dyld.h> | 36 #include <mach-o/dyld.h> |
35 #include <mach-o/getsect.h> | 37 #include <mach-o/getsect.h> |
36 | 38 |
37 #include <AvailabilityMacros.h> | 39 #include <AvailabilityMacros.h> |
38 | 40 |
39 #include <pthread.h> | 41 #include <pthread.h> |
40 #include <semaphore.h> | 42 #include <semaphore.h> |
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 return pthread_equal(data_->thread_, pthread_self()); | 406 return pthread_equal(data_->thread_, pthread_self()); |
405 } | 407 } |
406 | 408 |
407 | 409 |
408 bool ThreadHandle::IsValid() const { | 410 bool ThreadHandle::IsValid() const { |
409 return data_->thread_ != kNoThread; | 411 return data_->thread_ != kNoThread; |
410 } | 412 } |
411 | 413 |
412 | 414 |
413 Thread::Thread() : ThreadHandle(ThreadHandle::INVALID) { | 415 Thread::Thread() : ThreadHandle(ThreadHandle::INVALID) { |
| 416 set_name("v8:<unknown>"); |
| 417 } |
| 418 |
| 419 |
| 420 Thread::Thread(const char* name) : ThreadHandle(ThreadHandle::INVALID) { |
| 421 set_name(name); |
414 } | 422 } |
415 | 423 |
416 | 424 |
417 Thread::~Thread() { | 425 Thread::~Thread() { |
418 } | 426 } |
419 | 427 |
420 | 428 |
| 429 |
| 430 static void SetThreadName(const char* name) { |
| 431 // pthread_setname_np is only available in 10.6 or later, so test |
| 432 // for it at runtime. |
| 433 int (*dynamic_pthread_setname_np)(const char*); |
| 434 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = |
| 435 dlsym(RTLD_DEFAULT, "pthread_setname_np"); |
| 436 if (!dynamic_pthread_setname_np) |
| 437 return; |
| 438 |
| 439 // Mac OS X does not expose the length limit of the name, so hardcode it. |
| 440 const int kMaxNameLength = 63; |
| 441 std::string shortened_name = std::string(name).substr(0, kMaxNameLength); |
| 442 dynamic_pthread_setname_np(shortened_name.c_str()); |
| 443 } |
| 444 |
| 445 |
421 static void* ThreadEntry(void* arg) { | 446 static void* ThreadEntry(void* arg) { |
422 Thread* thread = reinterpret_cast<Thread*>(arg); | 447 Thread* thread = reinterpret_cast<Thread*>(arg); |
423 // This is also initialized by the first argument to pthread_create() but we | 448 // This is also initialized by the first argument to pthread_create() but we |
424 // don't know which thread will run first (the original thread or the new | 449 // don't know which thread will run first (the original thread or the new |
425 // one) so we initialize it here too. | 450 // one) so we initialize it here too. |
426 thread->thread_handle_data()->thread_ = pthread_self(); | 451 thread->thread_handle_data()->thread_ = pthread_self(); |
| 452 SetThreadName(thread->name()); |
427 ASSERT(thread->IsValid()); | 453 ASSERT(thread->IsValid()); |
428 thread->Run(); | 454 thread->Run(); |
429 return NULL; | 455 return NULL; |
430 } | 456 } |
431 | 457 |
432 | 458 |
| 459 void Thread::set_name(const char* name) { |
| 460 strncpy(name_, name, sizeof(name_)); |
| 461 name_[sizeof(name_) - 1] = '\0'; |
| 462 } |
| 463 |
| 464 |
433 void Thread::Start() { | 465 void Thread::Start() { |
434 pthread_create(&thread_handle_data()->thread_, NULL, ThreadEntry, this); | 466 pthread_create(&thread_handle_data()->thread_, NULL, ThreadEntry, this); |
435 } | 467 } |
436 | 468 |
437 | 469 |
438 void Thread::Join() { | 470 void Thread::Join() { |
439 pthread_join(thread_handle_data()->thread_, NULL); | 471 pthread_join(thread_handle_data()->thread_, NULL); |
440 } | 472 } |
441 | 473 |
442 | 474 |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
678 Top::WakeUpRuntimeProfilerThreadBeforeShutdown(); | 710 Top::WakeUpRuntimeProfilerThreadBeforeShutdown(); |
679 pthread_join(data_->sampler_thread_, NULL); | 711 pthread_join(data_->sampler_thread_, NULL); |
680 | 712 |
681 // Deallocate Mach port for thread. | 713 // Deallocate Mach port for thread. |
682 mach_port_deallocate(data_->task_self_, data_->profiled_thread_); | 714 mach_port_deallocate(data_->task_self_, data_->profiled_thread_); |
683 } | 715 } |
684 | 716 |
685 #endif // ENABLE_LOGGING_AND_PROFILING | 717 #endif // ENABLE_LOGGING_AND_PROFILING |
686 | 718 |
687 } } // namespace v8::internal | 719 } } // namespace v8::internal |
OLD | NEW |