-
Notifications
You must be signed in to change notification settings - Fork 48
/
sdlmutex.inc
194 lines (168 loc) · 6.61 KB
/
sdlmutex.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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
191
192
193
194
//from "sdl_mutex.h"
{**
* Synchronization functions which can time out return this value
* if they time out.
*}
const
SDL_MUTEX_TIMEDOUT = 1;
{**
* This is the timeout value which corresponds to never time out.
*}
//SDL_MUTEX_MAXWAIT (~(Uint32)0)
{**
* Mutex functions
*}
type
{* The SDL mutex structure, defined in SDL_mutex.c *}
PSDL_Mutex = Pointer; //todo!
{**
* Create a mutex, initialized unlocked.
*}
function SDL_CreateMutex: PSDL_Mutex cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_CreateMutex' {$ENDIF} {$ENDIF};
{**
* Lock the mutex.
*
* 0, or -1 on error.
*}
//#define SDL_mutexP(m) SDL_LockMutex(m)
function SDL_LockMutex(mutex: PSDL_Mutex): SInt32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_LockMutex' {$ENDIF} {$ENDIF};
{**
* Try to lock the mutex
*
* 0, SDL_MUTEX_TIMEDOUT, or -1 on error
*}
function SDL_TryLockMutex(mutex: PSDL_Mutex): SInt32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_TryLockMutex' {$ENDIF} {$ENDIF};
{**
* Unlock the mutex.
*
* 0, or -1 on error.
*
* It is an error to unlock a mutex that has not been locked by
* the current thread, and doing so results in undefined behavior.
*}
//#define SDL_mutexV(m) SDL_UnlockMutex(m)
function SDL_UnlockMutex(mutex: PSDL_Mutex): SInt32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_UnlockMutex' {$ENDIF} {$ENDIF};
{**
* Destroy a mutex.
*}
procedure SDL_DestroyMutex(mutex: PSDL_Mutex) cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_DestroyMutex' {$ENDIF} {$ENDIF};
{*Mutex functions*}
{**
* Semaphore functions
*}
type
{* The SDL semaphore structure, defined in SDL_sem.c *}
PSDL_Sem = Pointer; //todo!
{**
* Create a semaphore, initialized with value, returns NULL on failure.
*}
function SDL_CreateSemaphore(initial_value: UInt32): PSDL_sem cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_CreateSemaphore' {$ENDIF} {$ENDIF};
{**
* Destroy a semaphore.
*}
procedure SDL_DestroySemaphore(sem: PSDL_Sem) cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_DestroySemaphore' {$ENDIF} {$ENDIF};
{**
* This function suspends the calling thread until the semaphore pointed
* to by sem has a positive count. It then atomically decreases the
* semaphore count.
*}
function SDL_SemWait(sem: PSDL_Sem): SInt32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_SemWait' {$ENDIF} {$ENDIF};
{**
* Non-blocking variant of SDL_SemWait().
*
* 0 if the wait succeeds, SDL_MUTEX_TIMEDOUT if the wait would
* block, and -1 on error.
*}
function SDL_SemTryWait(sem: PSDL_Sem): SInt32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_SemTryWait' {$ENDIF} {$ENDIF};
{**
* Variant of SDL_SemWait() with a timeout in milliseconds.
*
* 0 if the wait succeeds, ::SDL_MUTEX_TIMEDOUT if the wait does not
* succeed in the allotted time, and -1 on error.
*
* On some platforms this function is implemented by looping with a
* delay of 1 ms, and so should be avoided if possible.
*}
function SDL_SemWaitTimeout(sem: PSDL_Sem; ms: UInt32): SInt32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_SemWaitTimeout' {$ENDIF} {$ENDIF};
{**
* Atomically increases the semaphore'_S count (not blocking).
*
* 0, or -1 on error.
*}
function SDL_SemPost(sem: PSDL_Sem): SInt32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_SemPost' {$ENDIF} {$ENDIF};
{**
* Returns the current count of the semaphore.
*}
function SDL_SemValue(sem: PSDL_Sem): UInt32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_SemValue' {$ENDIF} {$ENDIF};
{*Semaphore functions*}
{**
* Condition variable functions
* }
type
{* The SDL condition variable structure, defined in SDL_cond.c *}
PSDL_Cond = Pointer; //todo!!
{**
* Create a condition variable.
*
* Typical use of condition variables:
*
* Thread A:
* SDL_LockMutex(lock);
* while ( not condition )
* begin
* SDL_CondWait(cond, lock);
* end;
* 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.
*}
function SDL_CreateCond: PSDL_Cond cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_CreateCond' {$ENDIF} {$ENDIF};
{**
* Destroy a condition variable.
*}
procedure SDL_DestroyCond(cond: PSDL_Cond) cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_DestroyCond' {$ENDIF} {$ENDIF};
{**
* Restart one of the threads that are waiting on the condition variable.
*
* 0 or -1 on error.
*}
function SDL_CondSignal(cond: PSDL_Cond): SInt32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_CondSignal' {$ENDIF} {$ENDIF};
{**
* Restart all threads that are waiting on the condition variable.
*
* 0 or -1 on error.
*}
function SDL_CondBroadcast(cond: PSDL_Cond): SInt32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_CondBroadcast' {$ENDIF} {$ENDIF};
{**
* Wait on the condition variable, unlocking the provided mutex.
*
* The mutex must be locked before entering this function!
*
* The mutex is re-locked once the condition variable is signaled.
*
* 0 when it is signaled, or -1 on error.
*}
function SDL_CondWait(cond: PSDL_Cond; mutex: PSDL_Mutex): SInt32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_CondWait' {$ENDIF} {$ENDIF};
{**
* Waits for at most 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.
*
* On some platforms this function is implemented by looping with a
* delay of 1 ms, and so should be avoided if possible.
*}
function SDL_CondWaitTimeout(cond: PSDL_Cond; mutex: PSDL_Mutex; ms: UInt32): SInt32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_CondWaitTimeout' {$ENDIF} {$ENDIF};