From 5ff6f85f5872c5096d05d7da52900f68383b8f90 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 15 Aug 2013 21:52:24 +0200 Subject: [PATCH] darwin: call pthread_setname_np() if available When setting the process title, also call pthread_setname_np() when supported (OS X >= 10.6 and iOS >= 3.2.) --- src/unix/darwin-proctitle.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/unix/darwin-proctitle.c b/src/unix/darwin-proctitle.c index 56c141766..48f0486d2 100644 --- a/src/unix/darwin-proctitle.c +++ b/src/unix/darwin-proctitle.c @@ -19,6 +19,7 @@ */ #include +#include #if !TARGET_OS_IPHONE # include @@ -26,9 +27,30 @@ #endif +static int uv__pthread_setname_np(const char* name) { + int (*dynamic_pthread_setname_np)(const char* name); + char namebuf[64]; /* MAXTHREADNAMESIZE */ + int err; + + /* pthread_setname_np() first appeared in OS X 10.6 and iOS 3.2. */ + dynamic_pthread_setname_np = dlsym(RTLD_DEFAULT, "pthread_setname_np"); + if (dynamic_pthread_setname_np == NULL) + return -ENOSYS; + + strncpy(namebuf, name, sizeof(namebuf) - 1); + namebuf[sizeof(namebuf) - 1] = '\0'; + + err = dynamic_pthread_setname_np(namebuf); + if (err) + return -err; + + return 0; +} + + int uv__set_process_title(const char* title) { #if TARGET_OS_IPHONE - return -ENOSYS; + return uv__pthread_setname_np(title); #else typedef CFTypeRef (*LSGetCurrentApplicationASNType)(void); typedef OSStatus (*LSSetApplicationInformationItemType)(int, @@ -84,6 +106,8 @@ int uv__set_process_title(const char* title) { if (err != noErr) return -ENOENT; + uv__pthread_setname_np(title); /* Don't care if it fails. */ + return 0; #endif /* !TARGET_OS_IPHONE */ }