@@ -13,10 +13,12 @@ * 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 + #import "macros.h" bool of_thread_attr_init(of_thread_attr_t *attr) { @@ -31,18 +33,30 @@ const of_thread_attr_t *attr) { *thread = CreateThread(NULL, (attr != NULL ? attr->stackSize : 0), (LPTHREAD_START_ROUTINE)function, (void *)object, 0, NULL); - if (thread == NULL) - return false; + if (thread == NULL) { + switch (GetLastError()) { + case ERROR_NOT_ENOUGH_MEMORY: + errno = ENOMEM; + return false; + case ERROR_ACCESS_DENIED: + errno = EACCES; + return false; + default: + OF_ENSURE(0); + } + } if (attr != NULL && attr->priority != 0) { DWORD priority; - if (attr->priority < -1 || attr->priority > 1) + if (attr->priority < -1 || attr->priority > 1) { + errno = EINVAL; return false; + } if (attr->priority < 0) priority = THREAD_PRIORITY_LOWEST + (1.0 + attr->priority) * (THREAD_PRIORITY_NORMAL - THREAD_PRIORITY_LOWEST); @@ -49,26 +63,34 @@ else priority = THREAD_PRIORITY_NORMAL + attr->priority * (THREAD_PRIORITY_HIGHEST - THREAD_PRIORITY_NORMAL); - if (!SetThreadPriority(*thread, priority)) - return false; + OF_ENSURE(!SetThreadPriority(*thread, priority)); } return true; } bool of_thread_join(of_thread_t thread) { - if (WaitForSingleObject(thread, INFINITE)) - return false; - - CloseHandle(thread); - - return true; + switch (WaitForSingleObject(thread, INFINITE)) { + case WAIT_OBJECT_0: + CloseHandle(thread); + return true; + case WAIT_FAILED: + switch (GetLastError()) { + case ERROR_INVALID_HANDLE: + errno = EINVAL; + return false; + default: + OF_ENSURE(0); + } + default: + OF_ENSURE(0); + } } bool of_thread_detach(of_thread_t thread) {