From 9170b24fee8fc6233969c1799e9b597f72310411 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Fri, 1 Mar 2019 12:54:15 +1100 Subject: [PATCH] [c-host] added linux thread support --- c-host/Makefile | 1 + c-host/linux/platform.c | 51 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/c-host/Makefile b/c-host/Makefile index d780c2e3..492d42a5 100644 --- a/c-host/Makefile +++ b/c-host/Makefile @@ -29,6 +29,7 @@ ifdef OS else CC = gcc OBJS += linux/platform.o + LIBS += -lpthread endif all: $(OBJS) $(DLLS) diff --git a/c-host/linux/platform.c b/c-host/linux/platform.c index 3c70f8c5..2322ea91 100644 --- a/c-host/linux/platform.c +++ b/c-host/linux/platform.c @@ -19,6 +19,17 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include "app.h" #include "debug.h" +#include +#include + +struct osThreadHandle +{ + const char * name; + osThreadFunction function; + void * opaque; + pthread_t handle; + int resultCode; +}; int main(int argc, char * argv[]) { @@ -42,4 +53,44 @@ bool os_shmemMmap(void **ptr) void os_shmemUnmap() { // TODO +} + +static void * threadWrapper(void * opaque) +{ + osThreadHandle * handle = (osThreadHandle *)opaque; + handle->resultCode = handle->function(handle->opaque); + return NULL; +} + +bool os_createThread(const char * name, osThreadFunction function, void * opaque, osThreadHandle ** handle) +{ + *handle = (osThreadHandle*)malloc(sizeof(osThreadHandle)); + (*handle)->name = name; + (*handle)->function = function; + (*handle)->opaque = opaque; + + if (pthread_create(&(*handle)->handle, NULL, threadWrapper, *handle) != 0) + { + DEBUG_ERROR("pthread_create failed for thread: %s", name); + free(*handle); + *handle = NULL; + return false; + } + return true; +} + +bool os_joinThread(osThreadHandle * handle, int * resultCode) +{ + if (pthread_join(handle->handle, NULL) != 0) + { + DEBUG_ERROR("pthread_join failed for thread: %s", handle->name); + free(handle); + return false; + } + + if (resultCode) + *resultCode = handle->resultCode; + + free(handle); + return true; } \ No newline at end of file