summaryrefslogtreecommitdiff
path: root/apps/plugins/sdl/src/thread/generic
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/sdl/src/thread/generic')
-rw-r--r--apps/plugins/sdl/src/thread/generic/SDL_syscond.c215
-rw-r--r--apps/plugins/sdl/src/thread/generic/SDL_sysmutex.c129
-rw-r--r--apps/plugins/sdl/src/thread/generic/SDL_sysmutex_c.h23
-rw-r--r--apps/plugins/sdl/src/thread/generic/SDL_syssem.c211
-rw-r--r--apps/plugins/sdl/src/thread/generic/SDL_systhread.c54
-rw-r--r--apps/plugins/sdl/src/thread/generic/SDL_systhread_c.h25
6 files changed, 657 insertions, 0 deletions
diff --git a/apps/plugins/sdl/src/thread/generic/SDL_syscond.c b/apps/plugins/sdl/src/thread/generic/SDL_syscond.c
new file mode 100644
index 0000000000..f6e7223e3d
--- /dev/null
+++ b/apps/plugins/sdl/src/thread/generic/SDL_syscond.c
@@ -0,0 +1,215 @@
1/*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2012 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 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 Sam Lantinga
20 slouken@libsdl.org
21*/
22#include "SDL_config.h"
23
24/* An implementation of condition variables using semaphores and mutexes */
25/*
26 This implementation borrows heavily from the BeOS condition variable
27 implementation, written by Christopher Tate and Owen Smith. Thanks!
28 */
29
30#include "SDL_thread.h"
31
32struct SDL_cond
33{
34 SDL_mutex *lock;
35 int waiting;
36 int signals;
37 SDL_sem *wait_sem;
38 SDL_sem *wait_done;
39};
40
41/* Create a condition variable */
42SDL_cond * SDL_CreateCond(void)
43{
44 SDL_cond *cond;
45
46 cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
47 if ( cond ) {
48 cond->lock = SDL_CreateMutex();
49 cond->wait_sem = SDL_CreateSemaphore(0);
50 cond->wait_done = SDL_CreateSemaphore(0);
51 cond->waiting = cond->signals = 0;
52 if ( ! cond->lock || ! cond->wait_sem || ! cond->wait_done ) {
53 SDL_DestroyCond(cond);
54 cond = NULL;
55 }
56 } else {
57 SDL_OutOfMemory();
58 }
59 return(cond);
60}
61
62/* Destroy a condition variable */
63void SDL_DestroyCond(SDL_cond *cond)
64{
65 if ( cond ) {
66 if ( cond->wait_sem ) {
67 SDL_DestroySemaphore(cond->wait_sem);
68 }
69 if ( cond->wait_done ) {
70 SDL_DestroySemaphore(cond->wait_done);
71 }
72 if ( cond->lock ) {
73 SDL_DestroyMutex(cond->lock);
74 }
75 SDL_free(cond);
76 }
77}
78
79/* Restart one of the threads that are waiting on the condition variable */
80int SDL_CondSignal(SDL_cond *cond)
81{
82 if ( ! cond ) {
83 SDL_SetError("Passed a NULL condition variable");
84 return -1;
85 }
86
87 /* If there are waiting threads not already signalled, then
88 signal the condition and wait for the thread to respond.
89 */
90 SDL_LockMutex(cond->lock);
91 if ( cond->waiting > cond->signals ) {
92 ++cond->signals;
93 SDL_SemPost(cond->wait_sem);
94 SDL_UnlockMutex(cond->lock);
95 SDL_SemWait(cond->wait_done);
96 } else {
97 SDL_UnlockMutex(cond->lock);
98 }
99
100 return 0;
101}
102
103/* Restart all threads that are waiting on the condition variable */
104int SDL_CondBroadcast(SDL_cond *cond)
105{
106 if ( ! cond ) {
107 SDL_SetError("Passed a NULL condition variable");
108 return -1;
109 }
110
111 /* If there are waiting threads not already signalled, then
112 signal the condition and wait for the thread to respond.
113 */
114 SDL_LockMutex(cond->lock);
115 if ( cond->waiting > cond->signals ) {
116 int i, num_waiting;
117
118 num_waiting = (cond->waiting - cond->signals);
119 cond->signals = cond->waiting;
120 for ( i=0; i<num_waiting; ++i ) {
121 SDL_SemPost(cond->wait_sem);
122 }
123 /* Now all released threads are blocked here, waiting for us.
124 Collect them all (and win fabulous prizes!) :-)
125 */
126 SDL_UnlockMutex(cond->lock);
127 for ( i=0; i<num_waiting; ++i ) {
128 SDL_SemWait(cond->wait_done);
129 }
130 } else {
131 SDL_UnlockMutex(cond->lock);
132 }
133
134 return 0;
135}
136
137/* Wait on the condition variable for at most 'ms' milliseconds.
138 The mutex must be locked before entering this function!
139 The mutex is unlocked during the wait, and locked again after the wait.
140
141Typical use:
142
143Thread A:
144 SDL_LockMutex(lock);
145 while ( ! condition ) {
146 SDL_CondWait(cond);
147 }
148 SDL_UnlockMutex(lock);
149
150Thread B:
151 SDL_LockMutex(lock);
152 ...
153 condition = true;
154 ...
155 SDL_UnlockMutex(lock);
156 */
157int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
158{
159 int retval;
160
161 if ( ! cond ) {
162 SDL_SetError("Passed a NULL condition variable");
163 return -1;
164 }
165
166 /* Obtain the protection mutex, and increment the number of waiters.
167 This allows the signal mechanism to only perform a signal if there
168 are waiting threads.
169 */
170 SDL_LockMutex(cond->lock);
171 ++cond->waiting;
172 SDL_UnlockMutex(cond->lock);
173
174 /* Unlock the mutex, as is required by condition variable semantics */
175 SDL_UnlockMutex(mutex);
176
177 /* Wait for a signal */
178 if ( ms == SDL_MUTEX_MAXWAIT ) {
179 retval = SDL_SemWait(cond->wait_sem);
180 } else {
181 retval = SDL_SemWaitTimeout(cond->wait_sem, ms);
182 }
183
184 /* Let the signaler know we have completed the wait, otherwise
185 the signaler can race ahead and get the condition semaphore
186 if we are stopped between the mutex unlock and semaphore wait,
187 giving a deadlock. See the following URL for details:
188 http://www-classic.be.com/aboutbe/benewsletter/volume_III/Issue40.html
189 */
190 SDL_LockMutex(cond->lock);
191 if ( cond->signals > 0 ) {
192 /* If we timed out, we need to eat a condition signal */
193 if ( retval > 0 ) {
194 SDL_SemWait(cond->wait_sem);
195 }
196 /* We always notify the signal thread that we are done */
197 SDL_SemPost(cond->wait_done);
198
199 /* Signal handshake complete */
200 --cond->signals;
201 }
202 --cond->waiting;
203 SDL_UnlockMutex(cond->lock);
204
205 /* Lock the mutex, as is required by condition variable semantics */
206 SDL_LockMutex(mutex);
207
208 return retval;
209}
210
211/* Wait on the condition variable forever */
212int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
213{
214 return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
215}
diff --git a/apps/plugins/sdl/src/thread/generic/SDL_sysmutex.c b/apps/plugins/sdl/src/thread/generic/SDL_sysmutex.c
new file mode 100644
index 0000000000..ecfea876a5
--- /dev/null
+++ b/apps/plugins/sdl/src/thread/generic/SDL_sysmutex.c
@@ -0,0 +1,129 @@
1/*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2012 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 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 Sam Lantinga
20 slouken@libsdl.org
21*/
22#include "SDL_config.h"
23
24/* An implementation of mutexes using semaphores */
25
26#include "SDL_thread.h"
27#include "SDL_systhread_c.h"
28
29
30struct SDL_mutex {
31 int recursive;
32 Uint32 owner;
33 SDL_sem *sem;
34};
35
36/* Create a mutex */
37SDL_mutex *SDL_CreateMutex(void)
38{
39 SDL_mutex *mutex;
40
41 /* Allocate mutex memory */
42 mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex));
43 if ( mutex ) {
44 /* Create the mutex semaphore, with initial value 1 */
45 mutex->sem = SDL_CreateSemaphore(1);
46 mutex->recursive = 0;
47 mutex->owner = 0;
48 if ( ! mutex->sem ) {
49 SDL_free(mutex);
50 mutex = NULL;
51 }
52 } else {
53 SDL_OutOfMemory();
54 }
55 return mutex;
56}
57
58/* Free the mutex */
59void SDL_DestroyMutex(SDL_mutex *mutex)
60{
61 if ( mutex ) {
62 if ( mutex->sem ) {
63 SDL_DestroySemaphore(mutex->sem);
64 }
65 SDL_free(mutex);
66 }
67}
68
69/* Lock the semaphore */
70int SDL_mutexP(SDL_mutex *mutex)
71{
72#if SDL_THREADS_DISABLED
73 return 0;
74#else
75 Uint32 this_thread;
76
77 if ( mutex == NULL ) {
78 SDL_SetError("Passed a NULL mutex");
79 return -1;
80 }
81
82 this_thread = SDL_ThreadID();
83 if ( mutex->owner == this_thread ) {
84 ++mutex->recursive;
85 } else {
86 /* The order of operations is important.
87 We set the locking thread id after we obtain the lock
88 so unlocks from other threads will fail.
89 */
90 SDL_SemWait(mutex->sem);
91 mutex->owner = this_thread;
92 mutex->recursive = 0;
93 }
94
95 return 0;
96#endif /* SDL_THREADS_DISABLED */
97}
98
99/* Unlock the mutex */
100int SDL_mutexV(SDL_mutex *mutex)
101{
102#if SDL_THREADS_DISABLED
103 return 0;
104#else
105 if ( mutex == NULL ) {
106 SDL_SetError("Passed a NULL mutex");
107 return -1;
108 }
109
110 /* If we don't own the mutex, we can't unlock it */
111 if ( SDL_ThreadID() != mutex->owner ) {
112 SDL_SetError("mutex not owned by this thread");
113 return -1;
114 }
115
116 if ( mutex->recursive ) {
117 --mutex->recursive;
118 } else {
119 /* The order of operations is important.
120 First reset the owner so another thread doesn't lock
121 the mutex and set the ownership before we reset it,
122 then release the lock semaphore.
123 */
124 mutex->owner = 0;
125 SDL_SemPost(mutex->sem);
126 }
127 return 0;
128#endif /* SDL_THREADS_DISABLED */
129}
diff --git a/apps/plugins/sdl/src/thread/generic/SDL_sysmutex_c.h b/apps/plugins/sdl/src/thread/generic/SDL_sysmutex_c.h
new file mode 100644
index 0000000000..1120b2d80d
--- /dev/null
+++ b/apps/plugins/sdl/src/thread/generic/SDL_sysmutex_c.h
@@ -0,0 +1,23 @@
1/*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2012 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 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 Sam Lantinga
20 slouken@libsdl.org
21*/
22#include "SDL_config.h"
23
diff --git a/apps/plugins/sdl/src/thread/generic/SDL_syssem.c b/apps/plugins/sdl/src/thread/generic/SDL_syssem.c
new file mode 100644
index 0000000000..1d289c05d0
--- /dev/null
+++ b/apps/plugins/sdl/src/thread/generic/SDL_syssem.c
@@ -0,0 +1,211 @@
1/*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2012 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 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 Sam Lantinga
20 slouken@libsdl.org
21*/
22#include "SDL_config.h"
23
24/* An implementation of semaphores using mutexes and condition variables */
25
26#include "SDL_timer.h"
27#include "SDL_thread.h"
28#include "SDL_systhread_c.h"
29
30
31#if SDL_THREADS_DISABLED
32
33SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
34{
35 SDL_SetError("SDL not configured with thread support");
36 return (SDL_sem *)0;
37}
38
39void SDL_DestroySemaphore(SDL_sem *sem)
40{
41 return;
42}
43
44int SDL_SemTryWait(SDL_sem *sem)
45{
46 SDL_SetError("SDL not configured with thread support");
47 return -1;
48}
49
50int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
51{
52 SDL_SetError("SDL not configured with thread support");
53 return -1;
54}
55
56int SDL_SemWait(SDL_sem *sem)
57{
58 SDL_SetError("SDL not configured with thread support");
59 return -1;
60}
61
62Uint32 SDL_SemValue(SDL_sem *sem)
63{
64 return 0;
65}
66
67int SDL_SemPost(SDL_sem *sem)
68{
69 SDL_SetError("SDL not configured with thread support");
70 return -1;
71}
72
73#else
74
75struct SDL_semaphore
76{
77 Uint32 count;
78 Uint32 waiters_count;
79 SDL_mutex *count_lock;
80 SDL_cond *count_nonzero;
81};
82
83SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
84{
85 SDL_sem *sem;
86
87 sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
88 if ( ! sem ) {
89 SDL_OutOfMemory();
90 return NULL;
91 }
92 sem->count = initial_value;
93 sem->waiters_count = 0;
94
95 sem->count_lock = SDL_CreateMutex();
96 sem->count_nonzero = SDL_CreateCond();
97 if ( ! sem->count_lock || ! sem->count_nonzero ) {
98 SDL_DestroySemaphore(sem);
99 return NULL;
100 }
101
102 return sem;
103}
104
105/* WARNING:
106 You cannot call this function when another thread is using the semaphore.
107*/
108void SDL_DestroySemaphore(SDL_sem *sem)
109{
110 if ( sem ) {
111 sem->count = 0xFFFFFFFF;
112 while ( sem->waiters_count > 0) {
113 SDL_CondSignal(sem->count_nonzero);
114 SDL_Delay(10);
115 }
116 SDL_DestroyCond(sem->count_nonzero);
117 if ( sem->count_lock ) {
118 SDL_mutexP(sem->count_lock);
119 SDL_mutexV(sem->count_lock);
120 SDL_DestroyMutex(sem->count_lock);
121 }
122 SDL_free(sem);
123 }
124}
125
126int SDL_SemTryWait(SDL_sem *sem)
127{
128 int retval;
129
130 if ( ! sem ) {
131 SDL_SetError("Passed a NULL semaphore");
132 return -1;
133 }
134
135 retval = SDL_MUTEX_TIMEDOUT;
136 SDL_LockMutex(sem->count_lock);
137 if ( sem->count > 0 ) {
138 --sem->count;
139 retval = 0;
140 }
141 SDL_UnlockMutex(sem->count_lock);
142
143 return retval;
144}
145
146int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
147{
148 int retval;
149
150 if ( ! sem ) {
151 SDL_SetError("Passed a NULL semaphore");
152 return -1;
153 }
154
155 /* A timeout of 0 is an easy case */
156 if ( timeout == 0 ) {
157 return SDL_SemTryWait(sem);
158 }
159
160 SDL_LockMutex(sem->count_lock);
161 ++sem->waiters_count;
162 retval = 0;
163 while ( (sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT) ) {
164 retval = SDL_CondWaitTimeout(sem->count_nonzero,
165 sem->count_lock, timeout);
166 }
167 --sem->waiters_count;
168 if (retval == 0) {
169 --sem->count;
170 }
171 SDL_UnlockMutex(sem->count_lock);
172
173 return retval;
174}
175
176int SDL_SemWait(SDL_sem *sem)
177{
178 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
179}
180
181Uint32 SDL_SemValue(SDL_sem *sem)
182{
183 Uint32 value;
184
185 value = 0;
186 if ( sem ) {
187 SDL_LockMutex(sem->count_lock);
188 value = sem->count;
189 SDL_UnlockMutex(sem->count_lock);
190 }
191 return value;
192}
193
194int SDL_SemPost(SDL_sem *sem)
195{
196 if ( ! sem ) {
197 SDL_SetError("Passed a NULL semaphore");
198 return -1;
199 }
200
201 SDL_LockMutex(sem->count_lock);
202 if ( sem->waiters_count > 0 ) {
203 SDL_CondSignal(sem->count_nonzero);
204 }
205 ++sem->count;
206 SDL_UnlockMutex(sem->count_lock);
207
208 return 0;
209}
210
211#endif /* SDL_THREADS_DISABLED */
diff --git a/apps/plugins/sdl/src/thread/generic/SDL_systhread.c b/apps/plugins/sdl/src/thread/generic/SDL_systhread.c
new file mode 100644
index 0000000000..c380121ff6
--- /dev/null
+++ b/apps/plugins/sdl/src/thread/generic/SDL_systhread.c
@@ -0,0 +1,54 @@
1/*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2012 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 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 Sam Lantinga
20 slouken@libsdl.org
21*/
22#include "SDL_config.h"
23
24/* Thread management routines for SDL */
25
26#include "SDL_thread.h"
27#include "../SDL_systhread.h"
28
29int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
30{
31 SDL_SetError("Threads are not supported on this platform");
32 return(-1);
33}
34
35void SDL_SYS_SetupThread(void)
36{
37 return;
38}
39
40Uint32 SDL_ThreadID(void)
41{
42 return(0);
43}
44
45void SDL_SYS_WaitThread(SDL_Thread *thread)
46{
47 return;
48}
49
50void SDL_SYS_KillThread(SDL_Thread *thread)
51{
52 return;
53}
54
diff --git a/apps/plugins/sdl/src/thread/generic/SDL_systhread_c.h b/apps/plugins/sdl/src/thread/generic/SDL_systhread_c.h
new file mode 100644
index 0000000000..1535b50191
--- /dev/null
+++ b/apps/plugins/sdl/src/thread/generic/SDL_systhread_c.h
@@ -0,0 +1,25 @@
1/*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2012 Sam Lantinga
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 Sam Lantinga
20 slouken@libsdl.org
21*/
22#include "SDL_config.h"
23
24/* Stub until we implement threads on this platform */
25typedef int SYS_ThreadHandle;