Debian Codegraph @

libretro-common/rthreads/rthreads.c

raw symbols SCIP
1 /* Copyright (C) 2010-2015 The RetroArch team
2 *
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (rthreads.c).
5 * ---------------------------------------------------------------------------------------
6 *
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 #include <stdlib.h>
24
25 #include <boolean.h>cxx . . $ `<file>/libretro-common/include/boolean.h`/
26 #include <rthreads/rthreads.h>cxx . . $ `<file>/libretro-common/include/rthreads/rthreads.h`/
27
28 #if defined(_WIN32)
29 #ifdef _XBOX
30 #include <xtl.h>
31 #else
32 #define WIN32_LEAN_AND_MEAN
33 #include <windows.h>
34 #endif
35 #elif defined(GEKKO)
36 #include "gx_pthread.h"
37 #elif defined(PSP)
38 #include "psp_pthread.h"
39 #else
40 #include <pthread.h>
41 #include <time.h>
42 #endif
43
44 #ifdef __MACH__
45 #include <mach/clock.h>
46 #include <mach/mach.h>
47 #endif
48
49 struct thread_datacxx . . $ thread_data#
50 {
51 void (*funccxx . . $ thread_data#func.)(void*);
52 void *userdatacxx . . $ thread_data#userdata.;
53 };
54
55 struct sthreadcxx . . $ sthread#
56 {
57 #ifdef _WIN32
58 HANDLE thread;
59 #else
60 pthread_tcxx . . $ pthread_t# idcxx . . $ sthread#id.;
61 #endif
62 };
63
64 struct slockcxx . . $ slock#
65 {
66 #ifdef _WIN32
67 HANDLE lock;
68 #else
69 pthread_mutex_tcxx . . $ pthread_mutex_t# lockcxx . . $ slock#lock.;
70 #endif
71 };
72
73 struct scondcxx . . $ scond#
74 {
75 #ifdef _WIN32
76 HANDLE event;
77 #else
78 pthread_cond_tcxx . . $ pthread_cond_t# condcxx . . $ scond#cond.;
79 #endif
80 };
81
82 #ifdef _WIN32
83 static DWORD CALLBACK thread_wrap(void *data_)
84 #else
85 static void *thread_wrapcxx . . $ thread_wrap(9f162e79bfc6c74).(void *data_)
86 #endif
87 {
88 struct thread_datacxx . . $ thread_data# *data = (struct thread_datacxx . . $ thread_data#*)data_;
89 if (!data)
90 return 0;
91 data->funccxx . . $ thread_data#func.(data->userdatacxx . . $ thread_data#userdata.);
92 freecxx . . $ free(5d22bdacc48458e8).(data);
93 return 0;
94 }
95
96 /**
97 * sthread_create:
98 * @start_routine : thread entry callback function
99 * @userdata : pointer to userdata that will be made
100 * available in thread entry callback function
101 *
102 * Create a new thread.
103 *
104 * Returns: pointer to new thread if successful, otherwise NULL.
105 */
106 sthread_tcxx . . $ sthread_t# *sthread_createcxx . . $ sthread_create(2788eb92f33e38c1).(void (*thread_func)(void*), void *userdata)
107 {
108 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! thread_created = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
109 struct thread_datacxx . . $ thread_data# *data = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
110 sthread_tcxx . . $ sthread_t# *thread = (sthread_tcxx . . $ sthread_t#*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*thread));
111
112 if (!thread)
113 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
114
115 data = (struct thread_datacxx . . $ thread_data#*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*data));
116 if (!data)
117 goto error;
118
119 data->funccxx . . $ thread_data#func. = thread_func;
120 data->userdatacxx . . $ thread_data#userdata. = userdata;
121
122 #ifdef _WIN32
123 thread->thread = CreateThread(NULL, 0, thread_wrap, data, 0, NULL);
124 thread_created = thread->thread;
125 #else
126 thread_created = pthread_createcxx . . $ pthread_create(2141a84bcac899e5).(&thread->idcxx . . $ sthread#id., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, thread_wrapcxx . . $ thread_wrap(9f162e79bfc6c74)., data) == 0;
127 #endif
128
129 if (!thread_created)
130 goto error;
131
132 return thread;
133
134 error:
135 if (data)
136 freecxx . . $ free(5d22bdacc48458e8).(data);
137 freecxx . . $ free(5d22bdacc48458e8).(thread);
138 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
139 }
140
141 /**
142 * sthread_detach:
143 * @thread : pointer to thread object
144 *
145 * Detach a thread. When a detached thread terminates, its
146 * resource sare automatically released back to the system
147 * without the need for another thread to join with the
148 * terminated thread.
149 *
150 * Returns: 0 on success, otherwise it returns a non-zero error number.
151 */
152 int sthread_detachcxx . . $ sthread_detach(10ea2221a8221d1b).(sthread_tcxx . . $ sthread_t# *thread)
153 {
154 #ifdef _WIN32
155 CloseHandle(thread->thread);
156 free(thread);
157 return 0;
158 #else
159 return pthread_detachcxx . . $ pthread_detach(15f694ef3c96d230).(thread->idcxx . . $ sthread#id.);
160 #endif
161 }
162
163 /**
164 * sthread_join:
165 * @thread : pointer to thread object
166 *
167 * Join with a terminated thread. Waits for the thread specified by
168 * @thread to terminate. If that thread has already terminated, then
169 * it will return immediately. The thread specified by @thread must
170 * be joinable.
171 *
172 * Returns: 0 on success, otherwise it returns a non-zero error number.
173 */
174 void sthread_joincxx . . $ sthread_join(468af6a3f60c1e97).(sthread_tcxx . . $ sthread_t# *thread)
175 {
176 #ifdef _WIN32
177 WaitForSingleObject(thread->thread, INFINITE);
178 CloseHandle(thread->thread);
179 #else
180 pthread_joincxx . . $ pthread_join(269902cf949f12c5).(thread->idcxx . . $ sthread#id., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
181 #endif
182 freecxx . . $ free(5d22bdacc48458e8).(thread);
183 }
184
185 /**
186 * slock_new:
187 *
188 * Create and initialize a new mutex. Must be manually
189 * freed.
190 *
191 * Returns: pointer to a new mutex if successful, otherwise NULL.
192 **/
193 slock_tcxx . . $ slock_t# *slock_newcxx . . $ slock_new(b4bc758a43c71b8d).(void)
194 {
195 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! mutex_created = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
196 slock_tcxx . . $ slock_t# *lock = (slock_tcxx . . $ slock_t#*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*lock));
197 if (!lock)
198 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
199
200 #ifdef _WIN32
201 lock->lock = CreateMutex(NULL, FALSE, NULL);
202 mutex_created = lock->lock;
203 #else
204 mutex_created = (pthread_mutex_initcxx . . $ pthread_mutex_init(db9d29dab44ea1aa).(&lock->lockcxx . . $ slock#lock., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) == 0);
205 #endif
206
207 if (!mutex_created)
208 goto error;
209
210 return lock;
211
212 error:
213 freecxx . . $ free(5d22bdacc48458e8).(lock);
214 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
215 }
216
217 /**
218 * slock_free:
219 * @lock : pointer to mutex object
220 *
221 * Frees a mutex.
222 **/
223 void slock_freecxx . . $ slock_free(d3f4c71e491680df).(slock_tcxx . . $ slock_t# *lock)
224 {
225 if (!lock)
226 return;
227
228 #ifdef _WIN32
229 CloseHandle(lock->lock);
230 #else
231 pthread_mutex_destroycxx . . $ pthread_mutex_destroy(d89ae235521888f9).(&lock->lockcxx . . $ slock#lock.);
232 #endif
233 freecxx . . $ free(5d22bdacc48458e8).(lock);
234 }
235
236 /**
237 * slock_lock:
238 * @lock : pointer to mutex object
239 *
240 * Locks a mutex. If a mutex is already locked by
241 * another thread, the calling thread shall block until
242 * the mutex becomes available.
243 **/
244 void slock_lockcxx . . $ slock_lock(d3f4c71e491680df).(slock_tcxx . . $ slock_t# *lock)
245 {
246 #ifdef _WIN32
247 WaitForSingleObject(lock->lock, INFINITE);
248 #else
249 pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&lock->lockcxx . . $ slock#lock.);
250 #endif
251 }
252
253 /**
254 * slock_unlock:
255 * @lock : pointer to mutex object
256 *
257 * Unlocks a mutex.
258 **/
259 void slock_unlockcxx . . $ slock_unlock(d3f4c71e491680df).(slock_tcxx . . $ slock_t# *lock)
260 {
261 #ifdef _WIN32
262 ReleaseMutex(lock->lock);
263 #else
264 pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&lock->lockcxx . . $ slock#lock.);
265 #endif
266 }
267
268 /**
269 * scond_new:
270 *
271 * Creates and initializes a condition variable. Must
272 * be manually freed.
273 *
274 * Returns: pointer to new condition variable on success,
275 * otherwise NULL.
276 **/
277 scond_tcxx . . $ scond_t# *scond_newcxx . . $ scond_new(d0199b3a2ac9b979).(void)
278 {
279 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! event_created = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
280 scond_tcxx . . $ scond_t# *cond = (scond_tcxx . . $ scond_t#*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*cond));
281
282 if (!cond)
283 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
284
285 #ifdef _WIN32
286 cond->event = CreateEvent(NULL, FALSE, FALSE, NULL);
287 event_created = cond->event;
288 #else
289 event_created = (pthread_cond_initcxx . . $ pthread_cond_init(fc876bc1ec1249fc).(&cond->condcxx . . $ scond#cond., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) == 0);
290 #endif
291
292 if (!event_created)
293 goto error;
294
295 return cond;
296
297 error:
298 freecxx . . $ free(5d22bdacc48458e8).(cond);
299 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
300 }
301
302 /**
303 * scond_free:
304 * @cond : pointer to condition variable object
305 *
306 * Frees a condition variable.
307 **/
308 void scond_freecxx . . $ scond_free(81e6553486d8235a).(scond_tcxx . . $ scond_t# *cond)
309 {
310 if (!cond)
311 return;
312
313 #ifdef _WIN32
314 CloseHandle(cond->event);
315 #else
316 pthread_cond_destroycxx . . $ pthread_cond_destroy(98095c34a2b0149d).(&cond->condcxx . . $ scond#cond.);
317 #endif
318 freecxx . . $ free(5d22bdacc48458e8).(cond);
319 }
320
321 /**
322 * scond_wait:
323 * @cond : pointer to condition variable object
324 * @lock : pointer to mutex object
325 *
326 * Block on a condition variable (i.e. wait on a condition).
327 **/
328 void scond_waitcxx . . $ scond_wait(57500220b3388075).(scond_tcxx . . $ scond_t# *cond, slock_tcxx . . $ slock_t# *lock)
329 {
330 #ifdef _WIN32
331 WaitForSingleObject(cond->event, 0);
332
333 SignalObjectAndWait(lock->lock, cond->event, INFINITE, FALSE);
334 slock_lock(lock);
335 #else
336 pthread_cond_waitcxx . . $ pthread_cond_wait(6145be15a5d9db5d).(&cond->condcxx . . $ scond#cond., &lock->lockcxx . . $ slock#lock.);
337 #endif
338 }
339
340 /**
341 * scond_broadcast:
342 * @cond : pointer to condition variable object
343 *
344 * Broadcast a condition. Unblocks all threads currently blocked
345 * on the specified condition variable @cond.
346 **/
347 int scond_broadcastcxx . . $ scond_broadcast(6fc383b23a525837).(scond_tcxx . . $ scond_t# *cond)
348 {
349 #ifdef _WIN32
350 /* FIXME _- check how this function should differ
351 * from scond_signal implementation. */
352 SetEvent(cond->event);
353 return 0;
354 #else
355 return pthread_cond_broadcastcxx . . $ pthread_cond_broadcast(98095c34a2b0149d).(&cond->condcxx . . $ scond#cond.);
356 #endif
357 }
358
359 /**
360 * scond_signal:
361 * @cond : pointer to condition variable object
362 *
363 * Signal a condition. Unblocks at least one of the threads currently blocked
364 * on the specified condition variable @cond.
365 **/
366 void scond_signalcxx . . $ scond_signal(81e6553486d8235a).(scond_tcxx . . $ scond_t# *cond)
367 {
368 #ifdef _WIN32
369 SetEvent(cond->event);
370 #else
371 pthread_cond_signalcxx . . $ pthread_cond_signal(98095c34a2b0149d).(&cond->condcxx . . $ scond#cond.);
372 #endif
373 }
374
375 /**
376 * scond_wait_timeout:
377 * @cond : pointer to condition variable object
378 * @lock : pointer to mutex object
379 * @timeout_us : timeout (in microseconds)
380 *
381 * Try to block on a condition variable (i.e. wait on a condition) until
382 * @timeout_us elapses.
383 *
384 * Returns: false (0) if timeout elapses before condition variable is
385 * signaled or broadcast, otherwise true (1).
386 **/
387 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! scond_wait_timeoutcxx . . $ scond_wait_timeout(d95c067d76ec07e8).(scond_tcxx . . $ scond_t# *cond, slock_tcxx . . $ slock_t# *lock, int64_tcxx . . $ int64_t# timeout_us)
388 {
389 #ifdef _WIN32
390 DWORD ret;
391
392 WaitForSingleObject(cond->event, 0);
393 ret = SignalObjectAndWait(lock->lock, cond->event,
394 (DWORD)(timeout_us) / 1000, FALSE);
395
396 slock_lock(lock);
397 return ret == WAIT_OBJECT_0;
398 #else
399 int ret;
400 int64_tcxx . . $ int64_t# seconds, remainder;
401 struct timespeccxx . . $ timespec# now = {0};
402
403 #ifdef __MACH__
404 /* OSX doesn't have clock_gettime. */
405 clock_serv_t cclock;
406 mach_timespec_t mts;
407
408 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
409 clock_get_time(cclock, &mts);
410 mach_port_deallocate(mach_task_self(), cclock);
411 now.tv_sec = mts.tv_sec;
412 now.tv_nsec = mts.tv_nsec;
413 #elif defined(__CELLOS_LV2__)
414 sys_time_sec_t s;
415 sys_time_nsec_t n;
416
417 sys_time_get_current_time(&s, &n);
418 now.tv_sec = s;
419 now.tv_nsec = n;
420 #elif defined(__mips__)
421 struct timeval tm;
422
423 gettimeofday(&tm, NULL);
424 now.tv_sec = tm.tv_sec;
425 now.tv_nsec = tm.tv_usec * 1000;
426 #elif !defined(GEKKO)
427 /* timeout on libogc is duration, not end time. */
428 clock_gettimecxx . . $ clock_gettime(65aaefb2502b4987).(CLOCK_REALTIMEcxx . . $ `<external>/89459239771508533/time.h:46:10`!, &now);
429 #endif
430
431 seconds = timeout_us / INT64_Ccxx . . $ `<external>/3415975807581488833/stdint.h:241:11`!(1000000);
432 remainder = timeout_us % INT64_Ccxx . . $ `<external>/3415975807581488833/stdint.h:241:11`!(1000000);
433
434 now.tv_seccxx . . $ timespec#tv_sec. += seconds;
435 now.tv_nseccxx . . $ timespec#tv_nsec. += remainder * INT64_Ccxx . . $ `<external>/3415975807581488833/stdint.h:241:11`!(1000);
436
437 ret = pthread_cond_timedwaitcxx . . $ pthread_cond_timedwait(d8fcb9edac03ad51).(&cond->condcxx . . $ scond#cond., &lock->lockcxx . . $ slock#lock., &now);
438 return (ret == 0);
439 #endif
440 }