@@ -28,10 +28,11 @@ #endif #import "OFThread.h" #import "OFList.h" #import "OFDate.h" +#import "OFSortedList.h" #import "OFAutoreleasePool.h" #ifdef _WIN32 # include #endif @@ -257,12 +258,17 @@ - initWithBlock: (of_thread_block_t)block_ object: (id)object_ { self = [super init]; - block = [block_ copy]; - object = [object_ retain]; + @try { + block = [block_ copy]; + object = [object_ retain]; + } @catch (id e) { + [self release]; + @throw e; + } return self; } #endif @@ -274,10 +280,17 @@ return nil; } - (void)handleTermination { + @synchronized (timersQueue) { + /* + * Make sure we don't run old timers when the thread is + * restarted. + */ + [timersQueue removeAllObjects]; + } } - (void)start { if (running == OF_THREAD_RUNNING) @@ -311,10 +324,22 @@ 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]; +} - (void)dealloc { if (running == OF_THREAD_RUNNING) @throw [OFThreadStillRunningException @@ -328,10 +353,11 @@ if (running == OF_THREAD_WAITING_FOR_JOIN) of_thread_detach(thread); [object release]; [returnValue release]; + [timersQueue release]; [super dealloc]; } @end