Index: configure.ac ================================================================== --- configure.ac +++ configure.ac @@ -438,11 +438,18 @@ ]) ;; esac AC_DEFINE(OF_THREADS, 1, [Whether we have threads]) - AC_SUBST(OFTHREAD_M, "OFThread.m") + AC_SUBST(THREADING_SOURCES, " \ + OFThread.m \ + OFThreadPool.m \ + OFTLSKey.m \ + OFMutex.m \ + OFRecursiveMutex.m \ + OFCondition.m \ + ") AC_SUBST(OFTHREADTESTS_M, "OFThreadTests.m") AC_SUBST(OFHTTPREQUESTTESTS_M, "OFHTTPRequestTests.m") AC_SUBST(THREADING_H, "threading.h") AC_MSG_CHECKING(whether __thread works) Index: extra.mk.in ================================================================== --- extra.mk.in +++ extra.mk.in @@ -23,11 +23,10 @@ OFPLUGIN_M = @OFPLUGIN_M@ OFPLUGINTESTS_M = @OFPLUGINTESTS_M@ OFSTREAMOBSERVER_KQUEUE_M = @OFSTREAMOBSERVER_KQUEUE_M@ OFSTREAMOBSERVER_POLL_M = @OFSTREAMOBSERVER_POLL_M@ OFSTREAMOBSERVER_SELECT_M = @OFSTREAMOBSERVER_SELECT_M@ -OFTHREAD_M = @OFTHREAD_M@ OFTHREADTESTS_M = @OFTHREADTESTS_M@ PROPERTIESTESTS_M = @PROPERTIESTESTS_M@ REEXPORT_LIBOBJC = @REEXPORT_LIBOBJC@ RUNTIME = @RUNTIME@ RUNTIME_A = @RUNTIME_A@ @@ -36,5 +35,6 @@ RUNTIME_LIB_A = @RUNTIME_LIB_A@ TESTPLUGIN = @TESTPLUGIN@ TESTS = @TESTS@ TEST_LAUNCHER = @TEST_LAUNCHER@ THREADING_H = @THREADING_H@ +THREADING_SOURCES = @THREADING_SOURCES@ Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -49,12 +49,10 @@ OFString+Serialization.m \ OFString+URLEncoding.m \ OFString+XMLEscaping.m \ OFString+XMLUnescaping.m \ OFTCPSocket.m \ - ${OFTHREAD_M} \ - OFThreadPool.m \ OFTimer.m \ OFURL.m \ OFXMLAttribute.m \ OFXMLCDATA.m \ OFXMLCharacters.m \ @@ -63,10 +61,11 @@ OFXMLElement+Serialization.m \ OFXMLElementBuilder.m \ OFXMLNode.m \ OFXMLParser.m \ OFXMLProcessingInstructions.m \ + ${THREADING_SOURCES} \ base64.m \ of_asprintf.m \ of_strptime.m \ unicode.m ADDED src/OFCondition.h Index: src/OFCondition.h ================================================================== --- src/OFCondition.h +++ src/OFCondition.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "OFMutex.h" + +/** + * \brief A class implementing a condition variable for thread synchronization. + */ +@interface OFCondition: OFMutex +{ + of_condition_t condition; + BOOL conditionInitialized; +} + +/** + * \brief Creates a new condition. + * + * \return A new, autoreleased OFCondition + */ ++ (instancetype)condition; + +/** + * \brief Blocks the current thread until another thread calls -[signal] or + * -[broadcast]. + */ +- (void)wait; + +/** + * \brief Signals the next waiting thread to continue. + */ +- (void)signal; + +/** + * \brief Signals all threads to continue. + */ +- (void)broadcast; +@end ADDED src/OFCondition.m Index: src/OFCondition.m ================================================================== --- src/OFCondition.m +++ src/OFCondition.m @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#import "OFCondition.h" + +#import "OFConditionBroadcastFailedException.h" +#import "OFConditionSignalFailedException.h" +#import "OFConditionStillWaitingException.h" +#import "OFConditionWaitFailedException.h" +#import "OFInitializationFailedException.h" + +@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 ADDED src/OFMutex.h Index: src/OFMutex.h ================================================================== --- src/OFMutex.h +++ src/OFMutex.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "OFObject.h" + +#import "threading.h" + +/** + * \brief A class for creating mutual exclusions. + */ +@interface OFMutex: OFObject +{ + of_mutex_t mutex; + BOOL initialized; +} + +/** + * \brief Creates a new mutex. + * + * \return A new autoreleased mutex. + */ ++ (instancetype)mutex; + +- OF_initWithoutCreatingMutex; + +/** + * \brief Locks the mutex. + */ +- (void)lock; + +/** + * \brief Tries to lock the mutex. + * + * \return A boolean whether the mutex could be acquired + */ +- (BOOL)tryLock; + +/** + * \brief Unlocks the mutex. + */ +- (void)unlock; +@end ADDED src/OFMutex.m Index: src/OFMutex.m ================================================================== --- src/OFMutex.m +++ src/OFMutex.m @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#import "OFMutex.h" + +#import "OFInitializationFailedException.h" +#import "OFMutexLockFailedException.h" +#import "OFMutexStillLockedException.h" +#import "OFMutexUnlockFailedException.h" + +@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 + ADDED src/OFRecursiveMutex.h Index: src/OFRecursiveMutex.h ================================================================== --- src/OFRecursiveMutex.h +++ src/OFRecursiveMutex.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "OFMutex.h" + +/** + * \brief A class for creating mutual exclusions which can be entered + * recursively. + */ +@interface OFRecursiveMutex: OFMutex +{ + of_rmutex_t rmutex; +} +@end ADDED src/OFRecursiveMutex.m Index: src/OFRecursiveMutex.m ================================================================== --- src/OFRecursiveMutex.m +++ src/OFRecursiveMutex.m @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#import "OFRecursiveMutex.h" + +#import "OFInitializationFailedException.h" +#import "OFMutexLockFailedException.h" +#import "OFMutexStillLockedException.h" +#import "OFMutexUnlockFailedException.h" + +@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 Index: src/OFStreamObserver.m ================================================================== --- src/OFStreamObserver.m +++ src/OFStreamObserver.m @@ -29,11 +29,13 @@ #import "OFStream.h" #import "OFDataArray.h" #ifdef _WIN32 # import "OFTCPSocket.h" #endif -#import "OFThread.h" +#ifdef OF_THREADS +# import "OFMutex.h" +#endif #ifdef HAVE_KQUEUE # import "OFStreamObserver_kqueue.h" #endif #ifdef HAVE_POLL_H ADDED src/OFTLSKey.h Index: src/OFTLSKey.h ================================================================== --- src/OFTLSKey.h +++ src/OFTLSKey.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "OFObject.h" +#import "OFList.h" + +#import "threading.h" + +/** + * \brief A class for Thread Local Storage keys. + */ +@interface OFTLSKey: OFObject +{ +@public + of_tlskey_t key; +@protected + void (*destructor)(id); + of_list_object_t *listObject; + BOOL initialized; +} + +/** + * \brief Creates a new Thread Local Storage key + * + * \return A new, autoreleased Thread Local Storage key + */ ++ (instancetype)TLSKey; + +/** + * \brief Creates a new Thread Local Storage key with the specified destructor. + * + * \param destructor A destructor that is called when the thread is terminated + * \return A new autoreleased Thread Local Storage key + */ ++ (instancetype)TLSKeyWithDestructor: (void(*)(id))destructor; + ++ (void)OF_callAllDestructors; + +/** + * \brief Initializes an already allocated Thread Local Storage Key with the + * specified destructor. + * + * \param destructor A destructor that is called when the thread is terminated + * \return An initialized Thread Local Storage key + */ +- initWithDestructor: (void(*)(id))destructor; +@end ADDED src/OFTLSKey.m Index: src/OFTLSKey.m ================================================================== --- src/OFTLSKey.m +++ src/OFTLSKey.m @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#import "OFTLSKey.h" + +#import "OFInitializationFailedException.h" + +static OFList *TLSKeys; + +@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 Index: src/OFThread.h ================================================================== --- src/OFThread.h +++ src/OFThread.h @@ -13,11 +13,11 @@ * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #import "OFObject.h" -#import "OFList.h" +#import "OFTLSKey.h" #import "threading.h" @class OFDate; @class OFSortedList; @@ -25,54 +25,10 @@ #ifdef OF_HAVE_BLOCKS typedef id (^of_thread_block_t)(id object); #endif -/** - * \brief A class for Thread Local Storage keys. - */ -@interface OFTLSKey: OFObject -{ -@public - of_tlskey_t key; -/* Work around a bug in gcc 4.4.4 (possibly only on Haiku) */ -#if !defined(__GNUC__) || __GNUC__ != 4 || __GNUC_MINOR__ != 4 || \ - __GNUC_PATCHLEVEL__ != 4 -@protected -#endif - void (*destructor)(id); - of_list_object_t *listObject; - BOOL initialized; -} - -/** - * \brief Creates a new Thread Local Storage key - * - * \return A new, autoreleased Thread Local Storage key - */ -+ (instancetype)TLSKey; - -/** - * \brief Creates a new Thread Local Storage key with the specified destructor. - * - * \param destructor A destructor that is called when the thread is terminated - * \return A new autoreleased Thread Local Storage key - */ -+ (instancetype)TLSKeyWithDestructor: (void(*)(id))destructor; - -+ (void)OF_callAllDestructors; - -/** - * \brief Initializes an already allocated Thread Local Storage Key with the - * specified destructor. - * - * \param destructor A destructor that is called when the thread is terminated - * \return An initialized Thread Local Storage key - */ -- initWithDestructor: (void(*)(id))destructor; -@end - /** * \brief A class which provides portable threads. * * To use it, you should create a new class derived from it and reimplement * main. @@ -258,82 +214,5 @@ * * \return The run loop for the thread */ - (OFRunLoop*)runLoop; @end - -/** - * \brief A class for creating mutual exclusions. - */ -@interface OFMutex: OFObject -{ - of_mutex_t mutex; - BOOL initialized; -} - -/** - * \brief Creates a new mutex. - * - * \return A new autoreleased mutex. - */ -+ (instancetype)mutex; - -/** - * \brief Locks the mutex. - */ -- (void)lock; - -/** - * \brief Tries to lock the mutex. - * - * \return A boolean whether the mutex could be acquired - */ -- (BOOL)tryLock; - -/** - * \brief Unlocks the mutex. - */ -- (void)unlock; -@end - -/** - * \brief A class for creating mutual exclusions which can be entered - * recursively. - */ -@interface OFRecursiveMutex: OFMutex -{ - of_rmutex_t rmutex; -} -@end - -/** - * \brief A class implementing a condition variable for thread synchronization. - */ -@interface OFCondition: OFMutex -{ - of_condition_t condition; - BOOL conditionInitialized; -} - -/** - * \brief Creates a new condition. - * - * \return A new, autoreleased OFCondition - */ -+ (instancetype)condition; - -/** - * \brief Blocks the current thread until another thread calls -[signal] or - * -[broadcast]. - */ -- (void)wait; - -/** - * \brief Signals the next waiting thread to continue. - */ -- (void)signal; - -/** - * \brief Signals all threads to continue. - */ -- (void)broadcast; -@end Index: src/OFThread.m ================================================================== --- src/OFThread.m +++ src/OFThread.m @@ -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 Index: src/OFThreadPool.m ================================================================== --- src/OFThreadPool.m +++ src/OFThreadPool.m @@ -18,10 +18,11 @@ #import "OFThreadPool.h" #import "OFArray.h" #import "OFList.h" #import "OFThread.h" +#import "OFCondition.h" #import "autorelease.h" @interface OFThreadPoolJob: OFObject { Index: src/OFTimer.m ================================================================== --- src/OFTimer.m +++ src/OFTimer.m @@ -17,11 +17,11 @@ #include "config.h" #import "OFTimer.h" #import "OFDate.h" #import "OFRunLoop.h" -#import "OFThread.h" +#import "OFCondition.h" #import "OFInvalidArgumentException.h" #import "OFNotImplementedException.h" #import "autorelease.h" Index: src/ObjFW.h ================================================================== --- src/ObjFW.h +++ src/ObjFW.h @@ -80,14 +80,16 @@ #import "OFAlreadyConnectedException.h" #import "OFBindFailedException.h" #import "OFChangeDirectoryFailedException.h" #import "OFChangeFileModeFailedException.h" #import "OFChangeFileOwnerFailedException.h" -#import "OFConditionBroadcastFailedException.h" -#import "OFConditionSignalFailedException.h" -#import "OFConditionStillWaitingException.h" -#import "OFConditionWaitFailedException.h" +#ifdef OF_THREADS +# import "OFConditionBroadcastFailedException.h" +# import "OFConditionSignalFailedException.h" +# import "OFConditionStillWaitingException.h" +# import "OFConditionWaitFailedException.h" +#endif #import "OFConnectionFailedException.h" #import "OFCopyFileFailedException.h" #import "OFCreateDirectoryFailedException.h" #import "OFDeleteDirectoryFailedException.h" #import "OFDeleteFileFailedException.h" @@ -102,13 +104,15 @@ #import "OFInvalidServerReplyException.h" #import "OFLinkFailedException.h" #import "OFListenFailedException.h" #import "OFMalformedXMLException.h" #import "OFMemoryNotPartOfObjectException.h" -#import "OFMutexLockFailedException.h" -#import "OFMutexStillLockedException.h" -#import "OFMutexUnlockFailedException.h" +#ifdef OF_THREADS +# import "OFMutexLockFailedException.h" +# import "OFMutexStillLockedException.h" +# import "OFMutexUnlockFailedException.h" +#endif #import "OFNotConnectedException.h" #import "OFNotImplementedException.h" #import "OFOpenFileFailedException.h" #import "OFOutOfMemoryException.h" #import "OFOutOfRangeException.h" @@ -116,13 +120,15 @@ #import "OFReadOrWriteFailedException.h" #import "OFRenameFileFailedException.h" #import "OFSeekFailedException.h" #import "OFSetOptionFailedException.h" #import "OFSymlinkFailedException.h" -#import "OFThreadJoinFailedException.h" -#import "OFThreadStartFailedException.h" -#import "OFThreadStillRunningException.h" +#ifdef OF_THREADS +# import "OFThreadJoinFailedException.h" +# import "OFThreadStartFailedException.h" +# import "OFThreadStillRunningException.h" +#endif #import "OFTruncatedDataException.h" #import "OFUnboundNamespaceException.h" #import "OFUnsupportedProtocolException.h" #import "OFWriteFailedException.h" @@ -135,15 +141,19 @@ #ifdef OF_ATOMIC_OPS # import "atomic.h" #endif #ifdef OF_THREADS +# import "threading.h" # import "OFThread.h" # import "OFThreadPool.h" -# import "threading.h" +# import "OFTLSKey.h" +# import "OFMutex.h" +# import "OFRecursiveMutex.h" +# import "OFCondition.h" #endif #import "autorelease.h" #import "asprintf.h" #import "base64.h" #import "of_asprintf.h" #import "of_strptime.h" Index: src/exceptions/OFConditionBroadcastFailedException.m ================================================================== --- src/exceptions/OFConditionBroadcastFailedException.m +++ src/exceptions/OFConditionBroadcastFailedException.m @@ -16,11 +16,11 @@ #include "config.h" #import "OFConditionBroadcastFailedException.h" #import "OFString.h" -#import "OFThread.h" +#import "OFCondition.h" #import "OFNotImplementedException.h" @implementation OFConditionBroadcastFailedException + (instancetype)exceptionWithClass: (Class)class_ Index: src/exceptions/OFConditionSignalFailedException.m ================================================================== --- src/exceptions/OFConditionSignalFailedException.m +++ src/exceptions/OFConditionSignalFailedException.m @@ -16,11 +16,11 @@ #include "config.h" #import "OFConditionSignalFailedException.h" #import "OFString.h" -#import "OFThread.h" +#import "OFCondition.h" #import "OFNotImplementedException.h" @implementation OFConditionSignalFailedException + (instancetype)exceptionWithClass: (Class)class_ Index: src/exceptions/OFConditionStillWaitingException.m ================================================================== --- src/exceptions/OFConditionStillWaitingException.m +++ src/exceptions/OFConditionStillWaitingException.m @@ -16,11 +16,11 @@ #include "config.h" #import "OFConditionStillWaitingException.h" #import "OFString.h" -#import "OFThread.h" +#import "OFCondition.h" #import "OFNotImplementedException.h" @implementation OFConditionStillWaitingException + (instancetype)exceptionWithClass: (Class)class_ Index: src/exceptions/OFConditionWaitFailedException.m ================================================================== --- src/exceptions/OFConditionWaitFailedException.m +++ src/exceptions/OFConditionWaitFailedException.m @@ -16,11 +16,11 @@ #include "config.h" #import "OFConditionWaitFailedException.h" #import "OFString.h" -#import "OFThread.h" +#import "OFCondition.h" #import "OFNotImplementedException.h" @implementation OFConditionWaitFailedException + (instancetype)exceptionWithClass: (Class)class_ Index: src/exceptions/OFMutexLockFailedException.m ================================================================== --- src/exceptions/OFMutexLockFailedException.m +++ src/exceptions/OFMutexLockFailedException.m @@ -16,11 +16,11 @@ #include "config.h" #import "OFMutexLockFailedException.h" #import "OFString.h" -#import "OFThread.h" +#import "OFMutex.h" #import "OFNotImplementedException.h" @implementation OFMutexLockFailedException + (instancetype)exceptionWithClass: (Class)class_ Index: src/exceptions/OFMutexStillLockedException.m ================================================================== --- src/exceptions/OFMutexStillLockedException.m +++ src/exceptions/OFMutexStillLockedException.m @@ -16,11 +16,11 @@ #include "config.h" #import "OFMutexStillLockedException.h" #import "OFString.h" -#import "OFThread.h" +#import "OFMutex.h" #import "OFNotImplementedException.h" @implementation OFMutexStillLockedException + (instancetype)exceptionWithClass: (Class)class_ Index: src/exceptions/OFMutexUnlockFailedException.m ================================================================== --- src/exceptions/OFMutexUnlockFailedException.m +++ src/exceptions/OFMutexUnlockFailedException.m @@ -16,11 +16,11 @@ #include "config.h" #import "OFMutexUnlockFailedException.h" #import "OFString.h" -#import "OFThread.h" +#import "OFMutex.h" #import "OFNotImplementedException.h" @implementation OFMutexUnlockFailedException + (instancetype)exceptionWithClass: (Class)class_ Index: tests/OFHTTPRequestTests.m ================================================================== --- tests/OFHTTPRequestTests.m +++ tests/OFHTTPRequestTests.m @@ -24,10 +24,11 @@ #import "OFHTTPRequest.h" #import "OFString.h" #import "OFTCPSocket.h" #import "OFThread.h" +#import "OFCondition.h" #import "OFURL.h" #import "OFDictionary.h" #import "OFAutoreleasePool.h" #import "TestsAppDelegate.h"