aboutsummaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorMax <post@wickenrode.com>2014-12-30 02:29:26 +0100
committerMax <post@wickenrode.com>2014-12-30 02:29:26 +0100
commitae8a9465306eaee5bb1478b74057e1bdf540b1db (patch)
tree30b1392656e29620bb3e26d7a499886020f7749b /Source
parent19226617658f7452d8f02a68c52556aca6810c36 (diff)
downloadsequelpro-ae8a9465306eaee5bb1478b74057e1bdf540b1db.tar.gz
sequelpro-ae8a9465306eaee5bb1478b74057e1bdf540b1db.tar.bz2
sequelpro-ae8a9465306eaee5bb1478b74057e1bdf540b1db.zip
+[NSThread detachNewThreadWithName:…] changes
The method never did the one extra thing it should do, because… see comment in the code. Let's see how this works out :)
Diffstat (limited to 'Source')
-rw-r--r--Source/SPThreadAdditions.m60
1 files changed, 56 insertions, 4 deletions
diff --git a/Source/SPThreadAdditions.m b/Source/SPThreadAdditions.m
index c1f0a52b..74476888 100644
--- a/Source/SPThreadAdditions.m
+++ b/Source/SPThreadAdditions.m
@@ -29,17 +29,69 @@
// More info at <https://github.com/sequelpro/sequelpro>
#import "SPThreadAdditions.h"
+#import <objc/objc-runtime.h>
+
+//this is a "private" class only needed by the +detachNewThreadWithName:… method below.
+@interface SPNamedThread : NSObject {
+ @private
+ NSString *name;
+ id object;
+ SEL selector;
+}
+- (id)initWithTarget:(id)aObject selector:(SEL)aSelector name:(NSString *)aName;
+- (void)run:(id)argument;
+@end
@implementation NSThread (SPThreadAdditions)
+ (void)detachNewThreadWithName:(NSString *)aName target:(id)aTarget selector:(SEL)aSelector object:(id)anArgument
{
- NSThread *newThread = [[NSThread alloc] initWithTarget:aTarget selector:aSelector object:anArgument];
- if (aName) {
- [newThread setName:aName];
- }
+ // -[NSThread setName:] has two limitations when it comes to visibility in Xcode:
+ // a) Xcode only updates the thread name in UI once (on the first time the thread is shown in the debugger).
+ // b) Internally this method calls
+ // int pthread_setname_np(const char*);
+ // which, as can be seen, does not allow to specify a thread id. Therefore it is skipped if <calling thread != self>.
+ // Unfortunately this (and not the property of the NSThread) seems to be the actual name shown in Xcode.
+ // The consequence is, we can only set a thread's name from within the thread, so let's add a proxy object to do that.
+ SPNamedThread *namedThread = [[SPNamedThread alloc] initWithTarget:aTarget selector:aSelector name:aName];
+
+ NSThread *newThread = [[NSThread alloc] initWithTarget:namedThread selector:@selector(run:) object:anArgument];
[newThread start];
[newThread autorelease];
+ [namedThread autorelease];
+}
+
+@end
+
+#pragma mark -
+
+@implementation SPNamedThread
+
+- (id)initWithTarget:(id)aObject selector:(SEL)aSelector name:(NSString *)aName
+{
+ if(self = [super init]) {
+ name = [aName copy];
+ object = [aObject retain];
+ selector = aSelector;
+ }
+ return self;
+}
+
+- (void)run:(id)argument
+{
+ [[NSThread currentThread] setName:name];
+
+ void (*msgsend)(id, SEL, id) = (void (*)(id, SEL, id)) objc_msgSend; //hint for the compiler
+
+ msgsend(object,selector,argument);
+}
+
+- (void)dealloc
+{
+ [object release], object = nil;
+ selector = NULL;
+ [name release], name = nil;
+ [super dealloc];
}
@end