Add thread support
This commit is contained in:
45
include/bh/internal/thread.h
Normal file
45
include/bh/internal/thread.h
Normal 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 */
|
||||
30
include/bh/internal/thread_posix.h
Normal file
30
include/bh/internal/thread_posix.h
Normal 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);
|
||||
Reference in New Issue
Block a user