#ifndef BH_THREAD_H #define BH_THREAD_H #include "bh.h" #define BH_THREAD_CLEANUP 0x0001 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_semaphore_s bh_semaphore_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; typedef struct bh_spinlock_s bh_spinlock_t; #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(BH_USE_WINTHREAD) #include #include typedef uintptr_t (__cdecl *bh_thread_begin_cb_t)(void *, unsigned, unsigned (__stdcall *)(void *), void *, unsigned, unsigned *); typedef void (__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 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); int bh_thread_sleep(unsigned long timeout); 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); 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); 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); bh_spinlock_t *bh_spinlock_new(void); void bh_spinlock_free(bh_spinlock_t *lock); int bh_spinlock_lock(bh_spinlock_t *lock); int bh_spinlock_unlock(bh_spinlock_t *lock); int bh_spinlock_try_lock(bh_spinlock_t *lock); int bh_tls_alloc(bh_generic_cb_t cleanup); void *bh_tls_get(int slot); void bh_tls_set(int slot, void* data); #endif /* BH_THREAD_H */