diff options
author | Martin Sustrik <sustrik@250bpm.com> | 2012-02-16 10:09:19 +0900 |
---|---|---|
committer | Martin Sustrik <sustrik@250bpm.com> | 2012-02-16 10:09:19 +0900 |
commit | 891809e1074d16afa733165a164d0225937e1af9 (patch) | |
tree | 525c0359ce9c97b87f72da4c60f9a4eaf672e7c5 /src | |
parent | 97d0cf95d668a45646007239a48778b71fca9254 (diff) |
xs_thread_create and xs_thread_join implemented for POSIX platforms
Signed-off-by: Martin Sustrik <sustrik@250bpm.com>
Diffstat (limited to 'src')
-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 |