Implement TLS and spinlocks, fix bug in Windows threads

This commit is contained in:
2024-06-10 22:24:57 +03:00
parent 8939076f94
commit ada9d3ada5
10 changed files with 359 additions and 3 deletions

View File

@@ -27,6 +27,7 @@
#define BH_NOT_FOUND 0x0006
#define BH_TIMEOUT 0x0007
typedef void (*bh_generic_cb_t)(void *data);
typedef int (*bh_equal_cb_t)(const void *, const void *);
typedef size_t (*bh_hash_cb_t)(const void *);

View File

@@ -6,6 +6,8 @@
#define BH_THREAD_DONE 0x0100
#define BH_MAX_TLS 32
#if defined(BH_USE_PTHREAD)
#include "thread_posix.h"
#elif defined(BH_USE_WINTHREAD)
@@ -33,6 +35,18 @@ struct bh_thread_pool_s
int shutdown;
};
typedef struct bh_tls_info_s
{
size_t counter;
bh_spinlock_t lock;
bh_generic_cb_t cleanup[BH_MAX_TLS];
} bh_tls_info_t;
typedef struct bh_tls_s
{
void *data[BH_MAX_TLS];
} bh_tls_t;
void bh_task_init(bh_task_t *task,
void (*func)(void *),
void *data,
@@ -42,4 +56,14 @@ void bh_task_destroy(bh_task_t *task);
void bh_thread_pool_worker(void *arg);
void bh_spinlock_init(bh_spinlock_t *lock);
void bh_spinlock_destroy(bh_spinlock_t *lock);
bh_tls_info_t *bh_tls_info(void);
bh_tls_t *bh_tls_fetch(void);
void bh_tls_cleanup(void);
#endif /* BH_INTERNAL_THREAD_H */

View File

@@ -18,6 +18,11 @@ struct bh_cond_s
void *handle;
};
struct bh_spinlock_s
{
int handle;
};
int bh_thread_init(bh_thread_t *thread,
bh_task_t *task);
@@ -30,4 +35,4 @@ 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);
void bh_thread_pool_destroy(bh_thread_pool_t *pool);

View File

@@ -22,6 +22,11 @@ struct bh_cond_s
pthread_cond_t handle;
};
struct bh_spinlock_s
{
int handle;
};
int bh_thread_init(bh_thread_t *thread,
bh_task_t *task);

View File

@@ -39,6 +39,11 @@ struct bh_cond_s
};
#endif
struct bh_spinlock_s
{
LONG handle;
};
int bh_thread_init_base(bh_thread_t *thread,
bh_task_t *task,
bh_thread_begin_cb_t begin,

View File

@@ -12,6 +12,7 @@ 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);
@@ -108,4 +109,20 @@ 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 */