This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_mutex.h
248 lines (206 loc) · 6.42 KB
1
/*
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
20
21
22
23
24
*/
#ifndef _SDL_mutex_h
#define _SDL_mutex_h
25
/**
26
27
28
* \file SDL_mutex.h
*
* Functions to provide thread synchronization primitives.
29
*/
30
31
#include "SDL_stdinc.h"
32
#include "SDL_error.h"
33
34
35
36
#include "begin_code.h"
/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
37
/* *INDENT-OFF* */
38
extern "C" {
39
/* *INDENT-ON* */
40
41
#endif
42
43
44
45
/**
* Synchronization functions which can time out return this value
* if they time out.
*/
46
47
#define SDL_MUTEX_TIMEDOUT 1
48
49
50
/**
* This is the timeout value which corresponds to never time out.
*/
51
52
53
#define SDL_MUTEX_MAXWAIT (~(Uint32)0)
54
55
56
57
/**
* \name Mutex functions
*/
/*@{*/
58
59
60
61
62
/* The SDL mutex structure, defined in SDL_mutex.c */
struct SDL_mutex;
typedef struct SDL_mutex SDL_mutex;
63
64
65
/**
* Create a mutex, initialized unlocked.
*/
66
extern DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void);
67
68
69
70
71
72
/**
* Lock the mutex.
*
* \return 0, or -1 on error.
*/
73
#define SDL_LockMutex(m) SDL_mutexP(m)
74
extern DECLSPEC int SDLCALL SDL_mutexP(SDL_mutex * mutex);
75
76
77
78
79
80
81
82
/**
* Unlock the mutex.
*
* \return 0, or -1 on error.
*
* \warning It is an error to unlock a mutex that has not been locked by
* the current thread, and doing so results in undefined behavior.
83
*/
84
#define SDL_UnlockMutex(m) SDL_mutexV(m)
85
extern DECLSPEC int SDLCALL SDL_mutexV(SDL_mutex * mutex);
86
87
88
89
/**
* Destroy a mutex.
*/
90
extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex * mutex);
91
92
93
/*@}*//*Mutex functions*/
94
95
96
97
98
/**
* \name Semaphore functions
*/
/*@{*/
99
100
101
102
103
/* The SDL semaphore structure, defined in SDL_sem.c */
struct SDL_semaphore;
typedef struct SDL_semaphore SDL_sem;
104
105
106
/**
* Create a semaphore, initialized with value, returns NULL on failure.
*/
107
extern DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
108
109
110
111
/**
* Destroy a semaphore.
*/
112
extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem * sem);
113
114
115
116
117
/**
* This function suspends the calling thread until the semaphore pointed
* to by \c sem has a positive count. It then atomically decreases the
* semaphore count.
118
*/
119
extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem * sem);
120
121
122
123
124
125
126
/**
* Non-blocking variant of SDL_SemWait().
*
* \return 0 if the wait succeeds, ::SDL_MUTEX_TIMEDOUT if the wait would
* block, and -1 on error.
*/
127
extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem);
128
129
130
131
132
133
134
135
136
137
/**
* Variant of SDL_SemWait() with a timeout in milliseconds.
*
* \return 0 if the wait succeeds, ::SDL_MUTEX_TIMEDOUT if the wait does not
* succeed in the allotted time, and -1 on error.
*
* \warning On some platforms this function is implemented by looping with a
* delay of 1 ms, and so should be avoided if possible.
*/
138
extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem * sem, Uint32 ms);
139
140
141
142
143
/**
* Atomically increases the semaphore's count (not blocking).
*
* \return 0, or -1 on error.
144
*/
145
extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem * sem);
146
147
148
149
/**
* Returns the current count of the semaphore.
*/
150
extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem * sem);
151
152
153
/*@}*//*Semaphore functions*/
154
155
156
157
158
/**
* \name Condition variable functions
*/
/*@{*/
159
160
161
162
163
/* The SDL condition variable structure, defined in SDL_cond.c */
struct SDL_cond;
typedef struct SDL_cond SDL_cond;
164
165
/**
* Create a condition variable.
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
*
* Typical use of condition variables:
*
* Thread A:
* SDL_LockMutex(lock);
* while ( ! condition ) {
* SDL_CondWait(cond, lock);
* }
* SDL_UnlockMutex(lock);
*
* Thread B:
* SDL_LockMutex(lock);
* ...
* condition = true;
* ...
* SDL_CondSignal(cond);
* SDL_UnlockMutex(lock);
*
* There is some discussion whether to signal the condition variable
* with the mutex locked or not. There is some potential performance
* benefit to unlocking first on some platforms, but there are some
* potential race conditions depending on how your code is structured.
*
* In general it's safer to signal the condition variable while the
* mutex is locked.
191
*/
192
extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);
193
194
195
196
/**
* Destroy a condition variable.
*/
197
extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond * cond);
198
199
200
201
202
/**
* Restart one of the threads that are waiting on the condition variable.
*
* \return 0 or -1 on error.
203
*/
204
extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond);
205
206
207
/**
* Restart all threads that are waiting on the condition variable.
208
*
209
* \return 0 or -1 on error.
210
*/
211
extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond);
212
213
214
215
216
217
218
219
220
/**
* Wait on the condition variable, unlocking the provided mutex.
*
* \warning The mutex must be locked before entering this function!
*
* The mutex is re-locked once the condition variable is signaled.
*
* \return 0 when it is signaled, or -1 on error.
221
*/
222
extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex);
223
224
225
226
227
228
229
230
231
/**
* Waits for at most \c ms milliseconds, and returns 0 if the condition
* variable is signaled, ::SDL_MUTEX_TIMEDOUT if the condition is not
* signaled in the allotted time, and -1 on error.
*
* \warning On some platforms this function is implemented by looping with a
* delay of 1 ms, and so should be avoided if possible.
*/
232
233
extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond * cond,
SDL_mutex * mutex, Uint32 ms);
234
235
236
237
/*@}*//*Condition variable functions*/
238
239
/* Ends C function definitions when using C++ */
#ifdef __cplusplus
240
/* *INDENT-OFF* */
241
}
242
/* *INDENT-ON* */
243
244
245
246
#endif
#include "close_code.h"
#endif /* _SDL_mutex_h */
247
248
/* vi: set ts=4 sw=4 expandtab: */