aboutsummaryrefslogtreecommitdiff
path: root/include/bh/thread.h
blob: 3360c4260602dc08a9c95fe8a030596eba2fa884 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#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 <windows.h>
#include <process.h>

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