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.)
This commit is contained in:
Ben Noordhuis 2013-08-15 21:52:24 +02:00
parent c82e7033a5
commit 5ff6f85f58

View File

@ -19,6 +19,7 @@
*/
#include <TargetConditionals.h>
#include <dlfcn.h>
#if !TARGET_OS_IPHONE
# include <CoreFoundation/CoreFoundation.h>
@ -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 */
}