This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_mutex.h
249 lines (206 loc) · 6.35 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2010 Sam Lantinga
4
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) any later version.
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Lesser General Public License for more details.
14
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
Sam Lantinga
20
slouken@libsdl.org
21
22
23
24
25
*/
#ifndef _SDL_mutex_h
#define _SDL_mutex_h
26
/**
27
28
29
* \file SDL_mutex.h
*
* Functions to provide thread synchronization primitives.
30
*/
31
32
#include "SDL_stdinc.h"
33
#include "SDL_error.h"
34
35
36
37
#include "begin_code.h"
/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
38
/* *INDENT-OFF* */
39
extern "C" {
40
/* *INDENT-ON* */
41
42
#endif
43
44
45
46
/**
* Synchronization functions which can time out return this value
* if they time out.
*/
47
48
#define SDL_MUTEX_TIMEDOUT 1
49
50
51
/**
* This is the timeout value which corresponds to never time out.
*/
52
53
54
#define SDL_MUTEX_MAXWAIT (~(Uint32)0)
55
56
57
58
/**
* \name Mutex functions
*/
/*@{*/
59
60
61
62
63
/* The SDL mutex structure, defined in SDL_mutex.c */
struct SDL_mutex;
typedef struct SDL_mutex SDL_mutex;
64
65
66
/**
* Create a mutex, initialized unlocked.
*/
67
extern DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void);
68
69
70
71
72
73
/**
* Lock the mutex.
*
* \return 0, or -1 on error.
*/
74
#define SDL_LockMutex(m) SDL_mutexP(m)
75
extern DECLSPEC int SDLCALL SDL_mutexP(SDL_mutex * mutex);
76
77
78
79
80
81
82
83
/**
* 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.
84
*/
85
#define SDL_UnlockMutex(m) SDL_mutexV(m)
86
extern DECLSPEC int SDLCALL SDL_mutexV(SDL_mutex * mutex);
87
88
89
90
/**
* Destroy a mutex.
*/
91
extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex * mutex);
92
93
94
/*@}*//*Mutex functions*/
95
96
97
98
99
/**
* \name Semaphore functions
*/
/*@{*/
100
101
102
103
104
/* The SDL semaphore structure, defined in SDL_sem.c */
struct SDL_semaphore;
typedef struct SDL_semaphore SDL_sem;
105
106
107
/**
* Create a semaphore, initialized with value, returns NULL on failure.
*/
108
extern DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
109
110
111
112
/**
* Destroy a semaphore.
*/
113
extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem * sem);
114
115
116
117
118
/**
* 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.
119
*/
120
extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem * sem);
121
122
123
124
125
126
127
/**
* Non-blocking variant of SDL_SemWait().
*
* \return 0 if the wait succeeds, ::SDL_MUTEX_TIMEDOUT if the wait would
* block, and -1 on error.
*/
128
extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem);
129
130
131
132
133
134
135
136
137
138
/**
* 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.
*/
139
extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem * sem, Uint32 ms);
140
141
142
143
144
/**
* Atomically increases the semaphore's count (not blocking).
*
* \return 0, or -1 on error.
145
*/
146
extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem * sem);
147
148
149
150
/**
* Returns the current count of the semaphore.
*/
151
extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem * sem);
152
153
154
/*@}*//*Semaphore functions*/
155
156
157
158
159
/**
* \name Condition variable functions
*/
/*@{*/
160
161
162
163
164
/* The SDL condition variable structure, defined in SDL_cond.c */
struct SDL_cond;
typedef struct SDL_cond SDL_cond;
165
166
/**
* Create a condition variable.
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
*
* 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.
192
*/
193
extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);
194
195
196
197
/**
* Destroy a condition variable.
*/
198
extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond * cond);
199
200
201
202
203
/**
* Restart one of the threads that are waiting on the condition variable.
*
* \return 0 or -1 on error.
204
*/
205
extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond);
206
207
208
/**
* Restart all threads that are waiting on the condition variable.
209
*
210
* \return 0 or -1 on error.
211
*/
212
extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond);
213
214
215
216
217
218
219
220
221
/**
* 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.
222
*/
223
extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex);
224
225
226
227
228
229
230
231
232
/**
* 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.
*/
233
234
extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond * cond,
SDL_mutex * mutex, Uint32 ms);
235
236
237
238
/*@}*//*Condition variable functions*/
239
240
/* Ends C function definitions when using C++ */
#ifdef __cplusplus
241
/* *INDENT-OFF* */
242
}
243
/* *INDENT-ON* */
244
245
246
247
#endif
#include "close_code.h"
#endif /* _SDL_mutex_h */
248
249
/* vi: set ts=4 sw=4 expandtab: */