From b29de8f3702759027f7f8fff5fd4d8be33074707 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Sat, 2 Mar 2019 20:22:35 +1100 Subject: [PATCH] [c-host] add platform event interface and linux support --- c-host/app.h | 11 ++++- c-host/linux/platform.c | 100 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 1 deletion(-) diff --git a/c-host/app.h b/c-host/app.h index 721d3fd3..08c8a3ca 100644 --- a/c-host/app.h +++ b/c-host/app.h @@ -35,4 +35,13 @@ typedef struct osThreadHandle osThreadHandle; typedef int (*osThreadFunction)(void * opaque); bool os_createThread(const char * name, osThreadFunction function, void * opaque, osThreadHandle ** handle); -bool os_joinThread (osThreadHandle * handle, int * resultCode); \ No newline at end of file +bool os_joinThread (osThreadHandle * handle, int * resultCode); + +// os specific event functions + +typedef struct osEventHandle osEventHandle; + +osEventHandle * os_createEvent(); +void os_freeEvent (osEventHandle * handle); +bool os_waitEvent (osEventHandle * handle); +bool os_signalEvent(osEventHandle * handle); \ No newline at end of file diff --git a/c-host/linux/platform.c b/c-host/linux/platform.c index 62c9d656..24d1ef97 100644 --- a/c-host/linux/platform.c +++ b/c-host/linux/platform.c @@ -19,6 +19,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include "app.h" #include "debug.h" +#include #include #include #include @@ -234,5 +235,104 @@ bool os_joinThread(osThreadHandle * handle, int * resultCode) *resultCode = handle->resultCode; free(handle); + return true; +} + +struct osEventHandle +{ + pthread_mutex_t mutex; + pthread_cond_t cond; + bool flag; +}; + +osEventHandle * os_createEvent() +{ + osEventHandle * handle = (osEventHandle *)calloc(sizeof(osEventHandle), 1); + if (!handle) + { + DEBUG_ERROR("Failed to allocate memory"); + return NULL; + } + + if (pthread_mutex_init(&handle->mutex, NULL) != 0) + { + DEBUG_ERROR("Failed to create the mutex"); + free(handle); + return NULL; + } + + if (pthread_cond_init(&handle->cond, NULL) != 0) + { + pthread_mutex_destroy(&handle->mutex); + free(handle); + return NULL; + } + + return handle; +} + +void os_freeEvent(osEventHandle * handle) +{ + assert(handle); + + pthread_cond_destroy (&handle->cond ); + pthread_mutex_destroy(&handle->mutex); + free(handle); +} + +bool os_waitEvent(osEventHandle * handle) +{ + assert(handle); + + if (pthread_mutex_lock(&handle->mutex) != 0) + { + DEBUG_ERROR("Failed to lock the mutex"); + return false; + } + + while(!handle->flag) + { + if (pthread_cond_wait(&handle->cond, &handle->mutex) != 0) + { + DEBUG_ERROR("Wait to wait on the condition"); + return false; + } + } + + handle->flag = false; + + if (pthread_mutex_unlock(&handle->mutex) != 0) + { + DEBUG_ERROR("Failed to unlock the mutex"); + return false; + } + + return true; +} + +bool os_signalEvent(osEventHandle * handle) +{ + assert(handle); + + if (pthread_mutex_lock(&handle->mutex) != 0) + { + DEBUG_ERROR("Failed to lock the mutex"); + return false; + } + + handle->flag = true; + + if (pthread_mutex_unlock(&handle->mutex) != 0) + { + DEBUG_ERROR("Failed to unlock the mutex"); + return false; + } + + if (pthread_cond_signal(&handle->cond) != 0) + { + DEBUG_ERROR("Failed to signal the condition"); + return false; + } + return true; } \ No newline at end of file