1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22#ifndef SDL_mutex_h_
23#define SDL_mutex_h_
24
25/**
26 * \file SDL_mutex.h
27 *
28 * Functions to provide thread synchronization primitives.
29 */
30
31#include "SDL_stdinc.h"
32#include "SDL_error.h"
33
34#include "begin_code.h"
35/* Set up for C function definitions, even when using C++ */
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40/**
41 * Synchronization functions which can time out return this value
42 * if they time out.
43 */
44#define SDL_MUTEX_TIMEDOUT 1
45
46/**
47 * This is the timeout value which corresponds to never time out.
48 */
49#define SDL_MUTEX_MAXWAIT (~(Uint32)0)
50
51
52/**
53 * \name Mutex functions
54 */
55/* @{ */
56
57/* The SDL mutex structure, defined in SDL_sysmutex.c */
58struct SDL_mutex;
59typedef struct SDL_mutex SDL_mutex;
60
61/**
62 * Create a new mutex.
63 *
64 * All newly-created mutexes begin in the _unlocked_ state.
65 *
66 * Calls to SDL_LockMutex() will not return while the mutex is locked by
67 * another thread. See SDL_TryLockMutex() to attempt to lock without blocking.
68 *
69 * SDL mutexes are reentrant.
70 *
71 * \returns the initialized and unlocked mutex or NULL on failure; call
72 * SDL_GetError() for more information.
73 *
74 * \sa SDL_DestroyMutex
75 * \sa SDL_LockMutex
76 * \sa SDL_TryLockMutex
77 * \sa SDL_UnlockMutex
78 */
79extern DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void);
80
81/**
82 * Lock the mutex.
83 *
84 * This will block until the mutex is available, which is to say it is in the
85 * unlocked state and the OS has chosen the caller as the next thread to lock
86 * it. Of all threads waiting to lock the mutex, only one may do so at a time.
87 *
88 * It is legal for the owning thread to lock an already-locked mutex. It must
89 * unlock it the same number of times before it is actually made available
90 * for other threads in the system (this is known as a "recursive mutex").
91 *
92 * \param mutex the mutex to lock
93 * \return 0, or -1 on error.
94 */
95extern DECLSPEC int SDLCALL SDL_LockMutex(SDL_mutex * mutex);
96#define SDL_mutexP(m) SDL_LockMutex(m)
97
98/**
99 * Try to lock a mutex without blocking.
100 *
101 * This works just like SDL_LockMutex(), but if the mutex is not available,
102 * this function returns `SDL_MUTEX_TIMEOUT` immediately.
103 *
104 * This technique is useful if you need exclusive access to a resource but
105 * don't want to wait for it, and will return to it to try again later.
106 *
107 * \param mutex the mutex to try to lock
108 * \returns return 0, `SDL_MUTEX_TIMEDOUT`, or -1 on error; call
109 * SDL_GetError() for more information.
110 *
111 * \sa SDL_CreateMutex
112 * \sa SDL_DestroyMutex
113 * \sa SDL_LockMutex
114 * \sa SDL_UnlockMutex
115 */
116extern DECLSPEC int SDLCALL SDL_TryLockMutex(SDL_mutex * mutex);
117
118/**
119 * Unlock the mutex.
120 *
121 * It is legal for the owning thread to lock an already-locked mutex. It must
122 * unlock it the same number of times before it is actually made available
123 * for other threads in the system (this is known as a "recursive mutex").
124 *
125 * It is an error to unlock a mutex that has not been locked by the current
126 * thread, and doing so results in undefined behavior.
127 *
128 * It is also an error to unlock a mutex that isn't locked at all.
129 *
130 * \param mutex the mutex to unlock.
131 * \returns 0, or -1 on error.
132 */
133extern DECLSPEC int SDLCALL SDL_UnlockMutex(SDL_mutex * mutex);
134#define SDL_mutexV(m) SDL_UnlockMutex(m)
135
136/**
137 * Destroy a mutex created with SDL_CreateMutex().
138 *
139 * This function must be called on any mutex that is no longer needed. Failure
140 * to destroy a mutex will result in a system memory or resource leak. While
141 * it is safe to destroy a mutex that is _unlocked_, it is not safe to attempt
142 * to destroy a locked mutex, and may result in undefined behavior depending
143 * on the platform.
144 *
145 * \param mutex the mutex to destroy
146 *
147 * \sa SDL_CreateMutex
148 * \sa SDL_LockMutex
149 * \sa SDL_TryLockMutex
150 * \sa SDL_UnlockMutex
151 */
152extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex * mutex);
153
154/* @} *//* Mutex functions */
155
156
157/**
158 * \name Semaphore functions
159 */
160/* @{ */
161
162/* The SDL semaphore structure, defined in SDL_syssem.c */
163struct SDL_semaphore;
164typedef struct SDL_semaphore SDL_sem;
165
166/**
167 * Create a semaphore.
168 *
169 * This function creates a new semaphore and initializes it with the value
170 * `initial_value`. Each wait operation on the semaphore will atomically
171 * decrement the semaphore value and potentially block if the semaphore value
172 * is 0. Each post operation will atomically increment the semaphore value and
173 * wake waiting threads and allow them to retry the wait operation.
174 *
175 * \param initial_value the starting value of the semaphore
176 * \returns a new semaphore or NULL on failure; call SDL_GetError() for more
177 * information.
178 *
179 * \sa SDL_DestroySemaphore
180 * \sa SDL_SemPost
181 * \sa SDL_SemTryWait
182 * \sa SDL_SemValue
183 * \sa SDL_SemWait
184 * \sa SDL_SemWaitTimeout
185 */
186extern DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
187
188/**
189 * Destroy a semaphore.
190 *
191 * It is not safe to destroy a semaphore if there are threads currently
192 * waiting on it.
193 *
194 * \param sem the semaphore to destroy
195 *
196 * \sa SDL_CreateSemaphore
197 * \sa SDL_SemPost
198 * \sa SDL_SemTryWait
199 * \sa SDL_SemValue
200 * \sa SDL_SemWait
201 * \sa SDL_SemWaitTimeout
202 */
203extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem * sem);
204
205/**
206 * Wait until a semaphore has a positive value and then decrements it.
207 *
208 * This function suspends the calling thread until either the semaphore
209 * pointed to by `sem` has a positive value or the call is interrupted by a
210 * signal or error. If the call is successful it will atomically decrement the
211 * semaphore value.
212 *
213 * This function is the equivalent of calling SDL_SemWaitTimeout() with a time
214 * length of `SDL_MUTEX_MAXWAIT`.
215 *
216 * \param sem the semaphore wait on
217 * \returns 0 on success or a negative error code on failure; call
218 * SDL_GetError() for more information.
219 *
220 * \sa SDL_CreateSemaphore
221 * \sa SDL_DestroySemaphore
222 * \sa SDL_SemPost
223 * \sa SDL_SemTryWait
224 * \sa SDL_SemValue
225 * \sa SDL_SemWait
226 * \sa SDL_SemWaitTimeout
227 */
228extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem * sem);
229
230/**
231 * See if a semaphore has a positive value and decrement it if it does.
232 *
233 * This function checks to see if the semaphore pointed to by `sem` has a
234 * positive value and atomically decrements the semaphore value if it does. If
235 * the semaphore doesn't have a positive value, the function immediately
236 * returns SDL_MUTEX_TIMEDOUT.
237 *
238 * \param sem the semaphore to wait on
239 * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait would
240 * block, or a negative error code on failure; call SDL_GetError()
241 * for more information.
242 *
243 * \sa SDL_CreateSemaphore
244 * \sa SDL_DestroySemaphore
245 * \sa SDL_SemPost
246 * \sa SDL_SemValue
247 * \sa SDL_SemWait
248 * \sa SDL_SemWaitTimeout
249 */
250extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem);
251
252/**
253 * Wait until a semaphore has a positive value and then
254 * decrements it.
255 *
256 * This function suspends the calling thread until either the semaphore
257 * pointed to by `sem` has a positive value, the call is interrupted by a
258 * signal or error, or the specified time has elapsed. If the call is
259 * successful it will atomically decrement the semaphore value.
260 *
261 * \param sem the semaphore to wait on
262 * \param ms the length of the timeout, in milliseconds
263 * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait does not
264 * succeed in the allotted time, or a negative error code on failure;
265 * call SDL_GetError() for more information.
266 *
267 * \sa SDL_CreateSemaphore
268 * \sa SDL_DestroySemaphore
269 * \sa SDL_SemPost
270 * \sa SDL_SemTryWait
271 * \sa SDL_SemValue
272 * \sa SDL_SemWait
273 */
274extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem * sem, Uint32 ms);
275
276/**
277 * Atomically increment a semaphore's value and wake waiting threads.
278 *
279 * \param sem the semaphore to increment
280 * \returns 0 on success or a negative error code on failure; call
281 * SDL_GetError() for more information.
282 *
283 * \sa SDL_CreateSemaphore
284 * \sa SDL_DestroySemaphore
285 * \sa SDL_SemTryWait
286 * \sa SDL_SemValue
287 * \sa SDL_SemWait
288 * \sa SDL_SemWaitTimeout
289 */
290extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem * sem);
291
292/**
293 * Get the current value of a semaphore.
294 *
295 * \param sem the semaphore to query
296 * \returns the current value of the semaphore.
297 *
298 * \sa SDL_CreateSemaphore
299 */
300extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem * sem);
301
302/* @} *//* Semaphore functions */
303
304
305/**
306 * \name Condition variable functions
307 */
308/* @{ */
309
310/* The SDL condition variable structure, defined in SDL_syscond.c */
311struct SDL_cond;
312typedef struct SDL_cond SDL_cond;
313
314/**
315 * Create a condition variable.
316 *
317 * \returns a new condition variable or NULL on failure; call SDL_GetError()
318 * for more information.
319 *
320 * \sa SDL_CondBroadcast
321 * \sa SDL_CondSignal
322 * \sa SDL_CondWait
323 * \sa SDL_CondWaitTimeout
324 * \sa SDL_DestroyCond
325 */
326extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);
327
328/**
329 * Destroy a condition variable.
330 *
331 * \param cond the condition variable to destroy
332 *
333 * \sa SDL_CondBroadcast
334 * \sa SDL_CondSignal
335 * \sa SDL_CondWait
336 * \sa SDL_CondWaitTimeout
337 * \sa SDL_CreateCond
338 */
339extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond * cond);
340
341/**
342 * Restart one of the threads that are waiting on the condition variable.
343 *
344 * \param cond the condition variable to signal
345 * \returns 0 on success or a negative error code on failure; call
346 * SDL_GetError() for more information.
347 *
348 * \sa SDL_CondBroadcast
349 * \sa SDL_CondWait
350 * \sa SDL_CondWaitTimeout
351 * \sa SDL_CreateCond
352 * \sa SDL_DestroyCond
353 */
354extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond);
355
356/**
357 * Restart all threads that are waiting on the condition variable.
358 *
359 * \param cond the condition variable to signal
360 * \returns 0 on success or a negative error code on failure; call
361 * SDL_GetError() for more information.
362 *
363 * \sa SDL_CondSignal
364 * \sa SDL_CondWait
365 * \sa SDL_CondWaitTimeout
366 * \sa SDL_CreateCond
367 * \sa SDL_DestroyCond
368 */
369extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond);
370
371/**
372 * Wait until a condition variable is signaled.
373 *
374 * This function unlocks the specified `mutex` and waits for another thread
375 * to call SDL_CondSignal() or SDL_CondBroadcast() on the condition variable
376 * `cond`. Once the condition variable is signaled, the mutex is re-locked
377 * and the function returns.
378 *
379 * The mutex must be locked before calling this function.
380 *
381 * This function is the equivalent of calling SDL_CondWaitTimeout() with a
382 * time length of `SDL_MUTEX_MAXWAIT`.
383 *
384 * \param cond the condition variable to wait on
385 * \param mutex the mutex used to coordinate thread access
386 * \returns 0 when it is signaled or a negative error code on failure; call
387 * SDL_GetError() for more information.
388 *
389 * \sa SDL_CondBroadcast
390 * \sa SDL_CondSignal
391 * \sa SDL_CondWaitTimeout
392 * \sa SDL_CreateCond
393 * \sa SDL_DestroyCond
394 */
395extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex);
396
397/**
398 * Wait until a condition variable is signaled or a certain time has passed.
399 *
400 * This function unlocks the specified `mutex` and waits for another thread
401 * to call SDL_CondSignal() or SDL_CondBroadcast() on the condition variable
402 * `cond`, or for the specified time to elapse. Once the condition variable
403 * is signaled or the time elapsed, the mutex is re-locked and the function
404 * returns.
405 *
406 * The mutex must be locked before calling this function.
407 *
408 * \param cond the condition variable to wait on
409 * \param mutex the mutex used to coordinate thread access
410 * \param ms the maximum time to wait, in milliseconds, or `SDL_MUTEX_MAXWAIT`
411 * to wait indefinitely
412 * \returns 0 if the condition variable is signaled, `SDL_MUTEX_TIMEDOUT` if
413 * the condition is not signaled in the allotted time, or a negative
414 * error code on failure; call SDL_GetError() for more information.
415 *
416 * \sa SDL_CondBroadcast
417 * \sa SDL_CondSignal
418 * \sa SDL_CondWait
419 * \sa SDL_CreateCond
420 * \sa SDL_DestroyCond
421 */
422extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond * cond,
423 SDL_mutex * mutex, Uint32 ms);
424
425/* @} *//* Condition variable functions */
426
427
428/* Ends C function definitions when using C++ */
429#ifdef __cplusplus
430}
431#endif
432#include "close_code.h"
433
434#endif /* SDL_mutex_h_ */
435
436/* vi: set ts=4 sw=4 expandtab: */
437