summary refs log tree commit diff stats
path: root/vendor/SDL2/include/SDL_mutex.h
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/SDL2/include/SDL_mutex.h')
-rw-r--r--vendor/SDL2/include/SDL_mutex.h471
1 files changed, 471 insertions, 0 deletions
diff --git a/vendor/SDL2/include/SDL_mutex.h b/vendor/SDL2/include/SDL_mutex.h new file mode 100644 index 0000000..173468f --- /dev/null +++ b/vendor/SDL2/include/SDL_mutex.h
@@ -0,0 +1,471 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2022 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 * \since This function is available since SDL 2.0.0.
75 *
76 * \sa SDL_DestroyMutex
77 * \sa SDL_LockMutex
78 * \sa SDL_TryLockMutex
79 * \sa SDL_UnlockMutex
80 */
81extern DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void);
82
83/**
84 * Lock the mutex.
85 *
86 * This will block until the mutex is available, which is to say it is in the
87 * unlocked state and the OS has chosen the caller as the next thread to lock
88 * it. Of all threads waiting to lock the mutex, only one may do so at a time.
89 *
90 * It is legal for the owning thread to lock an already-locked mutex. It must
91 * unlock it the same number of times before it is actually made available for
92 * other threads in the system (this is known as a "recursive mutex").
93 *
94 * \param mutex the mutex to lock
95 * \return 0, or -1 on error.
96 *
97 * \since This function is available since SDL 2.0.0.
98 */
99extern DECLSPEC int SDLCALL SDL_LockMutex(SDL_mutex * mutex);
100#define SDL_mutexP(m) SDL_LockMutex(m)
101
102/**
103 * Try to lock a mutex without blocking.
104 *
105 * This works just like SDL_LockMutex(), but if the mutex is not available,
106 * this function returns `SDL_MUTEX_TIMEOUT` immediately.
107 *
108 * This technique is useful if you need exclusive access to a resource but
109 * don't want to wait for it, and will return to it to try again later.
110 *
111 * \param mutex the mutex to try to lock
112 * \returns 0, `SDL_MUTEX_TIMEDOUT`, or -1 on error; call SDL_GetError() for
113 * more information.
114 *
115 * \since This function is available since SDL 2.0.0.
116 *
117 * \sa SDL_CreateMutex
118 * \sa SDL_DestroyMutex
119 * \sa SDL_LockMutex
120 * \sa SDL_UnlockMutex
121 */
122extern DECLSPEC int SDLCALL SDL_TryLockMutex(SDL_mutex * mutex);
123
124/**
125 * Unlock the mutex.
126 *
127 * It is legal for the owning thread to lock an already-locked mutex. It must
128 * unlock it the same number of times before it is actually made available for
129 * other threads in the system (this is known as a "recursive mutex").
130 *
131 * It is an error to unlock a mutex that has not been locked by the current
132 * thread, and doing so results in undefined behavior.
133 *
134 * It is also an error to unlock a mutex that isn't locked at all.
135 *
136 * \param mutex the mutex to unlock.
137 * \returns 0, or -1 on error.
138 *
139 * \since This function is available since SDL 2.0.0.
140 */
141extern DECLSPEC int SDLCALL SDL_UnlockMutex(SDL_mutex * mutex);
142#define SDL_mutexV(m) SDL_UnlockMutex(m)
143
144/**
145 * Destroy a mutex created with SDL_CreateMutex().
146 *
147 * This function must be called on any mutex that is no longer needed. Failure
148 * to destroy a mutex will result in a system memory or resource leak. While
149 * it is safe to destroy a mutex that is _unlocked_, it is not safe to attempt
150 * to destroy a locked mutex, and may result in undefined behavior depending
151 * on the platform.
152 *
153 * \param mutex the mutex to destroy
154 *
155 * \since This function is available since SDL 2.0.0.
156 *
157 * \sa SDL_CreateMutex
158 * \sa SDL_LockMutex
159 * \sa SDL_TryLockMutex
160 * \sa SDL_UnlockMutex
161 */
162extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex * mutex);
163
164/* @} *//* Mutex functions */
165
166
167/**
168 * \name Semaphore functions
169 */
170/* @{ */
171
172/* The SDL semaphore structure, defined in SDL_syssem.c */
173struct SDL_semaphore;
174typedef struct SDL_semaphore SDL_sem;
175
176/**
177 * Create a semaphore.
178 *
179 * This function creates a new semaphore and initializes it with the value
180 * `initial_value`. Each wait operation on the semaphore will atomically
181 * decrement the semaphore value and potentially block if the semaphore value
182 * is 0. Each post operation will atomically increment the semaphore value and
183 * wake waiting threads and allow them to retry the wait operation.
184 *
185 * \param initial_value the starting value of the semaphore
186 * \returns a new semaphore or NULL on failure; call SDL_GetError() for more
187 * information.
188 *
189 * \since This function is available since SDL 2.0.0.
190 *
191 * \sa SDL_DestroySemaphore
192 * \sa SDL_SemPost
193 * \sa SDL_SemTryWait
194 * \sa SDL_SemValue
195 * \sa SDL_SemWait
196 * \sa SDL_SemWaitTimeout
197 */
198extern DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
199
200/**
201 * Destroy a semaphore.
202 *
203 * It is not safe to destroy a semaphore if there are threads currently
204 * waiting on it.
205 *
206 * \param sem the semaphore to destroy
207 *
208 * \since This function is available since SDL 2.0.0.
209 *
210 * \sa SDL_CreateSemaphore
211 * \sa SDL_SemPost
212 * \sa SDL_SemTryWait
213 * \sa SDL_SemValue
214 * \sa SDL_SemWait
215 * \sa SDL_SemWaitTimeout
216 */
217extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem * sem);
218
219/**
220 * Wait until a semaphore has a positive value and then decrements it.
221 *
222 * This function suspends the calling thread until either the semaphore
223 * pointed to by `sem` has a positive value or the call is interrupted by a
224 * signal or error. If the call is successful it will atomically decrement the
225 * semaphore value.
226 *
227 * This function is the equivalent of calling SDL_SemWaitTimeout() with a time
228 * length of `SDL_MUTEX_MAXWAIT`.
229 *
230 * \param sem the semaphore wait on
231 * \returns 0 on success or a negative error code on failure; call
232 * SDL_GetError() for more information.
233 *
234 * \since This function is available since SDL 2.0.0.
235 *
236 * \sa SDL_CreateSemaphore
237 * \sa SDL_DestroySemaphore
238 * \sa SDL_SemPost
239 * \sa SDL_SemTryWait
240 * \sa SDL_SemValue
241 * \sa SDL_SemWait
242 * \sa SDL_SemWaitTimeout
243 */
244extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem * sem);
245
246/**
247 * See if a semaphore has a positive value and decrement it if it does.
248 *
249 * This function checks to see if the semaphore pointed to by `sem` has a
250 * positive value and atomically decrements the semaphore value if it does. If
251 * the semaphore doesn't have a positive value, the function immediately
252 * returns SDL_MUTEX_TIMEDOUT.
253 *
254 * \param sem the semaphore to wait on
255 * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait would
256 * block, or a negative error code on failure; call SDL_GetError()
257 * for more information.
258 *
259 * \since This function is available since SDL 2.0.0.
260 *
261 * \sa SDL_CreateSemaphore
262 * \sa SDL_DestroySemaphore
263 * \sa SDL_SemPost
264 * \sa SDL_SemValue
265 * \sa SDL_SemWait
266 * \sa SDL_SemWaitTimeout
267 */
268extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem);
269
270/**
271 * Wait until a semaphore has a positive value and then decrements it.
272 *
273 * This function suspends the calling thread until either the semaphore
274 * pointed to by `sem` has a positive value, the call is interrupted by a
275 * signal or error, or the specified time has elapsed. If the call is
276 * successful it will atomically decrement the semaphore value.
277 *
278 * \param sem the semaphore to wait on
279 * \param ms the length of the timeout, in milliseconds
280 * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait does not
281 * succeed in the allotted time, or a negative error code on failure;
282 * call SDL_GetError() for more information.
283 *
284 * \since This function is available since SDL 2.0.0.
285 *
286 * \sa SDL_CreateSemaphore
287 * \sa SDL_DestroySemaphore
288 * \sa SDL_SemPost
289 * \sa SDL_SemTryWait
290 * \sa SDL_SemValue
291 * \sa SDL_SemWait
292 */
293extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem * sem, Uint32 ms);
294
295/**
296 * Atomically increment a semaphore's value and wake waiting threads.
297 *
298 * \param sem the semaphore to increment
299 * \returns 0 on success or a negative error code on failure; call
300 * SDL_GetError() for more information.
301 *
302 * \since This function is available since SDL 2.0.0.
303 *
304 * \sa SDL_CreateSemaphore
305 * \sa SDL_DestroySemaphore
306 * \sa SDL_SemTryWait
307 * \sa SDL_SemValue
308 * \sa SDL_SemWait
309 * \sa SDL_SemWaitTimeout
310 */
311extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem * sem);
312
313/**
314 * Get the current value of a semaphore.
315 *
316 * \param sem the semaphore to query
317 * \returns the current value of the semaphore.
318 *
319 * \since This function is available since SDL 2.0.0.
320 *
321 * \sa SDL_CreateSemaphore
322 */
323extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem * sem);
324
325/* @} *//* Semaphore functions */
326
327
328/**
329 * \name Condition variable functions
330 */
331/* @{ */
332
333/* The SDL condition variable structure, defined in SDL_syscond.c */
334struct SDL_cond;
335typedef struct SDL_cond SDL_cond;
336
337/**
338 * Create a condition variable.
339 *
340 * \returns a new condition variable or NULL on failure; call SDL_GetError()
341 * for more information.
342 *
343 * \since This function is available since SDL 2.0.0.
344 *
345 * \sa SDL_CondBroadcast
346 * \sa SDL_CondSignal
347 * \sa SDL_CondWait
348 * \sa SDL_CondWaitTimeout
349 * \sa SDL_DestroyCond
350 */
351extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);
352
353/**
354 * Destroy a condition variable.
355 *
356 * \param cond the condition variable to destroy
357 *
358 * \since This function is available since SDL 2.0.0.
359 *
360 * \sa SDL_CondBroadcast
361 * \sa SDL_CondSignal
362 * \sa SDL_CondWait
363 * \sa SDL_CondWaitTimeout
364 * \sa SDL_CreateCond
365 */
366extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond * cond);
367
368/**
369 * Restart one of the threads that are waiting on the condition variable.
370 *
371 * \param cond the condition variable to signal
372 * \returns 0 on success or a negative error code on failure; call
373 * SDL_GetError() for more information.
374 *
375 * \since This function is available since SDL 2.0.0.
376 *
377 * \sa SDL_CondBroadcast
378 * \sa SDL_CondWait
379 * \sa SDL_CondWaitTimeout
380 * \sa SDL_CreateCond
381 * \sa SDL_DestroyCond
382 */
383extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond);
384
385/**
386 * Restart all threads that are waiting on the condition variable.
387 *
388 * \param cond the condition variable to signal
389 * \returns 0 on success or a negative error code on failure; call
390 * SDL_GetError() for more information.
391 *
392 * \since This function is available since SDL 2.0.0.
393 *
394 * \sa SDL_CondSignal
395 * \sa SDL_CondWait
396 * \sa SDL_CondWaitTimeout
397 * \sa SDL_CreateCond
398 * \sa SDL_DestroyCond
399 */
400extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond);
401
402/**
403 * Wait until a condition variable is signaled.
404 *
405 * This function unlocks the specified `mutex` and waits for another thread to
406 * call SDL_CondSignal() or SDL_CondBroadcast() on the condition variable
407 * `cond`. Once the condition variable is signaled, the mutex is re-locked and
408 * the function returns.
409 *
410 * The mutex must be locked before calling this function.
411 *
412 * This function is the equivalent of calling SDL_CondWaitTimeout() with a
413 * time length of `SDL_MUTEX_MAXWAIT`.
414 *
415 * \param cond the condition variable to wait on
416 * \param mutex the mutex used to coordinate thread access
417 * \returns 0 when it is signaled or a negative error code on failure; call
418 * SDL_GetError() for more information.
419 *
420 * \since This function is available since SDL 2.0.0.
421 *
422 * \sa SDL_CondBroadcast
423 * \sa SDL_CondSignal
424 * \sa SDL_CondWaitTimeout
425 * \sa SDL_CreateCond
426 * \sa SDL_DestroyCond
427 */
428extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex);
429
430/**
431 * Wait until a condition variable is signaled or a certain time has passed.
432 *
433 * This function unlocks the specified `mutex` and waits for another thread to
434 * call SDL_CondSignal() or SDL_CondBroadcast() on the condition variable
435 * `cond`, or for the specified time to elapse. Once the condition variable is
436 * signaled or the time elapsed, the mutex is re-locked and the function
437 * returns.
438 *
439 * The mutex must be locked before calling this function.
440 *
441 * \param cond the condition variable to wait on
442 * \param mutex the mutex used to coordinate thread access
443 * \param ms the maximum time to wait, in milliseconds, or `SDL_MUTEX_MAXWAIT`
444 * to wait indefinitely
445 * \returns 0 if the condition variable is signaled, `SDL_MUTEX_TIMEDOUT` if
446 * the condition is not signaled in the allotted time, or a negative
447 * error code on failure; call SDL_GetError() for more information.
448 *
449 * \since This function is available since SDL 2.0.0.
450 *
451 * \sa SDL_CondBroadcast
452 * \sa SDL_CondSignal
453 * \sa SDL_CondWait
454 * \sa SDL_CreateCond
455 * \sa SDL_DestroyCond
456 */
457extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond * cond,
458 SDL_mutex * mutex, Uint32 ms);
459
460/* @} *//* Condition variable functions */
461
462
463/* Ends C function definitions when using C++ */
464#ifdef __cplusplus
465}
466#endif
467#include "close_code.h"
468
469#endif /* SDL_mutex_h_ */
470
471/* vi: set ts=4 sw=4 expandtab: */