@@ -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; }