diff --git a/base_c/src/thread.c b/base_c/src/thread.c index 01240db..dc26db5 100644 --- a/base_c/src/thread.c +++ b/base_c/src/thread.c @@ -80,7 +80,7 @@ void embb_thread_yield() { int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set, embb_thread_start_t func, void *arg) { - assert(thread != NULL); + if (thread == NULL) return EMBB_ERROR; thread->embb_internal_arg = (embb_internal_thread_arg_t*) embb_alloc(sizeof(embb_internal_thread_arg_t)); if (thread->embb_internal_arg == NULL) { @@ -98,6 +98,8 @@ int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set, 0, /* no creation arguments */ 0); /* no system thread ID */ if (thread->embb_internal_handle == NULL) { + embb_free(thread->embb_internal_arg); + thread->embb_internal_arg = NULL; return EMBB_ERROR; } @@ -121,6 +123,7 @@ int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set, } int embb_thread_join(embb_thread_t* thread, int* result_code) { + if (thread == NULL) return EMBB_ERROR; BOOL success; DWORD result; result = WaitForSingleObject(thread->embb_internal_handle, INFINITE); @@ -206,6 +209,7 @@ void embb_thread_yield() { int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set, embb_thread_start_t func, void* arg) { + if (thread == NULL) return EMBB_ERROR; pthread_attr_t attr; /* Used to set thread affinities */ int status = pthread_attr_init(&attr); if (status != 0) return EMBB_ERROR; @@ -226,7 +230,11 @@ int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set, } } status = pthread_attr_setaffinity_np(&attr, sizeof(cpuset), &cpuset); - if (status != 0) return EMBB_ERROR; + if (status != 0) { + thread->embb_internal_arg = NULL; + thread->embb_internal_handle = NULL; + return EMBB_ERROR; + } #else embb_log_write("base_c", EMBB_LOG_LEVEL_WARNING, "Could not set thread " "affinity, since no implementation available!\n"); @@ -259,11 +267,14 @@ int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set, int embb_thread_join(embb_thread_t* thread, int *result_code) { int status = 0; + if (thread == NULL) return EMBB_ERROR; status = pthread_join(thread->embb_internal_handle, NULL); - if (result_code != NULL) { - *result_code = thread->embb_internal_arg->result; + if (thread->embb_internal_arg != NULL) { + if (result_code != NULL) { + *result_code = thread->embb_internal_arg->result; + } + embb_free(thread->embb_internal_arg); } - embb_free(thread->embb_internal_arg); if (status != 0) { return EMBB_ERROR; }