diff options
-rw-r--r-- | src/xs_utils.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
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 <unistd.h> +#include <pthread.h> #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 |