summaryrefslogtreecommitdiff
path: root/apps/plugins/sdl/src/thread
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2018-02-07 20:04:46 -0500
committerFranklin Wei <git@fwei.tk>2018-03-12 20:52:01 -0400
commit6039eb05ba6d82ef56f2868c96654c552d117bf9 (patch)
tree9db7016bcbf66cfdf7b9bc998d84c6eaff9c8378 /apps/plugins/sdl/src/thread
parentef373c03b96b0be08babca581d9f10bccfd4931f (diff)
downloadrockbox-6039eb05ba6d82ef56f2868c96654c552d117bf9.tar.gz
rockbox-6039eb05ba6d82ef56f2868c96654c552d117bf9.zip
sdl: remove non-rockbox drivers
We never use any of these other drivers, so having them around just takes up space. Change-Id: Iced812162df1fef3fd55522b7e700acb6c3bcd41
Diffstat (limited to 'apps/plugins/sdl/src/thread')
-rw-r--r--apps/plugins/sdl/src/thread/beos/SDL_syssem.c142
-rw-r--r--apps/plugins/sdl/src/thread/beos/SDL_systhread.c96
-rw-r--r--apps/plugins/sdl/src/thread/beos/SDL_systhread_c.h31
-rw-r--r--apps/plugins/sdl/src/thread/dc/SDL_syscond.c215
-rw-r--r--apps/plugins/sdl/src/thread/dc/SDL_syscond_c.h23
-rw-r--r--apps/plugins/sdl/src/thread/dc/SDL_sysmutex.c122
-rw-r--r--apps/plugins/sdl/src/thread/dc/SDL_sysmutex_c.h23
-rw-r--r--apps/plugins/sdl/src/thread/dc/SDL_syssem.c173
-rw-r--r--apps/plugins/sdl/src/thread/dc/SDL_syssem_c.h23
-rw-r--r--apps/plugins/sdl/src/thread/dc/SDL_systhread.c60
-rw-r--r--apps/plugins/sdl/src/thread/dc/SDL_systhread_c.h24
-rw-r--r--apps/plugins/sdl/src/thread/irix/SDL_syssem.c219
-rw-r--r--apps/plugins/sdl/src/thread/irix/SDL_systhread.c85
-rw-r--r--apps/plugins/sdl/src/thread/irix/SDL_systhread_c.h27
-rw-r--r--apps/plugins/sdl/src/thread/os2/SDL_syscond.c215
-rw-r--r--apps/plugins/sdl/src/thread/os2/SDL_syscond_c.h23
-rw-r--r--apps/plugins/sdl/src/thread/os2/SDL_sysmutex.c108
-rw-r--r--apps/plugins/sdl/src/thread/os2/SDL_syssem.c192
-rw-r--r--apps/plugins/sdl/src/thread/os2/SDL_systhread.c108
-rw-r--r--apps/plugins/sdl/src/thread/os2/SDL_systhread_c.h28
-rw-r--r--apps/plugins/sdl/src/thread/pth/SDL_syscond.c164
-rw-r--r--apps/plugins/sdl/src/thread/pth/SDL_sysmutex.c87
-rw-r--r--apps/plugins/sdl/src/thread/pth/SDL_sysmutex_c.h31
-rw-r--r--apps/plugins/sdl/src/thread/pth/SDL_systhread.c103
-rw-r--r--apps/plugins/sdl/src/thread/pth/SDL_systhread_c.h31
-rw-r--r--apps/plugins/sdl/src/thread/pthread/SDL_syscond.c155
-rw-r--r--apps/plugins/sdl/src/thread/pthread/SDL_sysmutex.c153
-rw-r--r--apps/plugins/sdl/src/thread/pthread/SDL_sysmutex_c.h31
-rw-r--r--apps/plugins/sdl/src/thread/pthread/SDL_syssem.c190
-rw-r--r--apps/plugins/sdl/src/thread/pthread/SDL_systhread.c120
-rw-r--r--apps/plugins/sdl/src/thread/pthread/SDL_systhread_c.h26
-rw-r--r--apps/plugins/sdl/src/thread/riscos/SDL_syscond.c160
-rw-r--r--apps/plugins/sdl/src/thread/riscos/SDL_sysmutex.c153
-rw-r--r--apps/plugins/sdl/src/thread/riscos/SDL_sysmutex_c.h34
-rw-r--r--apps/plugins/sdl/src/thread/riscos/SDL_syssem.c203
-rw-r--r--apps/plugins/sdl/src/thread/riscos/SDL_systhread.c144
-rw-r--r--apps/plugins/sdl/src/thread/riscos/SDL_systhread_c.h34
-rw-r--r--apps/plugins/sdl/src/thread/symbian/SDL_sysmutex.cpp130
-rw-r--r--apps/plugins/sdl/src/thread/symbian/SDL_syssem.cpp214
-rw-r--r--apps/plugins/sdl/src/thread/symbian/SDL_systhread.cpp146
-rw-r--r--apps/plugins/sdl/src/thread/symbian/SDL_systhread_c.h30
-rw-r--r--apps/plugins/sdl/src/thread/win32/SDL_sysmutex.c95
-rw-r--r--apps/plugins/sdl/src/thread/win32/SDL_syssem.c164
-rw-r--r--apps/plugins/sdl/src/thread/win32/SDL_systhread.c162
-rw-r--r--apps/plugins/sdl/src/thread/win32/SDL_systhread_c.h28
-rw-r--r--apps/plugins/sdl/src/thread/win32/win_ce_semaphore.c216
-rw-r--r--apps/plugins/sdl/src/thread/win32/win_ce_semaphore.h22
47 files changed, 0 insertions, 4963 deletions
diff --git a/apps/plugins/sdl/src/thread/beos/SDL_syssem.c b/apps/plugins/sdl/src/thread/beos/SDL_syssem.c
deleted file mode 100644
index eba1944bc8..0000000000
--- a/apps/plugins/sdl/src/thread/beos/SDL_syssem.c
+++ /dev/null
@@ -1,142 +0,0 @@
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/* Semaphores in the BeOS environment */
25
26#include <be/kernel/OS.h>
27
28#include "SDL_thread.h"
29
30
31struct SDL_semaphore {
32 sem_id id;
33};
34
35/* Create a counting semaphore */
36SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
37{
38 SDL_sem *sem;
39
40 sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
41 if ( sem ) {
42 sem->id = create_sem(initial_value, "SDL semaphore");
43 if ( sem->id < B_NO_ERROR ) {
44 SDL_SetError("create_sem() failed");
45 SDL_free(sem);
46 sem = NULL;
47 }
48 } else {
49 SDL_OutOfMemory();
50 }
51 return(sem);
52}
53
54/* Free the semaphore */
55void SDL_DestroySemaphore(SDL_sem *sem)
56{
57 if ( sem ) {
58 if ( sem->id >= B_NO_ERROR ) {
59 delete_sem(sem->id);
60 }
61 SDL_free(sem);
62 }
63}
64
65int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
66{
67 int32 val;
68 int retval;
69
70 if ( ! sem ) {
71 SDL_SetError("Passed a NULL semaphore");
72 return -1;
73 }
74
75 tryagain:
76 if ( timeout == SDL_MUTEX_MAXWAIT ) {
77 val = acquire_sem(sem->id);
78 } else {
79 timeout *= 1000; /* BeOS uses a timeout in microseconds */
80 val = acquire_sem_etc(sem->id, 1, B_RELATIVE_TIMEOUT, timeout);
81 }
82 switch (val) {
83 case B_INTERRUPTED:
84 goto tryagain;
85 case B_NO_ERROR:
86 retval = 0;
87 break;
88 case B_TIMED_OUT:
89 retval = SDL_MUTEX_TIMEDOUT;
90 break;
91 case B_WOULD_BLOCK:
92 retval = SDL_MUTEX_TIMEDOUT;
93 break;
94 default:
95 SDL_SetError("acquire_sem() failed");
96 retval = -1;
97 break;
98 }
99
100 return retval;
101}
102
103int SDL_SemTryWait(SDL_sem *sem)
104{
105 return SDL_SemWaitTimeout(sem, 0);
106}
107
108int SDL_SemWait(SDL_sem *sem)
109{
110 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
111}
112
113/* Returns the current count of the semaphore */
114Uint32 SDL_SemValue(SDL_sem *sem)
115{
116 int32 count;
117 Uint32 value;
118
119 value = 0;
120 if ( sem ) {
121 get_sem_count(sem->id, &count);
122 if ( count > 0 ) {
123 value = (Uint32)count;
124 }
125 }
126 return value;
127}
128
129/* Atomically increases the semaphore's count (not blocking) */
130int SDL_SemPost(SDL_sem *sem)
131{
132 if ( ! sem ) {
133 SDL_SetError("Passed a NULL semaphore");
134 return -1;
135 }
136
137 if ( release_sem(sem->id) != B_NO_ERROR ) {
138 SDL_SetError("release_sem() failed");
139 return -1;
140 }
141 return 0;
142}
diff --git a/apps/plugins/sdl/src/thread/beos/SDL_systhread.c b/apps/plugins/sdl/src/thread/beos/SDL_systhread.c
deleted file mode 100644
index 63b52075f0..0000000000
--- a/apps/plugins/sdl/src/thread/beos/SDL_systhread.c
+++ /dev/null
@@ -1,96 +0,0 @@
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/* BeOS thread management routines for SDL */
25
26#include <stdio.h>
27#include <signal.h>
28#include <be/kernel/OS.h>
29
30#include "SDL_mutex.h"
31#include "SDL_thread.h"
32#include "../SDL_thread_c.h"
33#include "../SDL_systhread.h"
34
35
36static int sig_list[] = {
37 SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGWINCH, 0
38};
39
40void SDL_MaskSignals(sigset_t *omask)
41{
42 sigset_t mask;
43 int i;
44
45 sigemptyset(&mask);
46 for ( i=0; sig_list[i]; ++i ) {
47 sigaddset(&mask, sig_list[i]);
48 }
49 sigprocmask(SIG_BLOCK, &mask, omask);
50}
51void SDL_UnmaskSignals(sigset_t *omask)
52{
53 sigprocmask(SIG_SETMASK, omask, NULL);
54}
55
56static int32 RunThread(void *data)
57{
58 SDL_RunThread(data);
59 return(0);
60}
61
62int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
63{
64 /* Create the thread and go! */
65 thread->handle=spawn_thread(RunThread, "SDL", B_NORMAL_PRIORITY, args);
66 if ( (thread->handle == B_NO_MORE_THREADS) ||
67 (thread->handle == B_NO_MEMORY) ) {
68 SDL_SetError("Not enough resources to create thread");
69 return(-1);
70 }
71 resume_thread(thread->handle);
72 return(0);
73}
74
75void SDL_SYS_SetupThread(void)
76{
77 /* Mask asynchronous signals for this thread */
78 SDL_MaskSignals(NULL);
79}
80
81Uint32 SDL_ThreadID(void)
82{
83 return((Uint32)find_thread(NULL));
84}
85
86void SDL_SYS_WaitThread(SDL_Thread *thread)
87{
88 status_t the_status;
89
90 wait_for_thread(thread->handle, &the_status);
91}
92
93void SDL_SYS_KillThread(SDL_Thread *thread)
94{
95 kill_thread(thread->handle);
96}
diff --git a/apps/plugins/sdl/src/thread/beos/SDL_systhread_c.h b/apps/plugins/sdl/src/thread/beos/SDL_systhread_c.h
deleted file mode 100644
index f82548e463..0000000000
--- a/apps/plugins/sdl/src/thread/beos/SDL_systhread_c.h
+++ /dev/null
@@ -1,31 +0,0 @@
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#include <signal.h>
25#include <be/kernel/OS.h>
26
27typedef thread_id SYS_ThreadHandle;
28
29/* Functions needed to work with system threads in other portions of SDL */
30extern void SDL_MaskSignals(sigset_t *omask);
31extern void SDL_UnmaskSignals(sigset_t *omask);
diff --git a/apps/plugins/sdl/src/thread/dc/SDL_syscond.c b/apps/plugins/sdl/src/thread/dc/SDL_syscond.c
deleted file mode 100644
index f6e7223e3d..0000000000
--- a/apps/plugins/sdl/src/thread/dc/SDL_syscond.c
+++ /dev/null
@@ -1,215 +0,0 @@
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/dc/SDL_syscond_c.h b/apps/plugins/sdl/src/thread/dc/SDL_syscond_c.h
deleted file mode 100644
index 1120b2d80d..0000000000
--- a/apps/plugins/sdl/src/thread/dc/SDL_syscond_c.h
+++ /dev/null
@@ -1,23 +0,0 @@
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/dc/SDL_sysmutex.c b/apps/plugins/sdl/src/thread/dc/SDL_sysmutex.c
deleted file mode 100644
index c6c465131b..0000000000
--- a/apps/plugins/sdl/src/thread/dc/SDL_sysmutex.c
+++ /dev/null
@@ -1,122 +0,0 @@
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#include <arch/spinlock.h>
30
31struct SDL_mutex {
32 int recursive;
33 Uint32 owner;
34 spinlock_t mutex;
35};
36
37/* Create a mutex */
38SDL_mutex *SDL_CreateMutex(void)
39{
40 SDL_mutex *mutex;
41
42 /* Allocate mutex memory */
43 mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex));
44 if ( mutex ) {
45 spinlock_init(&mutex->mutex);
46 mutex->recursive = 0;
47 mutex->owner = 0;
48 } else {
49 SDL_OutOfMemory();
50 }
51 return mutex;
52}
53
54/* Free the mutex */
55void SDL_DestroyMutex(SDL_mutex *mutex)
56{
57 if ( mutex ) {
58 SDL_free(mutex);
59 }
60}
61
62/* Lock the semaphore */
63int SDL_mutexP(SDL_mutex *mutex)
64{
65#if SDL_THREADS_DISABLED
66 return SDL_arraysize(return ),0;
67#else
68 Uint32 this_thread;
69
70 if ( mutex == NULL ) {
71 SDL_SetError("Passed a NULL mutex");
72 return -1;
73 }
74
75 this_thread = SDL_ThreadID();
76 if ( mutex->owner == this_thread ) {
77 ++mutex->recursive;
78 } else {
79 /* The order of operations is important.
80 We set the locking thread id after we obtain the lock
81 so unlocks from other threads will fail.
82 */
83 spinlock_lock(&mutex->mutex);
84 mutex->owner = this_thread;
85 mutex->recursive = 0;
86 }
87
88 return 0;
89#endif /* SDL_THREADS_DISABLED */
90}
91
92/* Unlock the mutex */
93int SDL_mutexV(SDL_mutex *mutex)
94{
95#if SDL_THREADS_DISABLED
96 return 0;
97#else
98 if ( mutex == NULL ) {
99 SDL_SetError("Passed a NULL mutex");
100 return -1;
101 }
102
103 /* If we don't own the mutex, we can't unlock it */
104 if ( SDL_ThreadID() != mutex->owner ) {
105 SDL_SetError("mutex not owned by this thread");
106 return -1;
107 }
108
109 if ( mutex->recursive ) {
110 --mutex->recursive;
111 } else {
112 /* The order of operations is important.
113 First reset the owner so another thread doesn't lock
114 the mutex and set the ownership before we reset it,
115 then release the lock semaphore.
116 */
117 mutex->owner = 0;
118 spinlock_unlock(&mutex->mutex);
119 }
120 return 0;
121#endif /* SDL_THREADS_DISABLED */
122}
diff --git a/apps/plugins/sdl/src/thread/dc/SDL_sysmutex_c.h b/apps/plugins/sdl/src/thread/dc/SDL_sysmutex_c.h
deleted file mode 100644
index 1120b2d80d..0000000000
--- a/apps/plugins/sdl/src/thread/dc/SDL_sysmutex_c.h
+++ /dev/null
@@ -1,23 +0,0 @@
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/dc/SDL_syssem.c b/apps/plugins/sdl/src/thread/dc/SDL_syssem.c
deleted file mode 100644
index 9831ccdbd7..0000000000
--- a/apps/plugins/sdl/src/thread/dc/SDL_syssem.c
+++ /dev/null
@@ -1,173 +0,0 @@
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
23#include <errno.h>
24
25#include "SDL_config.h"
26
27/* An implementation of semaphores using mutexes and condition variables */
28
29#include "SDL_timer.h"
30#include "SDL_thread.h"
31#include "SDL_systhread_c.h"
32
33
34#if SDL_THREADS_DISABLED
35
36SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
37{
38 SDL_SetError("SDL not configured with thread support");
39 return (SDL_sem *)0;
40}
41
42void SDL_DestroySemaphore(SDL_sem *sem)
43{
44 return;
45}
46
47int SDL_SemTryWait(SDL_sem *sem)
48{
49 SDL_SetError("SDL not configured with thread support");
50 return -1;
51}
52
53int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
54{
55 SDL_SetError("SDL not configured with thread support");
56 return -1;
57}
58
59int SDL_SemWait(SDL_sem *sem)
60{
61 SDL_SetError("SDL not configured with thread support");
62 return -1;
63}
64
65Uint32 SDL_SemValue(SDL_sem *sem)
66{
67 return 0;
68}
69
70int SDL_SemPost(SDL_sem *sem)
71{
72 SDL_SetError("SDL not configured with thread support");
73 return -1;
74}
75
76#else
77
78#include <kos/sem.h>
79
80struct SDL_semaphore
81{
82 semaphore_t sem;
83};
84
85SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
86{
87 return (SDL_sem *)sem_create(initial_value);
88}
89
90/* WARNING:
91 You cannot call this function when another thread is using the semaphore.
92*/
93void SDL_DestroySemaphore(SDL_sem *sem)
94{
95 if ( ! sem ) {
96 SDL_SetError("Passed a NULL semaphore");
97 return;
98 }
99
100 sem_destroy(&sem->sem);
101}
102
103int SDL_SemTryWait(SDL_sem *sem)
104{
105 int retval;
106
107 if ( ! sem ) {
108 SDL_SetError("Passed a NULL semaphore");
109 return -1;
110 }
111
112 retval = sem_trywait(&sem->sem);
113 if (retval==0) return 0;
114 else return SDL_MUTEX_TIMEDOUT;
115
116 return retval;
117}
118
119int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
120{
121 int retval;
122
123 if ( ! sem ) {
124 SDL_SetError("Passed a NULL semaphore");
125 return -1;
126 }
127
128 /* A timeout of 0 is an easy case */
129 if ( timeout == 0 ) {
130 return SDL_SemTryWait(sem);
131 }
132
133 retval = sem_wait_timed(&sem->sem,timeout);
134 if (retval==-1) retval= SDL_MUTEX_TIMEDOUT;
135
136 return retval;
137}
138
139int SDL_SemWait(SDL_sem *sem)
140{
141 int retval;
142
143 if ( ! sem ) {
144 SDL_SetError("Passed a NULL semaphore");
145 return -1;
146 }
147
148 while ( ((retval = sem_wait(&sem->sem)) == -1) && (errno == EINTR) ) {}
149 return retval;
150}
151
152Uint32 SDL_SemValue(SDL_sem *sem)
153{
154 if ( ! sem ) {
155 SDL_SetError("Passed a NULL semaphore");
156 return -1;
157 }
158
159 return sem_count(&sem->sem);
160}
161
162int SDL_SemPost(SDL_sem *sem)
163{
164 if ( ! sem ) {
165 SDL_SetError("Passed a NULL semaphore");
166 return -1;
167 }
168
169 sem_signal(&sem->sem);
170 return 0;
171}
172
173#endif /* SDL_THREADS_DISABLED */
diff --git a/apps/plugins/sdl/src/thread/dc/SDL_syssem_c.h b/apps/plugins/sdl/src/thread/dc/SDL_syssem_c.h
deleted file mode 100644
index 1120b2d80d..0000000000
--- a/apps/plugins/sdl/src/thread/dc/SDL_syssem_c.h
+++ /dev/null
@@ -1,23 +0,0 @@
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/dc/SDL_systhread.c b/apps/plugins/sdl/src/thread/dc/SDL_systhread.c
deleted file mode 100644
index dd26675491..0000000000
--- a/apps/plugins/sdl/src/thread/dc/SDL_systhread.c
+++ /dev/null
@@ -1,60 +0,0 @@
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_thread_c.h"
28#include "../SDL_systhread.h"
29
30#include <kos/thread.h>
31
32int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
33{
34 thread->handle = thd_create(SDL_RunThread,args);
35 if (thread->handle == NULL) {
36 SDL_SetError("Not enough resources to create thread");
37 return(-1);
38 }
39 return(0);
40}
41
42void SDL_SYS_SetupThread(void)
43{
44 return;
45}
46
47Uint32 SDL_ThreadID(void)
48{
49 return (Uint32)thd_get_current();
50}
51
52void SDL_SYS_WaitThread(SDL_Thread *thread)
53{
54 thd_wait(thread->handle);
55}
56
57void SDL_SYS_KillThread(SDL_Thread *thread)
58{
59 thd_destroy(thread->handle);
60}
diff --git a/apps/plugins/sdl/src/thread/dc/SDL_systhread_c.h b/apps/plugins/sdl/src/thread/dc/SDL_systhread_c.h
deleted file mode 100644
index 3cda1a4a51..0000000000
--- a/apps/plugins/sdl/src/thread/dc/SDL_systhread_c.h
+++ /dev/null
@@ -1,24 +0,0 @@
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
24typedef struct kthread* SYS_ThreadHandle;
diff --git a/apps/plugins/sdl/src/thread/irix/SDL_syssem.c b/apps/plugins/sdl/src/thread/irix/SDL_syssem.c
deleted file mode 100644
index 208c379cdd..0000000000
--- a/apps/plugins/sdl/src/thread/irix/SDL_syssem.c
+++ /dev/null
@@ -1,219 +0,0 @@
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#include "SDL_thread.h"
25#include "SDL_timer.h"
26
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <sys/types.h>
31#include <sys/ipc.h>
32#include <sys/sem.h>
33#include <errno.h>
34
35#include "SDL_error.h"
36#include "SDL_thread.h"
37
38
39struct SDL_semaphore {
40 int id;
41};
42
43/* Not defined by many operating systems, use configure to detect */
44/*
45#if !defined(HAVE_SEMUN)
46union semun {
47 int val;
48 struct semid_ds *buf;
49 ushort *array;
50};
51#endif
52*/
53
54static struct sembuf op_trywait[2] = {
55 { 0, -1, (IPC_NOWAIT|SEM_UNDO) } /* Decrement semaphore, no block */
56};
57static struct sembuf op_wait[2] = {
58 { 0, -1, SEM_UNDO } /* Decrement semaphore */
59};
60static struct sembuf op_post[1] = {
61 { 0, 1, (IPC_NOWAIT|SEM_UNDO) } /* Increment semaphore */
62};
63
64/* Create a blockable semaphore */
65SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
66{
67 extern int _creating_thread_lock; /* SDL_threads.c */
68 SDL_sem *sem;
69 union semun init;
70
71 sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
72 if ( sem == NULL ) {
73 SDL_OutOfMemory();
74 return(NULL);
75 }
76 sem->id = semget(IPC_PRIVATE, 1, (0600|IPC_CREAT));
77 if ( sem->id < 0 ) {
78 SDL_SetError("Couldn't create semaphore");
79 SDL_free(sem);
80 return(NULL);
81 }
82 init.val = initial_value; /* Initialize semaphore */
83 semctl(sem->id, 0, SETVAL, init);
84 return(sem);
85}
86
87void SDL_DestroySemaphore(SDL_sem *sem)
88{
89 if ( sem ) {
90#ifdef __IRIX__
91 semctl(sem->id, 0, IPC_RMID);
92#else
93 union semun dummy;
94 dummy.val = 0;
95 semctl(sem->id, 0, IPC_RMID, dummy);
96#endif
97 SDL_free(sem);
98 }
99}
100
101int SDL_SemTryWait(SDL_sem *sem)
102{
103 int retval;
104
105 if ( ! sem ) {
106 SDL_SetError("Passed a NULL semaphore");
107 return -1;
108 }
109
110 retval = 0;
111 tryagain:
112 if ( semop(sem->id, op_trywait, 1) < 0 ) {
113 if ( errno == EINTR ) {
114 goto tryagain;
115 }
116 retval = SDL_MUTEX_TIMEDOUT;
117 }
118 return retval;
119}
120
121int SDL_SemWait(SDL_sem *sem)
122{
123 int retval;
124
125 if ( ! sem ) {
126 SDL_SetError("Passed a NULL semaphore");
127 return -1;
128 }
129
130 retval = 0;
131 tryagain:
132 if ( semop(sem->id, op_wait, 1) < 0 ) {
133 if ( errno == EINTR ) {
134 goto tryagain;
135 }
136 SDL_SetError("Semaphore operation error");
137 retval = -1;
138 }
139 return retval;
140}
141
142int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
143{
144 int retval;
145
146 if ( ! sem ) {
147 SDL_SetError("Passed a NULL semaphore");
148 return -1;
149 }
150
151 /* Try the easy cases first */
152 if ( timeout == 0 ) {
153 return SDL_SemTryWait(sem);
154 }
155 if ( timeout == SDL_MUTEX_MAXWAIT ) {
156 return SDL_SemWait(sem);
157 }
158
159 /* Ack! We have to busy wait... */
160 timeout += SDL_GetTicks();
161 do {
162 retval = SDL_SemTryWait(sem);
163 if ( retval == 0 ) {
164 break;
165 }
166 SDL_Delay(1);
167 } while ( SDL_GetTicks() < timeout );
168
169 return retval;
170}
171
172Uint32 SDL_SemValue(SDL_sem *sem)
173{
174 int semval;
175 Uint32 value;
176
177 value = 0;
178 if ( sem ) {
179 tryagain:
180#ifdef __IRIX__
181 semval = semctl(sem->id, 0, GETVAL);
182#else
183 {
184 union semun arg;
185 arg.val = 0;
186 semval = semctl(sem->id, 0, GETVAL, arg);
187 }
188#endif
189 if ( semval < 0 ) {
190 if ( errno == EINTR ) {
191 goto tryagain;
192 }
193 } else {
194 value = (Uint32)semval;
195 }
196 }
197 return value;
198}
199
200int SDL_SemPost(SDL_sem *sem)
201{
202 int retval;
203
204 if ( ! sem ) {
205 SDL_SetError("Passed a NULL semaphore");
206 return -1;
207 }
208
209 retval = 0;
210 tryagain:
211 if ( semop(sem->id, op_post, 1) < 0 ) {
212 if ( errno == EINTR ) {
213 goto tryagain;
214 }
215 SDL_SetError("Semaphore operation error");
216 retval = -1;
217 }
218 return retval;
219}
diff --git a/apps/plugins/sdl/src/thread/irix/SDL_systhread.c b/apps/plugins/sdl/src/thread/irix/SDL_systhread.c
deleted file mode 100644
index 7ae7fc6771..0000000000
--- a/apps/plugins/sdl/src/thread/irix/SDL_systhread.c
+++ /dev/null
@@ -1,85 +0,0 @@
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/* IRIX thread management routines for SDL */
25
26#include <errno.h>
27#include <signal.h>
28#include <sys/types.h>
29#include <sys/wait.h>
30#include <sys/prctl.h>
31
32#include "SDL_thread.h"
33#include "../SDL_systhread.h"
34
35
36static int sig_list[] = {
37 SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCLD, SIGWINCH,
38 SIGVTALRM, SIGPROF, 0
39};
40
41
42int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
43{
44 /* Create the thread and go! */
45 if ( sproc(SDL_RunThread, PR_SALL, args) < 0 ) {
46 SDL_SetError("Not enough resources to create thread");
47 return(-1);
48 }
49 return(0);
50}
51
52void SDL_SYS_SetupThread(void)
53{
54 int i;
55 sigset_t mask;
56
57 /* Mask asynchronous signals for this thread */
58 sigemptyset(&mask);
59 for ( i=0; sig_list[i]; ++i ) {
60 sigaddset(&mask, sig_list[i]);
61 }
62 sigprocmask(SIG_BLOCK, &mask, NULL);
63}
64
65/* WARNING: This may not work for systems with 64-bit pid_t */
66Uint32 SDL_ThreadID(void)
67{
68 return((Uint32)getpid());
69}
70
71/* WARNING: This may not work for systems with 64-bit pid_t */
72void SDL_WaitThread(SDL_Thread *thread, int *status)
73{
74 errno = 0;
75 while ( errno != ECHILD ) {
76 waitpid(thread->handle, NULL, 0);
77 }
78}
79
80/* WARNING: This may not work for systems with 64-bit pid_t */
81void SDL_KillThread(SDL_Thread *thread)
82{
83 kill(thread->handle, SIGKILL);
84}
85
diff --git a/apps/plugins/sdl/src/thread/irix/SDL_systhread_c.h b/apps/plugins/sdl/src/thread/irix/SDL_systhread_c.h
deleted file mode 100644
index ee28634fb1..0000000000
--- a/apps/plugins/sdl/src/thread/irix/SDL_systhread_c.h
+++ /dev/null
@@ -1,27 +0,0 @@
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#include <sys/types.h>
25
26typedef pid_t SYS_ThreadHandle;
27
diff --git a/apps/plugins/sdl/src/thread/os2/SDL_syscond.c b/apps/plugins/sdl/src/thread/os2/SDL_syscond.c
deleted file mode 100644
index 3e80594c2a..0000000000
--- a/apps/plugins/sdl/src/thread/os2/SDL_syscond.c
+++ /dev/null
@@ -1,215 +0,0 @@
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 */
42DECLSPEC SDL_cond * SDLCALL 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 */
63DECLSPEC void SDLCALL 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 */
80DECLSPEC int SDLCALL 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 */
104DECLSPEC int SDLCALL 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 */
157DECLSPEC int SDLCALL 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 */
212DECLSPEC int SDLCALL 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/os2/SDL_syscond_c.h b/apps/plugins/sdl/src/thread/os2/SDL_syscond_c.h
deleted file mode 100644
index 1120b2d80d..0000000000
--- a/apps/plugins/sdl/src/thread/os2/SDL_syscond_c.h
+++ /dev/null
@@ -1,23 +0,0 @@
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/os2/SDL_sysmutex.c b/apps/plugins/sdl/src/thread/os2/SDL_sysmutex.c
deleted file mode 100644
index 1e21897656..0000000000
--- a/apps/plugins/sdl/src/thread/os2/SDL_sysmutex.c
+++ /dev/null
@@ -1,108 +0,0 @@
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/* Mutex functions using the OS/2 API */
25
26#define INCL_DOSERRORS
27#define INCL_DOSSEMAPHORES
28#include <os2.h>
29
30#include "SDL_mutex.h"
31
32
33struct SDL_mutex {
34 HMTX hmtxID;
35};
36
37/* Create a mutex */
38DECLSPEC SDL_mutex * SDLCALL SDL_CreateMutex(void)
39{
40 SDL_mutex *mutex;
41 APIRET ulrc;
42
43 /* Allocate mutex memory */
44 mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex));
45 if (mutex)
46 {
47 /* Create the mutex, with initial value signaled */
48 ulrc = DosCreateMutexSem(NULL, // Create unnamed semaphore
49 &(mutex->hmtxID), // Pointer to handle
50 0L, // Flags: create it private (not shared)
51 FALSE); // Initial value: unowned
52 if (ulrc!=NO_ERROR)
53 {
54 SDL_SetError("Couldn't create mutex");
55 SDL_free(mutex);
56 mutex = NULL;
57 }
58 } else {
59 SDL_OutOfMemory();
60 }
61 return(mutex);
62}
63
64/* Free the mutex */
65DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex *mutex)
66{
67 if ( mutex )
68 {
69 if ( mutex->hmtxID )
70 {
71 DosCloseMutexSem(mutex->hmtxID);
72 mutex->hmtxID = 0;
73 }
74 SDL_free(mutex);
75 }
76}
77
78/* Lock the mutex */
79DECLSPEC int SDLCALL SDL_mutexP(SDL_mutex *mutex)
80{
81 if ( mutex == NULL )
82 {
83 SDL_SetError("Passed a NULL mutex");
84 return -1;
85 }
86 if ( DosRequestMutexSem(mutex->hmtxID, SEM_INDEFINITE_WAIT) != NO_ERROR )
87 {
88 SDL_SetError("Couldn't wait on mutex");
89 return -1;
90 }
91 return(0);
92}
93
94/* Unlock the mutex */
95DECLSPEC int SDLCALL SDL_mutexV(SDL_mutex *mutex)
96{
97 if ( mutex == NULL )
98 {
99 SDL_SetError("Passed a NULL mutex");
100 return -1;
101 }
102 if ( DosReleaseMutexSem(mutex->hmtxID) != NO_ERROR )
103 {
104 SDL_SetError("Couldn't release mutex");
105 return -1;
106 }
107 return(0);
108}
diff --git a/apps/plugins/sdl/src/thread/os2/SDL_syssem.c b/apps/plugins/sdl/src/thread/os2/SDL_syssem.c
deleted file mode 100644
index d6dfba67bb..0000000000
--- a/apps/plugins/sdl/src/thread/os2/SDL_syssem.c
+++ /dev/null
@@ -1,192 +0,0 @@
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/* Semaphore functions using the OS/2 API */
25
26#define INCL_DOS
27#define INCL_DOSERRORS
28#define INCL_DOSSEMAPHORES
29#include <os2.h>
30
31#include "SDL_thread.h"
32#include "SDL_timer.h"
33
34
35struct SDL_semaphore {
36 HMTX id;
37 HEV changed;
38 Uint32 value;
39};
40
41
42/* Create a semaphore */
43DECLSPEC SDL_sem * SDLCALL SDL_CreateSemaphore(Uint32 initial_value)
44{
45 SDL_sem *sem;
46 ULONG ulrc;
47
48 /* Allocate sem memory */
49 sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
50 if ( sem ) {
51 /* Create the mutex semaphore */
52 ulrc = DosCreateMutexSem(NULL,&(sem->id),0,TRUE);
53 if ( ulrc ) {
54 SDL_SetError("Couldn't create semaphore");
55 SDL_free(sem);
56 sem = NULL;
57 } else
58 {
59 DosCreateEventSem(NULL, &(sem->changed), 0, FALSE);
60 sem->value = initial_value;
61 DosReleaseMutexSem(sem->id);
62 }
63 } else {
64 SDL_OutOfMemory();
65 }
66 return(sem);
67}
68
69/* Free the semaphore */
70DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem *sem)
71{
72 if ( sem ) {
73 if ( sem->id ) {
74 DosCloseEventSem(sem->changed);
75 DosCloseMutexSem(sem->id);
76 sem->id = 0;
77 }
78 SDL_free(sem);
79 }
80}
81
82DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
83{
84 ULONG ulrc;
85
86 if ( ! sem ) {
87 SDL_SetError("Passed a NULL sem");
88 return -1;
89 }
90
91 if ( timeout == SDL_MUTEX_MAXWAIT ) {
92 while (1) {
93 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT);
94 if (ulrc) {
95 /* if error waiting mutex */
96 SDL_SetError("DosRequestMutexSem() failed");
97 return -1;
98 } else if (sem->value) {
99 sem->value--;
100 DosReleaseMutexSem(sem->id);
101 return 0;
102 } else {
103 ULONG ulPostCount;
104 DosResetEventSem(sem->changed, &ulPostCount);
105 DosReleaseMutexSem(sem->id);
106 /* continue waiting until somebody posts the semaphore */
107 DosWaitEventSem(sem->changed, SEM_INDEFINITE_WAIT);
108 }
109 }
110 } else
111 if ( timeout == 0 )
112 {
113 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT);
114 if (ulrc==NO_ERROR)
115 {
116 if (sem->value)
117 {
118 sem->value--;
119 DosReleaseMutexSem(sem->id);
120 return 0;
121 } else
122 {
123 DosReleaseMutexSem(sem->id);
124 return SDL_MUTEX_TIMEDOUT;
125 }
126 } else
127 {
128 SDL_SetError("DosRequestMutexSem() failed");
129 return -1;
130 }
131 } else {
132 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT);
133 if (ulrc) {
134 /* if error waiting mutex */
135 SDL_SetError("DosRequestMutexSem() failed");
136 return -1;
137 } else
138 if (sem->value) {
139 sem->value--;
140 DosReleaseMutexSem(sem->id);
141 return 0;
142 } else {
143 ULONG ulPostCount;
144 DosResetEventSem(sem->changed, &ulPostCount);
145 DosReleaseMutexSem(sem->id);
146 /* continue waiting until somebody posts the semaphore */
147 ulrc = DosWaitEventSem(sem->changed, timeout);
148 if (ulrc==NO_ERROR)
149 return 0;
150 else
151 return SDL_MUTEX_TIMEDOUT;
152 }
153 }
154 /* never reached */
155 return -1;
156}
157
158DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem *sem)
159{
160 return SDL_SemWaitTimeout(sem, 0);
161}
162
163DECLSPEC int SDLCALL SDL_SemWait(SDL_sem *sem)
164{
165 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
166}
167
168/* Returns the current count of the semaphore */
169DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem *sem)
170{
171 if ( ! sem ) {
172 SDL_SetError("Passed a NULL sem");
173 return 0;
174 }
175 return sem->value;
176}
177
178DECLSPEC int SDLCALL SDL_SemPost(SDL_sem *sem)
179{
180 if ( ! sem ) {
181 SDL_SetError("Passed a NULL sem");
182 return -1;
183 }
184 if ( DosRequestMutexSem(sem->id,SEM_INDEFINITE_WAIT) ) {
185 SDL_SetError("DosRequestMutexSem() failed");
186 return -1;
187 }
188 sem->value++;
189 DosPostEventSem(sem->changed);
190 DosReleaseMutexSem(sem->id);
191 return 0;
192}
diff --git a/apps/plugins/sdl/src/thread/os2/SDL_systhread.c b/apps/plugins/sdl/src/thread/os2/SDL_systhread.c
deleted file mode 100644
index 33f815a5fb..0000000000
--- a/apps/plugins/sdl/src/thread/os2/SDL_systhread.c
+++ /dev/null
@@ -1,108 +0,0 @@
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/* OS/2 thread management routines for SDL */
25
26#include <process.h>
27#define INCL_DOSERRORS
28#define INCL_DOSPROCESS
29#include <os2.h>
30
31#include "SDL_thread.h"
32#include "../SDL_systhread.h"
33#include "../SDL_thread_c.h"
34
35typedef struct ThreadStartParms
36{
37 void *args;
38 pfnSDL_CurrentEndThread pfnCurrentEndThread;
39} tThreadStartParms, *pThreadStartParms;
40
41static void threadfunc(void *pparm)
42{
43 pThreadStartParms pThreadParms = pparm;
44 pfnSDL_CurrentEndThread pfnCurrentEndThread = NULL;
45
46 // Call the thread function!
47 SDL_RunThread(pThreadParms->args);
48
49 // Get the current endthread we have to use!
50 if (pThreadParms)
51 {
52 pfnCurrentEndThread = pThreadParms->pfnCurrentEndThread;
53 SDL_free(pThreadParms);
54 }
55 // Call endthread!
56 if (pfnCurrentEndThread)
57 (*pfnCurrentEndThread)();
58}
59
60int SDL_SYS_CreateThread(SDL_Thread *thread, void *args, pfnSDL_CurrentBeginThread pfnBeginThread, pfnSDL_CurrentEndThread pfnEndThread)
61{
62 pThreadStartParms pThreadParms = SDL_malloc(sizeof(tThreadStartParms));
63 if (!pThreadParms)
64 {
65 SDL_SetError("Not enough memory to create thread");
66 return(-1);
67 }
68
69 // Save the function which we will have to call to clear the RTL of calling app!
70 pThreadParms->pfnCurrentEndThread = pfnEndThread;
71 // Also save the real parameters we have to pass to thread function
72 pThreadParms->args = args;
73 // Start the thread using the runtime library of calling app!
74 thread->threadid = thread->handle = (*pfnBeginThread)(threadfunc, NULL, 512*1024, pThreadParms);
75 if ((int)thread->threadid <= 0)
76 {
77 SDL_SetError("Not enough resources to create thread");
78 return(-1);
79 }
80 return(0);
81}
82
83void SDL_SYS_SetupThread(void)
84{
85 return;
86}
87
88DECLSPEC Uint32 SDLCALL SDL_ThreadID(void)
89{
90 PTIB tib;
91 DosGetInfoBlocks(&tib, NULL);
92 return((Uint32) (tib->tib_ptib2->tib2_ultid));
93}
94
95void SDL_SYS_WaitThread(SDL_Thread *thread)
96{
97 TID tid = thread->handle;
98 DosWaitThread(&tid, DCWW_WAIT);
99}
100
101/* WARNING: This function is really a last resort.
102 * Threads should be signaled and then exit by themselves.
103 * TerminateThread() doesn't perform stack and DLL cleanup.
104 */
105void SDL_SYS_KillThread(SDL_Thread *thread)
106{
107 DosKillThread(thread->handle);
108}
diff --git a/apps/plugins/sdl/src/thread/os2/SDL_systhread_c.h b/apps/plugins/sdl/src/thread/os2/SDL_systhread_c.h
deleted file mode 100644
index 3b94dfed53..0000000000
--- a/apps/plugins/sdl/src/thread/os2/SDL_systhread_c.h
+++ /dev/null
@@ -1,28 +0,0 @@
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#define INCL_DOSPROCESS
25#include <os2.h>
26
27typedef TID SYS_ThreadHandle;
28
diff --git a/apps/plugins/sdl/src/thread/pth/SDL_syscond.c b/apps/plugins/sdl/src/thread/pth/SDL_syscond.c
deleted file mode 100644
index ede74aaeae..0000000000
--- a/apps/plugins/sdl/src/thread/pth/SDL_syscond.c
+++ /dev/null
@@ -1,164 +0,0 @@
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/*
25 * GNU pth conditions variables
26 *
27 * Patrice Mandin
28 */
29
30#include <pth.h>
31
32#include "SDL_thread.h"
33#include "SDL_sysmutex_c.h"
34
35struct SDL_cond
36{
37 pth_cond_t condpth_p;
38};
39
40/* Create a condition variable */
41SDL_cond * SDL_CreateCond(void)
42{
43 SDL_cond *cond;
44
45 cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
46 if ( cond ) {
47 if ( pth_cond_init(&(cond->condpth_p)) < 0 ) {
48 SDL_SetError("pthread_cond_init() failed");
49 SDL_free(cond);
50 cond = NULL;
51 }
52 } else {
53 SDL_OutOfMemory();
54 }
55 return(cond);
56}
57
58/* Destroy a condition variable */
59void SDL_DestroyCond(SDL_cond *cond)
60{
61 if ( cond ) {
62 SDL_free(cond);
63 }
64}
65
66/* Restart one of the threads that are waiting on the condition variable */
67int SDL_CondSignal(SDL_cond *cond)
68{
69 int retval;
70
71 if ( ! cond ) {
72 SDL_SetError("Passed a NULL condition variable");
73 return -1;
74 }
75
76 retval = 0;
77 if ( pth_cond_notify(&(cond->condpth_p), FALSE) != 0 ) {
78 SDL_SetError("pth_cond_notify() failed");
79 retval = -1;
80 }
81 return retval;
82}
83
84/* Restart all threads that are waiting on the condition variable */
85int SDL_CondBroadcast(SDL_cond *cond)
86{
87 int retval;
88
89 if ( ! cond ) {
90 SDL_SetError("Passed a NULL condition variable");
91 return -1;
92 }
93
94 retval = 0;
95 if ( pth_cond_notify(&(cond->condpth_p), TRUE) != 0 ) {
96 SDL_SetError("pth_cond_notify() failed");
97 retval = -1;
98 }
99 return retval;
100}
101
102/* Wait on the condition variable for at most 'ms' milliseconds.
103 The mutex must be locked before entering this function!
104 The mutex is unlocked during the wait, and locked again after the wait.
105
106Typical use:
107
108Thread A:
109 SDL_LockMutex(lock);
110 while ( ! condition ) {
111 SDL_CondWait(cond);
112 }
113 SDL_UnlockMutex(lock);
114
115Thread B:
116 SDL_LockMutex(lock);
117 ...
118 condition = true;
119 ...
120 SDL_UnlockMutex(lock);
121 */
122int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
123{
124 int retval;
125 pth_event_t ev;
126 int sec;
127
128 if ( ! cond ) {
129 SDL_SetError("Passed a NULL condition variable");
130 return -1;
131 }
132
133 retval = 0;
134
135 sec = ms/1000;
136 ev = pth_event(PTH_EVENT_TIME, pth_timeout(sec,(ms-sec*1000)*1000));
137
138 if ( pth_cond_await(&(cond->condpth_p), &(mutex->mutexpth_p), ev) != 0 ) {
139 SDL_SetError("pth_cond_await() failed");
140 retval = -1;
141 }
142
143 pth_event_free(ev, PTH_FREE_ALL);
144
145 return retval;
146}
147
148/* Wait on the condition variable forever */
149int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
150{
151 int retval;
152
153 if ( ! cond ) {
154 SDL_SetError("Passed a NULL condition variable");
155 return -1;
156 }
157
158 retval = 0;
159 if ( pth_cond_await(&(cond->condpth_p), &(mutex->mutexpth_p), NULL) != 0 ) {
160 SDL_SetError("pth_cond_await() failed");
161 retval = -1;
162 }
163 return retval;
164}
diff --git a/apps/plugins/sdl/src/thread/pth/SDL_sysmutex.c b/apps/plugins/sdl/src/thread/pth/SDL_sysmutex.c
deleted file mode 100644
index ca83b92c24..0000000000
--- a/apps/plugins/sdl/src/thread/pth/SDL_sysmutex.c
+++ /dev/null
@@ -1,87 +0,0 @@
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/*
25 * GNU pth mutexes
26 *
27 * Patrice Mandin
28 */
29
30#include <pth.h>
31
32#include "SDL_mutex.h"
33#include "SDL_sysmutex_c.h"
34
35/* Create a mutex */
36SDL_mutex *SDL_CreateMutex(void)
37{
38 SDL_mutex *mutex;
39
40 /* Allocate mutex memory */
41 mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex));
42 if ( mutex ) {
43 /* Create the mutex, with initial value signaled */
44 if (!pth_mutex_init(&(mutex->mutexpth_p))) {
45 SDL_SetError("Couldn't create mutex");
46 SDL_free(mutex);
47 mutex = NULL;
48 }
49 } else {
50 SDL_OutOfMemory();
51 }
52 return(mutex);
53}
54
55/* Free the mutex */
56void SDL_DestroyMutex(SDL_mutex *mutex)
57{
58 if ( mutex ) {
59 SDL_free(mutex);
60 }
61}
62
63/* Lock the mutex */
64int SDL_mutexP(SDL_mutex *mutex)
65{
66 if ( mutex == NULL ) {
67 SDL_SetError("Passed a NULL mutex");
68 return -1;
69 }
70
71 pth_mutex_acquire(&(mutex->mutexpth_p), FALSE, NULL);
72
73 return(0);
74}
75
76/* Unlock the mutex */
77int SDL_mutexV(SDL_mutex *mutex)
78{
79 if ( mutex == NULL ) {
80 SDL_SetError("Passed a NULL mutex");
81 return -1;
82 }
83
84 pth_mutex_release(&(mutex->mutexpth_p));
85
86 return(0);
87}
diff --git a/apps/plugins/sdl/src/thread/pth/SDL_sysmutex_c.h b/apps/plugins/sdl/src/thread/pth/SDL_sysmutex_c.h
deleted file mode 100644
index d29060e9ff..0000000000
--- a/apps/plugins/sdl/src/thread/pth/SDL_sysmutex_c.h
+++ /dev/null
@@ -1,31 +0,0 @@
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#ifndef _SDL_SYSMUTEX_C_H_
25#define _SDL_SYSMUTEX_C_H_
26
27struct SDL_mutex {
28 pth_mutex_t mutexpth_p;
29};
30
31#endif /* _SDL_SYSMUTEX_C_H_ */
diff --git a/apps/plugins/sdl/src/thread/pth/SDL_systhread.c b/apps/plugins/sdl/src/thread/pth/SDL_systhread.c
deleted file mode 100644
index bfccaf340f..0000000000
--- a/apps/plugins/sdl/src/thread/pth/SDL_systhread.c
+++ /dev/null
@@ -1,103 +0,0 @@
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/*
25 * GNU pth threads
26 *
27 * Patrice Mandin
28 */
29
30#include <pth.h>
31#include <signal.h>
32
33#include "SDL_thread.h"
34#include "../SDL_thread_c.h"
35#include "../SDL_systhread.h"
36
37/* List of signals to mask in the subthreads */
38static int sig_list[] = {
39 SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
40 SIGVTALRM, SIGPROF, 0
41};
42
43static void *RunThread(void *data)
44{
45 SDL_RunThread(data);
46 pth_exit((void*)0);
47 return((void *)0); /* Prevent compiler warning */
48}
49
50int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
51{
52 pth_attr_t type;
53
54 /* Create a new attribute */
55 type = pth_attr_new();
56 if ( type == NULL ) {
57 SDL_SetError("Couldn't initialize pth attributes");
58 return(-1);
59 }
60 pth_attr_set(type, PTH_ATTR_JOINABLE, TRUE);
61
62 /* Create the thread and go! */
63 thread->handle = pth_spawn(type, RunThread, args);
64 if ( thread->handle == NULL ) {
65 SDL_SetError("Not enough resources to create thread");
66 return(-1);
67 }
68 return(0);
69}
70
71void SDL_SYS_SetupThread(void)
72{
73 int i;
74 sigset_t mask;
75 int oldstate;
76
77 /* Mask asynchronous signals for this thread */
78 sigemptyset(&mask);
79 for ( i=0; sig_list[i]; ++i ) {
80 sigaddset(&mask, sig_list[i]);
81 }
82 pth_sigmask(SIG_BLOCK, &mask, 0);
83
84 /* Allow ourselves to be asynchronously cancelled */
85 pth_cancel_state(PTH_CANCEL_ASYNCHRONOUS, &oldstate);
86}
87
88/* WARNING: This may not work for systems with 64-bit pid_t */
89Uint32 SDL_ThreadID(void)
90{
91 return((Uint32)pth_self());
92}
93
94void SDL_SYS_WaitThread(SDL_Thread *thread)
95{
96 pth_join(thread->handle, NULL);
97}
98
99void SDL_SYS_KillThread(SDL_Thread *thread)
100{
101 pth_cancel(thread->handle);
102 pth_join(thread->handle, NULL);
103}
diff --git a/apps/plugins/sdl/src/thread/pth/SDL_systhread_c.h b/apps/plugins/sdl/src/thread/pth/SDL_systhread_c.h
deleted file mode 100644
index 50bb26df54..0000000000
--- a/apps/plugins/sdl/src/thread/pth/SDL_systhread_c.h
+++ /dev/null
@@ -1,31 +0,0 @@
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#ifndef _SDL_SYSTHREAD_C_H_
25#define _SDL_SYSTHREAD_C_H_
26
27#include <pth.h>
28
29typedef pth_t SYS_ThreadHandle;
30
31#endif /* _SDL_SYSTHREAD_C_H_ */
diff --git a/apps/plugins/sdl/src/thread/pthread/SDL_syscond.c b/apps/plugins/sdl/src/thread/pthread/SDL_syscond.c
deleted file mode 100644
index 15bce96614..0000000000
--- a/apps/plugins/sdl/src/thread/pthread/SDL_syscond.c
+++ /dev/null
@@ -1,155 +0,0 @@
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#include <sys/time.h>
25#include <unistd.h>
26#include <errno.h>
27#include <pthread.h>
28
29#include "SDL_thread.h"
30#include "SDL_sysmutex_c.h"
31
32struct SDL_cond
33{
34 pthread_cond_t cond;
35};
36
37/* Create a condition variable */
38SDL_cond * SDL_CreateCond(void)
39{
40 SDL_cond *cond;
41
42 cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
43 if ( cond ) {
44 if ( pthread_cond_init(&cond->cond, NULL) < 0 ) {
45 SDL_SetError("pthread_cond_init() failed");
46 SDL_free(cond);
47 cond = NULL;
48 }
49 }
50 return(cond);
51}
52
53/* Destroy a condition variable */
54void SDL_DestroyCond(SDL_cond *cond)
55{
56 if ( cond ) {
57 pthread_cond_destroy(&cond->cond);
58 SDL_free(cond);
59 }
60}
61
62/* Restart one of the threads that are waiting on the condition variable */
63int SDL_CondSignal(SDL_cond *cond)
64{
65 int retval;
66
67 if ( ! cond ) {
68 SDL_SetError("Passed a NULL condition variable");
69 return -1;
70 }
71
72 retval = 0;
73 if ( pthread_cond_signal(&cond->cond) != 0 ) {
74 SDL_SetError("pthread_cond_signal() failed");
75 retval = -1;
76 }
77 return retval;
78}
79
80/* Restart all threads that are waiting on the condition variable */
81int SDL_CondBroadcast(SDL_cond *cond)
82{
83 int retval;
84
85 if ( ! cond ) {
86 SDL_SetError("Passed a NULL condition variable");
87 return -1;
88 }
89
90 retval = 0;
91 if ( pthread_cond_broadcast(&cond->cond) != 0 ) {
92 SDL_SetError("pthread_cond_broadcast() failed");
93 retval = -1;
94 }
95 return retval;
96}
97
98int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
99{
100 int retval;
101 struct timeval delta;
102 struct timespec abstime;
103
104 if ( ! cond ) {
105 SDL_SetError("Passed a NULL condition variable");
106 return -1;
107 }
108
109 gettimeofday(&delta, NULL);
110
111 abstime.tv_sec = delta.tv_sec + (ms/1000);
112 abstime.tv_nsec = (delta.tv_usec + (ms%1000) * 1000) * 1000;
113 if ( abstime.tv_nsec > 1000000000 ) {
114 abstime.tv_sec += 1;
115 abstime.tv_nsec -= 1000000000;
116 }
117
118 tryagain:
119 retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime);
120 switch (retval) {
121 case EINTR:
122 goto tryagain;
123 break;
124 case ETIMEDOUT:
125 retval = SDL_MUTEX_TIMEDOUT;
126 break;
127 case 0:
128 break;
129 default:
130 SDL_SetError("pthread_cond_timedwait() failed");
131 retval = -1;
132 break;
133 }
134 return retval;
135}
136
137/* Wait on the condition variable, unlocking the provided mutex.
138 The mutex must be locked before entering this function!
139 */
140int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
141{
142 int retval;
143
144 if ( ! cond ) {
145 SDL_SetError("Passed a NULL condition variable");
146 return -1;
147 }
148
149 retval = 0;
150 if ( pthread_cond_wait(&cond->cond, &mutex->id) != 0 ) {
151 SDL_SetError("pthread_cond_wait() failed");
152 retval = -1;
153 }
154 return retval;
155}
diff --git a/apps/plugins/sdl/src/thread/pthread/SDL_sysmutex.c b/apps/plugins/sdl/src/thread/pthread/SDL_sysmutex.c
deleted file mode 100644
index c3b8ce2c37..0000000000
--- a/apps/plugins/sdl/src/thread/pthread/SDL_sysmutex.c
+++ /dev/null
@@ -1,153 +0,0 @@
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#include <pthread.h>
25
26#include "SDL_thread.h"
27
28#if !SDL_THREAD_PTHREAD_RECURSIVE_MUTEX && \
29 !SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP
30#define FAKE_RECURSIVE_MUTEX
31#endif
32
33struct SDL_mutex {
34 pthread_mutex_t id;
35#if FAKE_RECURSIVE_MUTEX
36 int recursive;
37 pthread_t owner;
38#endif
39};
40
41SDL_mutex *SDL_CreateMutex (void)
42{
43 SDL_mutex *mutex;
44 pthread_mutexattr_t attr;
45
46 /* Allocate the structure */
47 mutex = (SDL_mutex *)SDL_calloc(1, sizeof(*mutex));
48 if ( mutex ) {
49 pthread_mutexattr_init(&attr);
50#if SDL_THREAD_PTHREAD_RECURSIVE_MUTEX
51 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
52#elif SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP
53 pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
54#else
55 /* No extra attributes necessary */
56#endif
57 if ( pthread_mutex_init(&mutex->id, &attr) != 0 ) {
58 SDL_SetError("pthread_mutex_init() failed");
59 SDL_free(mutex);
60 mutex = NULL;
61 }
62 } else {
63 SDL_OutOfMemory();
64 }
65 return(mutex);
66}
67
68void SDL_DestroyMutex(SDL_mutex *mutex)
69{
70 if ( mutex ) {
71 pthread_mutex_destroy(&mutex->id);
72 SDL_free(mutex);
73 }
74}
75
76/* Lock the mutex */
77int SDL_mutexP(SDL_mutex *mutex)
78{
79 int retval;
80#if FAKE_RECURSIVE_MUTEX
81 pthread_t this_thread;
82#endif
83
84 if ( mutex == NULL ) {
85 SDL_SetError("Passed a NULL mutex");
86 return -1;
87 }
88
89 retval = 0;
90#if FAKE_RECURSIVE_MUTEX
91 this_thread = pthread_self();
92 if ( mutex->owner == this_thread ) {
93 ++mutex->recursive;
94 } else {
95 /* The order of operations is important.
96 We set the locking thread id after we obtain the lock
97 so unlocks from other threads will fail.
98 */
99 if ( pthread_mutex_lock(&mutex->id) == 0 ) {
100 mutex->owner = this_thread;
101 mutex->recursive = 0;
102 } else {
103 SDL_SetError("pthread_mutex_lock() failed");
104 retval = -1;
105 }
106 }
107#else
108 if ( pthread_mutex_lock(&mutex->id) < 0 ) {
109 SDL_SetError("pthread_mutex_lock() failed");
110 retval = -1;
111 }
112#endif
113 return retval;
114}
115
116int SDL_mutexV(SDL_mutex *mutex)
117{
118 int retval;
119
120 if ( mutex == NULL ) {
121 SDL_SetError("Passed a NULL mutex");
122 return -1;
123 }
124
125 retval = 0;
126#if FAKE_RECURSIVE_MUTEX
127 /* We can only unlock the mutex if we own it */
128 if ( pthread_self() == mutex->owner ) {
129 if ( mutex->recursive ) {
130 --mutex->recursive;
131 } else {
132 /* The order of operations is important.
133 First reset the owner so another thread doesn't lock
134 the mutex and set the ownership before we reset it,
135 then release the lock semaphore.
136 */
137 mutex->owner = 0;
138 pthread_mutex_unlock(&mutex->id);
139 }
140 } else {
141 SDL_SetError("mutex not owned by this thread");
142 retval = -1;
143 }
144
145#else
146 if ( pthread_mutex_unlock(&mutex->id) < 0 ) {
147 SDL_SetError("pthread_mutex_unlock() failed");
148 retval = -1;
149 }
150#endif /* FAKE_RECURSIVE_MUTEX */
151
152 return retval;
153}
diff --git a/apps/plugins/sdl/src/thread/pthread/SDL_sysmutex_c.h b/apps/plugins/sdl/src/thread/pthread/SDL_sysmutex_c.h
deleted file mode 100644
index 5258890156..0000000000
--- a/apps/plugins/sdl/src/thread/pthread/SDL_sysmutex_c.h
+++ /dev/null
@@ -1,31 +0,0 @@
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#ifndef _SDL_mutex_c_h
25#define _SDL_mutex_c_h
26
27struct SDL_mutex {
28 pthread_mutex_t id;
29};
30
31#endif /* _SDL_mutex_c_h */
diff --git a/apps/plugins/sdl/src/thread/pthread/SDL_syssem.c b/apps/plugins/sdl/src/thread/pthread/SDL_syssem.c
deleted file mode 100644
index a03870fa35..0000000000
--- a/apps/plugins/sdl/src/thread/pthread/SDL_syssem.c
+++ /dev/null
@@ -1,190 +0,0 @@
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#include <pthread.h>
25#include <semaphore.h>
26#include <errno.h>
27#include <sys/time.h>
28
29#include "SDL_thread.h"
30#include "SDL_timer.h"
31
32/* Wrapper around POSIX 1003.1b semaphores */
33
34#ifdef __MACOSX__
35/* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
36#include "../generic/SDL_syssem.c"
37#else
38
39struct SDL_semaphore {
40 sem_t sem;
41};
42
43/* Create a semaphore, initialized with value */
44SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
45{
46 SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
47 if ( sem ) {
48 if ( sem_init(&sem->sem, 0, initial_value) < 0 ) {
49 SDL_SetError("sem_init() failed");
50 SDL_free(sem);
51 sem = NULL;
52 }
53 } else {
54 SDL_OutOfMemory();
55 }
56 return sem;
57}
58
59void SDL_DestroySemaphore(SDL_sem *sem)
60{
61 if ( sem ) {
62 sem_destroy(&sem->sem);
63 SDL_free(sem);
64 }
65}
66
67int SDL_SemTryWait(SDL_sem *sem)
68{
69 int retval;
70
71 if ( ! sem ) {
72 SDL_SetError("Passed a NULL semaphore");
73 return -1;
74 }
75 retval = SDL_MUTEX_TIMEDOUT;
76 if ( sem_trywait(&sem->sem) == 0 ) {
77 retval = 0;
78 }
79 return retval;
80}
81
82int SDL_SemWait(SDL_sem *sem)
83{
84 int retval;
85
86 if ( ! sem ) {
87 SDL_SetError("Passed a NULL semaphore");
88 return -1;
89 }
90
91 while ( ((retval = sem_wait(&sem->sem)) == -1) && (errno == EINTR) ) {}
92 if ( retval < 0 ) {
93 SDL_SetError("sem_wait() failed");
94 }
95 return retval;
96}
97
98int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
99{
100 int retval;
101#ifdef HAVE_SEM_TIMEDWAIT
102 struct timeval now;
103 struct timespec ts_timeout;
104#else
105 Uint32 end;
106#endif
107
108 if ( ! sem ) {
109 SDL_SetError("Passed a NULL semaphore");
110 return -1;
111 }
112
113 /* Try the easy cases first */
114 if ( timeout == 0 ) {
115 return SDL_SemTryWait(sem);
116 }
117 if ( timeout == SDL_MUTEX_MAXWAIT ) {
118 return SDL_SemWait(sem);
119 }
120
121#ifdef HAVE_SEM_TIMEDWAIT
122 /* Setup the timeout. sem_timedwait doesn't wait for
123 * a lapse of time, but until we reach a certain time.
124 * This time is now plus the timeout.
125 */
126 gettimeofday(&now, NULL);
127
128 /* Add our timeout to current time */
129 now.tv_usec += (timeout % 1000) * 1000;
130 now.tv_sec += timeout / 1000;
131
132 /* Wrap the second if needed */
133 if ( now.tv_usec >= 1000000 ) {
134 now.tv_usec -= 1000000;
135 now.tv_sec ++;
136 }
137
138 /* Convert to timespec */
139 ts_timeout.tv_sec = now.tv_sec;
140 ts_timeout.tv_nsec = now.tv_usec * 1000;
141
142 /* Wait. */
143 do
144 retval = sem_timedwait(&sem->sem, &ts_timeout);
145 while (retval == -1 && errno == EINTR);
146
147 if (retval == -1)
148 SDL_SetError(strerror(errno));
149#else
150 end = SDL_GetTicks() + timeout;
151 while ((retval = SDL_SemTryWait(sem)) == SDL_MUTEX_TIMEDOUT) {
152 if ((SDL_GetTicks() - end) >= 0) {
153 break;
154 }
155 SDL_Delay(0);
156 }
157#endif /* HAVE_SEM_TIMEDWAIT */
158
159 return retval;
160}
161
162Uint32 SDL_SemValue(SDL_sem *sem)
163{
164 int ret = 0;
165 if ( sem ) {
166 sem_getvalue(&sem->sem, &ret);
167 if ( ret < 0 ) {
168 ret = 0;
169 }
170 }
171 return (Uint32)ret;
172}
173
174int SDL_SemPost(SDL_sem *sem)
175{
176 int retval;
177
178 if ( ! sem ) {
179 SDL_SetError("Passed a NULL semaphore");
180 return -1;
181 }
182
183 retval = sem_post(&sem->sem);
184 if ( retval < 0 ) {
185 SDL_SetError("sem_post() failed");
186 }
187 return retval;
188}
189
190#endif /* __MACOSX__ */
diff --git a/apps/plugins/sdl/src/thread/pthread/SDL_systhread.c b/apps/plugins/sdl/src/thread/pthread/SDL_systhread.c
deleted file mode 100644
index 40cc3b717d..0000000000
--- a/apps/plugins/sdl/src/thread/pthread/SDL_systhread.c
+++ /dev/null
@@ -1,120 +0,0 @@
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#include <pthread.h>
25#include <signal.h>
26
27#include "SDL_thread.h"
28#include "../SDL_thread_c.h"
29#include "../SDL_systhread.h"
30
31/* List of signals to mask in the subthreads */
32static int sig_list[] = {
33 SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
34 SIGVTALRM, SIGPROF, 0
35};
36
37#ifdef __RISCOS__
38/* RISC OS needs to know the main thread for
39 * it's timer and event processing. */
40int riscos_using_threads = 0;
41Uint32 riscos_main_thread = 0; /* Thread running events */
42#endif
43
44
45static void *RunThread(void *data)
46{
47 SDL_RunThread(data);
48 pthread_exit((void*)0);
49 return((void *)0); /* Prevent compiler warning */
50}
51
52int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
53{
54 pthread_attr_t type;
55
56 /* Set the thread attributes */
57 if ( pthread_attr_init(&type) != 0 ) {
58 SDL_SetError("Couldn't initialize pthread attributes");
59 return(-1);
60 }
61 pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
62
63 /* Create the thread and go! */
64 if ( pthread_create(&thread->handle, &type, RunThread, args) != 0 ) {
65 SDL_SetError("Not enough resources to create thread");
66 return(-1);
67 }
68
69#ifdef __RISCOS__
70 if (riscos_using_threads == 0) {
71 riscos_using_threads = 1;
72 riscos_main_thread = SDL_ThreadID();
73 }
74#endif
75
76 return(0);
77}
78
79void SDL_SYS_SetupThread(void)
80{
81 int i;
82 sigset_t mask;
83
84 /* Mask asynchronous signals for this thread */
85 sigemptyset(&mask);
86 for ( i=0; sig_list[i]; ++i ) {
87 sigaddset(&mask, sig_list[i]);
88 }
89 pthread_sigmask(SIG_BLOCK, &mask, 0);
90
91#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
92 /* Allow ourselves to be asynchronously cancelled */
93 { int oldstate;
94 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
95 }
96#endif
97}
98
99/* WARNING: This may not work for systems with 64-bit pid_t */
100Uint32 SDL_ThreadID(void)
101{
102 return((Uint32)((size_t)pthread_self()));
103}
104
105void SDL_SYS_WaitThread(SDL_Thread *thread)
106{
107 pthread_join(thread->handle, 0);
108}
109
110void SDL_SYS_KillThread(SDL_Thread *thread)
111{
112#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
113 pthread_cancel(thread->handle);
114#else
115#ifdef __FREEBSD__
116#warning For some reason, this doesnt actually kill a thread - FreeBSD 3.2
117#endif
118 pthread_kill(thread->handle, SIGKILL);
119#endif
120}
diff --git a/apps/plugins/sdl/src/thread/pthread/SDL_systhread_c.h b/apps/plugins/sdl/src/thread/pthread/SDL_systhread_c.h
deleted file mode 100644
index 33ed750ff2..0000000000
--- a/apps/plugins/sdl/src/thread/pthread/SDL_systhread_c.h
+++ /dev/null
@@ -1,26 +0,0 @@
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#include <pthread.h>
25
26typedef pthread_t SYS_ThreadHandle;
diff --git a/apps/plugins/sdl/src/thread/riscos/SDL_syscond.c b/apps/plugins/sdl/src/thread/riscos/SDL_syscond.c
deleted file mode 100644
index ee5566647f..0000000000
--- a/apps/plugins/sdl/src/thread/riscos/SDL_syscond.c
+++ /dev/null
@@ -1,160 +0,0 @@
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/* RISC OS implementations uses pthreads based on linux code */
25
26#if SDL_THREADS_DISABLED
27#include "../generic/SDL_syscond.c"
28#else
29#include <sys/time.h>
30#include <unistd.h>
31#include <pthread.h>
32
33#include "SDL_thread.h"
34#include "SDL_sysmutex_c.h"
35
36struct SDL_cond
37{
38 pthread_cond_t cond;
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 if ( pthread_cond_init(&cond->cond, NULL) < 0 ) {
49 SDL_SetError("pthread_cond_init() failed");
50 SDL_free(cond);
51 cond = NULL;
52 }
53 }
54 return(cond);
55}
56
57/* Destroy a condition variable */
58void SDL_DestroyCond(SDL_cond *cond)
59{
60 if ( cond ) {
61 pthread_cond_destroy(&cond->cond);
62 SDL_free(cond);
63 }
64}
65
66/* Restart one of the threads that are waiting on the condition variable */
67int SDL_CondSignal(SDL_cond *cond)
68{
69 int retval;
70
71 if ( ! cond ) {
72 SDL_SetError("Passed a NULL condition variable");
73 return -1;
74 }
75
76 retval = 0;
77 if ( pthread_cond_signal(&cond->cond) != 0 ) {
78 SDL_SetError("pthread_cond_signal() failed");
79 retval = -1;
80 }
81 return retval;
82}
83
84/* Restart all threads that are waiting on the condition variable */
85int SDL_CondBroadcast(SDL_cond *cond)
86{
87 int retval;
88
89 if ( ! cond ) {
90 SDL_SetError("Passed a NULL condition variable");
91 return -1;
92 }
93
94 retval = 0;
95 if ( pthread_cond_broadcast(&cond->cond) != 0 ) {
96 SDL_SetError("pthread_cond_broadcast() failed");
97 retval = -1;
98 }
99 return retval;
100}
101
102int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
103{
104 int retval;
105 struct timeval delta;
106 struct timespec abstime;
107
108 if ( ! cond ) {
109 SDL_SetError("Passed a NULL condition variable");
110 return -1;
111 }
112
113 gettimeofday(&delta, NULL);
114
115 abstime.tv_sec = delta.tv_sec + (ms/1000);
116 abstime.tv_nsec = (delta.tv_usec + (ms%1000) * 1000) * 1000;
117 if ( abstime.tv_nsec > 1000000000 ) {
118 abstime.tv_sec += 1;
119 abstime.tv_nsec -= 1000000000;
120 }
121
122 tryagain:
123 retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime);
124 switch (retval) {
125 case EINTR:
126 goto tryagain;
127 break;
128 case ETIMEDOUT:
129 retval = SDL_MUTEX_TIMEDOUT;
130 break;
131 case 0:
132 break;
133 default:
134 SDL_SetError("pthread_cond_timedwait() failed");
135 retval = -1;
136 break;
137 }
138 return retval;
139}
140
141/* Wait on the condition variable, unlocking the provided mutex.
142 The mutex must be locked before entering this function!
143 */
144int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
145{
146 int retval;
147
148 if ( ! cond ) {
149 SDL_SetError("Passed a NULL condition variable");
150 return -1;
151 }
152
153 retval = 0;
154 if ( pthread_cond_wait(&cond->cond, &mutex->id) != 0 ) {
155 SDL_SetError("pthread_cond_wait() failed");
156 retval = -1;
157 }
158 return retval;
159}
160#endif
diff --git a/apps/plugins/sdl/src/thread/riscos/SDL_sysmutex.c b/apps/plugins/sdl/src/thread/riscos/SDL_sysmutex.c
deleted file mode 100644
index e9ac139b36..0000000000
--- a/apps/plugins/sdl/src/thread/riscos/SDL_sysmutex.c
+++ /dev/null
@@ -1,153 +0,0 @@
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/* RISC OS implementations uses pthreads based on linux code */
25
26#include "SDL_thread.h"
27
28#if SDL_THREADS_DISABLED
29#include "../generic/SDL_sysmutex.c"
30#else
31
32#include <pthread.h>
33
34struct SDL_mutex {
35 pthread_mutex_t id;
36#if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX
37 int recursive;
38 pthread_t owner;
39#endif
40};
41
42SDL_mutex *SDL_CreateMutex (void)
43{
44 SDL_mutex *mutex;
45 pthread_mutexattr_t attr;
46
47 /* Allocate the structure */
48 mutex = (SDL_mutex *)SDL_calloc(1, sizeof(*mutex));
49 if ( mutex ) {
50 pthread_mutexattr_init(&attr);
51#if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX
52 /* No extra attributes necessary */
53#else
54 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
55#endif /* SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX */
56 if ( pthread_mutex_init(&mutex->id, &attr) != 0 ) {
57 SDL_SetError("pthread_mutex_init() failed");
58 SDL_free(mutex);
59 mutex = NULL;
60 }
61 } else {
62 SDL_OutOfMemory();
63 }
64 return(mutex);
65}
66
67void SDL_DestroyMutex(SDL_mutex *mutex)
68{
69 if ( mutex ) {
70 pthread_mutex_destroy(&mutex->id);
71 SDL_free(mutex);
72 }
73}
74
75/* Lock the mutex */
76int SDL_mutexP(SDL_mutex *mutex)
77{
78 int retval;
79#if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX
80 pthread_t this_thread;
81#endif
82
83 if ( mutex == NULL ) {
84 SDL_SetError("Passed a NULL mutex");
85 return -1;
86 }
87
88 retval = 0;
89#if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX
90 this_thread = pthread_self();
91 if ( mutex->owner == this_thread ) {
92 ++mutex->recursive;
93 } else {
94 /* The order of operations is important.
95 We set the locking thread id after we obtain the lock
96 so unlocks from other threads will fail.
97 */
98 if ( pthread_mutex_lock(&mutex->id) == 0 ) {
99 mutex->owner = this_thread;
100 mutex->recursive = 0;
101 } else {
102 SDL_SetError("pthread_mutex_lock() failed");
103 retval = -1;
104 }
105 }
106#else
107 if ( pthread_mutex_lock(&mutex->id) < 0 ) {
108 SDL_SetError("pthread_mutex_lock() failed");
109 retval = -1;
110 }
111#endif
112 return retval;
113}
114
115int SDL_mutexV(SDL_mutex *mutex)
116{
117 int retval;
118
119 if ( mutex == NULL ) {
120 SDL_SetError("Passed a NULL mutex");
121 return -1;
122 }
123
124 retval = 0;
125#if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX
126 /* We can only unlock the mutex if we own it */
127 if ( pthread_self() == mutex->owner ) {
128 if ( mutex->recursive ) {
129 --mutex->recursive;
130 } else {
131 /* The order of operations is important.
132 First reset the owner so another thread doesn't lock
133 the mutex and set the ownership before we reset it,
134 then release the lock semaphore.
135 */
136 mutex->owner = 0;
137 pthread_mutex_unlock(&mutex->id);
138 }
139 } else {
140 SDL_SetError("mutex not owned by this thread");
141 retval = -1;
142 }
143
144#else
145 if ( pthread_mutex_unlock(&mutex->id) < 0 ) {
146 SDL_SetError("pthread_mutex_unlock() failed");
147 retval = -1;
148 }
149#endif /* SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX */
150
151 return retval;
152}
153#endif
diff --git a/apps/plugins/sdl/src/thread/riscos/SDL_sysmutex_c.h b/apps/plugins/sdl/src/thread/riscos/SDL_sysmutex_c.h
deleted file mode 100644
index 2391c3c108..0000000000
--- a/apps/plugins/sdl/src/thread/riscos/SDL_sysmutex_c.h
+++ /dev/null
@@ -1,34 +0,0 @@
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#ifndef _SDL_mutex_c_h
25#define _SDL_mutex_c_h
26
27#if !SDL_THREADS_DISABLED
28struct SDL_mutex {
29 pthread_mutex_t id;
30};
31#endif
32
33
34#endif /* _SDL_mutex_c_h */
diff --git a/apps/plugins/sdl/src/thread/riscos/SDL_syssem.c b/apps/plugins/sdl/src/thread/riscos/SDL_syssem.c
deleted file mode 100644
index 127211bf76..0000000000
--- a/apps/plugins/sdl/src/thread/riscos/SDL_syssem.c
+++ /dev/null
@@ -1,203 +0,0 @@
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
23#include <errno.h>
24
25#include "SDL_config.h"
26
27/* RISC OS semiphores based on linux code */
28
29
30#include "SDL_timer.h"
31#include "SDL_thread.h"
32#include "SDL_systhread_c.h"
33
34#if !SDL_THREADS_DISABLED
35
36SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
37{
38 SDL_SetError("SDL not configured with thread support");
39 return (SDL_sem *)0;
40}
41
42void SDL_DestroySemaphore(SDL_sem *sem)
43{
44 return;
45}
46
47int SDL_SemTryWait(SDL_sem *sem)
48{
49 SDL_SetError("SDL not configured with thread support");
50 return -1;
51}
52
53int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
54{
55 SDL_SetError("SDL not configured with thread support");
56 return -1;
57}
58
59int SDL_SemWait(SDL_sem *sem)
60{
61 SDL_SetError("SDL not configured with thread support");
62 return -1;
63}
64
65Uint32 SDL_SemValue(SDL_sem *sem)
66{
67 return 0;
68}
69
70int SDL_SemPost(SDL_sem *sem)
71{
72 SDL_SetError("SDL not configured with thread support");
73 return -1;
74}
75
76#else
77
78
79#include <unistd.h> /* For getpid() */
80#include <pthread.h>
81#include <semaphore.h>
82
83struct SDL_semaphore {
84 sem_t *sem;
85 sem_t sem_data;
86};
87
88/* Create a semaphore, initialized with value */
89SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
90{
91 SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
92 if ( sem ) {
93 if ( sem_init(&sem->sem_data, 0, initial_value) < 0 ) {
94 SDL_SetError("sem_init() failed");
95 SDL_free(sem);
96 sem = NULL;
97 } else {
98 sem->sem = &sem->sem_data;
99 }
100 } else {
101 SDL_OutOfMemory();
102 }
103 return sem;
104}
105
106void SDL_DestroySemaphore(SDL_sem *sem)
107{
108 if ( sem ) {
109 sem_destroy(sem->sem);
110 SDL_free(sem);
111 }
112}
113
114int SDL_SemTryWait(SDL_sem *sem)
115{
116 int retval;
117
118 if ( ! sem ) {
119 SDL_SetError("Passed a NULL semaphore");
120 return -1;
121 }
122 retval = SDL_MUTEX_TIMEDOUT;
123 if ( sem_trywait(sem->sem) == 0 ) {
124 retval = 0;
125 }
126 return retval;
127}
128
129int SDL_SemWait(SDL_sem *sem)
130{
131 int retval;
132
133 if ( ! sem ) {
134 SDL_SetError("Passed a NULL semaphore");
135 return -1;
136 }
137
138 while ( ((retval = sem_wait(sem->sem)) == -1) && (errno == EINTR) ) {}
139 if ( retval < 0 ) {
140 SDL_SetError("sem_wait() failed");
141 }
142 return retval;
143}
144
145int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
146{
147 int retval;
148
149 if ( ! sem ) {
150 SDL_SetError("Passed a NULL semaphore");
151 return -1;
152 }
153
154 /* Try the easy cases first */
155 if ( timeout == 0 ) {
156 return SDL_SemTryWait(sem);
157 }
158 if ( timeout == SDL_MUTEX_MAXWAIT ) {
159 return SDL_SemWait(sem);
160 }
161
162 /* Ack! We have to busy wait... */
163 timeout += SDL_GetTicks();
164 do {
165 retval = SDL_SemTryWait(sem);
166 if ( retval == 0 ) {
167 break;
168 }
169 SDL_Delay(1);
170 } while ( SDL_GetTicks() < timeout );
171
172 return retval;
173}
174
175Uint32 SDL_SemValue(SDL_sem *sem)
176{
177 int ret = 0;
178 if ( sem ) {
179 sem_getvalue(sem->sem, &ret);
180 if ( ret < 0 ) {
181 ret = 0;
182 }
183 }
184 return (Uint32)ret;
185}
186
187int SDL_SemPost(SDL_sem *sem)
188{
189 int retval;
190
191 if ( ! sem ) {
192 SDL_SetError("Passed a NULL semaphore");
193 return -1;
194 }
195
196 retval = sem_post(sem->sem);
197 if ( retval < 0 ) {
198 SDL_SetError("sem_post() failed");
199 }
200 return retval;
201}
202
203#endif /* !SDL_THREADS_DISABLED */
diff --git a/apps/plugins/sdl/src/thread/riscos/SDL_systhread.c b/apps/plugins/sdl/src/thread/riscos/SDL_systhread.c
deleted file mode 100644
index 38fc333ae8..0000000000
--- a/apps/plugins/sdl/src/thread/riscos/SDL_systhread.c
+++ /dev/null
@@ -1,144 +0,0 @@
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/* RISC OS version based on pthreads linux source */
25
26#include "SDL_thread.h"
27#include "../SDL_systhread.h"
28
29#if SDL_THREADS_DISABLED
30
31int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
32{
33 SDL_SetError("Threads have not been compiled into this version of the library");
34 return(-1);
35}
36
37void SDL_SYS_SetupThread(void)
38{
39 return;
40}
41
42Uint32 SDL_ThreadID(void)
43{
44 return(0);
45}
46
47void SDL_SYS_WaitThread(SDL_Thread *thread)
48{
49 return;
50}
51
52void SDL_SYS_KillThread(SDL_Thread *thread)
53{
54 return;
55}
56
57#else
58
59#include <signal.h>
60
61/* List of signals to mask in the subthreads */
62static int sig_list[] = {
63 SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
64 SIGVTALRM, SIGPROF, 0
65};
66
67#include <pthread.h>
68
69int riscos_using_threads = 0;
70Uint32 riscos_main_thread = 0; /* Thread running events */
71
72static void *RunThread(void *data)
73{
74 SDL_RunThread(data);
75 pthread_exit((void*)0);
76 return((void *)0); /* Prevent compiler warning */
77}
78
79int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
80{
81 pthread_attr_t type;
82
83 /* Set the thread attributes */
84 if ( pthread_attr_init(&type) != 0 ) {
85 SDL_SetError("Couldn't initialize pthread attributes");
86 return(-1);
87 }
88 pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
89
90 /* Create the thread and go! */
91 if ( pthread_create(&thread->handle, &type, RunThread, args) != 0 ) {
92 SDL_SetError("Not enough resources to create thread");
93 return(-1);
94 }
95
96 if (riscos_using_threads == 0)
97 {
98 riscos_using_threads = 1;
99 riscos_main_thread = SDL_ThreadID();
100 }
101
102 return(0);
103}
104
105void SDL_SYS_SetupThread(void)
106{
107 int i;
108 sigset_t mask;
109
110 /* Mask asynchronous signals for this thread */
111 sigemptyset(&mask);
112 for ( i=0; sig_list[i]; ++i ) {
113 sigaddset(&mask, sig_list[i]);
114 }
115 pthread_sigmask(SIG_BLOCK, &mask, 0);
116
117#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
118 /* Allow ourselves to be asynchronously cancelled */
119 { int oldstate;
120 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
121 }
122#endif
123}
124
125Uint32 SDL_ThreadID(void)
126{
127 return((Uint32)pthread_self());
128}
129
130void SDL_SYS_WaitThread(SDL_Thread *thread)
131{
132 pthread_join(thread->handle, 0);
133}
134
135void SDL_SYS_KillThread(SDL_Thread *thread)
136{
137#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
138 pthread_cancel(thread->handle);
139#else
140 pthread_kill(thread->handle, SIGKILL);
141#endif
142}
143
144#endif
diff --git a/apps/plugins/sdl/src/thread/riscos/SDL_systhread_c.h b/apps/plugins/sdl/src/thread/riscos/SDL_systhread_c.h
deleted file mode 100644
index 9e1d2c8c5c..0000000000
--- a/apps/plugins/sdl/src/thread/riscos/SDL_systhread_c.h
+++ /dev/null
@@ -1,34 +0,0 @@
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#if SDL_THREADS_DISABLED
25
26typedef int SYS_ThreadHandle;
27
28#else
29
30#include <pthread.h>
31
32typedef pthread_t SYS_ThreadHandle;
33
34#endif
diff --git a/apps/plugins/sdl/src/thread/symbian/SDL_sysmutex.cpp b/apps/plugins/sdl/src/thread/symbian/SDL_sysmutex.cpp
deleted file mode 100644
index f4b1aeaad2..0000000000
--- a/apps/plugins/sdl/src/thread/symbian/SDL_sysmutex.cpp
+++ /dev/null
@@ -1,130 +0,0 @@
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@devolution.com
21*/
22
23/*
24 SDL_sysmutex.cpp
25
26 Epoc version by Markus Mertama (w@iki.fi)
27*/
28
29
30#ifdef SAVE_RCSID
31static char rcsid =
32 "@(#) $Id: SDL_sysmutex.c,v 1.1.2.3 2000/06/22 15:25:23 hercules Exp $";
33#endif
34
35/* Mutex functions using the Win32 API */
36
37//#include <stdio.h>
38//#include <stdlib.h>
39
40#include <e32std.h>
41
42#include "epoc_sdl.h"
43
44#include "SDL_error.h"
45#include "SDL_mutex.h"
46
47
48#ifdef EKA2 //???
49struct SDL_mutex
50 {
51 TInt handle;
52 };
53#else
54struct _SDL_mutex
55 {
56 TInt handle;
57 };
58#endif
59
60extern TInt CreateUnique(TInt (*aFunc)(const TDesC& aName, TAny*, TAny*), TAny*, TAny*);
61
62TInt NewMutex(const TDesC& aName, TAny* aPtr1, TAny*)
63 {
64 return ((RMutex*)aPtr1)->CreateGlobal(aName);
65 }
66
67void DeleteMutex(TAny* aMutex)
68 {
69 SDL_DestroyMutex ((SDL_mutex*) aMutex);
70 }
71
72/* Create a mutex */
73SDL_mutex *SDL_CreateMutex(void)
74{
75 RMutex rmutex;
76
77 TInt status = CreateUnique(NewMutex, &rmutex, NULL);
78 if(status != KErrNone)
79 {
80 SDL_SetError("Couldn't create mutex");
81 }
82 SDL_mutex* mutex = new /*(ELeave)*/ SDL_mutex;
83 mutex->handle = rmutex.Handle();
84 EpocSdlEnv::AppendCleanupItem(TSdlCleanupItem(DeleteMutex, mutex));
85 return(mutex);
86}
87
88/* Free the mutex */
89void SDL_DestroyMutex(SDL_mutex *mutex)
90{
91 if ( mutex )
92 {
93 RMutex rmutex;
94 rmutex.SetHandle(mutex->handle);
95 if(rmutex.IsHeld())
96 {
97 rmutex.Signal();
98 }
99 rmutex.Close();
100 EpocSdlEnv::RemoveCleanupItem(mutex);
101 delete(mutex);
102 mutex = NULL;
103 }
104}
105
106/* Lock the mutex */
107int SDL_mutexP(SDL_mutex *mutex)
108{
109 if ( mutex == NULL ) {
110 SDL_SetError("Passed a NULL mutex");
111 return -1;
112 }
113 RMutex rmutex;
114 rmutex.SetHandle(mutex->handle);
115 rmutex.Wait();
116 return(0);
117}
118
119/* Unlock the mutex */
120int SDL_mutexV(SDL_mutex *mutex)
121{
122 if ( mutex == NULL ) {
123 SDL_SetError("Passed a NULL mutex");
124 return -1;
125 }
126 RMutex rmutex;
127 rmutex.SetHandle(mutex->handle);
128 rmutex.Signal();
129 return(0);
130}
diff --git a/apps/plugins/sdl/src/thread/symbian/SDL_syssem.cpp b/apps/plugins/sdl/src/thread/symbian/SDL_syssem.cpp
deleted file mode 100644
index 00f9901ee5..0000000000
--- a/apps/plugins/sdl/src/thread/symbian/SDL_syssem.cpp
+++ /dev/null
@@ -1,214 +0,0 @@
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@devolution.com
21*/
22
23/*
24 SDL_syssem.cpp
25
26 Epoc version by Markus Mertama (w@iki.fi)
27*/
28
29#ifdef SAVE_RCSID
30static char rcsid =
31 "@(#) $Id: SDL_syssem.c,v 1.1.2.4 2000/06/22 15:24:48 hercules Exp $";
32#endif
33
34/* Semaphore functions using the Win32 API */
35
36//#include <stdio.h>
37//#include <stdlib.h>
38#include <e32std.h>
39
40#include "SDL_error.h"
41#include "SDL_thread.h"
42
43
44#define SDL_MUTEX_TIMEOUT -2
45
46struct SDL_semaphore
47 {
48 TInt handle;
49 TInt count;
50 };
51
52
53extern TInt CreateUnique(TInt (*aFunc)(const TDesC& aName, TAny*, TAny*), TAny*, TAny*);
54#ifndef EKA2
55extern TInt NewThread(const TDesC& aName, TAny* aPtr1, TAny* aPtr2);
56#endif
57
58TInt NewSema(const TDesC& aName, TAny* aPtr1, TAny* aPtr2)
59 {
60 TInt value = *((TInt*) aPtr2);
61 return ((RSemaphore*)aPtr1)->CreateGlobal(aName, value);
62 }
63
64/* Create a semaphore */
65SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
66{
67 RSemaphore s;
68 TInt status = CreateUnique(NewSema, &s, &initial_value);
69 if(status != KErrNone)
70 {
71 SDL_SetError("Couldn't create semaphore");
72 }
73 SDL_semaphore* sem = new /*(ELeave)*/ SDL_semaphore;
74 sem->handle = s.Handle();
75 sem->count = initial_value;
76 return(sem);
77}
78
79/* Free the semaphore */
80void SDL_DestroySemaphore(SDL_sem *sem)
81{
82 if ( sem )
83 {
84 RSemaphore sema;
85 sema.SetHandle(sem->handle);
86 while(--sem->count)
87 sema.Signal();
88 sema.Close();
89 delete sem;
90 sem = NULL;
91 }
92}
93
94#ifndef EKA2
95
96 struct TInfo
97 {
98 TInfo(TInt aTime, TInt aHandle) :
99 iTime(aTime), iHandle(aHandle), iVal(0) {}
100 TInt iTime;
101 TInt iHandle;
102 TInt iVal;
103 };
104
105
106
107TBool ThreadRun(TAny* aInfo)
108 {
109 TInfo* info = STATIC_CAST(TInfo*, aInfo);
110 User::After(info->iTime);
111 RSemaphore sema;
112 sema.SetHandle(info->iHandle);
113 sema.Signal();
114 info->iVal = SDL_MUTEX_TIMEOUT;
115 return 0;
116 }
117
118#endif
119
120
121void _WaitAll(SDL_sem *sem)
122 {
123 //since SemTryWait may changed the counter.
124 //this may not be atomic, but hopes it works.
125 RSemaphore sema;
126 sema.SetHandle(sem->handle);
127 sema.Wait();
128 while(sem->count < 0)
129 {
130 sema.Wait();
131 }
132 }
133
134int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
135{
136 if ( ! sem ) {
137 SDL_SetError("Passed a NULL sem");
138 return -1;
139 }
140
141 if ( timeout == SDL_MUTEX_MAXWAIT )
142 {
143 _WaitAll(sem);
144 return SDL_MUTEX_MAXWAIT;
145 }
146
147#ifdef EKA2
148
149 RSemaphore sema;
150 sema.SetHandle(sem->handle);
151 if(KErrNone == sema.Wait(timeout))
152 return 0;
153 return -1;
154#else
155 RThread thread;
156
157 TInfo* info = new (ELeave)TInfo(timeout, sem->handle);
158
159 TInt status = CreateUnique(NewThread, &thread, info);
160
161 if(status != KErrNone)
162 return status;
163
164 thread.Resume();
165
166 _WaitAll(sem);
167
168 if(thread.ExitType() == EExitPending)
169 {
170 thread.Kill(SDL_MUTEX_TIMEOUT);
171 }
172
173 thread.Close();
174
175 return info->iVal;
176#endif
177}
178
179int SDL_SemTryWait(SDL_sem *sem)
180{
181 if(sem->count > 0)
182 {
183 sem->count--;
184 }
185 return SDL_MUTEX_TIMEOUT;
186}
187
188int SDL_SemWait(SDL_sem *sem)
189{
190 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
191}
192
193/* Returns the current count of the semaphore */
194Uint32 SDL_SemValue(SDL_sem *sem)
195{
196 if ( ! sem ) {
197 SDL_SetError("Passed a NULL sem");
198 return 0;
199 }
200 return sem->count;
201}
202
203int SDL_SemPost(SDL_sem *sem)
204{
205 if ( ! sem ) {
206 SDL_SetError("Passed a NULL sem");
207 return -1;
208 }
209 sem->count++;
210 RSemaphore sema;
211 sema.SetHandle(sem->handle);
212 sema.Signal();
213 return 0;
214}
diff --git a/apps/plugins/sdl/src/thread/symbian/SDL_systhread.cpp b/apps/plugins/sdl/src/thread/symbian/SDL_systhread.cpp
deleted file mode 100644
index 5e7adc5540..0000000000
--- a/apps/plugins/sdl/src/thread/symbian/SDL_systhread.cpp
+++ /dev/null
@@ -1,146 +0,0 @@
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@devolution.com
21*/
22
23/*
24 SDL_systhread.cpp
25 Epoc thread management routines for SDL
26
27 Epoc version by Markus Mertama (w@iki.fi)
28*/
29
30#include "epoc_sdl.h"
31
32//#include <stdlib.h>
33//#include <stdio.h>
34
35
36
37extern "C" {
38#undef NULL
39#include "SDL_error.h"
40#include "SDL_thread.h"
41#include "SDL_systhread.h"
42#include "SDL_thread_c.h"
43 }
44
45#include <e32std.h>
46#include "epoc_sdl.h"
47
48
49static int object_count;
50
51int RunThread(TAny* data)
52{
53 CTrapCleanup* cleanup = CTrapCleanup::New();
54 TRAPD(err, SDL_RunThread(data));
55 EpocSdlEnv::CleanupItems();
56 delete cleanup;
57 return(err);
58}
59
60
61TInt NewThread(const TDesC& aName, TAny* aPtr1, TAny* aPtr2)
62 {
63 return ((RThread*)(aPtr1))->Create(aName,
64 RunThread,
65 KDefaultStackSize,
66 NULL,
67 aPtr2);
68 }
69
70int CreateUnique(TInt (*aFunc)(const TDesC& aName, TAny*, TAny*), TAny* aPtr1, TAny* aPtr2)
71 {
72 TBuf<16> name;
73 TInt status = KErrNone;
74 do
75 {
76 object_count++;
77 name.Format(_L("SDL_%x"), object_count);
78 status = aFunc(name, aPtr1, aPtr2);
79 }
80 while(status == KErrAlreadyExists);
81 return status;
82 }
83
84
85int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
86{
87 RThread rthread;
88
89 const TInt status = CreateUnique(NewThread, &rthread, args);
90 if (status != KErrNone)
91 {
92 delete(((RThread*)(thread->handle)));
93 thread->handle = NULL;
94 SDL_SetError("Not enough resources to create thread");
95 return(-1);
96 }
97 rthread.Resume();
98 thread->handle = rthread.Handle();
99 return(0);
100}
101
102void SDL_SYS_SetupThread(void)
103{
104 return;
105}
106
107Uint32 SDL_ThreadID(void)
108{
109 RThread current;
110 const TThreadId id = current.Id();
111 return id;
112}
113
114void SDL_SYS_WaitThread(SDL_Thread *thread)
115{
116 SDL_TRACE1("Close thread", thread);
117 RThread t;
118 const TInt err = t.Open(thread->threadid);
119 if(err == KErrNone && t.ExitType() == EExitPending)
120 {
121 TRequestStatus status;
122 t.Logon(status);
123 User::WaitForRequest(status);
124 }
125 t.Close();
126
127 /* RUndertaker taker;
128 taker.Create();
129 TRequestStatus status;
130 taker.Logon(status, thread->handle);
131 User::WaitForRequest(status);
132 taker.Close();*/
133 SDL_TRACE1("Closed thread", thread);
134}
135
136/* WARNING: This function is really a last resort.
137 * Threads should be signaled and then exit by themselves.
138 * TerminateThread() doesn't perform stack and DLL cleanup.
139 */
140void SDL_SYS_KillThread(SDL_Thread *thread)
141{
142 RThread rthread;
143 rthread.SetHandle(thread->handle);
144 rthread.Kill(0);
145 rthread.Close();
146}
diff --git a/apps/plugins/sdl/src/thread/symbian/SDL_systhread_c.h b/apps/plugins/sdl/src/thread/symbian/SDL_systhread_c.h
deleted file mode 100644
index f5f1729cda..0000000000
--- a/apps/plugins/sdl/src/thread/symbian/SDL_systhread_c.h
+++ /dev/null
@@ -1,30 +0,0 @@
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@devolution.com
21*/
22
23/*
24 SDL_systhread_c.h
25
26 Epoc version by Markus Mertama (w@iki.fi)
27*/
28
29typedef int SYS_ThreadHandle;
30
diff --git a/apps/plugins/sdl/src/thread/win32/SDL_sysmutex.c b/apps/plugins/sdl/src/thread/win32/SDL_sysmutex.c
deleted file mode 100644
index 1d7805c062..0000000000
--- a/apps/plugins/sdl/src/thread/win32/SDL_sysmutex.c
+++ /dev/null
@@ -1,95 +0,0 @@
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/* Mutex functions using the Win32 API */
25
26#define WIN32_LEAN_AND_MEAN
27#include <windows.h>
28
29#include "SDL_mutex.h"
30
31
32struct SDL_mutex {
33 HANDLE id;
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, with initial value signaled */
45 mutex->id = CreateMutex(NULL, FALSE, NULL);
46 if ( ! mutex->id ) {
47 SDL_SetError("Couldn't create mutex");
48 SDL_free(mutex);
49 mutex = NULL;
50 }
51 } else {
52 SDL_OutOfMemory();
53 }
54 return(mutex);
55}
56
57/* Free the mutex */
58void SDL_DestroyMutex(SDL_mutex *mutex)
59{
60 if ( mutex ) {
61 if ( mutex->id ) {
62 CloseHandle(mutex->id);
63 mutex->id = 0;
64 }
65 SDL_free(mutex);
66 }
67}
68
69/* Lock the mutex */
70int SDL_mutexP(SDL_mutex *mutex)
71{
72 if ( mutex == NULL ) {
73 SDL_SetError("Passed a NULL mutex");
74 return -1;
75 }
76 if ( WaitForSingleObject(mutex->id, INFINITE) == WAIT_FAILED ) {
77 SDL_SetError("Couldn't wait on mutex");
78 return -1;
79 }
80 return(0);
81}
82
83/* Unlock the mutex */
84int SDL_mutexV(SDL_mutex *mutex)
85{
86 if ( mutex == NULL ) {
87 SDL_SetError("Passed a NULL mutex");
88 return -1;
89 }
90 if ( ReleaseMutex(mutex->id) == FALSE ) {
91 SDL_SetError("Couldn't release mutex");
92 return -1;
93 }
94 return(0);
95}
diff --git a/apps/plugins/sdl/src/thread/win32/SDL_syssem.c b/apps/plugins/sdl/src/thread/win32/SDL_syssem.c
deleted file mode 100644
index 261d24cbfe..0000000000
--- a/apps/plugins/sdl/src/thread/win32/SDL_syssem.c
+++ /dev/null
@@ -1,164 +0,0 @@
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/* Semaphore functions using the Win32 API */
25
26#define WIN32_LEAN_AND_MEAN
27#include <windows.h>
28
29#include "SDL_thread.h"
30#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
31#include "win_ce_semaphore.h"
32#endif
33
34
35struct SDL_semaphore {
36#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
37 SYNCHHANDLE id;
38#else
39 HANDLE id;
40#endif
41 volatile LONG count;
42};
43
44
45/* Create a semaphore */
46SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
47{
48 SDL_sem *sem;
49
50 /* Allocate sem memory */
51 sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
52 if ( sem ) {
53 /* Create the semaphore, with max value 32K */
54#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
55 sem->id = CreateSemaphoreCE(NULL, initial_value, 32*1024, NULL);
56#else
57 sem->id = CreateSemaphore(NULL, initial_value, 32*1024, NULL);
58#endif
59 sem->count = (LONG) initial_value;
60 if ( ! sem->id ) {
61 SDL_SetError("Couldn't create semaphore");
62 SDL_free(sem);
63 sem = NULL;
64 }
65 } else {
66 SDL_OutOfMemory();
67 }
68 return(sem);
69}
70
71/* Free the semaphore */
72void SDL_DestroySemaphore(SDL_sem *sem)
73{
74 if ( sem ) {
75 if ( sem->id ) {
76#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
77 CloseSynchHandle(sem->id);
78#else
79 CloseHandle(sem->id);
80#endif
81 sem->id = 0;
82 }
83 SDL_free(sem);
84 }
85}
86
87int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
88{
89 int retval;
90 DWORD dwMilliseconds;
91
92 if ( ! sem ) {
93 SDL_SetError("Passed a NULL sem");
94 return -1;
95 }
96
97 if ( timeout == SDL_MUTEX_MAXWAIT ) {
98 dwMilliseconds = INFINITE;
99 } else {
100 dwMilliseconds = (DWORD)timeout;
101 }
102#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
103 switch (WaitForSemaphoreCE(sem->id, dwMilliseconds)) {
104#else
105 switch (WaitForSingleObject(sem->id, dwMilliseconds)) {
106#endif
107 case WAIT_OBJECT_0:
108 InterlockedDecrement(&sem->count);
109 retval = 0;
110 break;
111 case WAIT_TIMEOUT:
112 retval = SDL_MUTEX_TIMEDOUT;
113 break;
114 default:
115 SDL_SetError("WaitForSingleObject() failed");
116 retval = -1;
117 break;
118 }
119 return retval;
120}
121
122int SDL_SemTryWait(SDL_sem *sem)
123{
124 return SDL_SemWaitTimeout(sem, 0);
125}
126
127int SDL_SemWait(SDL_sem *sem)
128{
129 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
130}
131
132/* Returns the current count of the semaphore */
133Uint32 SDL_SemValue(SDL_sem *sem)
134{
135 if ( ! sem ) {
136 SDL_SetError("Passed a NULL sem");
137 return 0;
138 }
139 return (Uint32) sem->count;
140}
141
142int SDL_SemPost(SDL_sem *sem)
143{
144 if ( ! sem ) {
145 SDL_SetError("Passed a NULL sem");
146 return -1;
147 }
148 /* Increase the counter in the first place, because
149 * after a successful release the semaphore may
150 * immediately get destroyed by another thread which
151 * is waiting for this semaphore.
152 */
153 InterlockedIncrement(&sem->count);
154#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
155 if ( ReleaseSemaphoreCE(sem->id, 1, NULL) == FALSE ) {
156#else
157 if ( ReleaseSemaphore(sem->id, 1, NULL) == FALSE ) {
158#endif
159 InterlockedDecrement(&sem->count); /* restore */
160 SDL_SetError("ReleaseSemaphore() failed");
161 return -1;
162 }
163 return 0;
164}
diff --git a/apps/plugins/sdl/src/thread/win32/SDL_systhread.c b/apps/plugins/sdl/src/thread/win32/SDL_systhread.c
deleted file mode 100644
index 55cb88a58c..0000000000
--- a/apps/plugins/sdl/src/thread/win32/SDL_systhread.c
+++ /dev/null
@@ -1,162 +0,0 @@
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/* Win32 thread management routines for SDL */
25
26#define WIN32_LEAN_AND_MEAN
27#include <windows.h>
28
29#include "SDL_thread.h"
30#include "../SDL_thread_c.h"
31#include "../SDL_systhread.h"
32
33#ifndef SDL_PASSED_BEGINTHREAD_ENDTHREAD
34#ifndef _WIN32_WCE
35/* We'll use the C library from this DLL */
36#include <process.h>
37#endif
38
39#if defined(__WATCOMC__)
40/* This is for Watcom targets except OS2 */
41#if __WATCOMC__ < 1240
42#define __watcall
43#endif
44typedef unsigned long (__watcall *pfnSDL_CurrentBeginThread) (void *, unsigned,
45 unsigned (__stdcall *func)(void *), void *arg,
46 unsigned, unsigned *threadID);
47typedef void (__watcall *pfnSDL_CurrentEndThread)(unsigned code);
48#elif (defined(__MINGW32__) && (__GNUC__ < 4))
49typedef unsigned long (__cdecl *pfnSDL_CurrentBeginThread) (void *, unsigned,
50 unsigned (__stdcall *func)(void *), void *arg,
51 unsigned, unsigned *threadID);
52typedef void (__cdecl *pfnSDL_CurrentEndThread)(unsigned code);
53#else
54typedef uintptr_t (__cdecl *pfnSDL_CurrentBeginThread) (void *, unsigned,
55 unsigned (__stdcall *func)(void *), void *arg,
56 unsigned, unsigned *threadID);
57typedef void (__cdecl *pfnSDL_CurrentEndThread)(unsigned code);
58#endif
59#endif /* !SDL_PASSED_BEGINTHREAD_ENDTHREAD */
60
61
62typedef struct ThreadStartParms
63{
64 void *args;
65 pfnSDL_CurrentEndThread pfnCurrentEndThread;
66} tThreadStartParms, *pThreadStartParms;
67
68static DWORD RunThread(void *data)
69{
70 pThreadStartParms pThreadParms = (pThreadStartParms)data;
71 pfnSDL_CurrentEndThread pfnCurrentEndThread = NULL;
72
73 // Call the thread function!
74 SDL_RunThread(pThreadParms->args);
75
76 // Get the current endthread we have to use!
77 if (pThreadParms)
78 {
79 pfnCurrentEndThread = pThreadParms->pfnCurrentEndThread;
80 SDL_free(pThreadParms);
81 }
82 // Call endthread!
83 if (pfnCurrentEndThread)
84 (*pfnCurrentEndThread)(0);
85 return(0);
86}
87
88static DWORD WINAPI RunThreadViaCreateThread(LPVOID data)
89{
90 return RunThread(data);
91}
92
93static unsigned __stdcall RunThreadViaBeginThreadEx(void *data)
94{
95 return (unsigned) RunThread(data);
96}
97
98#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
99int SDL_SYS_CreateThread(SDL_Thread *thread, void *args, pfnSDL_CurrentBeginThread pfnBeginThread, pfnSDL_CurrentEndThread pfnEndThread)
100{
101#else
102int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
103{
104#ifdef _WIN32_WCE
105 pfnSDL_CurrentBeginThread pfnBeginThread = NULL;
106 pfnSDL_CurrentEndThread pfnEndThread = NULL;
107#else
108 pfnSDL_CurrentBeginThread pfnBeginThread = _beginthreadex;
109 pfnSDL_CurrentEndThread pfnEndThread = _endthreadex;
110#endif
111#endif /* SDL_PASSED_BEGINTHREAD_ENDTHREAD */
112 pThreadStartParms pThreadParms = (pThreadStartParms)SDL_malloc(sizeof(tThreadStartParms));
113 if (!pThreadParms) {
114 SDL_OutOfMemory();
115 return(-1);
116 }
117
118 // Save the function which we will have to call to clear the RTL of calling app!
119 pThreadParms->pfnCurrentEndThread = pfnEndThread;
120 // Also save the real parameters we have to pass to thread function
121 pThreadParms->args = args;
122
123 if (pfnBeginThread) {
124 unsigned threadid = 0;
125 thread->handle = (SYS_ThreadHandle)
126 ((size_t) pfnBeginThread(NULL, 0, RunThreadViaBeginThreadEx,
127 pThreadParms, 0, &threadid));
128 } else {
129 DWORD threadid = 0;
130 thread->handle = CreateThread(NULL, 0, RunThreadViaCreateThread, pThreadParms, 0, &threadid);
131 }
132 if (thread->handle == NULL) {
133 SDL_SetError("Not enough resources to create thread");
134 return(-1);
135 }
136 return(0);
137}
138
139void SDL_SYS_SetupThread(void)
140{
141 return;
142}
143
144Uint32 SDL_ThreadID(void)
145{
146 return((Uint32)GetCurrentThreadId());
147}
148
149void SDL_SYS_WaitThread(SDL_Thread *thread)
150{
151 WaitForSingleObject(thread->handle, INFINITE);
152 CloseHandle(thread->handle);
153}
154
155/* WARNING: This function is really a last resort.
156 * Threads should be signaled and then exit by themselves.
157 * TerminateThread() doesn't perform stack and DLL cleanup.
158 */
159void SDL_SYS_KillThread(SDL_Thread *thread)
160{
161 TerminateThread(thread->handle, FALSE);
162}
diff --git a/apps/plugins/sdl/src/thread/win32/SDL_systhread_c.h b/apps/plugins/sdl/src/thread/win32/SDL_systhread_c.h
deleted file mode 100644
index 10b0a7d6b5..0000000000
--- a/apps/plugins/sdl/src/thread/win32/SDL_systhread_c.h
+++ /dev/null
@@ -1,28 +0,0 @@
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#define WIN32_LEAN_AND_MEAN
25#include <windows.h>
26
27typedef HANDLE SYS_ThreadHandle;
28
diff --git a/apps/plugins/sdl/src/thread/win32/win_ce_semaphore.c b/apps/plugins/sdl/src/thread/win32/win_ce_semaphore.c
deleted file mode 100644
index 9db45c4391..0000000000
--- a/apps/plugins/sdl/src/thread/win32/win_ce_semaphore.c
+++ /dev/null
@@ -1,216 +0,0 @@
1/* win_ce_semaphore.c
2
3 Copyright (c) 1998, Johnson M. Hart
4 (with corrections 2001 by Rainer Loritz)
5 Permission is granted for any and all use providing that this
6 copyright is properly acknowledged.
7 There are no assurances of suitability for any use whatsoever.
8
9 WINDOWS CE: There is a collection of Windows CE functions to simulate
10 semaphores using only a mutex and an event. As Windows CE events cannot
11 be named, these simulated semaphores cannot be named either.
12
13 Implementation notes:
14 1. All required internal data structures are allocated on the process's heap.
15 2. Where appropriate, a new error code is returned (see the header
16 file), or, if the error is a Win32 error, that code is unchanged.
17 3. Notice the new handle type "SYNCHHANDLE" that has handles, counters,
18 and other information. This structure will grow as new objects are added
19 to this set; some members are specific to only one or two of the objects.
20 4. Mutexes are used for critical sections. These could be replaced with
21 CRITICAL_SECTION objects but then this would give up the time out
22 capability.
23 5. The implementation shows several interesting aspects of synchronization, some
24 of which are specific to Win32 and some of which are general. These are pointed
25 out in the comments as appropriate.
26 6. The wait function emulates WaitForSingleObject only. An emulation of
27 WaitForMultipleObjects is much harder to implement outside the kernel,
28 and it is not clear how to handle a mixture of WCE semaphores and normal
29 events and mutexes. */
30
31#define WIN32_LEAN_AND_MEAN
32#include <windows.h>
33
34#include "win_ce_semaphore.h"
35
36static SYNCHHANDLE CleanUp (SYNCHHANDLE hSynch, DWORD Flags);
37
38SYNCHHANDLE CreateSemaphoreCE (
39
40 LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, /* pointer to security attributes */
41 LONG lInitialCount, /* initial count */
42 LONG lMaximumCount, /* maximum count */
43 LPCTSTR lpName )
44
45/* Semaphore for use with Windows CE that does not support them directly.
46 Requires a counter, a mutex to protect the counter, and an
47 autoreset event.
48
49 Here are the rules that must always hold between the autoreset event
50 and the mutex (any violation of these rules by the CE semaphore functions
51 will, in all likelihood, result in a defect):
52 1. No thread can set, pulse, or reset the event,
53 nor can it access any part of the SYNCHHANDLE structure,
54 without first gaining ownership of the mutex.
55 BUT, a thread can wait on the event without owning the mutex
56 (this is clearly necessary or else the event could never be set).
57 2. The event is in a signaled state if and only if the current semaphore
58 count ("CurCount") is greater than zero.
59 3. The semaphore count is always >= 0 and <= the maximum count */
60
61{
62 SYNCHHANDLE hSynch = NULL, result = NULL;
63
64 __try
65 {
66 if (lInitialCount > lMaximumCount || lMaximumCount < 0 || lInitialCount < 0)
67 {
68 /* Bad parameters */
69 SetLastError (SYNCH_ERROR);
70 __leave;
71 }
72
73 hSynch = HeapAlloc (GetProcessHeap(), HEAP_ZERO_MEMORY, SYNCH_HANDLE_SIZE);
74 if (hSynch == NULL) __leave;
75
76 hSynch->MaxCount = lMaximumCount;
77 hSynch->CurCount = lInitialCount;
78 hSynch->lpName = lpName;
79
80 hSynch->hMutex = CreateMutex (lpSemaphoreAttributes, FALSE, NULL);
81
82 WaitForSingleObject (hSynch->hMutex, INFINITE);
83 /* Create the event. It is initially signaled if and only if the
84 initial count is > 0 */
85 hSynch->hEvent = CreateEvent (lpSemaphoreAttributes, FALSE,
86 lInitialCount > 0, NULL);
87 ReleaseMutex (hSynch->hMutex);
88 hSynch->hSemph = NULL;
89 }
90 __finally
91 {
92 /* Return with the handle, or, if there was any error, return
93 a null after closing any open handles and freeing any allocated memory. */
94 result=CleanUp(hSynch, 6 /* An event and a mutex, but no semaphore. */);
95 }
96
97 return result;
98}
99
100BOOL ReleaseSemaphoreCE (SYNCHHANDLE hSemCE, LONG cReleaseCount, LPLONG lpPreviousCount)
101/* Windows CE equivalent to ReleaseSemaphore. */
102{
103 BOOL Result = TRUE;
104
105 /* Gain access to the object to assure that the release count
106 would not cause the total count to exceed the maximum. */
107
108 __try
109 {
110 WaitForSingleObject (hSemCE->hMutex, INFINITE);
111 /* reply only if asked to */
112 if (lpPreviousCount!=NULL)
113 *lpPreviousCount = hSemCE->CurCount;
114 if (hSemCE->CurCount + cReleaseCount > hSemCE->MaxCount || cReleaseCount <= 0)
115 {
116 SetLastError (SYNCH_ERROR);
117 Result = FALSE;
118 __leave;
119 }
120 hSemCE->CurCount += cReleaseCount;
121
122 /* Set the autoreset event, releasing exactly one waiting thread, now or
123 in the future. */
124
125 SetEvent (hSemCE->hEvent);
126 }
127 __finally
128 {
129 ReleaseMutex (hSemCE->hMutex);
130 }
131
132 return Result;
133}
134
135DWORD WaitForSemaphoreCE (SYNCHHANDLE hSemCE, DWORD dwMilliseconds)
136 /* Windows CE semaphore equivalent of WaitForSingleObject. */
137{
138 DWORD WaitResult;
139
140 WaitResult = WaitForSingleObject (hSemCE->hMutex, dwMilliseconds);
141 if (WaitResult != WAIT_OBJECT_0 && WaitResult != WAIT_ABANDONED_0) return WaitResult;
142 while (hSemCE->CurCount <= 0)
143 {
144
145 /* The count is 0, and the thread must wait on the event (which, by
146 the rules, is currently reset) for semaphore resources to become
147 available. First, of course, the mutex must be released so that another
148 thread will be capable of setting the event. */
149
150 ReleaseMutex (hSemCE->hMutex);
151
152 /* Wait for the event to be signaled, indicating a semaphore state change.
153 The event is autoreset and signaled with a SetEvent (not PulseEvent)
154 so exactly one waiting thread (whether or not there is currently
155 a waiting thread) is released as a result of the SetEvent. */
156
157 WaitResult = WaitForSingleObject (hSemCE->hEvent, dwMilliseconds);
158 if (WaitResult != WAIT_OBJECT_0) return WaitResult;
159
160 /* This is where the properties of setting of an autoreset event is critical
161 to assure that, even if the semaphore state changes between the
162 preceding Wait and the next, and even if NO threads are waiting
163 on the event at the time of the SetEvent, at least one thread
164 will be released.
165 Pulsing a manual reset event would appear to work, but it would have
166 a defect which could appear if the semaphore state changed between
167 the two waits. */
168
169 WaitResult = WaitForSingleObject (hSemCE->hMutex, dwMilliseconds);
170 if (WaitResult != WAIT_OBJECT_0 && WaitResult != WAIT_ABANDONED_0) return WaitResult;
171
172 }
173 /* The count is not zero and this thread owns the mutex. */
174
175 hSemCE->CurCount--;
176 /* The event is now unsignaled, BUT, the semaphore count may not be
177 zero, in which case the event should be signaled again
178 before releasing the mutex. */
179
180 if (hSemCE->CurCount > 0) SetEvent (hSemCE->hEvent);
181 ReleaseMutex (hSemCE->hMutex);
182 return WaitResult;
183}
184
185BOOL CloseSynchHandle (SYNCHHANDLE hSynch)
186/* Close a synchronization handle.
187 Improvement: Test for a valid handle before dereferencing the handle. */
188{
189 BOOL Result = TRUE;
190 if (hSynch->hEvent != NULL) Result = Result && CloseHandle (hSynch->hEvent);
191 if (hSynch->hMutex != NULL) Result = Result && CloseHandle (hSynch->hMutex);
192 if (hSynch->hSemph != NULL) Result = Result && CloseHandle (hSynch->hSemph);
193 HeapFree (GetProcessHeap (), 0, hSynch);
194 return (Result);
195}
196
197static SYNCHHANDLE CleanUp (SYNCHHANDLE hSynch, DWORD Flags)
198{ /* Prepare to return from a create of a synchronization handle.
199 If there was any failure, free any allocated resources.
200 "Flags" indicates which Win32 objects are required in the
201 synchronization handle. */
202
203 BOOL ok = TRUE;
204
205 if (hSynch == NULL) return NULL;
206 if ((Flags & 4) == 1 && (hSynch->hEvent == NULL)) ok = FALSE;
207 if ((Flags & 2) == 1 && (hSynch->hMutex == NULL)) ok = FALSE;
208 if ((Flags & 1) == 1 && (hSynch->hEvent == NULL)) ok = FALSE;
209 if (!ok)
210 {
211 CloseSynchHandle (hSynch);
212 return NULL;
213 }
214 /* Everything worked */
215 return hSynch;
216}
diff --git a/apps/plugins/sdl/src/thread/win32/win_ce_semaphore.h b/apps/plugins/sdl/src/thread/win32/win_ce_semaphore.h
deleted file mode 100644
index af2d7b613d..0000000000
--- a/apps/plugins/sdl/src/thread/win32/win_ce_semaphore.h
+++ /dev/null
@@ -1,22 +0,0 @@
1/* win_ce_semaphore.h - header file to go with win_ce_semaphore.c */
2
3typedef struct _SYNCH_HANDLE_STRUCTURE {
4 HANDLE hEvent;
5 HANDLE hMutex;
6 HANDLE hSemph;
7 LONG MaxCount;
8 volatile LONG CurCount;
9 LPCTSTR lpName;
10} SYNCH_HANDLE_STRUCTURE, *SYNCHHANDLE;
11
12#define SYNCH_HANDLE_SIZE sizeof (SYNCH_HANDLE_STRUCTURE)
13
14 /* Error codes - all must have bit 29 set */
15#define SYNCH_ERROR 0X20000000 /* EXERCISE - REFINE THE ERROR NUMBERS */
16
17extern SYNCHHANDLE CreateSemaphoreCE (LPSECURITY_ATTRIBUTES, LONG, LONG, LPCTSTR);
18
19extern BOOL ReleaseSemaphoreCE (SYNCHHANDLE, LONG, LPLONG);
20extern DWORD WaitForSemaphoreCE (SYNCHHANDLE, DWORD);
21
22extern BOOL CloseSynchHandle (SYNCHHANDLE);