aboutsummaryrefslogtreecommitdiff
path: root/include/BH/Thread.h
blob: 794abc57fe3982815da0c406e973f2a9dde4b791 (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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#ifndef BH_THREAD_H
#define BH_THREAD_H


#include "Common.h"


#define BH_MAX_TSS      128


typedef struct BH_Lock BH_Lock;
typedef struct BH_Thread BH_Thread;
typedef struct BH_Mutex BH_Mutex;
typedef struct BH_Semaphore BH_Semaphore;
typedef struct BH_Condition BH_Condition;
typedef int (*BH_ThreadCallback)(void *);


/**
 * Creates new thread with specified \a stack size, thread \a callback
 * function and \a data.
 *
 * \param stack     Stack size
 * \param callback  Callback function
 * \param data      User data
 *
 * \return On success, returns thread pointer.
 * \return On failure, returns NULL pointer.
 */
BH_Thread *BH_ThreadNew(size_t stack,
                        BH_ThreadCallback callback,
                        void *data);


/**
 * Joins the specified \a thread.
 *
 * \param thread  Thread
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_ThreadJoin(BH_Thread *thread);


/**
 * Detaches the specified \a thread.
 *
 * \param thread  Thread
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_ThreadDetach(BH_Thread *thread);


/**
 * Creates new mutex.
 *
 * \return On success, returns mutex pointer.
 * \return On failure, returns NULL pointer.
 */
BH_Mutex *BH_MutexNew(void);


/**
 * Frees the \a mutex.
 *
 * \param mutex  Mutex
 */
void BH_MutexFree(BH_Mutex *mutex);


/**
 * Locks the \a mutex.
 *
 * \param mutex  Mutex
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_MutexLock(BH_Mutex *mutex);


/**
 * Unlocks the \a mutex.
 *
 * \param mutex  Mutex
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_MutexUnlock(BH_Mutex *mutex);


/**
 * Tries to lock the \a mutex.
 *
 * \param mutex  Mutex
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_MutexLockTry(BH_Mutex *mutex);


/**
 * Creates new semaphore.
 *
 * \param value  Semaphore value
 *
 * \return On success, returns thread pointer.
 * \return On failure, returns NULL pointer.
 */
BH_Semaphore *BH_SemaphoreNew(int value);


/**
 * Frees the \a semaphore.
 *
 * \param semaphore  Semaphore
 */
void BH_SemaphoreFree(BH_Semaphore *semaphore);


/**
 * Posts/increments the \a semaphore.
 *
 * \param semaphore  Semaphore
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_SemaphorePost(BH_Semaphore *semaphore);


/**
 * Waits/decrements the \a semaphore.
 *
 * \param semaphore  Semaphore
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_SemaphoreWait(BH_Semaphore *semaphore);


/**
 * Tries to wait/decrement the \a semaphore.
 *
 * \param semaphore  Semaphore
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_SemaphoreWaitTry(BH_Semaphore *semaphore);


/**
 * Waits/decrements the \a semaphore with \a timeout.
 *
 * If timeout occures, return code will be BH_TIMEOUT.
 *
 * \param semaphore  Semaphore
 * \param timeout    Timeout in milliseconds
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_SemaphoreWaitFor(BH_Semaphore *semaphore,
                        uint32_t timeout);


/**
 * Creates new condition variable.
 *
 * \return On success, returns condition variable pointer.
 * \return On failure, returns NULL pointer.
 */
BH_Condition *BH_ConditionNew(void);


/**
 * Frees the \a condition variable.
 *
 * \param condition  Condition
 */
void BH_ConditionFree(BH_Condition *condition);


/**
 * Unlocks the \a mutex and waits for the \a condition variable.
 *
 * \param condition  Condition variable
 * \param mutex      Mutex
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_ConditionWait(BH_Condition *condition,
                     BH_Mutex *mutex);


/**
 * Unlocks the \a mutex and waits for the \a condition variable with \a timeout.
 *
 * If timeout occures, return code will be BH_TIMEOUT.
 *
 * \param condition  Condition variable
 * \param mutex      Mutex
 * \param timeout    Timeout in milliseconds
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_ConditionWaitFor(BH_Condition *condition,
                        BH_Mutex *mutex,
                        uint32_t timeout);


/**
 * Signals the \a condition variable.
 *
 * \param condition  Condition variable
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_ConditionSignal(BH_Condition *condition);


/**
 * Broadcasts the \a condition variable.
 *
 * \param condition  Condition variable
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_ConditionBroadcast(BH_Condition *condition);


/**
 * Locks the \a spinlock.
 *
 * \param lock  Spinlock
 */
void BH_SpinlockLock(int *lock);



/**
 * Tries to lock the \a spinlock.
 *
 * \param lock  Spinlock
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_SpinlockLockTry(int *lock);


/**
 * Unlocks the \a spinlock.
 *
 * \param lock  Spinlock
 */
void BH_SpinlockUnlock(int *lock);


/**
 * Create new TSS/TLS slot index and destruction \a callback.
 *
 * \param callback  Destruction callback
 *
 * \return On success, returns slot index.
 * \return On failure, returns error code.
 */
int BH_TssCreate(BH_GenericCallback callback);


/**
 * Reads the value associated with the TSS \a index.
 *
 * \param index  TSS index
 *
 * \return Index value
 */
void *BH_TssRead(int index);


/**
 * Writes the \a value to the TSS \a index.
 *
 * \param index  TSS index to
 * \param value  Value
 */
void BH_TssWrite(int index,
                 void *value);


#endif /* BH_THREAD_H */