@@ -18,18 +18,22 @@ #define OF_THREAD_M #define __NO_EXT_QNX +#include #include #ifndef _WIN32 # include +#endif + +#ifdef OF_HAVE_SCHED_YIELD # include #endif -#ifdef __HAIKU__ +#if defined(OF_HAVE_THREADS) && defined(__HAIKU__) # include #endif #import "OFRunLoop.h" #import "OFThread.h" @@ -42,20 +46,25 @@ # include #endif #import "OFInitializationFailedException.h" #import "OFInvalidArgumentException.h" +#import "OFNotImplementedException.h" #import "OFOutOfRangeException.h" -#import "OFThreadJoinFailedException.h" -#import "OFThreadStartFailedException.h" -#import "OFThreadStillRunningException.h" +#ifdef OF_HAVE_THREADS +# import "OFThreadJoinFailedException.h" +# import "OFThreadStartFailedException.h" +# import "OFThreadStillRunningException.h" +#endif #ifdef OF_HAVE_ATOMIC_OPS # import "atomic.h" #endif #import "autorelease.h" -#import "threading.h" + +#ifdef OF_HAVE_THREADS +# import "threading.h" static of_tlskey_t threadSelfKey; static OFThread *mainThread; static id @@ -71,54 +80,56 @@ /* * Nasty workaround for thread implementations which can't return a * value on join. */ -#ifdef OF_HAVE_BLOCKS +# ifdef OF_HAVE_BLOCKS if (thread->_block != NULL) thread->_returnValue = [thread->_block() retain]; else -#endif +# endif thread->_returnValue = [[thread main] retain]; [thread handleTermination]; thread->_running = OF_THREAD_WAITING_FOR_JOIN; [OFTLSKey OF_callAllDestructors]; -#ifdef OF_OBJFW_RUNTIME +# ifdef OF_OBJFW_RUNTIME /* * As the values returned by objc_autoreleasePoolPush() in the ObjFW * runtime are not actually pointers, but sequential numbers, 0 means * we pop everything. */ objc_autoreleasePoolPop(0); -#endif +# endif [thread release]; return 0; } static void set_thread_name(OFThread *thread) { -#ifdef __HAIKU__ +# ifdef __HAIKU__ OFString *name = thread->_name; if (name == nil) name = [thread className]; rename_thread(get_pthread_thread_id(thread->_thread), [name UTF8String]); -#endif +# endif } +#endif @implementation OFThread -#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS) +#ifdef OF_HAVE_THREADS +# if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS) @synthesize block = _block; -#endif +# endif + (void)initialize { if (self != [OFThread class]) return; @@ -131,16 +142,16 @@ + (instancetype)thread { return [[[self alloc] init] autorelease]; } -#ifdef OF_HAVE_BLOCKS +# ifdef OF_HAVE_BLOCKS + (instancetype)threadWithBlock: (of_thread_block_t)block { return [[[self alloc] initWithBlock: block] autorelease]; } -#endif +# endif + (void)setObject: (id)object forTLSKey: (OFTLSKey*)key { id oldObject = of_tlskey_get(key->_key); @@ -164,10 +175,11 @@ + (OFThread*)mainThread { return mainThread; } +#endif + (void)sleepForTimeInterval: (double)seconds { if (seconds < 0) @throw [OFOutOfRangeException exceptionWithClass: self]; @@ -211,10 +223,11 @@ #else [self sleepForTimeInterval: 0]; #endif } +#ifdef OF_HAVE_THREADS + (void)terminate { [self terminateWithObject: nil]; } @@ -229,18 +242,18 @@ thread->_running = OF_THREAD_WAITING_FOR_JOIN; } [OFTLSKey OF_callAllDestructors]; -#ifdef OF_OBJFW_RUNTIME +# ifdef OF_OBJFW_RUNTIME /* * As the values returned by objc_autoreleasePoolPush() in the ObjFW * runtime are not actually pointers, but sequential numbers, 0 means * we pop everything. */ objc_autoreleasePoolPop(0); -#endif +# endif [thread release]; of_thread_exit(); } @@ -253,11 +266,11 @@ if (!of_tlskey_set(threadSelfKey, mainThread)) @throw [OFInitializationFailedException exceptionWithClass: self]; } -#ifdef OF_HAVE_BLOCKS +# ifdef OF_HAVE_BLOCKS - initWithBlock: (of_thread_block_t)block { self = [super init]; @try { @@ -267,11 +280,11 @@ @throw e; } return self; } -#endif +# endif - (id)main { [[OFRunLoop currentRunLoop] run]; @@ -328,23 +341,23 @@ return [self retain]; } - (OFRunLoop*)runLoop { -#ifdef OF_HAVE_ATOMIC_OPS +# ifdef OF_HAVE_ATOMIC_OPS if (_runLoop == nil) { OFRunLoop *tmp = [[OFRunLoop alloc] init]; if (!of_atomic_cmpswap_ptr((void**)&_runLoop, nil, tmp)) [tmp release]; } -#else +# else @synchronized (self) { if (_runLoop == nil) _runLoop = [[OFRunLoop alloc] init]; } -#endif +# endif return [[_runLoop retain] autorelease]; } - (OFString*)name @@ -377,6 +390,19 @@ [_returnValue release]; [_runLoop release]; [super dealloc]; } +#else +- init +{ + @try { + [self doesNotRecognizeSelector: _cmd]; + } @catch (id e) { + [self release]; + @throw e; + } + + abort(); +} +#endif @end