Add thread support

This commit is contained in:
2024-04-14 22:32:58 +03:00
parent d6c396231f
commit a645a201d8
9 changed files with 837 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
#ifndef BHLIB_H
#define BHLIB_H
#include <bh/config.h>
#include <stddef.h>
#define BH_INT_TO_PTR(x) \

7
include/bh/config.in Normal file
View File

@@ -0,0 +1,7 @@
#ifndef BHLIB_CONFIG_H
#define BHLIB_CONFIG_H
#cmakedefine BHLIB_USE_WINTHREAD
#cmakedefine BHLIB_USE_PTHREAD
#endif /* BHLIB_CONFIG_H */

View File

@@ -0,0 +1,45 @@
#ifndef BHLIB_INTERNAL_THREAD_H
#define BHLIB_INTERNAL_THREAD_H
#include <bh/thread.h>
#include "queue.h"
#define BH_THREAD_DONE (1 << 8)
#if defined(BHLIB_USE_PTHREAD)
#include "thread_posix.h"
#elif defined(BHLIB_USE_WINTHREAD)
#include "thread_win.h"
#else
#include "thread_null.h"
#endif
struct bh_task_s
{
void (*func)(void *);
void *data;
int flags;
};
struct bh_thread_pool_s
{
bh_thread_t *threads;
bh_queue_t tasks;
bh_mutex_t lock;
bh_cond_t new_task;
bh_cond_t done_task;
size_t size;
size_t active;
int shutdown;
};
void bh_task_init(bh_task_t *task,
void (*func)(void *),
void *data,
int flags);
void bh_task_destroy(bh_task_t *task);
void bh_thread_pool_worker(void *arg);
#endif /* BHLIB_INTERNAL_THREAD_H */

View File

@@ -0,0 +1,30 @@
#include <pthread.h>
struct bh_thread_s
{
pthread_t handle;
};
struct bh_mutex_s
{
pthread_mutex_t handle;
};
struct bh_cond_s
{
pthread_cond_t handle;
};
int bh_thread_init(bh_thread_t *thread,
bh_task_t *task);
int bh_mutex_init(bh_mutex_t *mutex);
void bh_mutex_destroy(bh_mutex_t *mutex);
int bh_cond_init(bh_cond_t *cond);
void bh_cond_destroy(bh_cond_t *cond);
int bh_thread_pool_init(bh_thread_pool_t *pool,
size_t size);
void bh_thread_pool_destroy(bh_thread_pool_t *pool);

251
include/bh/thread.h Normal file
View File

@@ -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 */