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
|
#include "Thread.h"
#include <limits.h>
#include <stdlib.h>
#include <errno.h>
struct BH_ThreadContext
{
BH_ThreadCallback callback;
void *data;
};
static void *threadRun(void *context)
{
BH_ThreadCallback callback;
void *data;
callback = ((struct BH_ThreadContext *)context)->callback;
data = ((struct BH_ThreadContext *)context)->data;
free(context);
callback(data);
BH_TssCleanup();
pthread_exit(0);
}
static int threadInit(BH_Thread *thread,
size_t stack,
BH_ThreadCallback callback,
void *data)
{
struct BH_ThreadContext *context;
pthread_attr_t attributes;
int result;
context = malloc(sizeof(*context));
if (!context)
return BH_ERROR;
context->callback = callback;
context->data = data;
/* Create thread with specified stack size */
pthread_attr_init(&attributes);
if (!stack)
result = pthread_create(&thread->handle, NULL, threadRun, context);
else
{
if (stack < PTHREAD_STACK_MIN)
stack = PTHREAD_STACK_MIN;
pthread_attr_setstacksize(&attributes, stack);
result = pthread_create(&thread->handle, &attributes, threadRun, context);
}
pthread_attr_destroy(&attributes);
return result;
}
BH_Thread *BH_ThreadNew(size_t stack,
BH_ThreadCallback callback,
void *data)
{
BH_Thread *thread;
thread = malloc(sizeof(BH_Thread));
if (thread && threadInit(thread, stack, callback, data))
{
free(thread);
return NULL;
}
return thread;
}
int BH_ThreadJoin(BH_Thread *thread)
{
if (pthread_join(thread->handle, NULL))
return BH_ERROR;
free(thread);
return BH_OK;
}
int BH_ThreadDetach(BH_Thread *thread)
{
if (pthread_detach(thread->handle))
return BH_ERROR;
free(thread);
return BH_OK;
}
void BH_ThreadSleep(uint32_t timeout)
{
struct timespec ts;
int result;
/* We don't care about nanoseconds */
ts.tv_sec = timeout / 1000;
ts.tv_nsec = (timeout % 1000) * 1000000;
do
{
result = nanosleep(&ts, &ts);
if (errno != EINTR)
break;
} while (result);
}
|