This repository has been archived on 2026-04-17. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
bhlib-old/include/bh/thread.h

252 lines
5.9 KiB
C
Raw Normal View History

2024-04-14 22:32:58 +03:00
#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
2024-04-14 22:38:00 +03:00
*
2024-04-14 22:32:58 +03:00
* @brief Create new thread.
*
* @param task Thread task
2024-04-14 22:38:00 +03:00
*
2024-04-14 22:32:58 +03:00
* @return Pointer to thread
2024-04-14 22:38:00 +03:00
*
2024-04-14 22:32:58 +03:00
* @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
2024-04-14 22:38:00 +03:00
*
2024-04-14 22:32:58 +03:00
* @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
2024-04-14 22:38:00 +03:00
*
2024-04-14 22:32:58 +03:00
* @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
2024-04-14 22:38:00 +03:00
*
2024-04-14 22:32:58 +03:00
* @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
2024-04-14 22:38:00 +03:00
*
2024-04-14 22:32:58 +03:00
* @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
2024-04-14 22:38:00 +03:00
*
2024-04-14 22:32:58 +03:00
* @return 0 on success, non-zero otherwise
*
2024-04-14 22:38:00 +03:00
* @note Locking already locked mutex will block thread until mutex is
2024-04-14 22:32:58 +03:00
* 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
2024-04-14 22:38:00 +03:00
*
* @return 0 on success, positive value if mutex is locked, negative value on
2024-04-14 22:32:58 +03:00
* 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
2024-04-14 22:38:00 +03:00
*
2024-04-14 22:32:58 +03:00
* @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
2024-04-14 22:38:00 +03:00
*
2024-04-14 22:32:58 +03:00
* @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
2024-04-14 22:38:00 +03:00
*
2024-04-14 22:32:58 +03:00
* @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
2024-04-14 22:38:00 +03:00
*
2024-04-14 22:32:58 +03:00
* @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
2024-04-14 22:38:00 +03:00
*
2024-04-14 22:32:58 +03:00
* @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
2024-04-14 22:38:00 +03:00
*
2024-04-14 22:32:58 +03:00
* @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 */