@@ -12,10 +12,13 @@ * 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 +#include #include #include #include @@ -72,12 +75,14 @@ of_thread_new(of_thread_t *thread, void (*function)(id), id object, const of_thread_attr_t *attr) { OFMutableData *tags = nil; - if ((*thread = calloc(1, sizeof(**thread))) == NULL) + if ((*thread = calloc(1, sizeof(**thread))) == NULL) { + errno = ENOMEM; return false; + } @try { (*thread)->function = function; (*thread)->object = object; InitSemaphore(&(*thread)->semaphore); @@ -108,12 +113,14 @@ ADD_TAG(NP_CloseInput, FALSE) ADD_TAG(NP_CloseOutput, FALSE) ADD_TAG(NP_CloseError, FALSE) if (attr != NULL && attr->priority != 0) { - if (attr->priority < 1 || attr->priority > 1) + if (attr->priority < 1 || attr->priority > 1) { + errno = EINVAL; return false; + } /* * -1 should be -128 (lowest possible priority) while * +1 should be +127 (highest possible priority). */ @@ -131,10 +138,11 @@ #undef ADD_TAG (*thread)->task = (struct Task *)CreateNewProc(tags.items); if ((*thread)->task == NULL) { free(*thread); + errno = EAGAIN; return false; } } @catch (id e) { free(*thread); @throw e; @@ -152,37 +160,39 @@ } bool of_thread_join(of_thread_t thread) { - bool ret; - ObtainSemaphore(&thread->semaphore); @try { if (thread->done) { free(thread); return true; } - if (thread->detached || thread->joinTask != NULL) + if (thread->detached || thread->joinTask != NULL) { + errno = EINVAL; return false; + } - if ((thread->joinSigBit = AllocSignal(-1)) == -1) + if ((thread->joinSigBit = AllocSignal(-1)) == -1) { + errno = EAGAIN; return false; + } thread->joinTask = FindTask(NULL); } @finally { ReleaseSemaphore(&thread->semaphore); } - Wait(1 << thread->joinSigBit); + Wait(1ul << thread->joinSigBit); FreeSignal(thread->joinSigBit); - ret = thread->done; + assert(thread->done); free(thread); - return ret; + return true; } bool of_thread_detach(of_thread_t thread) {