@@ -46,10 +46,13 @@ # include <3ds/svc.h> #endif #import "OFThread.h" #import "OFThread+Private.h" +#ifdef OF_HAVE_ATOMIC_OPS +# import "OFAtomic.h" +#endif #import "OFDate.h" #import "OFDictionary.h" #ifdef OF_HAVE_SOCKETS # import "OFDNSResolver.h" #endif @@ -75,21 +78,17 @@ # import "OFThreadJoinFailedException.h" # import "OFThreadStartFailedException.h" # import "OFThreadStillRunningException.h" #endif -#ifdef OF_HAVE_ATOMIC_OPS -# import "atomic.h" -#endif - #if defined(OF_HAVE_THREADS) -# import "tlskey.h" +# import "OFTLSKey.h" # if defined(OF_AMIGAOS) && defined(OF_HAVE_SOCKETS) -# import "socket.h" +# import "OFSocket.h" # endif -static of_tlskey_t threadSelfKey; +static OFTLSKey threadSelfKey; static OFThread *mainThread; #elif defined(OF_HAVE_SOCKETS) static OFDNSResolver *DNSResolver; #endif @@ -99,28 +98,28 @@ callMain(id object) { OFThread *thread = (OFThread *)object; OFString *name; - if (of_tlskey_set(threadSelfKey, thread) != 0) + if (OFTLSKeySet(threadSelfKey, thread) != 0) @throw [OFInitializationFailedException exceptionWithClass: thread.class]; #ifndef OF_OBJFW_RUNTIME thread->_pool = objc_autoreleasePoolPush(); #endif name = thread.name; if (name != nil) - of_thread_set_name( + OFSetThreadName( [name cStringWithEncoding: [OFLocale encoding]]); else - of_thread_set_name(object_getClassName(thread)); + OFSetThreadName(object_getClassName(thread)); #if defined(OF_AMIGAOS) && defined(OF_HAVE_SOCKETS) if (thread.supportsSockets) - if (!of_socket_init()) + if (!OFSocketInit()) @throw [OFInitializationFailedException exceptionWithClass: thread.class]; #endif /* @@ -144,14 +143,14 @@ objc_autoreleasePoolPop(thread->_pool); #endif #if defined(OF_AMIGAOS) && !defined(OF_MORPHOS) && defined(OF_HAVE_SOCKETS) if (thread.supportsSockets) - of_socket_deinit(); + OFSocketDeinit(); #endif - thread->_running = OF_THREAD_WAITING_FOR_JOIN; + thread->_running = OFThreadStateWaitingForJoin; [thread release]; } @synthesize name = _name; @@ -162,11 +161,11 @@ + (void)initialize { if (self != [OFThread class]) return; - if (of_tlskey_new(&threadSelfKey) != 0) + if (OFTLSKeyNew(&threadSelfKey) != 0) @throw [OFInitializationFailedException exceptionWithClass: self]; } + (instancetype)thread @@ -173,19 +172,19 @@ { return [[[self alloc] init] autorelease]; } # ifdef OF_HAVE_BLOCKS -+ (instancetype)threadWithThreadBlock: (of_thread_block_t)threadBlock ++ (instancetype)threadWithThreadBlock: (OFThreadBlock)threadBlock { return [[[self alloc] initWithThreadBlock: threadBlock] autorelease]; } # endif + (OFThread *)currentThread { - return of_tlskey_get(threadSelfKey); + return OFTLSKeyGet(threadSelfKey); } + (OFThread *)mainThread { return mainThread; @@ -194,16 +193,16 @@ + (bool)isMainThread { if (mainThread == nil) return false; - return (of_tlskey_get(threadSelfKey) == mainThread); + return (OFTLSKeyGet(threadSelfKey) == mainThread); } + (OFMutableDictionary *)threadDictionary { - OFThread *thread = of_tlskey_get(threadSelfKey); + OFThread *thread = OFTLSKeyGet(threadSelfKey); if (thread == nil) return nil; if (thread->_threadDictionary == nil) @@ -215,11 +214,11 @@ #ifdef OF_HAVE_SOCKETS + (OFDNSResolver *)DNSResolver { # ifdef OF_HAVE_THREADS - OFThread *thread = of_tlskey_get(threadSelfKey); + OFThread *thread = OFTLSKeyGet(threadSelfKey); if (thread == nil) return nil; if (thread->_DNSResolver == nil) @@ -233,11 +232,11 @@ return DNSResolver; # endif } #endif -+ (void)sleepForTimeInterval: (of_time_interval_t)timeInterval ++ (void)sleepForTimeInterval: (OFTimeInterval)timeInterval { if (timeInterval < 0) return; #if defined(OF_WINDOWS) @@ -317,16 +316,16 @@ OF_UNREACHABLE } + (void)terminateWithObject: (id)object { - OFThread *thread = of_tlskey_get(threadSelfKey); + OFThread *thread = OFTLSKeyGet(threadSelfKey); if (thread == mainThread) @throw [OFInvalidArgumentException exception]; - OF_ENSURE(thread != nil); + OFEnsure(thread != nil); thread->_returnValue = [object retain]; longjmp(thread->_exitEnv, 1); OF_UNREACHABLE @@ -335,14 +334,14 @@ + (void)setName: (OFString *)name { [OFThread currentThread].name = name; if (name != nil) - of_thread_set_name( + OFSetThreadName( [name cStringWithEncoding: [OFLocale encoding]]); else - of_thread_set_name(class_getName([self class])); + OFSetThreadName(class_getName([self class])); } + (OFString *)name { return [OFThread currentThread].name; @@ -349,24 +348,24 @@ } + (void)of_createMainThread { mainThread = [[OFThread alloc] init]; - mainThread->_thread = of_thread_current(); - mainThread->_running = OF_THREAD_RUNNING; + mainThread->_thread = OFCurrentPlainThread(); + mainThread->_running = OFThreadStateRunning; - if (of_tlskey_set(threadSelfKey, mainThread) != 0) + if (OFTLSKeySet(threadSelfKey, mainThread) != 0) @throw [OFInitializationFailedException exceptionWithClass: self]; } - (instancetype)init { self = [super init]; @try { - if (of_thread_attr_init(&_attr) != 0) + if (OFPlainThreadAttributesInit(&_attr) != 0) @throw [OFInitializationFailedException exceptionWithClass: self.class]; } @catch (id e) { [self release]; @throw e; @@ -374,11 +373,11 @@ return self; } # ifdef OF_HAVE_BLOCKS -- (instancetype)initWithThreadBlock: (of_thread_block_t)threadBlock +- (instancetype)initWithThreadBlock: (OFThreadBlock)threadBlock { self = [self init]; @try { _threadBlock = [threadBlock copy]; @@ -415,24 +414,24 @@ - (void)start { int error; - if (_running == OF_THREAD_RUNNING) + if (_running == OFThreadStateRunning) @throw [OFThreadStillRunningException exceptionWithThread: self]; - if (_running == OF_THREAD_WAITING_FOR_JOIN) { - of_thread_detach(_thread); + if (_running == OFThreadStateWaitingForJoin) { + OFPlainThreadDetach(_thread); [_returnValue release]; } [self retain]; - _running = OF_THREAD_RUNNING; + _running = OFThreadStateRunning; - if ((error = of_thread_new(&_thread, [_name cStringWithEncoding: + if ((error = OFPlainThreadNew(&_thread, [_name cStringWithEncoding: [OFLocale encoding]], callMain, self, &_attr)) != 0) { [self release]; @throw [OFThreadStartFailedException exceptionWithThread: self errNo: error]; @@ -441,20 +440,20 @@ - (id)join { int error; - if (_running == OF_THREAD_NOT_RUNNING) + if (_running == OFThreadStateNotRunning) @throw [OFThreadJoinFailedException exceptionWithThread: self errNo: EINVAL]; - if ((error = of_thread_join(_thread)) != 0) + if ((error = OFPlainThreadJoin(_thread)) != 0) @throw [OFThreadJoinFailedException exceptionWithThread: self errNo: error]; - _running = OF_THREAD_NOT_RUNNING; + _running = OFThreadStateNotRunning; return _returnValue; } - (id)copy @@ -466,11 +465,12 @@ { # if defined(OF_HAVE_ATOMIC_OPS) && !defined(__clang_analyzer__) if (_runLoop == nil) { OFRunLoop *tmp = [[OFRunLoop alloc] init]; - if (!of_atomic_ptr_cmpswap((void **)&_runLoop, nil, tmp)) + if (!OFAtomicPointerCompareAndSwap( + (void **)&_runLoop, nil, tmp)) [tmp release]; } # else @synchronized (self) { if (_runLoop == nil) @@ -486,11 +486,11 @@ return _attr.priority; } - (void)setPriority: (float)priority { - if (_running == OF_THREAD_RUNNING) + if (_running == OFThreadStateRunning) @throw [OFThreadStillRunningException exceptionWithThread: self]; _attr.priority = priority; } @@ -500,11 +500,11 @@ return _attr.stackSize; } - (void)setStackSize: (size_t)stackSize { - if (_running == OF_THREAD_RUNNING) + if (_running == OFThreadStateRunning) @throw [OFThreadStillRunningException exceptionWithThread: self]; _attr.stackSize = stackSize; } @@ -514,29 +514,29 @@ return _supportsSockets; } - (void)setSupportsSockets: (bool)supportsSockets { - if (_running == OF_THREAD_RUNNING) + if (_running == OFThreadStateRunning) @throw [OFThreadStillRunningException exceptionWithThread: self]; _supportsSockets = supportsSockets; } - (void)dealloc { - if (_running == OF_THREAD_RUNNING) + if (_running == OFThreadStateRunning) @throw [OFThreadStillRunningException exceptionWithThread: self]; /* * We should not be running anymore, but call detach in order to free * the resources. */ - if (_running == OF_THREAD_WAITING_FOR_JOIN) - of_thread_detach(_thread); + if (_running == OFThreadStateWaitingForJoin) + OFPlainThreadDetach(_thread); [_returnValue release]; # ifdef OF_HAVE_BLOCKS [_threadBlock release]; # endif