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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
#include <bh/internal/thread.h>
#include <string.h>
#include <stdlib.h>
static void *bh_thread_run(void *arg)
{
bh_task_t *task;
task = (bh_task_t *)arg;
task->func(task->data);
task->flags |= BH_THREAD_DONE;
if (task->flags & BH_THREAD_CLEANUP)
bh_task_free(task);
return NULL;
}
int bh_thread_init(bh_thread_t *thread,
bh_task_t *task)
{
thread->allocated = 0;
return pthread_create(&thread->handle, NULL, bh_thread_run, task);
}
bh_thread_t *bh_thread_new(bh_task_t *task)
{
bh_thread_t *result;
result = malloc(sizeof(*result));
if (result && bh_thread_init(result, task))
{
free(result);
result = NULL;
}
if (result)
result->allocated = 1;
return result;
}
int bh_thread_join(bh_thread_t *thread)
{
int result = pthread_join(thread->handle, NULL);
if (thread->allocated)
free(thread);
return result;
}
int bh_thread_detach(bh_thread_t *thread)
{
int result = pthread_detach(thread->handle);
if (thread->allocated)
free(thread);
return result;
}
int bh_mutex_init(bh_mutex_t *mutex)
{
return pthread_mutex_init(&mutex->handle, NULL);
}
void bh_mutex_destroy(bh_mutex_t *mutex)
{
pthread_mutex_destroy(&mutex->handle);
}
int bh_mutex_lock(bh_mutex_t *mutex)
{
return pthread_mutex_lock(&mutex->handle);
}
int bh_mutex_try_lock(bh_mutex_t *mutex)
{
return pthread_mutex_trylock(&mutex->handle);
}
int bh_mutex_unlock(bh_mutex_t *mutex)
{
return pthread_mutex_unlock(&mutex->handle);
}
int bh_cond_init(bh_cond_t *cond)
{
return pthread_cond_init(&cond->handle, NULL);
}
void bh_cond_destroy(bh_cond_t *cond)
{
pthread_cond_destroy(&cond->handle);
}
int bh_cond_wait(bh_cond_t *cond,
bh_mutex_t *mutex)
{
return pthread_cond_wait(&cond->handle, &mutex->handle);
}
int bh_cond_wait_for(bh_cond_t *cond,
bh_mutex_t *mutex,
unsigned long timeout)
{
struct timespec ts;
ts.tv_sec = timeout / 1000;
ts.tv_nsec = (timeout - ts.tv_sec * 1000) * 1000000;
return pthread_cond_timedwait(&cond->handle, &mutex->handle, &ts);
}
int bh_cond_signal(bh_cond_t *cond)
{
return pthread_cond_signal(&cond->handle);
}
int bh_cond_broadcast(bh_cond_t *cond)
{
return pthread_cond_broadcast(&cond->handle);
}
int bh_thread_pool_init(bh_thread_pool_t *pool,
size_t size)
{
size_t i;
static bh_task_t pool_task;
static int pool_task_init = 0;
/* Initialize static thread pool task */
if (!pool_task_init)
{
bh_task_init(&pool_task, bh_thread_pool_worker, pool, 0);
pool_task_init = 1;
}
/* Zero out pool structure */
memset(pool, 0, sizeof(*pool));
/* Initialize mutex and condition variables */
if (bh_mutex_init(&pool->lock))
goto lock_fail;
if (bh_cond_init(&pool->new_task))
goto task_fail;
if (bh_cond_init(&pool->done_task))
goto done_fail;
/* Allocate array of threads */
pool->threads = malloc(size * sizeof(bh_thread_t));
pool->size = size;
if (!pool->threads)
goto thread_fail;
/* Initialize queue */
bh_queue_init(&pool->tasks);
if (bh_queue_reserve(&pool->tasks, 64))
goto queue_fail;
/* Initialize threads */
for (i = 0; i < size; i++)
{
if (bh_thread_init(&pool->threads[i], &pool_task))
{
pool->shutdown = 1;
bh_cond_broadcast(&pool->new_task);
for(; i; i--)
bh_thread_join(&pool->threads[i - 1]);
bh_queue_destroy(&pool->tasks);
goto queue_fail;
}
}
return 0;
queue_fail:
free(pool->threads);
thread_fail:
bh_cond_destroy(&pool->done_task);
done_fail:
bh_cond_destroy(&pool->new_task);
task_fail:
bh_mutex_destroy(&pool->lock);
lock_fail:
return -1;
}
bh_thread_pool_t *bh_thread_pool_new(size_t size)
{
bh_thread_pool_t *result;
result = malloc(sizeof(*result));
if (result && bh_thread_pool_init(result, size))
{
free(result);
result = NULL;
}
return result;
}
|