Include thread names in Mac OS X 10.6 crash reports (finally, ooSetCurrentThreadName: is actually doing something useful). Backporting because, er, well, that's what ooSetCurrentThreadName: was originally for, so making it actually work is a bug fix, right?

git-svn-id: http://svn.berlios.de/svnroot/repos/oolite-linux/trunk@2372 127b21dd-08f5-0310-b4b7-95ae10353056
This commit is contained in:
Jens Ayton 2009-09-03 21:34:57 +00:00
parent c59c9743e1
commit c9a743ed9d

View File

@ -51,6 +51,31 @@ SOFTWARE.
#import "NSThreadOOExtensions.h"
#ifndef OO_HAVE_PTHREAD_SETNAME_NP
#define OO_HAVE_PTHREAD_SETNAME_NP 0
#endif
#if !OO_HAVE_PTHREAD_SETNAME_NP && OOLITE_MAC_OS_X
#undef OO_HAVE_PTHREAD_SETNAME_NP
#define OO_HAVE_PTHREAD_SETNAME_NP 1
#define PTHREAD_SETNAME_DYNAMIC 1
#endif
#if !OO_HAVE_PTHREAD_SETNAME_NP
#define pthread_setname_np(name) do {} while (0)
#endif
#if PTHREAD_SETNAME_DYNAMIC
static int InitialSetNameFunc(const char *name);
static int (*PThreadSetNameNPFunc)(const char *name) = InitialSetNameFunc;
#define pthread_setname_np(name) do { if (PThreadSetNameNPFunc != NULL) PThreadSetNameNPFunc(name); } while (0)
#endif
@interface NSThread (LeopardAdditions)
- (void) setName:(NSString *)name;
@end
@ -79,6 +104,10 @@ SOFTWARE.
{
[thread setName:name];
}
/* Under Mac OS X 10.6, the name set by pthread_setname_np() is used in
crash reports, but, annoyingly, -[NSThread setName:] does not call it.
*/
pthread_setname_np([name UTF8String]);
}
@end
@ -121,3 +150,31 @@ SOFTWARE.
}
@end
#if PTHREAD_SETNAME_DYNAMIC
#import <dlfcn.h>
// Attempt to load pthread_setname_np() (available in Mac OS X 10.6 or later)
static int InitialSetNameFunc(const char *name)
{
@synchronized ([NSThread class]) // Thread functions should be thread safe.
{
if (PThreadSetNameNPFunc == InitialSetNameFunc) // Only look up once.
{
PThreadSetNameNPFunc = dlsym(RTLD_DEFAULT, "pthread_setname_np");
}
}
if (PThreadSetNameNPFunc != NULL)
{
return PThreadSetNameNPFunc(name);
}
else
{
return 0;
}
}
#endif