diff options
Diffstat (limited to 'include/bh/thread.h')
| -rw-r--r-- | include/bh/thread.h | 207 |
1 files changed, 11 insertions, 196 deletions
diff --git a/include/bh/thread.h b/include/bh/thread.h index cfc02e8..22bdf30 100644 --- a/include/bh/thread.h +++ b/include/bh/thread.h @@ -1,9 +1,9 @@ -#ifndef BHLIB_THREAD_H -#define BHLIB_THREAD_H +#ifndef BH_THREAD_H +#define BH_THREAD_H #include "bh.h" -#define BH_THREAD_CLEANUP (1 << 0) +#define BH_THREAD_CLEANUP 0x0001 typedef void (*bh_thread_cb_t)(void *); typedef struct bh_thread_s bh_thread_t; @@ -13,12 +13,12 @@ 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) +#if defined(BH_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) +#elif defined(BH_USE_WINTHREAD) #include <windows.h> #include <process.h> @@ -47,250 +47,65 @@ bh_thread_pool_t *bh_thread_pool_new_base(size_t size, #endif -/** - * @brief Create new task - * - * @param func Function - * @param data Function data - * @param flags Task flags - * - * @return Pointer to the new task - * - * @sa bh_task_free, bh_task_reuse, bh_task_done - */ bh_task_t *bh_task_new(void (*func)(void *), void *data, int flags); -/** - * @brief Free the task. - * - * @param task Pointer to the task - */ void bh_task_free(bh_task_t *task); -/** - * @brief Reuse task. - * - * @param task Pointer to the task - * @param func Function - * @param data Data - * - * @sa bh_task_free, bh_task_done - */ void bh_task_reuse(bh_task_t *task, void (*func)(void *), void *data); -/** - * @brief Check if task is done. - * - * @param task Pointer to the task - * - * @return The return value is boolean flag, indicating if the task is done. - */ int bh_task_done(bh_task_t *task); -/** - * @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); bh_semaphore_t *bh_semaphore_new(int count); + void bh_semaphore_free(bh_semaphore_t *semaphore); + int bh_semaphore_post(bh_semaphore_t *semaphore); + int bh_semaphore_wait(bh_semaphore_t *semaphore); + int bh_semaphore_wait_for(bh_semaphore_t *semaphore, unsigned long timeout); + int bh_semaphore_try_wait(bh_semaphore_t *semaphore); -/** - * @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); -/** - * @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 */ +#endif /* BH_THREAD_H */ |
