From 891809e1074d16afa733165a164d0225937e1af9 Mon Sep 17 00:00:00 2001 From: Martin Sustrik Date: Thu, 16 Feb 2012 10:09:19 +0900 Subject: xs_thread_create and xs_thread_join implemented for POSIX platforms Signed-off-by: Martin Sustrik --- src/xs_utils.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/xs_utils.cpp b/src/xs_utils.cpp index 976f540..54012c9 100644 --- a/src/xs_utils.cpp +++ b/src/xs_utils.cpp @@ -31,6 +31,7 @@ #if !defined XS_HAVE_WINDOWS #include +#include #else #include "windows.hpp" #endif @@ -101,4 +102,42 @@ void xs_thread_join (void *thread_) free (arg); } +#else + +struct arg_t +{ + pthread_t handle; + void (*fn) (void *arg); + void *arg; +}; + +extern "C" +{ + static void *thread_routine (void *arg_) + { + arg_t *arg = (arg_t*) arg_; + arg->fn (arg->arg); + return NULL; + } +} + +void *xs_thread_create (void (*fn_) (void *arg_), void *arg_) +{ + arg_t *arg = (arg_t*) malloc (sizeof (arg_t)); + alloc_assert (arg); + arg->fn = fn_; + arg->arg = arg_; + int rc = pthread_create (&arg->handle, NULL, thread_routine, (void*) arg); + posix_assert (rc); + return (void*) arg; +} + +void xs_thread_join (void *thread_) +{ + arg_t *arg = (arg_t*) thread_; + int rc = pthread_join (arg->handle, NULL); + posix_assert (rc); + free (arg); +} + #endif -- cgit v1.2.3