@@ -36,19 +36,12 @@ #ifdef _WIN32 # include #endif -#import "OFConditionBroadcastFailedException.h" -#import "OFConditionSignalFailedException.h" -#import "OFConditionStillWaitingException.h" -#import "OFConditionWaitFailedException.h" #import "OFInitializationFailedException.h" #import "OFInvalidArgumentException.h" -#import "OFMutexLockFailedException.h" -#import "OFMutexStillLockedException.h" -#import "OFMutexUnlockFailedException.h" #import "OFNotImplementedException.h" #import "OFOutOfRangeException.h" #import "OFThreadJoinFailedException.h" #import "OFThreadStartFailedException.h" #import "OFThreadStillRunningException.h" @@ -55,11 +48,10 @@ #import "atomic.h" #import "autorelease.h" #import "threading.h" -static OFList *TLSKeys; static of_tlskey_t threadSelfKey; static OFThread *mainThread; static id call_main(id object) @@ -359,253 +351,6 @@ - copy { return [self retain]; } -@end - -@implementation OFTLSKey -+ (void)initialize -{ - if (self == [OFTLSKey class]) - TLSKeys = [[OFList alloc] init]; -} - -+ (instancetype)TLSKey -{ - return [[[self alloc] init] autorelease]; -} - -+ (instancetype)TLSKeyWithDestructor: (void(*)(id))destructor -{ - return [[[self alloc] initWithDestructor: destructor] autorelease]; -} - -+ (void)OF_callAllDestructors -{ - of_list_object_t *iter; - - @synchronized (TLSKeys) { - for (iter = [TLSKeys firstListObject]; iter != NULL; - iter = iter->next) { - OFTLSKey *key = (OFTLSKey*)iter->object; - - if (key->destructor != NULL) - key->destructor(iter->object); - } - } -} - -- init -{ - self = [super init]; - - @try { - if (!of_tlskey_new(&key)) - @throw [OFInitializationFailedException - exceptionWithClass: [self class]]; - - initialized = YES; - - @synchronized (TLSKeys) { - listObject = [TLSKeys appendObject: self]; - } - } @catch (id e) { - [self release]; - @throw e; - } - - return self; -} - -- initWithDestructor: (void(*)(id))destructor_ -{ - self = [self init]; - - destructor = destructor_; - - return self; -} - -- (void)dealloc -{ - if (destructor != NULL) - destructor(self); - - if (initialized) - of_tlskey_free(key); - - /* In case we called [self release] in init */ - if (listObject != NULL) { - @synchronized (TLSKeys) { - [TLSKeys removeListObject: listObject]; - } - } - - [super dealloc]; -} -@end - -@implementation OFMutex -+ (instancetype)mutex -{ - return [[[self alloc] init] autorelease]; -} - -- init -{ - self = [super init]; - - if (!of_mutex_new(&mutex)) { - Class c = [self class]; - [self release]; - @throw [OFInitializationFailedException exceptionWithClass: c]; - } - - initialized = YES; - - return self; -} - -- OF_initWithoutCreatingMutex -{ - return [super init]; -} - -- (void)lock -{ - if (!of_mutex_lock(&mutex)) - @throw [OFMutexLockFailedException - exceptionWithClass: [self class] - mutex: self]; -} - -- (BOOL)tryLock -{ - return of_mutex_trylock(&mutex); -} - -- (void)unlock -{ - if (!of_mutex_unlock(&mutex)) - @throw [OFMutexUnlockFailedException - exceptionWithClass: [self class] - mutex: self]; -} - -- (void)dealloc -{ - if (initialized) - if (!of_mutex_free(&mutex)) - @throw [OFMutexStillLockedException - exceptionWithClass: [self class] - mutex: self]; - - [super dealloc]; -} -@end - -@implementation OFRecursiveMutex -- init -{ - self = [super OF_initWithoutCreatingMutex]; - - if (!of_rmutex_new(&rmutex)) { - Class c = [self class]; - [self release]; - @throw [OFInitializationFailedException exceptionWithClass: c]; - } - - initialized = YES; - - return self; -} - -- (void)lock -{ - if (!of_rmutex_lock(&rmutex)) - @throw [OFMutexLockFailedException - exceptionWithClass: [self class] - mutex: self]; -} - -- (BOOL)tryLock -{ - return of_rmutex_trylock(&rmutex); -} - -- (void)unlock -{ - if (!of_rmutex_unlock(&rmutex)) - @throw [OFMutexUnlockFailedException - exceptionWithClass: [self class] - mutex: self]; -} - -- (void)dealloc -{ - if (initialized) - if (!of_rmutex_free(&rmutex)) - @throw [OFMutexStillLockedException - exceptionWithClass: [self class] - mutex: self]; - - [super dealloc]; -} -@end - -@implementation OFCondition -+ (instancetype)condition -{ - return [[[self alloc] init] autorelease]; -} - -- init -{ - self = [super init]; - - if (!of_condition_new(&condition)) { - Class c = [self class]; - [self release]; - @throw [OFInitializationFailedException exceptionWithClass: c]; - } - - conditionInitialized = YES; - - return self; -} - -- (void)wait -{ - if (!of_condition_wait(&condition, &mutex)) - @throw [OFConditionWaitFailedException - exceptionWithClass: [self class] - condition: self]; -} - -- (void)signal -{ - if (!of_condition_signal(&condition)) - @throw [OFConditionSignalFailedException - exceptionWithClass: [self class] - condition: self]; -} - -- (void)broadcast -{ - if (!of_condition_broadcast(&condition)) - @throw [OFConditionBroadcastFailedException - exceptionWithClass: [self class] - condition: self]; -} - -- (void)dealloc -{ - if (conditionInitialized) - if (!of_condition_free(&condition)) - @throw [OFConditionStillWaitingException - exceptionWithClass: [self class] - condition: self]; - - [super dealloc]; -} @end