Index: src/OFApplication.m ================================================================== --- src/OFApplication.m +++ src/OFApplication.m @@ -298,12 +298,12 @@ { void *pool; OFRunLoop *runLoop; [OFThread _createMainThread]; - runLoop = [[[OFRunLoop alloc] init] autorelease]; - [OFRunLoop _setMainRunLoop: runLoop]; + runLoop = [OFRunLoop currentRunLoop]; + [OFRunLoop _setMainRunLoop]; pool = objc_autoreleasePoolPush(); [delegate applicationDidFinishLaunching]; objc_autoreleasePoolPop(pool); Index: src/OFRunLoop.h ================================================================== --- src/OFRunLoop.h +++ src/OFRunLoop.h @@ -48,11 +48,11 @@ * * \return The run loop for the current thread */ + (OFRunLoop*)currentRunLoop; -+ (void)_setMainRunLoop: (OFRunLoop*)mainRunLoop; ++ (void)_setMainRunLoop; #ifdef OF_HAVE_BLOCKS + (void)_addAsyncReadForStream: (OFStream*)stream buffer: (void*)buffer length: (size_t)length block: (of_stream_async_read_block_t)block; Index: src/OFRunLoop.m ================================================================== --- src/OFRunLoop.m +++ src/OFRunLoop.m @@ -26,12 +26,11 @@ #import "OFDate.h" #import "autorelease.h" #import "macros.h" -static OFTLSKey *currentRunLoopKey; -static OFRunLoop *mainRunLoop; +static OFRunLoop *mainRunLoop = nil; #ifdef OF_HAVE_BLOCKS @interface OFRunLoop_ReadQueueItem: OFObject { void *buffer; @@ -95,39 +94,34 @@ } @end #endif @implementation OFRunLoop -+ (void)initialize -{ - if (self == [OFRunLoop class]) - currentRunLoopKey = [[OFTLSKey alloc] init]; -} - + (OFRunLoop*)mainRunLoop { return [[mainRunLoop retain] autorelease]; } + (OFRunLoop*)currentRunLoop { - OFRunLoop *runLoop = [OFThread objectForTLSKey: currentRunLoopKey]; - - if (runLoop == nil) { - runLoop = [[OFRunLoop alloc] init]; - [OFThread setObject: runLoop - forTLSKey: currentRunLoopKey]; - } - - return [[runLoop retain] autorelease]; -} - -+ (void)_setMainRunLoop: (OFRunLoop*)mainRunLoop_ -{ - mainRunLoop = [mainRunLoop_ retain]; - [OFThread setObject: mainRunLoop - forTLSKey: currentRunLoopKey]; + OFThread *currentThread = [OFThread currentThread]; + OFRunLoop *runLoop = [currentThread runLoop]; + + if (runLoop != nil) + return runLoop; + + runLoop = [[[OFRunLoop alloc] init] autorelease]; + [currentThread _setRunLoop: runLoop]; + + return runLoop; +} + ++ (void)_setMainRunLoop +{ + void *pool = objc_autoreleasePoolPush(); + mainRunLoop = [[[OFThread currentThread] runLoop] retain]; + objc_autoreleasePoolPop(pool); } #ifdef OF_HAVE_BLOCKS + (void)_addAsyncReadForStream: (OFStream*)stream buffer: (void*)buffer @@ -211,20 +205,16 @@ - init { self = [super init]; @try { - void *pool = objc_autoreleasePoolPush(); - - timersQueue = [[[OFThread currentThread] _timersQueue] retain]; + timersQueue = [[OFSortedList alloc] init]; streamObserver = [[OFStreamObserver alloc] init]; [streamObserver setDelegate: self]; readQueues = [[OFMutableDictionary alloc] init]; - - objc_autoreleasePoolPop(pool); } @catch (id e) { [self release]; @throw e; } Index: src/OFThread.h ================================================================== --- src/OFThread.h +++ src/OFThread.h @@ -19,10 +19,11 @@ #import "threading.h" @class OFDate; @class OFSortedList; +@class OFRunLoop; #ifdef OF_HAVE_BLOCKS typedef id (^of_thread_block_t)(id object); #endif @@ -98,16 +99,11 @@ } running; #ifdef OF_HAVE_BLOCKS of_thread_block_t block; #endif id returnValue; -#ifdef OF_THREAD_M -@protected -#else -@private -#endif - OFSortedList *timersQueue; + OFRunLoop *runLoop; } #if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS) @property (copy) of_thread_block_t block; #endif @@ -276,11 +272,18 @@ * * \return The object returned by the main method of the thread. */ - (id)join; -- (OFSortedList*)_timersQueue; +/** + * \brief Returns the run loop for the thread or nil if it has no run loop. + * + * \return The run loop for the thread or nil if it has no run loop + */ +- (OFRunLoop*)runLoop; + +- (void)_setRunLoop: (OFRunLoop*)runLoop; @end /** * \brief A class for creating mutual exclusions. */ Index: src/OFThread.m ================================================================== --- src/OFThread.m +++ src/OFThread.m @@ -29,10 +29,11 @@ #import "OFThread.h" #import "OFList.h" #import "OFDate.h" #import "OFSortedList.h" +#import "OFRunLoop.h" #import "OFAutoreleasePool.h" #ifdef _WIN32 # include #endif @@ -280,17 +281,12 @@ return nil; } - (void)handleTermination { - @synchronized (timersQueue) { - /* - * Make sure we don't run old timers when the thread is - * restarted. - */ - [timersQueue removeAllObjects]; - } + [runLoop release]; + runLoop = nil; } - (void)start { if (running == OF_THREAD_RUNNING) @@ -325,20 +321,20 @@ running = OF_THREAD_NOT_RUNNING; return returnValue; } -- (OFSortedList*)_timersQueue -{ - if (timersQueue == nil) { - OFSortedList *tmp = [[OFSortedList alloc] init]; - - if (!of_atomic_cmpswap_ptr((void**)&timersQueue, nil, tmp)) - [tmp release]; - } - - return [[timersQueue retain] autorelease]; +- (OFRunLoop*)runLoop +{ + return [[runLoop retain] autorelease]; +} + +- (void)_setRunLoop: (OFRunLoop*)runLoop_ +{ + OFRunLoop *old = runLoop; + runLoop = [runLoop_ retain]; + [old release]; } - (void)dealloc { if (running == OF_THREAD_RUNNING) @@ -353,11 +349,10 @@ if (running == OF_THREAD_WAITING_FOR_JOIN) of_thread_detach(thread); [object release]; [returnValue release]; - [timersQueue release]; [super dealloc]; } - copy