2024-06-03 22:11:05 +03:00
|
|
|
#ifndef BH_THREAD_H
|
|
|
|
|
#define BH_THREAD_H
|
2024-04-14 22:32:58 +03:00
|
|
|
|
|
|
|
|
#include "bh.h"
|
|
|
|
|
|
2024-06-03 22:11:05 +03:00
|
|
|
#define BH_THREAD_CLEANUP 0x0001
|
2024-04-14 22:32:58 +03:00
|
|
|
|
|
|
|
|
typedef void (*bh_thread_cb_t)(void *);
|
|
|
|
|
typedef struct bh_thread_s bh_thread_t;
|
|
|
|
|
typedef struct bh_mutex_s bh_mutex_t;
|
2024-04-27 11:25:21 +03:00
|
|
|
typedef struct bh_semaphore_s bh_semaphore_t;
|
2024-04-14 22:32:58 +03:00
|
|
|
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;
|
|
|
|
|
|
2024-06-03 22:11:05 +03:00
|
|
|
#if defined(BH_USE_PTHREAD)
|
2024-04-14 22:32:58 +03:00
|
|
|
bh_thread_t *bh_thread_new(bh_task_t *task);
|
|
|
|
|
|
|
|
|
|
bh_thread_pool_t *bh_thread_pool_new(size_t size);
|
2024-04-24 00:17:36 +03:00
|
|
|
|
2024-06-03 22:11:05 +03:00
|
|
|
#elif defined(BH_USE_WINTHREAD)
|
2024-04-24 00:17:36 +03:00
|
|
|
#include <windows.h>
|
|
|
|
|
#include <process.h>
|
|
|
|
|
|
2024-04-14 22:32:58 +03:00
|
|
|
typedef uintptr_t (__cdecl *bh_thread_begin_cb_t)(void *,
|
|
|
|
|
unsigned,
|
|
|
|
|
unsigned (__stdcall *)(void *),
|
|
|
|
|
void *,
|
|
|
|
|
unsigned,
|
|
|
|
|
unsigned *);
|
|
|
|
|
|
2024-04-24 00:17:36 +03:00
|
|
|
typedef void (__cdecl *bh_thread_end_cb_t)(unsigned);
|
2024-04-14 22:32:58 +03:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2024-04-23 23:45:43 +03:00
|
|
|
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);
|
|
|
|
|
|
2024-04-14 22:32:58 +03:00
|
|
|
int bh_thread_join(bh_thread_t *thread);
|
|
|
|
|
|
|
|
|
|
int bh_thread_detach(bh_thread_t *thread);
|
|
|
|
|
|
|
|
|
|
bh_mutex_t *bh_mutex_new(void);
|
|
|
|
|
|
|
|
|
|
void bh_mutex_free(bh_mutex_t *mutex);
|
|
|
|
|
|
|
|
|
|
int bh_mutex_lock(bh_mutex_t *mutex);
|
|
|
|
|
|
|
|
|
|
int bh_mutex_try_lock(bh_mutex_t *mutex);
|
|
|
|
|
|
|
|
|
|
int bh_mutex_unlock(bh_mutex_t *mutex);
|
|
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
bh_semaphore_t *bh_semaphore_new(int count);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
void bh_semaphore_free(bh_semaphore_t *semaphore);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
int bh_semaphore_post(bh_semaphore_t *semaphore);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
int bh_semaphore_wait(bh_semaphore_t *semaphore);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
int bh_semaphore_wait_for(bh_semaphore_t *semaphore,
|
|
|
|
|
unsigned long timeout);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
int bh_semaphore_try_wait(bh_semaphore_t *semaphore);
|
|
|
|
|
|
2024-04-14 22:32:58 +03:00
|
|
|
bh_cond_t *bh_cond_new(void);
|
|
|
|
|
|
|
|
|
|
void bh_cond_free(bh_cond_t *cond);
|
|
|
|
|
|
|
|
|
|
int bh_cond_wait(bh_cond_t *cond,
|
|
|
|
|
bh_mutex_t *mutex);
|
|
|
|
|
|
|
|
|
|
int bh_cond_wait_for(bh_cond_t *cond,
|
|
|
|
|
bh_mutex_t *mutex,
|
|
|
|
|
unsigned long timeout);
|
|
|
|
|
|
|
|
|
|
int bh_cond_signal(bh_cond_t *cond);
|
|
|
|
|
|
|
|
|
|
int bh_cond_broadcast(bh_cond_t *cond);
|
|
|
|
|
|
|
|
|
|
int bh_thread_pool_add(bh_thread_pool_t *pool,
|
|
|
|
|
bh_task_t *task);
|
|
|
|
|
|
|
|
|
|
int bh_thread_pool_wait(bh_thread_pool_t *pool);
|
|
|
|
|
|
|
|
|
|
void bh_thread_pool_free(bh_thread_pool_t *pool);
|
|
|
|
|
|
2024-06-03 22:11:05 +03:00
|
|
|
#endif /* BH_THREAD_H */
|