diff options
author | David Robillard <d@drobilla.net> | 2012-01-31 23:57:21 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2012-01-31 23:57:21 +0000 |
commit | b0ca26a59a978d23ef5d3776a9106f03a05be7a2 (patch) | |
tree | 617c3e97933273ed3015df0a8d43d6671f189ac2 /zix | |
parent | 5b3bde6f967940fb22f7160b3c9127a465a58f35 (diff) | |
download | zix-b0ca26a59a978d23ef5d3776a9106f03a05be7a2.tar.gz zix-b0ca26a59a978d23ef5d3776a9106f03a05be7a2.tar.bz2 zix-b0ca26a59a978d23ef5d3776a9106f03a05be7a2.zip |
Add missing file.
git-svn-id: http://svn.drobilla.net/zix/trunk@54 df6676b4-ccc9-40e5-b5d6-7c4628a128e3
Diffstat (limited to 'zix')
-rw-r--r-- | zix/thread.h | 112 |
1 files changed, 112 insertions, 0 deletions
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 <http://drobilla.net> + + 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 <errno.h> +#include <pthread.h> + +#include "zix/common.h" + +#ifdef __cplusplus +extern "C" { +#else +# include <stdbool.h> +#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 */ |