diff options
Diffstat (limited to 'include/bh/thread.h')
| -rw-r--r-- | include/bh/thread.h | 251 |
1 files changed, 251 insertions, 0 deletions
diff --git a/include/bh/thread.h b/include/bh/thread.h new file mode 100644 index 0000000..c8547ce --- /dev/null +++ b/include/bh/thread.h @@ -0,0 +1,251 @@ +#ifndef BHLIB_THREAD_H +#define BHLIB_THREAD_H + +#include "bh.h" + +#define BH_THREAD_CLEANUP (1 << 0) + +typedef void (*bh_thread_cb_t)(void *); +typedef struct bh_thread_s bh_thread_t; +typedef struct bh_mutex_s bh_mutex_t; +typedef struct bh_cond_s bh_cond_t; +typedef struct bh_task_s bh_task_t; +typedef struct bh_thread_pool_s bh_thread_pool_t; + +#if defined(BHLIB_USE_PTHREAD) +bh_thread_t *bh_thread_new(bh_task_t *task); + +bh_thread_pool_t *bh_thread_pool_new(size_t size); +#elif defined(BHLIB_USE_WINTHREAD) +typedef uintptr_t (__cdecl *bh_thread_begin_cb_t)(void *, + unsigned, + unsigned (__stdcall *)(void *), + void *, + unsigned, + unsigned *); + +typedef uintptr_t (__cdecl *bh_thread_end_cb_t)(unsigned); + +bh_thread_t *bh_thread_new_base(bh_task_t *task, + bh_thread_begin_cb_t begin, + bh_thread_end_cb_t end); + +bh_thread_pool_t *bh_thread_pool_new_base(size_t size, + bh_thread_begin_cb_t begin, + bh_thread_end_cb_t end); + +#define bh_thread_new(task) \ + bh_thread_new_base((task), _beginthreadex, _endthreadex) + +#define bh_thread_pool_new(size) \ + bh_thread_pool_new_base((size), _beginthreadex, _endthreadex); + +#endif + +/** + * @function bh_thread_new + * + * @brief Create new thread. + * + * @param task Thread task + * + * @return Pointer to thread + * + * @sa bh_thread_join, bh_thread_detach + */ + +/** + * @function bh_thread_pool_new + * @brief Create new thread pool. + * + * @param pool Pointer to the thread pool + * @param size Amount of threads + * + * @return Pointer to thread pool + * + * @sa bh_thread_pool_add, bh_thread_pool_join, bh_thread_pool_free + */ + +/** + * @brief Join thread. + * + * @param thread Pointer to the thread + * + * @return 0 on success, non-zero otherwise + * + * @sa bh_thread_detach + */ +int bh_thread_join(bh_thread_t *thread); + +/** + * @brief Detach thread. + * + * @param thread Pointer to the thread + * + * @return 0 on success, non-zero otherwise + * + * @sa bh_thread_join + */ +int bh_thread_detach(bh_thread_t *thread); + +/** + * @brief Create mutex. + * + * @return Pointer to the mutex + * + * @sa bh_mutex_lock, bh_mutex_try_lock, bh_mutex_destroy + */ +bh_mutex_t *bh_mutex_new(void); + +/** + * @brief Free mutex. + * + * @param mutex Pointer ot the mutex + */ +void bh_mutex_free(bh_mutex_t *mutex); + +/** + * @brief Lock mutex. + * + * @param mutex Pointer to the mutex + * + * @return 0 on success, non-zero otherwise + * + * @note Locking already locked mutex will block thread until mutex is + * released + * + * @sa bh_mutex_try_lock, bh_mutex_unlock + */ +int bh_mutex_lock(bh_mutex_t *mutex); + +/** + * @brief Try to lock mutex. + * + * @param mutex Pointer to the mutex + * + * @return 0 on success, positive value if mutex is locked, negative value on + * error + * + * @sa bh_mutex_lock, bh_mutex_unlock + */ +int bh_mutex_try_lock(bh_mutex_t *mutex); + +/** + * @brief Unlock mutex. + * + * @param mutex Pointer to the mutex + * + * @return 0 on success, non-zero otherwise + * + * @sa bh_mutex_lock, bh_mutex_try_lock + */ +int bh_mutex_unlock(bh_mutex_t *mutex); + +/** + * @brief Create condition variable. + * + * @return Pointer to the condition variable + */ +bh_cond_t *bh_cond_new(void); + +/** + * @brief Destroy condition variable. + * + * @param cond Pointer to the conditional variable + */ +void bh_cond_free(bh_cond_t *cond); + +/** + * @brief Block on conditional variable. + * + * @param cond Pointer to the condition variable + * @param mutex Pointer to the mutex + * + * @return 0 on success, non-zero otherwise + * + * @sa bh_cond_wait_for, bh_cond_signal, bh_cond_broadcast + */ +int bh_cond_wait(bh_cond_t *cond, + bh_mutex_t *mutex); + +/** + * @brief Block on conditional variable for a period of the time. + * + * @param cond Pointer to the conditional variable + * @param mutex Pointer to the mutex + * @param timeout Timeout in miliseconds + * + * @return 0 on success, positive value on timeout, negative on error + * + * @sa bh_cond_wait, bh_cond_signal, bh_cond_broadcast + */ +int bh_cond_wait_for(bh_cond_t *cond, + bh_mutex_t *mutex, + unsigned long timeout); + +/** + * @brief Unblock (notify) thread. + * + * @param cond Pointer to the condition variable + * + * @return 0 on success, non-zero otherwise + * + * @sa bh_cond_broadcast, bh_cond_wait, bh_cond_wait_for + */ +int bh_cond_signal(bh_cond_t *cond); + +/** + * @brief Unblock all threads. + * + * @param cond Pointer to the conditional variable + * + * @return 0 on success, non-zero otherwise + * + * @sa bh_cond_signal, bh_cond_wait, bh_cond_wait_for + */ +int bh_cond_broadcast(bh_cond_t *cond); + +bh_task_t *bh_task_new(void (*func)(void *), + void *data, + int flags); + +void bh_task_free(bh_task_t *task); + +void bh_task_reuse(bh_task_t *task, + void (*func)(void *), + void *data); + +int bh_task_done(bh_task_t *task); + +/** + * @brief Submit task to the thread pool. + * + * @param pool Pointer to the thread pool + * @param func Task function + * @param data Task data + * + * @return 0 on success, non-zero otherwise + * + * @sa bh_thread_pool_join + */ +int bh_thread_pool_add(bh_thread_pool_t *pool, + bh_task_t *task); + +/** + * @brief Wait until all tasks are finished. + * + * @param pool Pointer to the thread pool + * @return 0 on success, non-zero otherwise + * + * @sa bh_thread_pool_add + */ +int bh_thread_pool_wait(bh_thread_pool_t *pool); + +/** + * @brief Destroy thread pool. + * + * @param pool Pointer to the thread pool + */ +void bh_thread_pool_free(bh_thread_pool_t *pool); + +#endif /* BHLIB_THREAD_H */ |
