comparison src/thread/riscos/SDL_syssem.c @ 1895:c121d94672cb

SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 10 Jul 2006 21:04:37 +0000
parents 536b0704b7d8
children e27bdcc80744
comparison
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 18
19 Sam Lantinga 19 Sam Lantinga
20 slouken@libsdl.org 20 slouken@libsdl.org
21 */ 21 */
22
23 #include <errno.h>
24
25 #include "SDL_config.h" 22 #include "SDL_config.h"
26 23
27 /* RISC OS semiphores based on linux code */ 24 /* RISC OS semiphores based on linux code */
28 25
29 26
31 #include "SDL_thread.h" 28 #include "SDL_thread.h"
32 #include "SDL_systhread_c.h" 29 #include "SDL_systhread_c.h"
33 30
34 #if !SDL_THREADS_DISABLED 31 #if !SDL_THREADS_DISABLED
35 32
36 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) 33 SDL_sem *
37 { 34 SDL_CreateSemaphore(Uint32 initial_value)
38 SDL_SetError("SDL not configured with thread support"); 35 {
39 return (SDL_sem *)0; 36 SDL_SetError("SDL not configured with thread support");
40 } 37 return (SDL_sem *) 0;
41 38 }
42 void SDL_DestroySemaphore(SDL_sem *sem) 39
43 { 40 void
44 return; 41 SDL_DestroySemaphore(SDL_sem * sem)
45 } 42 {
46 43 return;
47 int SDL_SemTryWait(SDL_sem *sem) 44 }
48 { 45
49 SDL_SetError("SDL not configured with thread support"); 46 int
50 return -1; 47 SDL_SemTryWait(SDL_sem * sem)
51 } 48 {
52 49 SDL_SetError("SDL not configured with thread support");
53 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) 50 return -1;
54 { 51 }
55 SDL_SetError("SDL not configured with thread support"); 52
56 return -1; 53 int
57 } 54 SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
58 55 {
59 int SDL_SemWait(SDL_sem *sem) 56 SDL_SetError("SDL not configured with thread support");
60 { 57 return -1;
61 SDL_SetError("SDL not configured with thread support"); 58 }
62 return -1; 59
63 } 60 int
64 61 SDL_SemWait(SDL_sem * sem)
65 Uint32 SDL_SemValue(SDL_sem *sem) 62 {
66 { 63 SDL_SetError("SDL not configured with thread support");
67 return 0; 64 return -1;
68 } 65 }
69 66
70 int SDL_SemPost(SDL_sem *sem) 67 Uint32
71 { 68 SDL_SemValue(SDL_sem * sem)
72 SDL_SetError("SDL not configured with thread support"); 69 {
73 return -1; 70 return 0;
71 }
72
73 int
74 SDL_SemPost(SDL_sem * sem)
75 {
76 SDL_SetError("SDL not configured with thread support");
77 return -1;
74 } 78 }
75 79
76 #else 80 #else
77 81
78 82
79 #include <unistd.h> /* For getpid() */ 83 #include <unistd.h> /* For getpid() */
80 #include <pthread.h> 84 #include <pthread.h>
81 #include <semaphore.h> 85 #include <semaphore.h>
82 86
83 struct SDL_semaphore { 87 struct SDL_semaphore
84 sem_t *sem; 88 {
85 sem_t sem_data; 89 sem_t *sem;
90 sem_t sem_data;
86 }; 91 };
87 92
88 /* Create a semaphore, initialized with value */ 93 /* Create a semaphore, initialized with value */
89 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) 94 SDL_sem *
90 { 95 SDL_CreateSemaphore(Uint32 initial_value)
91 SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem)); 96 {
92 if ( sem ) { 97 SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
93 if ( sem_init(&sem->sem_data, 0, initial_value) < 0 ) { 98 if (sem) {
94 SDL_SetError("sem_init() failed"); 99 if (sem_init(&sem->sem_data, 0, initial_value) < 0) {
95 SDL_free(sem); 100 SDL_SetError("sem_init() failed");
96 sem = NULL; 101 SDL_free(sem);
97 } else { 102 sem = NULL;
98 sem->sem = &sem->sem_data; 103 } else {
99 } 104 sem->sem = &sem->sem_data;
100 } else { 105 }
101 SDL_OutOfMemory(); 106 } else {
102 } 107 SDL_OutOfMemory();
103 return sem; 108 }
104 } 109 return sem;
105 110 }
106 void SDL_DestroySemaphore(SDL_sem *sem) 111
107 { 112 void
108 if ( sem ) { 113 SDL_DestroySemaphore(SDL_sem * sem)
109 sem_destroy(sem->sem); 114 {
110 SDL_free(sem); 115 if (sem) {
111 } 116 sem_destroy(sem->sem);
112 } 117 SDL_free(sem);
113 118 }
114 int SDL_SemTryWait(SDL_sem *sem) 119 }
115 { 120
116 int retval; 121 int
117 122 SDL_SemTryWait(SDL_sem * sem)
118 if ( ! sem ) { 123 {
119 SDL_SetError("Passed a NULL semaphore"); 124 int retval;
120 return -1; 125
121 } 126 if (!sem) {
122 retval = SDL_MUTEX_TIMEDOUT; 127 SDL_SetError("Passed a NULL semaphore");
123 if ( sem_trywait(sem->sem) == 0 ) { 128 return -1;
124 retval = 0; 129 }
125 } 130 retval = SDL_MUTEX_TIMEDOUT;
126 return retval; 131 if (sem_trywait(sem->sem) == 0) {
127 } 132 retval = 0;
128 133 }
129 int SDL_SemWait(SDL_sem *sem) 134 return retval;
130 { 135 }
131 int retval; 136
132 137 int
133 if ( ! sem ) { 138 SDL_SemWait(SDL_sem * sem)
134 SDL_SetError("Passed a NULL semaphore"); 139 {
135 return -1; 140 int retval;
136 } 141
137 142 if (!sem) {
138 while ( ((retval = sem_wait(sem->sem)) == -1) && (errno == EINTR) ) {} 143 SDL_SetError("Passed a NULL semaphore");
139 if ( retval < 0 ) { 144 return -1;
140 SDL_SetError("sem_wait() failed"); 145 }
141 } 146
142 return retval; 147 retval = sem_wait(sem->sem);
143 } 148 if (retval < 0) {
144 149 SDL_SetError("sem_wait() failed");
145 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) 150 }
146 { 151 return retval;
147 int retval; 152 }
148 153
149 if ( ! sem ) { 154 int
150 SDL_SetError("Passed a NULL semaphore"); 155 SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
151 return -1; 156 {
152 } 157 int retval;
153 158
154 /* Try the easy cases first */ 159 if (!sem) {
155 if ( timeout == 0 ) { 160 SDL_SetError("Passed a NULL semaphore");
156 return SDL_SemTryWait(sem); 161 return -1;
157 } 162 }
158 if ( timeout == SDL_MUTEX_MAXWAIT ) { 163
159 return SDL_SemWait(sem); 164 /* Try the easy cases first */
160 } 165 if (timeout == 0) {
161 166 return SDL_SemTryWait(sem);
162 /* Ack! We have to busy wait... */ 167 }
163 timeout += SDL_GetTicks(); 168 if (timeout == SDL_MUTEX_MAXWAIT) {
164 do { 169 return SDL_SemWait(sem);
165 retval = SDL_SemTryWait(sem); 170 }
166 if ( retval == 0 ) { 171
167 break; 172 /* Ack! We have to busy wait... */
168 } 173 timeout += SDL_GetTicks();
169 SDL_Delay(1); 174 do {
170 } while ( SDL_GetTicks() < timeout ); 175 retval = SDL_SemTryWait(sem);
171 176 if (retval == 0) {
172 return retval; 177 break;
173 } 178 }
174 179 SDL_Delay(1);
175 Uint32 SDL_SemValue(SDL_sem *sem) 180 }
176 { 181 while (SDL_GetTicks() < timeout);
177 int ret = 0; 182
178 if ( sem ) { 183 return retval;
179 sem_getvalue(sem->sem, &ret); 184 }
180 if ( ret < 0 ) { 185
181 ret = 0; 186 Uint32
182 } 187 SDL_SemValue(SDL_sem * sem)
183 } 188 {
184 return (Uint32)ret; 189 int ret = 0;
185 } 190 if (sem) {
186 191 sem_getvalue(sem->sem, &ret);
187 int SDL_SemPost(SDL_sem *sem) 192 if (ret < 0) {
188 { 193 ret = 0;
189 int retval; 194 }
190 195 }
191 if ( ! sem ) { 196 return (Uint32) ret;
192 SDL_SetError("Passed a NULL semaphore"); 197 }
193 return -1; 198
194 } 199 int
195 200 SDL_SemPost(SDL_sem * sem)
196 retval = sem_post(sem->sem); 201 {
197 if ( retval < 0 ) { 202 int retval;
198 SDL_SetError("sem_post() failed"); 203
199 } 204 if (!sem) {
200 return retval; 205 SDL_SetError("Passed a NULL semaphore");
206 return -1;
207 }
208
209 retval = sem_post(sem->sem);
210 if (retval < 0) {
211 SDL_SetError("sem_post() failed");
212 }
213 return retval;
201 } 214 }
202 215
203 #endif /* !SDL_THREADS_DISABLED */ 216 #endif /* !SDL_THREADS_DISABLED */
217 /* vi: set ts=4 sw=4 expandtab: */