Commit 3891cb15 by Marcus Winter

base_c: changed assertions on user input into checks with error code return

parent 36e27d5f
...@@ -33,8 +33,9 @@ ...@@ -33,8 +33,9 @@
int embb_condition_wait_for(embb_condition_t* condition_var, int embb_condition_wait_for(embb_condition_t* condition_var,
embb_mutex_t* mutex, embb_mutex_t* mutex,
const embb_duration_t* duration) { const embb_duration_t* duration) {
assert(condition_var != NULL); if (condition_var == NULL || mutex == NULL) {
assert(mutex != NULL); return EMBB_ERROR;
}
embb_time_t time; embb_time_t time;
int status = embb_time_in(&time, duration); int status = embb_time_in(&time, duration);
if (status != EMBB_SUCCESS) { if (status != EMBB_SUCCESS) {
...@@ -46,27 +47,34 @@ int embb_condition_wait_for(embb_condition_t* condition_var, ...@@ -46,27 +47,34 @@ int embb_condition_wait_for(embb_condition_t* condition_var,
#ifdef EMBB_PLATFORM_THREADING_WINTHREADS #ifdef EMBB_PLATFORM_THREADING_WINTHREADS
int embb_condition_init(embb_condition_t* condition_var) { int embb_condition_init(embb_condition_t* condition_var) {
assert(condition_var != NULL); if (condition_var == NULL) {
return EMBB_ERROR;
}
InitializeConditionVariable(condition_var); InitializeConditionVariable(condition_var);
return EMBB_SUCCESS; return EMBB_SUCCESS;
} }
int embb_condition_notify_one(embb_condition_t* condition_var) { int embb_condition_notify_one(embb_condition_t* condition_var) {
assert(condition_var != NULL); if (condition_var == NULL) {
return EMBB_ERROR;
}
WakeConditionVariable(condition_var); WakeConditionVariable(condition_var);
return EMBB_SUCCESS; return EMBB_SUCCESS;
} }
int embb_condition_notify_all(embb_condition_t* condition_var) { int embb_condition_notify_all(embb_condition_t* condition_var) {
assert(condition_var != NULL); if (condition_var == NULL) {
return EMBB_ERROR;
}
WakeAllConditionVariable(condition_var); WakeAllConditionVariable(condition_var);
return EMBB_SUCCESS; return EMBB_SUCCESS;
} }
int embb_condition_wait(embb_condition_t* condition_var, int embb_condition_wait(embb_condition_t* condition_var,
embb_mutex_t* mutex) { embb_mutex_t* mutex) {
assert(condition_var != NULL); if (condition_var == NULL || mutex == NULL) {
assert(mutex != NULL); return EMBB_ERROR;
}
if (SleepConditionVariableCS(condition_var, mutex, INFINITE)) { if (SleepConditionVariableCS(condition_var, mutex, INFINITE)) {
return EMBB_SUCCESS; return EMBB_SUCCESS;
} }
...@@ -75,9 +83,9 @@ int embb_condition_wait(embb_condition_t* condition_var, ...@@ -75,9 +83,9 @@ int embb_condition_wait(embb_condition_t* condition_var,
int embb_condition_wait_until(embb_condition_t* condition_var, int embb_condition_wait_until(embb_condition_t* condition_var,
embb_mutex_t* mutex, const embb_time_t* time) { embb_mutex_t* mutex, const embb_time_t* time) {
assert(condition_var != NULL); if (condition_var == NULL || mutex == NULL || time == NULL) {
assert(mutex != NULL); return EMBB_ERROR;
assert(time != NULL); }
/* The Windows API needs a time duration, so we need to convert the given time /* The Windows API needs a time duration, so we need to convert the given time
by using the time now. */ by using the time now. */
embb_time_t now; embb_time_t now;
...@@ -103,7 +111,9 @@ int embb_condition_wait_until(embb_condition_t* condition_var, ...@@ -103,7 +111,9 @@ int embb_condition_wait_until(embb_condition_t* condition_var,
} }
int embb_condition_destroy(embb_condition_t* condition_var) { int embb_condition_destroy(embb_condition_t* condition_var) {
assert(condition_var != NULL); if (condition_var == NULL) {
return EMBB_ERROR;
}
EMBB_UNUSED_IN_RELEASE(condition_var); EMBB_UNUSED_IN_RELEASE(condition_var);
return EMBB_SUCCESS; return EMBB_SUCCESS;
} }
...@@ -113,35 +123,42 @@ int embb_condition_destroy(embb_condition_t* condition_var) { ...@@ -113,35 +123,42 @@ int embb_condition_destroy(embb_condition_t* condition_var) {
#ifdef EMBB_PLATFORM_THREADING_POSIXTHREADS #ifdef EMBB_PLATFORM_THREADING_POSIXTHREADS
int embb_condition_init(embb_condition_t* condition_var) { int embb_condition_init(embb_condition_t* condition_var) {
assert(condition_var != NULL); if (condition_var == NULL) {
return EMBB_ERROR;
}
int result = pthread_cond_init(condition_var, NULL); int result = pthread_cond_init(condition_var, NULL);
return result == 0 ? EMBB_SUCCESS : EMBB_ERROR; return result == 0 ? EMBB_SUCCESS : EMBB_ERROR;
} }
int embb_condition_notify_one(embb_condition_t* condition_var) { int embb_condition_notify_one(embb_condition_t* condition_var) {
assert(condition_var != NULL); if (condition_var == NULL) {
return EMBB_ERROR;
}
int result = pthread_cond_signal(condition_var); int result = pthread_cond_signal(condition_var);
return result == 0 ? EMBB_SUCCESS : EMBB_ERROR; return result == 0 ? EMBB_SUCCESS : EMBB_ERROR;
} }
int embb_condition_notify_all(embb_condition_t* condition_var) { int embb_condition_notify_all(embb_condition_t* condition_var) {
assert(condition_var != NULL); if (condition_var == NULL) {
return EMBB_ERROR;
}
int result = pthread_cond_broadcast(condition_var); int result = pthread_cond_broadcast(condition_var);
return result == 0 ? EMBB_SUCCESS : EMBB_ERROR; return result == 0 ? EMBB_SUCCESS : EMBB_ERROR;
} }
int embb_condition_wait(embb_condition_t* condition_var, embb_mutex_t* mutex) { int embb_condition_wait(embb_condition_t* condition_var, embb_mutex_t* mutex) {
assert(condition_var != NULL); if (condition_var == NULL || mutex == NULL) {
assert(mutex != NULL); return EMBB_ERROR;
}
int result = pthread_cond_wait(condition_var, mutex); int result = pthread_cond_wait(condition_var, mutex);
return result == 0 ? EMBB_SUCCESS : EMBB_ERROR; return result == 0 ? EMBB_SUCCESS : EMBB_ERROR;
} }
int embb_condition_wait_until(embb_condition_t* condition_var, int embb_condition_wait_until(embb_condition_t* condition_var,
embb_mutex_t* mutex, const embb_time_t* time) { embb_mutex_t* mutex, const embb_time_t* time) {
assert(condition_var != NULL); if (condition_var == NULL || mutex == NULL || time == NULL) {
assert(mutex != NULL); return EMBB_ERROR;
assert(time != NULL); }
/* Convert EMBB time to Unix time format */ /* Convert EMBB time to Unix time format */
struct timespec unix_time; struct timespec unix_time;
unix_time.tv_sec = time->seconds; unix_time.tv_sec = time->seconds;
...@@ -157,7 +174,9 @@ int embb_condition_wait_until(embb_condition_t* condition_var, ...@@ -157,7 +174,9 @@ int embb_condition_wait_until(embb_condition_t* condition_var,
} }
int embb_condition_destroy(embb_condition_t* condition_var) { int embb_condition_destroy(embb_condition_t* condition_var) {
assert(condition_var != NULL); if (condition_var == NULL) {
return EMBB_ERROR;
}
int status = pthread_cond_destroy(condition_var); int status = pthread_cond_destroy(condition_var);
if (status != 0) { if (status != 0) {
return EMBB_ERROR; return EMBB_ERROR;
......
...@@ -30,23 +30,31 @@ ...@@ -30,23 +30,31 @@
#include <assert.h> #include <assert.h>
int embb_counter_init(embb_counter_t* counter) { int embb_counter_init(embb_counter_t* counter) {
assert(counter != NULL); if (counter == NULL) {
return EMBB_ERROR;
}
embb_atomic_store_unsigned_int(&(counter->value), 0); embb_atomic_store_unsigned_int(&(counter->value), 0);
return EMBB_SUCCESS; return EMBB_SUCCESS;
} }
unsigned int embb_counter_get(embb_counter_t* counter) { unsigned int embb_counter_get(embb_counter_t* counter) {
assert(counter != NULL); if (counter == NULL) {
return EMBB_ERROR;
}
return embb_atomic_load_unsigned_int(&(counter->value)); return embb_atomic_load_unsigned_int(&(counter->value));
} }
unsigned int embb_counter_increment(embb_counter_t* counter) { unsigned int embb_counter_increment(embb_counter_t* counter) {
assert(counter != NULL); if (counter == NULL) {
return EMBB_ERROR;
}
return embb_atomic_fetch_and_add_unsigned_int(&(counter->value), 1); return embb_atomic_fetch_and_add_unsigned_int(&(counter->value), 1);
} }
unsigned int embb_counter_decrement(embb_counter_t* counter) { unsigned int embb_counter_decrement(embb_counter_t* counter) {
assert(counter != NULL); if (counter == NULL) {
return EMBB_ERROR;
}
return embb_atomic_fetch_and_add_unsigned_int(&(counter->value), return embb_atomic_fetch_and_add_unsigned_int(&(counter->value),
(unsigned int)-1); (unsigned int)-1);
} }
......
...@@ -46,7 +46,9 @@ const embb_duration_t* embb_duration_zero() { ...@@ -46,7 +46,9 @@ const embb_duration_t* embb_duration_zero() {
int embb_duration_set_nanoseconds(embb_duration_t* duration, int embb_duration_set_nanoseconds(embb_duration_t* duration,
unsigned long long nanoseconds) { unsigned long long nanoseconds) {
assert(duration != NULL); if (duration == NULL) {
return EMBB_ERROR;
}
if (nanoseconds > 0) { if (nanoseconds > 0) {
if (embb_duration_min()->nanoseconds > nanoseconds) { if (embb_duration_min()->nanoseconds > nanoseconds) {
return EMBB_UNDERFLOW; return EMBB_UNDERFLOW;
...@@ -63,7 +65,9 @@ int embb_duration_set_nanoseconds(embb_duration_t* duration, ...@@ -63,7 +65,9 @@ int embb_duration_set_nanoseconds(embb_duration_t* duration,
int embb_duration_set_microseconds(embb_duration_t* duration, int embb_duration_set_microseconds(embb_duration_t* duration,
unsigned long long microseconds) { unsigned long long microseconds) {
assert(duration != NULL); if (duration == NULL) {
return EMBB_ERROR;
}
if (microseconds > 0) { if (microseconds > 0) {
if (embb_duration_min()->nanoseconds > microseconds*1000) { if (embb_duration_min()->nanoseconds > microseconds*1000) {
return EMBB_UNDERFLOW; return EMBB_UNDERFLOW;
...@@ -80,7 +84,9 @@ int embb_duration_set_microseconds(embb_duration_t* duration, ...@@ -80,7 +84,9 @@ int embb_duration_set_microseconds(embb_duration_t* duration,
int embb_duration_set_milliseconds(embb_duration_t* duration, int embb_duration_set_milliseconds(embb_duration_t* duration,
unsigned long long milliseconds) { unsigned long long milliseconds) {
assert(duration != NULL); if (duration == NULL) {
return EMBB_ERROR;
}
if (milliseconds > 0) { if (milliseconds > 0) {
if (embb_duration_min()->nanoseconds > milliseconds*1000000) { if (embb_duration_min()->nanoseconds > milliseconds*1000000) {
return EMBB_UNDERFLOW; return EMBB_UNDERFLOW;
...@@ -97,7 +103,9 @@ int embb_duration_set_milliseconds(embb_duration_t* duration, ...@@ -97,7 +103,9 @@ int embb_duration_set_milliseconds(embb_duration_t* duration,
int embb_duration_set_seconds(embb_duration_t* duration, int embb_duration_set_seconds(embb_duration_t* duration,
unsigned long long seconds) { unsigned long long seconds) {
assert(duration != NULL); if (duration == NULL) {
return EMBB_ERROR;
}
if (seconds > 0) { if (seconds > 0) {
if (embb_duration_min()->nanoseconds > seconds*1000000000) { if (embb_duration_min()->nanoseconds > seconds*1000000000) {
return EMBB_UNDERFLOW; return EMBB_UNDERFLOW;
...@@ -113,8 +121,9 @@ int embb_duration_set_seconds(embb_duration_t* duration, ...@@ -113,8 +121,9 @@ int embb_duration_set_seconds(embb_duration_t* duration,
} }
int embb_duration_add(embb_duration_t* lhs, const embb_duration_t* rhs) { int embb_duration_add(embb_duration_t* lhs, const embb_duration_t* rhs) {
assert(lhs != NULL); if (lhs == NULL || rhs == NULL) {
assert(rhs != NULL); return EMBB_ERROR;
}
int carry = (int)((lhs->nanoseconds + rhs->nanoseconds) / 1000000000); int carry = (int)((lhs->nanoseconds + rhs->nanoseconds) / 1000000000);
if (lhs->seconds + rhs->seconds + carry > EMBB_DURATION_MAX_SECONDS) { if (lhs->seconds + rhs->seconds + carry > EMBB_DURATION_MAX_SECONDS) {
return EMBB_OVERFLOW; return EMBB_OVERFLOW;
...@@ -126,8 +135,9 @@ int embb_duration_add(embb_duration_t* lhs, const embb_duration_t* rhs) { ...@@ -126,8 +135,9 @@ int embb_duration_add(embb_duration_t* lhs, const embb_duration_t* rhs) {
int embb_duration_as_nanoseconds(const embb_duration_t* duration, int embb_duration_as_nanoseconds(const embb_duration_t* duration,
unsigned long long* nanoseconds) { unsigned long long* nanoseconds) {
assert(duration != NULL); if (duration == NULL || nanoseconds == NULL) {
assert(nanoseconds != NULL); return EMBB_ERROR;
}
if (duration->seconds*1000000000 + duration->nanoseconds > ULLONG_MAX) { if (duration->seconds*1000000000 + duration->nanoseconds > ULLONG_MAX) {
return EMBB_OVERFLOW; return EMBB_OVERFLOW;
} }
...@@ -137,8 +147,9 @@ int embb_duration_as_nanoseconds(const embb_duration_t* duration, ...@@ -137,8 +147,9 @@ int embb_duration_as_nanoseconds(const embb_duration_t* duration,
int embb_duration_as_microseconds(const embb_duration_t* duration, int embb_duration_as_microseconds(const embb_duration_t* duration,
unsigned long long* microseconds) { unsigned long long* microseconds) {
assert(duration != NULL); if (duration == NULL || microseconds == NULL) {
assert(microseconds != NULL); return EMBB_ERROR;
}
if (duration->nanoseconds % 1000 > 0) { if (duration->nanoseconds % 1000 > 0) {
return EMBB_UNDERFLOW; return EMBB_UNDERFLOW;
} }
...@@ -151,8 +162,9 @@ int embb_duration_as_microseconds(const embb_duration_t* duration, ...@@ -151,8 +162,9 @@ int embb_duration_as_microseconds(const embb_duration_t* duration,
int embb_duration_as_milliseconds(const embb_duration_t* duration, int embb_duration_as_milliseconds(const embb_duration_t* duration,
unsigned long long* milliseconds) { unsigned long long* milliseconds) {
assert(duration != NULL); if (duration == NULL || milliseconds == NULL) {
assert(milliseconds != NULL); return EMBB_ERROR;
}
if (duration->nanoseconds % 1000000 > 0) { if (duration->nanoseconds % 1000000 > 0) {
return EMBB_UNDERFLOW; return EMBB_UNDERFLOW;
} }
...@@ -165,8 +177,9 @@ int embb_duration_as_milliseconds(const embb_duration_t* duration, ...@@ -165,8 +177,9 @@ int embb_duration_as_milliseconds(const embb_duration_t* duration,
int embb_duration_as_seconds(const embb_duration_t* duration, int embb_duration_as_seconds(const embb_duration_t* duration,
unsigned long long* seconds) { unsigned long long* seconds) {
assert(duration != NULL); if (duration == NULL || seconds == NULL) {
assert(seconds != NULL); return EMBB_ERROR;
}
if (duration->nanoseconds % 1000000000 > 0) { if (duration->nanoseconds % 1000000000 > 0) {
return EMBB_UNDERFLOW; return EMBB_UNDERFLOW;
} }
...@@ -180,11 +193,12 @@ int embb_duration_as_seconds(const embb_duration_t* duration, ...@@ -180,11 +193,12 @@ int embb_duration_as_seconds(const embb_duration_t* duration,
int embb_duration_compare(const embb_duration_t* lhs, int embb_duration_compare(const embb_duration_t* lhs,
const embb_duration_t* rhs) { const embb_duration_t* rhs) {
assert(lhs != NULL); if (lhs == NULL || rhs == NULL) {
assert(rhs != NULL); return EMBB_ERROR;
assert(lhs->nanoseconds < 1000000000); }
assert(rhs->nanoseconds < 1000000000); if (lhs->nanoseconds >= 1000000000 || rhs->nanoseconds >= 1000000000) {
return EMBB_ERROR;
}
if (lhs->seconds > rhs->seconds) { if (lhs->seconds > rhs->seconds) {
return 1; return 1;
} else if (lhs->seconds < rhs->seconds) { } else if (lhs->seconds < rhs->seconds) {
......
...@@ -62,7 +62,9 @@ void* embb_alloc(size_t bytes) { ...@@ -62,7 +62,9 @@ void* embb_alloc(size_t bytes) {
} }
void embb_free(void * ptr) { void embb_free(void * ptr) {
assert(ptr != NULL); if (ptr == NULL) {
return;
}
size_t * alloc_type = (size_t*)ptr - 1; size_t * alloc_type = (size_t*)ptr - 1;
size_t * bytes_allocated = (size_t*)ptr - 2; size_t * bytes_allocated = (size_t*)ptr - 2;
...@@ -134,7 +136,10 @@ void* embb_alloc_aligned(size_t alignment, size_t size) { ...@@ -134,7 +136,10 @@ void* embb_alloc_aligned(size_t alignment, size_t size) {
} }
void embb_free_aligned(void* ptr) { void embb_free_aligned(void* ptr) {
assert(ptr != NULL); if (ptr == NULL) {
return;
}
size_t* ptr_conv = (size_t*)ptr; size_t* ptr_conv = (size_t*)ptr;
// If embb_free_aligned is called, the memory block should have been allocated // If embb_free_aligned is called, the memory block should have been allocated
...@@ -160,7 +165,10 @@ void * embb_alloc(size_t bytes) { ...@@ -160,7 +165,10 @@ void * embb_alloc(size_t bytes) {
} }
void embb_free(void * ptr) { void embb_free(void * ptr) {
assert(ptr != NULL); if (ptr == NULL) {
return;
}
free(ptr); free(ptr);
} }
...@@ -193,6 +201,10 @@ void *embb_alloc_aligned(size_t alignment, size_t size) { ...@@ -193,6 +201,10 @@ void *embb_alloc_aligned(size_t alignment, size_t size) {
} }
void embb_free_aligned(void* ptr) { void embb_free_aligned(void* ptr) {
if (ptr == NULL) {
return;
}
#ifdef EMBB_PLATFORM_COMPILER_MSVC #ifdef EMBB_PLATFORM_COMPILER_MSVC
_aligned_free(ptr); _aligned_free(ptr);
#else #else
......
...@@ -33,6 +33,9 @@ ...@@ -33,6 +33,9 @@
#ifdef EMBB_PLATFORM_THREADING_WINTHREADS #ifdef EMBB_PLATFORM_THREADING_WINTHREADS
int embb_mutex_init(embb_mutex_t* mutex, int type) { int embb_mutex_init(embb_mutex_t* mutex, int type) {
if (NULL == mutex) {
return EMBB_ERROR;
}
/* Critical sections in Windows are always recursive */ /* Critical sections in Windows are always recursive */
InitializeCriticalSection(mutex); InitializeCriticalSection(mutex);
EMBB_UNUSED(type); EMBB_UNUSED(type);
...@@ -40,11 +43,17 @@ int embb_mutex_init(embb_mutex_t* mutex, int type) { ...@@ -40,11 +43,17 @@ int embb_mutex_init(embb_mutex_t* mutex, int type) {
} }
int embb_mutex_lock(embb_mutex_t* mutex) { int embb_mutex_lock(embb_mutex_t* mutex) {
if (NULL == mutex) {
return EMBB_ERROR;
}
EnterCriticalSection(mutex); EnterCriticalSection(mutex);
return EMBB_SUCCESS; return EMBB_SUCCESS;
} }
int embb_mutex_try_lock(embb_mutex_t* mutex) { int embb_mutex_try_lock(embb_mutex_t* mutex) {
if (NULL == mutex) {
return EMBB_ERROR;
}
BOOL success; BOOL success;
success = TryEnterCriticalSection(mutex); success = TryEnterCriticalSection(mutex);
if (success == FALSE) return EMBB_ERROR; if (success == FALSE) return EMBB_ERROR;
...@@ -52,11 +61,17 @@ int embb_mutex_try_lock(embb_mutex_t* mutex) { ...@@ -52,11 +61,17 @@ int embb_mutex_try_lock(embb_mutex_t* mutex) {
} }
int embb_mutex_unlock(embb_mutex_t* mutex) { int embb_mutex_unlock(embb_mutex_t* mutex) {
if (NULL == mutex) {
return EMBB_ERROR;
}
LeaveCriticalSection(mutex); LeaveCriticalSection(mutex);
return EMBB_SUCCESS; return EMBB_SUCCESS;
} }
void embb_mutex_destroy(embb_mutex_t* mutex) { void embb_mutex_destroy(embb_mutex_t* mutex) {
if (NULL == mutex) {
return;
}
DeleteCriticalSection(mutex); DeleteCriticalSection(mutex);
} }
...@@ -65,6 +80,9 @@ void embb_mutex_destroy(embb_mutex_t* mutex) { ...@@ -65,6 +80,9 @@ void embb_mutex_destroy(embb_mutex_t* mutex) {
#ifdef EMBB_PLATFORM_THREADING_POSIXTHREADS #ifdef EMBB_PLATFORM_THREADING_POSIXTHREADS
int embb_mutex_init(embb_mutex_t* mutex, int type) { int embb_mutex_init(embb_mutex_t* mutex, int type) {
if (NULL == mutex) {
return EMBB_ERROR;
}
if (type == EMBB_MUTEX_PLAIN) { if (type == EMBB_MUTEX_PLAIN) {
if (pthread_mutex_init(mutex, NULL) != 0) return EMBB_ERROR; if (pthread_mutex_init(mutex, NULL) != 0) return EMBB_ERROR;
} else { } else {
...@@ -85,6 +103,9 @@ int embb_mutex_init(embb_mutex_t* mutex, int type) { ...@@ -85,6 +103,9 @@ int embb_mutex_init(embb_mutex_t* mutex, int type) {
} }
int embb_mutex_lock(embb_mutex_t* mutex) { int embb_mutex_lock(embb_mutex_t* mutex) {
if (NULL == mutex) {
return EMBB_ERROR;
}
int result = pthread_mutex_lock(mutex); int result = pthread_mutex_lock(mutex);
if (result != 0) { if (result != 0) {
return EMBB_ERROR; return EMBB_ERROR;
...@@ -93,6 +114,9 @@ int embb_mutex_lock(embb_mutex_t* mutex) { ...@@ -93,6 +114,9 @@ int embb_mutex_lock(embb_mutex_t* mutex) {
} }
int embb_mutex_try_lock(embb_mutex_t* mutex) { int embb_mutex_try_lock(embb_mutex_t* mutex) {
if (NULL == mutex) {
return EMBB_ERROR;
}
int result = pthread_mutex_trylock(mutex); int result = pthread_mutex_trylock(mutex);
if (result == 0) { if (result == 0) {
return EMBB_SUCCESS; return EMBB_SUCCESS;
...@@ -104,6 +128,9 @@ int embb_mutex_try_lock(embb_mutex_t* mutex) { ...@@ -104,6 +128,9 @@ int embb_mutex_try_lock(embb_mutex_t* mutex) {
} }
int embb_mutex_unlock(embb_mutex_t* mutex) { int embb_mutex_unlock(embb_mutex_t* mutex) {
if (NULL == mutex) {
return EMBB_ERROR;
}
int result = pthread_mutex_unlock(mutex); int result = pthread_mutex_unlock(mutex);
if (result != 0) { if (result != 0) {
return EMBB_ERROR; return EMBB_ERROR;
...@@ -112,12 +139,18 @@ int embb_mutex_unlock(embb_mutex_t* mutex) { ...@@ -112,12 +139,18 @@ int embb_mutex_unlock(embb_mutex_t* mutex) {
} }
void embb_mutex_destroy(embb_mutex_t* mutex) { void embb_mutex_destroy(embb_mutex_t* mutex) {
if (NULL == mutex) {
return;
}
pthread_mutex_destroy(mutex); pthread_mutex_destroy(mutex);
} }
#endif /* EMBB_PLATFORM_THREADING_POSIXTHREADS */ #endif /* EMBB_PLATFORM_THREADING_POSIXTHREADS */
int embb_spin_init(embb_spinlock_t* spinlock) { int embb_spin_init(embb_spinlock_t* spinlock) {
if (NULL == spinlock) {
return EMBB_ERROR;
}
// For now, store the initial value. In the future will use atomic init // For now, store the initial value. In the future will use atomic init
// function (as soon as available). // function (as soon as available).
embb_atomic_store_int(&spinlock->atomic_spin_variable_, 0); embb_atomic_store_int(&spinlock->atomic_spin_variable_, 0);
...@@ -125,6 +158,9 @@ int embb_spin_init(embb_spinlock_t* spinlock) { ...@@ -125,6 +158,9 @@ int embb_spin_init(embb_spinlock_t* spinlock) {
} }
int embb_spin_lock(embb_spinlock_t* spinlock) { int embb_spin_lock(embb_spinlock_t* spinlock) {
if (NULL == spinlock) {
return EMBB_ERROR;
}
int expected = 0; int expected = 0;
int spins = 1; int spins = 1;
...@@ -143,6 +179,9 @@ int embb_spin_lock(embb_spinlock_t* spinlock) { ...@@ -143,6 +179,9 @@ int embb_spin_lock(embb_spinlock_t* spinlock) {
int embb_spin_try_lock(embb_spinlock_t* spinlock, int embb_spin_try_lock(embb_spinlock_t* spinlock,
unsigned int max_number_spins) { unsigned int max_number_spins) {
if (NULL == spinlock) {
return EMBB_ERROR;
}
if (max_number_spins == 0) if (max_number_spins == 0)
return EMBB_BUSY; return EMBB_BUSY;
...@@ -161,6 +200,9 @@ int embb_spin_try_lock(embb_spinlock_t* spinlock, ...@@ -161,6 +200,9 @@ int embb_spin_try_lock(embb_spinlock_t* spinlock,
} }
int embb_spin_unlock(embb_spinlock_t* spinlock) { int embb_spin_unlock(embb_spinlock_t* spinlock) {
if (NULL == spinlock) {
return EMBB_ERROR;
}
int expected = 1; int expected = 1;
return embb_atomic_compare_and_swap_int(&spinlock->atomic_spin_variable_, return embb_atomic_compare_and_swap_int(&spinlock->atomic_spin_variable_,
&expected, 0) ? &expected, 0) ?
......
...@@ -80,7 +80,9 @@ void embb_thread_yield() { ...@@ -80,7 +80,9 @@ void embb_thread_yield() {
int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set, int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set,
embb_thread_start_t func, void *arg) { 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*) thread->embb_internal_arg = (embb_internal_thread_arg_t*)
embb_alloc(sizeof(embb_internal_thread_arg_t)); embb_alloc(sizeof(embb_internal_thread_arg_t));
if (thread->embb_internal_arg == NULL) return EMBB_NOMEM; if (thread->embb_internal_arg == NULL) return EMBB_NOMEM;
...@@ -118,6 +120,9 @@ int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set, ...@@ -118,6 +120,9 @@ 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 embb_thread_join(embb_thread_t* thread, int* result_code) {
if (thread == NULL) {
return EMBB_ERROR;
}
BOOL success; BOOL success;
DWORD result; DWORD result;
result = WaitForSingleObject(thread->embb_internal_handle, INFINITE); result = WaitForSingleObject(thread->embb_internal_handle, INFINITE);
...@@ -143,6 +148,9 @@ int embb_thread_join(embb_thread_t* thread, int* result_code) { ...@@ -143,6 +148,9 @@ int embb_thread_join(embb_thread_t* thread, int* result_code) {
} }
int embb_thread_equal(const embb_thread_t* lhs, const embb_thread_t* rhs) { int embb_thread_equal(const embb_thread_t* lhs, const embb_thread_t* rhs) {
if (lhs == NULL || rhs == NULL) {
return 0;
}
embb_thread_id_t idLhs = GetThreadId(lhs->embb_internal_handle); embb_thread_id_t idLhs = GetThreadId(lhs->embb_internal_handle);
embb_thread_id_t idRhs = GetThreadId(rhs->embb_internal_handle); embb_thread_id_t idRhs = GetThreadId(rhs->embb_internal_handle);
if (idLhs == idRhs) { if (idLhs == idRhs) {
...@@ -203,6 +211,9 @@ void embb_thread_yield() { ...@@ -203,6 +211,9 @@ void embb_thread_yield() {
int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set, int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set,
embb_thread_start_t func, void* arg) { embb_thread_start_t func, void* arg) {
if (thread == NULL) {
return EMBB_ERROR;
}
pthread_attr_t attr; /* Used to set thread affinities */ pthread_attr_t attr; /* Used to set thread affinities */
int status = pthread_attr_init(&attr); int status = pthread_attr_init(&attr);
if (status != 0) return EMBB_ERROR; if (status != 0) return EMBB_ERROR;
...@@ -250,6 +261,9 @@ int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set, ...@@ -250,6 +261,9 @@ 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 embb_thread_join(embb_thread_t* thread, int *result_code) {
if (thread == NULL) {
return EMBB_ERROR;
}
int status = 0; int status = 0;
status = pthread_join(thread->embb_internal_handle, NULL); status = pthread_join(thread->embb_internal_handle, NULL);
if (result_code != NULL) { if (result_code != NULL) {
...@@ -263,6 +277,9 @@ int embb_thread_join(embb_thread_t* thread, int *result_code) { ...@@ -263,6 +277,9 @@ int embb_thread_join(embb_thread_t* thread, int *result_code) {
} }
int embb_thread_equal(const embb_thread_t* lhs, const embb_thread_t* rhs) { int embb_thread_equal(const embb_thread_t* lhs, const embb_thread_t* rhs) {
if (lhs == NULL || rhs == NULL) {
return 0;
}
return pthread_equal(lhs->embb_internal_handle, rhs->embb_internal_handle); return pthread_equal(lhs->embb_internal_handle, rhs->embb_internal_handle);
} }
......
...@@ -32,7 +32,9 @@ ...@@ -32,7 +32,9 @@
#include <assert.h> #include <assert.h>
int embb_tss_create(embb_tss_t* tss) { int embb_tss_create(embb_tss_t* tss) {
assert(tss != NULL); if (tss == NULL) {
return EMBB_ERROR;
}
tss->size = embb_thread_get_max_count(); tss->size = embb_thread_get_max_count();
tss->values = (void**) embb_alloc_cache_aligned(tss->size * sizeof(void*)); tss->values = (void**) embb_alloc_cache_aligned(tss->size * sizeof(void*));
if (tss->values == NULL) { if (tss->values == NULL) {
...@@ -45,7 +47,9 @@ int embb_tss_create(embb_tss_t* tss) { ...@@ -45,7 +47,9 @@ int embb_tss_create(embb_tss_t* tss) {
} }
int embb_tss_set(embb_tss_t* tss, void* value) { int embb_tss_set(embb_tss_t* tss, void* value) {
assert(tss != NULL); if (tss == NULL) {
return EMBB_ERROR;
}
unsigned int index = 0; unsigned int index = 0;
int status = embb_internal_thread_index(&index); int status = embb_internal_thread_index(&index);
if ((status != EMBB_SUCCESS) || (index >= tss->size)) { if ((status != EMBB_SUCCESS) || (index >= tss->size)) {
...@@ -56,8 +60,12 @@ int embb_tss_set(embb_tss_t* tss, void* value) { ...@@ -56,8 +60,12 @@ int embb_tss_set(embb_tss_t* tss, void* value) {
} }
void* embb_tss_get(const embb_tss_t* tss) { void* embb_tss_get(const embb_tss_t* tss) {
assert(tss != NULL); if (tss == NULL) {
assert(tss->values != NULL); return NULL;
}
if (tss->values == NULL) {
return NULL;
}
unsigned int index = 0; unsigned int index = 0;
int status = embb_internal_thread_index(&index); int status = embb_internal_thread_index(&index);
if ((status != EMBB_SUCCESS) || (index >= tss->size)) { if ((status != EMBB_SUCCESS) || (index >= tss->size)) {
...@@ -67,6 +75,8 @@ void* embb_tss_get(const embb_tss_t* tss) { ...@@ -67,6 +75,8 @@ void* embb_tss_get(const embb_tss_t* tss) {
} }
void embb_tss_delete(embb_tss_t* tss) { void embb_tss_delete(embb_tss_t* tss) {
assert(tss != NULL); if (tss == NULL) {
return;
}
embb_free_aligned(tss->values); embb_free_aligned(tss->values);
} }
...@@ -33,10 +33,12 @@ void embb_time_now(embb_time_t* time) { ...@@ -33,10 +33,12 @@ void embb_time_now(embb_time_t* time) {
} }
int embb_time_compare(const embb_time_t* lhs, const embb_time_t* rhs) { int embb_time_compare(const embb_time_t* lhs, const embb_time_t* rhs) {
assert(lhs != NULL); if (lhs == NULL || rhs == NULL) {
assert(rhs != NULL); return EMBB_ERROR;
assert(lhs->nanoseconds < 1000000000); }
assert(rhs->nanoseconds < 1000000000); if (lhs->nanoseconds >= 1000000000 || rhs->nanoseconds >= 1000000000) {
return EMBB_ERROR;
}
if (lhs->seconds > rhs->seconds) { if (lhs->seconds > rhs->seconds) {
return 1; return 1;
...@@ -56,8 +58,9 @@ int embb_time_compare(const embb_time_t* lhs, const embb_time_t* rhs) { ...@@ -56,8 +58,9 @@ int embb_time_compare(const embb_time_t* lhs, const embb_time_t* rhs) {
#ifdef EMBB_PLATFORM_THREADING_WINTHREADS #ifdef EMBB_PLATFORM_THREADING_WINTHREADS
int embb_time_in(embb_time_t* time, const embb_duration_t* duration) { int embb_time_in(embb_time_t* time, const embb_duration_t* duration) {
assert(time != NULL); if (time == NULL || duration == NULL) {
assert(duration != NULL); return EMBB_ERROR;
}
/* Get system time */ /* Get system time */
SYSTEMTIME system_time; SYSTEMTIME system_time;
GetLocalTime(&system_time); GetLocalTime(&system_time);
...@@ -87,8 +90,9 @@ int embb_time_in(embb_time_t* time, const embb_duration_t* duration) { ...@@ -87,8 +90,9 @@ int embb_time_in(embb_time_t* time, const embb_duration_t* duration) {
#ifdef EMBB_PLATFORM_THREADING_POSIXTHREADS #ifdef EMBB_PLATFORM_THREADING_POSIXTHREADS
int embb_time_in(embb_time_t* time, const embb_duration_t* duration) { int embb_time_in(embb_time_t* time, const embb_duration_t* duration) {
assert(time != NULL); if (time == NULL || duration == NULL) {
assert(duration != NULL); return EMBB_ERROR;
}
struct timespec unix_time; struct timespec unix_time;
clock_gettime(CLOCK_REALTIME, &unix_time); clock_gettime(CLOCK_REALTIME, &unix_time);
time->seconds = unix_time.tv_sec; time->seconds = unix_time.tv_sec;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment