diff options
author | Franklin Wei <git@fwei.tk> | 2017-01-21 15:18:31 -0500 |
---|---|---|
committer | Franklin Wei <git@fwei.tk> | 2017-12-23 21:01:26 -0500 |
commit | a855d6202536ff28e5aae4f22a0f31d8f5b325d0 (patch) | |
tree | 8c75f224dd64ed360505afa8843d016b0d75000b /apps/plugins/sdl/src/thread/os2 | |
parent | 01c6dcf6c7b9bb1ad2fa0450f99bacc5f3d3e04b (diff) | |
download | rockbox-a855d6202536ff28e5aae4f22a0f31d8f5b325d0.tar.gz rockbox-a855d6202536ff28e5aae4f22a0f31d8f5b325d0.zip |
Port of Duke Nukem 3D
This ports Fabien Sanglard's Chocolate Duke to run on a version of SDL
for Rockbox.
Change-Id: I8f2c4c78af19de10c1633ed7bb7a997b43256dd9
Diffstat (limited to 'apps/plugins/sdl/src/thread/os2')
-rw-r--r-- | apps/plugins/sdl/src/thread/os2/SDL_syscond.c | 215 | ||||
-rw-r--r-- | apps/plugins/sdl/src/thread/os2/SDL_syscond_c.h | 23 | ||||
-rw-r--r-- | apps/plugins/sdl/src/thread/os2/SDL_sysmutex.c | 108 | ||||
-rw-r--r-- | apps/plugins/sdl/src/thread/os2/SDL_syssem.c | 192 | ||||
-rw-r--r-- | apps/plugins/sdl/src/thread/os2/SDL_systhread.c | 108 | ||||
-rw-r--r-- | apps/plugins/sdl/src/thread/os2/SDL_systhread_c.h | 28 |
6 files changed, 674 insertions, 0 deletions
diff --git a/apps/plugins/sdl/src/thread/os2/SDL_syscond.c b/apps/plugins/sdl/src/thread/os2/SDL_syscond.c new file mode 100644 index 0000000000..3e80594c2a --- /dev/null +++ b/apps/plugins/sdl/src/thread/os2/SDL_syscond.c | |||
@@ -0,0 +1,215 @@ | |||
1 | /* | ||
2 | SDL - Simple DirectMedia Layer | ||
3 | Copyright (C) 1997-2012 Sam Lantinga | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Lesser General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2.1 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Lesser General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Lesser General Public | ||
16 | License along with this library; if not, write to the Free Software | ||
17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
18 | |||
19 | Sam Lantinga | ||
20 | slouken@libsdl.org | ||
21 | */ | ||
22 | #include "SDL_config.h" | ||
23 | |||
24 | /* An implementation of condition variables using semaphores and mutexes */ | ||
25 | /* | ||
26 | This implementation borrows heavily from the BeOS condition variable | ||
27 | implementation, written by Christopher Tate and Owen Smith. Thanks! | ||
28 | */ | ||
29 | |||
30 | #include "SDL_thread.h" | ||
31 | |||
32 | struct 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 */ | ||
42 | DECLSPEC 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 */ | ||
63 | DECLSPEC 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 */ | ||
80 | DECLSPEC 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 */ | ||
104 | DECLSPEC 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 | |||
141 | Typical use: | ||
142 | |||
143 | Thread A: | ||
144 | SDL_LockMutex(lock); | ||
145 | while ( ! condition ) { | ||
146 | SDL_CondWait(cond); | ||
147 | } | ||
148 | SDL_UnlockMutex(lock); | ||
149 | |||
150 | Thread B: | ||
151 | SDL_LockMutex(lock); | ||
152 | ... | ||
153 | condition = true; | ||
154 | ... | ||
155 | SDL_UnlockMutex(lock); | ||
156 | */ | ||
157 | DECLSPEC 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 */ | ||
212 | DECLSPEC 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 new file mode 100644 index 0000000000..1120b2d80d --- /dev/null +++ b/apps/plugins/sdl/src/thread/os2/SDL_syscond_c.h | |||
@@ -0,0 +1,23 @@ | |||
1 | /* | ||
2 | SDL - Simple DirectMedia Layer | ||
3 | Copyright (C) 1997-2012 Sam Lantinga | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Lesser General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2.1 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Lesser General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Lesser General Public | ||
16 | License along with this library; if not, write to the Free Software | ||
17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
18 | |||
19 | Sam Lantinga | ||
20 | slouken@libsdl.org | ||
21 | */ | ||
22 | #include "SDL_config.h" | ||
23 | |||
diff --git a/apps/plugins/sdl/src/thread/os2/SDL_sysmutex.c b/apps/plugins/sdl/src/thread/os2/SDL_sysmutex.c new file mode 100644 index 0000000000..1e21897656 --- /dev/null +++ b/apps/plugins/sdl/src/thread/os2/SDL_sysmutex.c | |||
@@ -0,0 +1,108 @@ | |||
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 | |||
33 | struct SDL_mutex { | ||
34 | HMTX hmtxID; | ||
35 | }; | ||
36 | |||
37 | /* Create a mutex */ | ||
38 | DECLSPEC 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 */ | ||
65 | DECLSPEC 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 */ | ||
79 | DECLSPEC 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 */ | ||
95 | DECLSPEC 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 new file mode 100644 index 0000000000..d6dfba67bb --- /dev/null +++ b/apps/plugins/sdl/src/thread/os2/SDL_syssem.c | |||
@@ -0,0 +1,192 @@ | |||
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 | |||
35 | struct SDL_semaphore { | ||
36 | HMTX id; | ||
37 | HEV changed; | ||
38 | Uint32 value; | ||
39 | }; | ||
40 | |||
41 | |||
42 | /* Create a semaphore */ | ||
43 | DECLSPEC 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 */ | ||
70 | DECLSPEC 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 | |||
82 | DECLSPEC 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 | |||
158 | DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem *sem) | ||
159 | { | ||
160 | return SDL_SemWaitTimeout(sem, 0); | ||
161 | } | ||
162 | |||
163 | DECLSPEC 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 */ | ||
169 | DECLSPEC 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 | |||
178 | DECLSPEC 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 new file mode 100644 index 0000000000..33f815a5fb --- /dev/null +++ b/apps/plugins/sdl/src/thread/os2/SDL_systhread.c | |||
@@ -0,0 +1,108 @@ | |||
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 | |||
35 | typedef struct ThreadStartParms | ||
36 | { | ||
37 | void *args; | ||
38 | pfnSDL_CurrentEndThread pfnCurrentEndThread; | ||
39 | } tThreadStartParms, *pThreadStartParms; | ||
40 | |||
41 | static 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 | |||
60 | int 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 | |||
83 | void SDL_SYS_SetupThread(void) | ||
84 | { | ||
85 | return; | ||
86 | } | ||
87 | |||
88 | DECLSPEC Uint32 SDLCALL SDL_ThreadID(void) | ||
89 | { | ||
90 | PTIB tib; | ||
91 | DosGetInfoBlocks(&tib, NULL); | ||
92 | return((Uint32) (tib->tib_ptib2->tib2_ultid)); | ||
93 | } | ||
94 | |||
95 | void 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 | */ | ||
105 | void 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 new file mode 100644 index 0000000000..3b94dfed53 --- /dev/null +++ b/apps/plugins/sdl/src/thread/os2/SDL_systhread_c.h | |||
@@ -0,0 +1,28 @@ | |||
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 | |||
27 | typedef TID SYS_ThreadHandle; | ||
28 | |||