diff --git a/src/unix/darwin-proctitle.c b/src/unix/darwin-proctitle.c index 56c14176..48f0486d 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 */ }