From b0ca26a59a978d23ef5d3776a9106f03a05be7a2 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 31 Jan 2012 23:57:21 +0000 Subject: Add missing file. git-svn-id: http://svn.drobilla.net/zix/trunk@54 df6676b4-ccc9-40e5-b5d6-7c4628a128e3 --- zix/thread.h | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 zix/thread.h diff --git a/zix/thread.h b/zix/thread.h new file mode 100644 index 0000000..05d7bdb --- /dev/null +++ b/zix/thread.h @@ -0,0 +1,112 @@ +/* + Copyright 2012 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef ZIX_THREAD_H +#define ZIX_THREAD_H + +#include +#include + +#include "zix/common.h" + +#ifdef __cplusplus +extern "C" { +#else +# include +#endif + +/** + @addtogroup zix + @{ + @name Thread + @{ +*/ + +typedef pthread_t ZixThread; + +/** + Initialize @c thread to a new thread. +*/ +static inline ZixStatus +zix_thread_create(ZixThread* thread, + size_t stack_size, + void* (*function)(void*), + void* arg); + +/** + Cancel @c thread. + + This function sends a cancellation request, but does not wait until the + thread actually exists. Use zix_thread_join() for that. +*/ +static inline ZixStatus +zix_thread_cancel(ZixThread thread); + +/** + Join @c thread (block until @c thread exits). +*/ +static inline ZixStatus +zix_thread_join(ZixThread thread, void** retval); + +static inline ZixStatus +zix_thread_create(ZixThread* thread, + size_t stack_size, + void* (*function)(void*), + void* arg) +{ + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setstacksize(&attr, stack_size); + + const int ret = pthread_create(thread, NULL, function, arg); + pthread_attr_destroy(&attr); + + if (ret == EAGAIN) { + return ZIX_STATUS_NO_MEM; + } else if (ret == EINVAL) { + return ZIX_STATUS_BAD_ARG; + } else if (ret == EPERM) { + return ZIX_STATUS_BAD_PERMS; + } else if (ret) { + return ZIX_STATUS_ERROR; + } + + return ZIX_STATUS_SUCCESS; +} + +static inline ZixStatus +zix_thread_cancel(ZixThread thread) +{ + return pthread_cancel(thread) ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS; +} + +static inline ZixStatus +zix_thread_join(ZixThread thread, void** retval) +{ + return pthread_join(thread, retval) + ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS; +} + +/** + @} + @} +*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* ZIX_THREAD_H */ -- cgit v1.2.1