diff options
Diffstat (limited to 'apps/plugins/sdl/src/thread/riscos')
-rw-r--r-- | apps/plugins/sdl/src/thread/riscos/SDL_syscond.c | 160 | ||||
-rw-r--r-- | apps/plugins/sdl/src/thread/riscos/SDL_sysmutex.c | 153 | ||||
-rw-r--r-- | apps/plugins/sdl/src/thread/riscos/SDL_sysmutex_c.h | 34 | ||||
-rw-r--r-- | apps/plugins/sdl/src/thread/riscos/SDL_syssem.c | 203 | ||||
-rw-r--r-- | apps/plugins/sdl/src/thread/riscos/SDL_systhread.c | 144 | ||||
-rw-r--r-- | apps/plugins/sdl/src/thread/riscos/SDL_systhread_c.h | 34 |
6 files changed, 728 insertions, 0 deletions
diff --git a/apps/plugins/sdl/src/thread/riscos/SDL_syscond.c b/apps/plugins/sdl/src/thread/riscos/SDL_syscond.c new file mode 100644 index 0000000000..ee5566647f --- /dev/null +++ b/apps/plugins/sdl/src/thread/riscos/SDL_syscond.c | |||
@@ -0,0 +1,160 @@ | |||
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 | |||
36 | struct SDL_cond | ||
37 | { | ||
38 | pthread_cond_t cond; | ||
39 | }; | ||
40 | |||
41 | /* Create a condition variable */ | ||
42 | SDL_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 */ | ||
58 | void 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 */ | ||
67 | int 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 */ | ||
85 | int 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 | |||
102 | int 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 | */ | ||
144 | int 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 new file mode 100644 index 0000000000..e9ac139b36 --- /dev/null +++ b/apps/plugins/sdl/src/thread/riscos/SDL_sysmutex.c | |||
@@ -0,0 +1,153 @@ | |||
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 | |||
34 | struct 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 | |||
42 | SDL_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 | |||
67 | void 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 */ | ||
76 | int 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 | |||
115 | int 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 new file mode 100644 index 0000000000..2391c3c108 --- /dev/null +++ b/apps/plugins/sdl/src/thread/riscos/SDL_sysmutex_c.h | |||
@@ -0,0 +1,34 @@ | |||
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 | ||
28 | struct 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 new file mode 100644 index 0000000000..127211bf76 --- /dev/null +++ b/apps/plugins/sdl/src/thread/riscos/SDL_syssem.c | |||
@@ -0,0 +1,203 @@ | |||
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 | |||
36 | SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) | ||
37 | { | ||
38 | SDL_SetError("SDL not configured with thread support"); | ||
39 | return (SDL_sem *)0; | ||
40 | } | ||
41 | |||
42 | void SDL_DestroySemaphore(SDL_sem *sem) | ||
43 | { | ||
44 | return; | ||
45 | } | ||
46 | |||
47 | int SDL_SemTryWait(SDL_sem *sem) | ||
48 | { | ||
49 | SDL_SetError("SDL not configured with thread support"); | ||
50 | return -1; | ||
51 | } | ||
52 | |||
53 | int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) | ||
54 | { | ||
55 | SDL_SetError("SDL not configured with thread support"); | ||
56 | return -1; | ||
57 | } | ||
58 | |||
59 | int SDL_SemWait(SDL_sem *sem) | ||
60 | { | ||
61 | SDL_SetError("SDL not configured with thread support"); | ||
62 | return -1; | ||
63 | } | ||
64 | |||
65 | Uint32 SDL_SemValue(SDL_sem *sem) | ||
66 | { | ||
67 | return 0; | ||
68 | } | ||
69 | |||
70 | int 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 | |||
83 | struct SDL_semaphore { | ||
84 | sem_t *sem; | ||
85 | sem_t sem_data; | ||
86 | }; | ||
87 | |||
88 | /* Create a semaphore, initialized with value */ | ||
89 | SDL_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 | |||
106 | void SDL_DestroySemaphore(SDL_sem *sem) | ||
107 | { | ||
108 | if ( sem ) { | ||
109 | sem_destroy(sem->sem); | ||
110 | SDL_free(sem); | ||
111 | } | ||
112 | } | ||
113 | |||
114 | int 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 | |||
129 | int 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 | |||
145 | int 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 | |||
175 | Uint32 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 | |||
187 | int 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 new file mode 100644 index 0000000000..38fc333ae8 --- /dev/null +++ b/apps/plugins/sdl/src/thread/riscos/SDL_systhread.c | |||
@@ -0,0 +1,144 @@ | |||
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 | |||
31 | int 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 | |||
37 | void SDL_SYS_SetupThread(void) | ||
38 | { | ||
39 | return; | ||
40 | } | ||
41 | |||
42 | Uint32 SDL_ThreadID(void) | ||
43 | { | ||
44 | return(0); | ||
45 | } | ||
46 | |||
47 | void SDL_SYS_WaitThread(SDL_Thread *thread) | ||
48 | { | ||
49 | return; | ||
50 | } | ||
51 | |||
52 | void 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 */ | ||
62 | static int sig_list[] = { | ||
63 | SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH, | ||
64 | SIGVTALRM, SIGPROF, 0 | ||
65 | }; | ||
66 | |||
67 | #include <pthread.h> | ||
68 | |||
69 | int riscos_using_threads = 0; | ||
70 | Uint32 riscos_main_thread = 0; /* Thread running events */ | ||
71 | |||
72 | static void *RunThread(void *data) | ||
73 | { | ||
74 | SDL_RunThread(data); | ||
75 | pthread_exit((void*)0); | ||
76 | return((void *)0); /* Prevent compiler warning */ | ||
77 | } | ||
78 | |||
79 | int 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 | |||
105 | void 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 | |||
125 | Uint32 SDL_ThreadID(void) | ||
126 | { | ||
127 | return((Uint32)pthread_self()); | ||
128 | } | ||
129 | |||
130 | void SDL_SYS_WaitThread(SDL_Thread *thread) | ||
131 | { | ||
132 | pthread_join(thread->handle, 0); | ||
133 | } | ||
134 | |||
135 | void 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 new file mode 100644 index 0000000000..9e1d2c8c5c --- /dev/null +++ b/apps/plugins/sdl/src/thread/riscos/SDL_systhread_c.h | |||
@@ -0,0 +1,34 @@ | |||
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 | |||
26 | typedef int SYS_ThreadHandle; | ||
27 | |||
28 | #else | ||
29 | |||
30 | #include <pthread.h> | ||
31 | |||
32 | typedef pthread_t SYS_ThreadHandle; | ||
33 | |||
34 | #endif | ||