aboutsummaryrefslogtreecommitdiff
path: root/doc/Manual/en/BH_Thread.pod
blob: 1be67b69d3b15ff9d1708cf55d57a6958dac4057 (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
=encoding UTF-8


=head1 NAME

BH_Thread - multithreading and synchronization primitives


=head1 SYNTAX

 #include <BH/Thread.h>

 cc prog.c -o prog -lbh


=head1 DESCRIPTION

The BH_Thread library provides a set of functions for working with
multithreading and thread synchronization. It includes functions for creating
and managing threads, working with mutexes, semaphores, condition variables, and
spinlocks.


=head1 API CALLS


=head2 BH_ThreadNew

 BH_Thread *BH_ThreadNew(size_t stack,
                         BH_ThreadCallback callback,
                         void *data);

Creates a thread with a specified stack size I<stack>, execution function
I<callback>, and data I<data>.

Returns a pointer to the thread object on success, otherwise NULL.


=head2 BH_ThreadJoin

 int BH_ThreadJoin(BH_Thread *thread);

Blocks the execution of the current thread until another thread completes.

Upon completion of the thread, the resources of the I<thread> are freed.

Returns 0 on success, otherwise an error code.


=head2 BH_ThreadDetach

 int BH_ThreadDetach(BH_Thread *thread);

Detaches the thread from the current process.

Upon completion of the thread, the resources of the I<thread> are freed.

Returns 0 on success, otherwise an error code.


=head2 BH_MutexNew

 BH_Mutex *BH_MutexNew(void);

Creates a mutex.

Returns a pointer to the mutex object on success, otherwise NULL.


=head2 BH_MutexFree

 void BH_MutexFree(BH_Mutex *mutex);

Destroys the mutex.

If the mutex is locked, the behavior is undefined.


=head2 BH_MutexLock

 int BH_MutexLock(BH_Mutex *mutex);

Locks the mutex.

If the mutex is already locked, the behavior is undefined.

Returns 0 on success, otherwise an error code.


=head2 BH_MutexUnlock

 int BH_MutexUnlock(BH_Mutex *mutex);

Unlocks the mutex.

If the mutex is locked by another thread, the behavior is undefined.

Returns 0 on success, otherwise an error code.


=head2 BH_MutexLockTry

 int BH_MutexLockTry(BH_Mutex *mutex);

Attempts to lock the mutex.

Returns 0 on success, otherwise an error code.


=head2 BH_SemaphoreNew

 BH_Semaphore *BH_SemaphoreNew(int value);

Creates a semaphore with a specified initial value I<value>.

Returns a pointer to the semaphore object on success, otherwise NULL.


=head2 BH_SemaphoreFree

 void BH_SemaphoreFree(BH_Semaphore *semaphore);

Destroys the semaphore.


=head2 BH_SemaphorePost

 int BH_SemaphorePost(BH_Semaphore *semaphore);

Increases the semaphore value by 1.

Returns 0 on success, otherwise an error code.


=head2 BH_SemaphoreWait

 int BH_SemaphoreWait(BH_Semaphore *semaphore);

Decreases the semaphore value by 1.

If the semaphore value is 0, blocks the execution of the current thread until
the semaphore value becomes greater than 0.

Returns 0 on success, otherwise an error code.


=head2 BH_SemaphoreWaitTry

 int BH_SemaphoreWaitTry(BH_Semaphore *semaphore);

Attempts to decrease the semaphore value by 1.

Returns 0 on success, otherwise an error code.


=head2 BH_SemaphoreWaitFor

 int BH_SemaphoreWaitFor(BH_Semaphore *semaphore,
                         uint32_t timeout);

Attempts to decrease the semaphore value by 1 within a specified time
I<timeout>.

The I<timeout> parameter specifies the waiting time in milliseconds.

Returns 0 on success, otherwise an error code.


=head2 BH_ConditionNew

 BH_Condition *BH_ConditionNew(void);

Creates a new condition variable.

Returns a pointer to the condition variable object on success, otherwise NULL.


=head2 BH_ConditionFree

 void BH_ConditionFree(BH_Condition *condition);

Destroys the condition variable.

If the condition variable is used by other threads, the behavior is undefined.


=head2 BH_ConditionWait

 int BH_ConditionWait(BH_Condition *condition,
                      BH_Mutex *mutex);

Blocks the execution of the current thread until another thread signals
a change in the condition.

In some situations, the signal of a condition change may be false.

The I<mutex> parameter specifies the mutex used in conjunction with the
condition variable.

Returns 0 on success, otherwise an error code.


=head2 BH_ConditionWaitFor

 int BH_ConditionWaitFor(BH_Condition *condition,
                         BH_Mutex *mutex,
                         uint32_t timeout);

Attempts to block the execution of the current thread until another thread
signals a change in the condition within a specified time I<timeout>.

In some situations, the signal of a condition change may be false.

The I<mutex> parameter specifies the mutex used in conjunction with the
condition variable.

The I<timeout> parameter specifies the waiting time in milliseconds.

Returns 0 on success, otherwise an error code.


=head2 BH_ConditionSignal

 int BH_ConditionSignal(BH_Condition *condition);

Signals one waiting thread about a change in the condition.

Returns 0 on success, otherwise an error code.


=head2 BH_ConditionBroadcast

 int BH_ConditionBroadcast(BH_Condition *condition);

Signals all waiting threads about a change in the condition.

Returns 0 on success, otherwise an error code.


=head2 BH_SpinlockLock

 void BH_SpinlockLock(int *lock);

Locks the spinlock.


=head2 BH_SpinlockLockTry

 int BH_SpinlockLockTry(int *lock);

Attempts to lock the spinlock.

Returns 0 on success, otherwise an error code.


=head2 BH_SpinlockUnlock

 void BH_SpinlockUnlock(int *lock);

Unlocks the spinlock.


=head2 BH_TssCreate

 int BH_TssCreate(BH_GenericCallback callback);

Creates a new TSS/TLS index with a cleanup function I<callback>.

Returns the TSS/TLS index on success, otherwise an error code.


=head2 BH_TssRead

 void *BH_TssRead(int index);

Reads data from the TSS/TLS slot.


=head2 BH_TssWrite

 void BH_TssWrite(int index,
                  void *value);

Writes data I<value> to the TSS/TLS slot.


=head1 SEE ALSO

L<BH>