Index: src/platform-macos.cc |
=================================================================== |
--- src/platform-macos.cc (revision 6139) |
+++ src/platform-macos.cc (working copy) |
@@ -28,6 +28,8 @@ |
// Platform specific code for MacOS goes here. For the POSIX comaptible parts |
// the implementation is in platform-posix.cc. |
+#include <dlfcn.h> |
+#include <string> |
#include <unistd.h> |
#include <sys/mman.h> |
#include <mach/mach_init.h> |
@@ -411,25 +413,55 @@ |
Thread::Thread() : ThreadHandle(ThreadHandle::INVALID) { |
+ set_name("v8:<unknown>"); |
} |
+Thread::Thread(const char* name) : ThreadHandle(ThreadHandle::INVALID) { |
+ set_name(name); |
+} |
+ |
+ |
Thread::~Thread() { |
} |
+ |
+static void SetThreadName(const char* name) { |
+ // pthread_setname_np is only available in 10.6 or later, so test |
+ // for it at runtime. |
+ int (*dynamic_pthread_setname_np)(const char*); |
+ *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = |
+ dlsym(RTLD_DEFAULT, "pthread_setname_np"); |
+ if (!dynamic_pthread_setname_np) |
+ return; |
+ |
+ // Mac OS X does not expose the length limit of the name, so hardcode it. |
+ const int kMaxNameLength = 63; |
+ std::string shortened_name = std::string(name).substr(0, kMaxNameLength); |
+ dynamic_pthread_setname_np(shortened_name.c_str()); |
+} |
+ |
+ |
static void* ThreadEntry(void* arg) { |
Thread* thread = reinterpret_cast<Thread*>(arg); |
// This is also initialized by the first argument to pthread_create() but we |
// don't know which thread will run first (the original thread or the new |
// one) so we initialize it here too. |
thread->thread_handle_data()->thread_ = pthread_self(); |
+ SetThreadName(thread->name()); |
ASSERT(thread->IsValid()); |
thread->Run(); |
return NULL; |
} |
+void Thread::set_name(const char* name) { |
+ strncpy(name_, name, sizeof(name_)); |
+ name_[sizeof(name_) - 1] = '\0'; |
+} |
+ |
+ |
void Thread::Start() { |
pthread_create(&thread_handle_data()->thread_, NULL, ThreadEntry, this); |
} |