Index: src/OFThread.m ================================================================== --- src/OFThread.m +++ src/OFThread.m @@ -51,10 +51,11 @@ #import "OFOutOfRangeException.h" #import "OFThreadJoinFailedException.h" #import "OFThreadStartFailedException.h" #import "OFThreadStillRunningException.h" +#import "atomic.h" #import "threading.h" static OFList *TLSKeys; static of_tlskey_t threadSelfKey; static OFThread *mainThread; @@ -238,27 +239,13 @@ if (!of_tlskey_set(threadSelfKey, mainThread)) @throw [OFInitializationFailedException exceptionWithClass: self]; } -- init -{ - self = [super init]; - - @try { - runLoop = [[OFRunLoop alloc] init]; - } @catch (id e) { - [self release]; - @throw e; - } - - return self; -} - - initWithObject: (id)object_ { - self = [self init]; + self = [super init]; object = [object_ retain]; return self; } @@ -271,11 +258,11 @@ } - initWithObject: (id)object_ block: (of_thread_block_t)block_ { - self = [self init]; + self = [super init]; @try { object = [object_ retain]; block = [block_ copy]; } @catch (id e) { @@ -294,13 +281,13 @@ return nil; } - (void)handleTermination { - OFRunLoop *old = runLoop; - runLoop = [[OFRunLoop alloc] init]; - [old release]; + OFRunLoop *tmp = runLoop; + runLoop = nil; + [tmp release]; } - (void)start { if (running == OF_THREAD_RUNNING) @@ -337,10 +324,17 @@ return returnValue; } - (OFRunLoop*)runLoop { + if (runLoop == nil) { + OFRunLoop *tmp = [[OFRunLoop alloc] init]; + + if (!of_atomic_cmpswap_ptr((void**)&runLoop, nil, tmp)) + [tmp release]; + } + return [[runLoop retain] autorelease]; } - (void)dealloc {