Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
248 lines (206 loc) · 6.42 KB

SDL_mutex.h

File metadata and controls

248 lines (206 loc) · 6.42 KB
 
Apr 26, 2001
Apr 26, 2001
1
/*
Apr 8, 2011
Apr 8, 2011
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.
Apr 26, 2001
Apr 26, 2001
20
21
22
23
24
*/
#ifndef _SDL_mutex_h
#define _SDL_mutex_h
Jul 10, 2006
Jul 10, 2006
25
/**
Oct 19, 2009
Oct 19, 2009
26
27
28
* \file SDL_mutex.h
*
* Functions to provide thread synchronization primitives.
Jul 10, 2006
Jul 10, 2006
29
*/
Apr 26, 2001
Apr 26, 2001
30
Feb 10, 2006
Feb 10, 2006
31
#include "SDL_stdinc.h"
Feb 10, 2006
Feb 10, 2006
32
#include "SDL_error.h"
Apr 26, 2001
Apr 26, 2001
33
34
35
36
#include "begin_code.h"
/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
Jul 10, 2006
Jul 10, 2006
37
/* *INDENT-OFF* */
Apr 26, 2001
Apr 26, 2001
38
extern "C" {
Jul 10, 2006
Jul 10, 2006
39
/* *INDENT-ON* */
Apr 26, 2001
Apr 26, 2001
40
41
#endif
Oct 19, 2009
Oct 19, 2009
42
43
44
45
/**
* Synchronization functions which can time out return this value
* if they time out.
*/
Apr 26, 2001
Apr 26, 2001
46
47
#define SDL_MUTEX_TIMEDOUT 1
Oct 19, 2009
Oct 19, 2009
48
49
50
/**
* This is the timeout value which corresponds to never time out.
*/
Apr 26, 2001
Apr 26, 2001
51
52
53
#define SDL_MUTEX_MAXWAIT (~(Uint32)0)
Oct 19, 2009
Oct 19, 2009
54
55
56
57
/**
* \name Mutex functions
*/
/*@{*/
Apr 26, 2001
Apr 26, 2001
58
59
60
61
62
/* The SDL mutex structure, defined in SDL_mutex.c */
struct SDL_mutex;
typedef struct SDL_mutex SDL_mutex;
Oct 19, 2009
Oct 19, 2009
63
64
65
/**
* Create a mutex, initialized unlocked.
*/
Jul 10, 2006
Jul 10, 2006
66
extern DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void);
Apr 26, 2001
Apr 26, 2001
67
Oct 19, 2009
Oct 19, 2009
68
69
70
71
72
/**
* Lock the mutex.
*
* \return 0, or -1 on error.
*/
Apr 26, 2001
Apr 26, 2001
73
#define SDL_LockMutex(m) SDL_mutexP(m)
Jul 10, 2006
Jul 10, 2006
74
extern DECLSPEC int SDLCALL SDL_mutexP(SDL_mutex * mutex);
Apr 26, 2001
Apr 26, 2001
75
Oct 19, 2009
Oct 19, 2009
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.
Jun 13, 2002
Jun 13, 2002
83
*/
Apr 26, 2001
Apr 26, 2001
84
#define SDL_UnlockMutex(m) SDL_mutexV(m)
Jul 10, 2006
Jul 10, 2006
85
extern DECLSPEC int SDLCALL SDL_mutexV(SDL_mutex * mutex);
Apr 26, 2001
Apr 26, 2001
86
Oct 19, 2009
Oct 19, 2009
87
88
89
/**
* Destroy a mutex.
*/
Jul 10, 2006
Jul 10, 2006
90
extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex * mutex);
Apr 26, 2001
Apr 26, 2001
91
Oct 19, 2009
Oct 19, 2009
92
93
/*@}*//*Mutex functions*/
Apr 26, 2001
Apr 26, 2001
94
Oct 19, 2009
Oct 19, 2009
95
96
97
98
/**
* \name Semaphore functions
*/
/*@{*/
Apr 26, 2001
Apr 26, 2001
99
100
101
102
103
/* The SDL semaphore structure, defined in SDL_sem.c */
struct SDL_semaphore;
typedef struct SDL_semaphore SDL_sem;
Oct 19, 2009
Oct 19, 2009
104
105
106
/**
* Create a semaphore, initialized with value, returns NULL on failure.
*/
Jul 10, 2006
Jul 10, 2006
107
extern DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
Apr 26, 2001
Apr 26, 2001
108
Oct 19, 2009
Oct 19, 2009
109
110
111
/**
* Destroy a semaphore.
*/
Jul 10, 2006
Jul 10, 2006
112
extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem * sem);
Apr 26, 2001
Apr 26, 2001
113
Oct 19, 2009
Oct 19, 2009
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.
Apr 26, 2001
Apr 26, 2001
118
*/
Jul 10, 2006
Jul 10, 2006
119
extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem * sem);
Apr 26, 2001
Apr 26, 2001
120
Oct 19, 2009
Oct 19, 2009
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.
*/
Jul 10, 2006
Jul 10, 2006
127
extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem);
Apr 26, 2001
Apr 26, 2001
128
Oct 19, 2009
Oct 19, 2009
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.
*/
Jul 10, 2006
Jul 10, 2006
138
extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem * sem, Uint32 ms);
Apr 26, 2001
Apr 26, 2001
139
Oct 19, 2009
Oct 19, 2009
140
141
142
143
/**
* Atomically increases the semaphore's count (not blocking).
*
* \return 0, or -1 on error.
Apr 26, 2001
Apr 26, 2001
144
*/
Jul 10, 2006
Jul 10, 2006
145
extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem * sem);
Apr 26, 2001
Apr 26, 2001
146
Oct 19, 2009
Oct 19, 2009
147
148
149
/**
* Returns the current count of the semaphore.
*/
Jul 10, 2006
Jul 10, 2006
150
extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem * sem);
Apr 26, 2001
Apr 26, 2001
151
Oct 19, 2009
Oct 19, 2009
152
153
/*@}*//*Semaphore functions*/
Apr 26, 2001
Apr 26, 2001
154
Oct 19, 2009
Oct 19, 2009
155
156
157
158
/**
* \name Condition variable functions
*/
/*@{*/
Apr 26, 2001
Apr 26, 2001
159
160
161
162
163
/* The SDL condition variable structure, defined in SDL_cond.c */
struct SDL_cond;
typedef struct SDL_cond SDL_cond;
Oct 19, 2009
Oct 19, 2009
164
165
/**
* Create a condition variable.
Jan 27, 2011
Jan 27, 2011
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.
Oct 19, 2009
Oct 19, 2009
191
*/
Jul 10, 2006
Jul 10, 2006
192
extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);
Apr 26, 2001
Apr 26, 2001
193
Oct 19, 2009
Oct 19, 2009
194
195
196
/**
* Destroy a condition variable.
*/
Jul 10, 2006
Jul 10, 2006
197
extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond * cond);
Apr 26, 2001
Apr 26, 2001
198
Oct 19, 2009
Oct 19, 2009
199
200
201
202
/**
* Restart one of the threads that are waiting on the condition variable.
*
* \return 0 or -1 on error.
Apr 26, 2001
Apr 26, 2001
203
*/
Jul 10, 2006
Jul 10, 2006
204
extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond);
Apr 26, 2001
Apr 26, 2001
205
Oct 19, 2009
Oct 19, 2009
206
207
/**
* Restart all threads that are waiting on the condition variable.
Jan 27, 2011
Jan 27, 2011
208
*
Oct 19, 2009
Oct 19, 2009
209
* \return 0 or -1 on error.
Apr 26, 2001
Apr 26, 2001
210
*/
Jul 10, 2006
Jul 10, 2006
211
extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond);
Apr 26, 2001
Apr 26, 2001
212
Oct 19, 2009
Oct 19, 2009
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.
Apr 26, 2001
Apr 26, 2001
221
*/
Jun 7, 2010
Jun 7, 2010
222
extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex);
Apr 26, 2001
Apr 26, 2001
223
Oct 19, 2009
Oct 19, 2009
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.
*/
Jul 10, 2006
Jul 10, 2006
232
233
extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond * cond,
SDL_mutex * mutex, Uint32 ms);
Apr 26, 2001
Apr 26, 2001
234
Oct 19, 2009
Oct 19, 2009
235
236
237
/*@}*//*Condition variable functions*/
Apr 26, 2001
Apr 26, 2001
238
239
/* Ends C function definitions when using C++ */
#ifdef __cplusplus
Jul 10, 2006
Jul 10, 2006
240
/* *INDENT-OFF* */
Apr 26, 2001
Apr 26, 2001
241
}
Jul 10, 2006
Jul 10, 2006
242
/* *INDENT-ON* */
Apr 26, 2001
Apr 26, 2001
243
244
245
246
#endif
#include "close_code.h"
#endif /* _SDL_mutex_h */
Jul 10, 2006
Jul 10, 2006
247
248
/* vi: set ts=4 sw=4 expandtab: */