Index: src/OFThread.h ================================================================== --- src/OFThread.h +++ src/OFThread.h @@ -62,16 +62,16 @@ @interface OFThread: OFObject #ifdef OF_HAVE_THREADS { @private - of_thread_t _thread; - of_thread_attr_t _attr; - enum of_thread_running { - OF_THREAD_NOT_RUNNING, - OF_THREAD_RUNNING, - OF_THREAD_WAITING_FOR_JOIN + OFPlainThread _thread; + OFPlainThreadAttributes _attr; + enum OFThreadState { + OFThreadStateNotRunning, + OFThreadStateRunning, + OFThreadStateWaitingForJoin } _running; # ifndef OF_OBJFW_RUNTIME void *_pool; # endif # ifdef OF_HAVE_BLOCKS Index: src/OFThread.m ================================================================== --- src/OFThread.m +++ src/OFThread.m @@ -109,14 +109,14 @@ 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()) @throw [OFInitializationFailedException @@ -147,11 +147,11 @@ #if defined(OF_AMIGAOS) && !defined(OF_MORPHOS) && defined(OF_HAVE_SOCKETS) if (thread.supportsSockets) of_socket_deinit(); #endif - thread->_running = OF_THREAD_WAITING_FOR_JOIN; + thread->_running = OFThreadStateWaitingForJoin; [thread release]; } @synthesize name = _name; @@ -335,14 +335,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,12 +349,12 @@ } + (void)of_createMainThread { mainThread = [[OFThread alloc] init]; - mainThread->_thread = of_thread_current(); - mainThread->_running = OF_THREAD_RUNNING; + mainThread->_thread = OFCurrentPlainThread(); + mainThread->_running = OFThreadStateRunning; if (OFTLSKeySet(threadSelfKey, mainThread) != 0) @throw [OFInitializationFailedException exceptionWithClass: self]; } @@ -362,11 +362,11 @@ - (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; @@ -415,24 +415,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 +441,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 @@ -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 Index: src/mutex.h ================================================================== --- src/mutex.h +++ src/mutex.h @@ -80,11 +80,11 @@ #endif /* Spinlocks are inlined for performance. */ static OF_INLINE void -of_thread_yield(void) +OFYieldThread(void) { #if defined(OF_HAVE_SCHED_YIELD) sched_yield(); #elif defined(OF_WINDOWS) Sleep(0); @@ -130,11 +130,11 @@ for (i = 0; i < OF_SPINCOUNT; i++) if (of_spinlock_trylock(spinlock) == 0) return 0; while (of_spinlock_trylock(spinlock) == EBUSY) - of_thread_yield(); + OFYieldThread(); return 0; #elif defined(OF_HAVE_PTHREAD_SPINLOCKS) return pthread_spin_lock(spinlock); #else Index: src/once.m ================================================================== --- src/once.m +++ src/once.m @@ -49,11 +49,11 @@ of_memory_barrier(); of_atomic_int_inc(control); } else while (*control == 1) - of_thread_yield(); + OFYieldThread(); #elif defined(OF_AMIGAOS) bool run = false; /* Avoid Forbid() in case it's already done. */ if (*control == 2) Index: src/platform/amiga/thread.m ================================================================== --- src/platform/amiga/thread.m +++ src/platform/amiga/thread.m @@ -39,12 +39,12 @@ static void functionWrapper(void) { bool detached = false; - of_thread_t thread = - (of_thread_t)((struct Process *)FindTask(NULL))->pr_ExitData; + OFPlainThread thread = + (OFPlainThread)((struct Process *)FindTask(NULL))->pr_ExitData; OF_ENSURE(OFTLSKeySet(threadKey, thread) == 0); thread->function(thread->object); ObtainSemaphore(&thread->semaphore); @@ -66,21 +66,21 @@ if (detached) free(thread); } int -of_thread_attr_init(of_thread_attr_t *attr) +OFPlainThreadAttributesInit(OFPlainThreadAttributes *attr) { attr->priority = 0; attr->stackSize = 0; return 0; } int -of_thread_new(of_thread_t *thread, const char *name, void (*function)(id), - id object, const of_thread_attr_t *attr) +OFPlainThreadNew(OFPlainThread *thread, const char *name, void (*function)(id), + id object, const OFPlainThreadAttributes *attr) { OFMutableData *tags = nil; if ((*thread = calloc(1, sizeof(**thread))) == NULL) return ENOMEM; @@ -153,18 +153,18 @@ } return 0; } -of_thread_t -of_thread_current(void) +OFPlainThread +OFCurrentPlainThread(void) { return OFTLSKeyGet(threadKey); } int -of_thread_join(of_thread_t thread) +OFPlainThreadJoin(OFPlainThread thread) { ObtainSemaphore(&thread->semaphore); if (thread->done) { ReleaseSemaphore(&thread->semaphore); @@ -193,11 +193,11 @@ return 0; } int -of_thread_detach(of_thread_t thread) +OFPlainThreadDetach(OFPlainThread thread) { ObtainSemaphore(&thread->semaphore); if (thread->done) free(thread); @@ -208,8 +208,8 @@ return 0; } void -of_thread_set_name(const char *name) +OFSetThreadName(const char *name) { } Index: src/platform/posix/thread.m ================================================================== --- src/platform/posix/thread.m +++ src/platform/posix/thread.m @@ -73,11 +73,11 @@ functionWrapper(void *data) { struct thread_ctx *ctx = data; if (ctx->name != NULL) - of_thread_set_name(ctx->name); + OFSetThreadName(ctx->name); pthread_cleanup_push(free, data); ctx->function(ctx->object); @@ -84,11 +84,11 @@ pthread_cleanup_pop(1); return NULL; } int -of_thread_attr_init(of_thread_attr_t *attr) +OFPlainThreadAttributesInit(OFPlainThreadAttributes *attr) { int error; pthread_attr_t POSIXAttr; attr->priority = 0; @@ -107,12 +107,12 @@ return error; } int -of_thread_new(of_thread_t *thread, const char *name, void (*function)(id), - id object, const of_thread_attr_t *attr) +OFPlainThreadNew(OFPlainThread *thread, const char *name, void (*function)(id), + id object, const OFPlainThreadAttributes *attr) { int error = 0; pthread_attr_t POSIXAttr; bool POSIXAttrAvailable = true; @@ -182,25 +182,25 @@ return error; } int -of_thread_join(of_thread_t thread) +OFPlainThreadJoin(OFPlainThread thread) { void *ret; return pthread_join(thread, &ret); } int -of_thread_detach(of_thread_t thread) +OFPlainThreadDetach(OFPlainThread thread) { return pthread_detach(thread); } void -of_thread_set_name(const char *name) +OFSetThreadName(const char *name) { #if defined(OF_HAIKU) rename_thread(find_thread(NULL), name); #elif defined(HAVE_PTHREAD_SET_NAME_NP) pthread_set_name_np(pthread_self(), name); Index: src/platform/windows/thread.m ================================================================== --- src/platform/windows/thread.m +++ src/platform/windows/thread.m @@ -34,21 +34,21 @@ free(context); } int -of_thread_attr_init(of_thread_attr_t *attr) +OFPlainThreadAttributesInit(OFPlainThreadAttributes *attr) { attr->priority = 0; attr->stackSize = 0; return 0; } int -of_thread_new(of_thread_t *thread, const char *name, void (*function)(id), - id object, const of_thread_attr_t *attr) +OFPlainThreadNew(OFPlainThread *thread, const char *name, void (*function)(id), + id object, const OFPlainThreadAttributes *attr) { DWORD priority = THREAD_PRIORITY_NORMAL; struct thread_context *context; DWORD threadID; @@ -98,11 +98,11 @@ return 0; } int -of_thread_join(of_thread_t thread) +OFPlainThreadJoin(OFPlainThread thread) { switch (WaitForSingleObject(thread, INFINITE)) { case WAIT_OBJECT_0: CloseHandle(thread); return 0; @@ -117,16 +117,16 @@ OF_ENSURE(0); } } int -of_thread_detach(of_thread_t thread) +OFPlainThreadDetach(OFPlainThread thread) { CloseHandle(thread); return 0; } void -of_thread_set_name(const char *name) +OFSetThreadName(const char *name) { } Index: src/thread.h ================================================================== --- src/thread.h +++ src/thread.h @@ -24,14 +24,14 @@ #import "macros.h" #if defined(OF_HAVE_PTHREADS) # include -typedef pthread_t of_thread_t; +typedef pthread_t OFPlainThread; #elif defined(OF_WINDOWS) # include -typedef HANDLE of_thread_t; +typedef HANDLE OFPlainThread; #elif defined(OF_AMIGAOS) # include # include typedef struct { struct Task *task; @@ -39,36 +39,59 @@ id object; struct SignalSemaphore semaphore; struct Task *joinTask; unsigned char joinSigBit; bool detached, done; -} *of_thread_t; +} *OFPlainThread; #endif -typedef struct of_thread_attr_t { +typedef struct OFPlainThreadAttributes { float priority; size_t stackSize; -} of_thread_attr_t; +} OFPlainThreadAttributes; #if defined(OF_HAVE_PTHREADS) -# define of_thread_is_current(t) pthread_equal(t, pthread_self()) -# define of_thread_current() pthread_self() +static OF_INLINE OFPlainThread +OFCurrentPlainThread(void) +{ + return pthread_self(); +} + +static OF_INLINE bool +OFPlainThreadIsCurrent(OFPlainThread thread) +{ + return pthread_equal(thread, pthread_self()); +} #elif defined(OF_WINDOWS) -# define of_thread_is_current(t) (t == GetCurrentThread()) -# define of_thread_current() GetCurrentThread() +static OF_INLINE OFPlainThread +OFCurrentPlainThread(void) +{ + return GetCurrentThread(); +} + +static OF_INLINE bool +OFPlainThreadIsCurrent(OFPlainThread thread) +{ + return (thread == GetCurrentThread()); +} #elif defined(OF_AMIGAOS) -# define of_thread_is_current(t) (t->thread == FindTask(NULL)) -extern of_thread_t of_thread_current(void); +extern OFPlainThread OFCurrentPlainThread(void); + +static OF_INLINE bool +OFPlainThreadIsCurrent(OFPlainThread thread) +{ + return (thread->thread == FindTask(NULL)); +} #endif #ifdef __cplusplus extern "C" { #endif -extern int of_thread_attr_init(of_thread_attr_t *attr); -extern int of_thread_new(of_thread_t *thread, const char *name, - void (*function)(id), id object, const of_thread_attr_t *attr); -extern void of_thread_set_name(const char *name); -extern int of_thread_join(of_thread_t thread); -extern int of_thread_detach(of_thread_t thread); +extern int OFPlainThreadAttributesInit(OFPlainThreadAttributes *attr); +extern int OFPlainThreadNew(OFPlainThread *thread, const char *name, + void (*function)(id), id object, const OFPlainThreadAttributes *attr); +extern void OFSetThreadName(const char *name); +extern int OFPlainThreadJoin(OFPlainThread thread); +extern int OFPlainThreadDetach(OFPlainThread thread); #ifdef __cplusplus } #endif