| 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> |
| 26 | #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_data |
| 50 | { |
| 51 | void (*func)(void*); |
| 52 | void *userdata; |
| 53 | }; |
| 54 | |
| 55 | struct sthread |
| 56 | { |
| 57 | #ifdef _WIN32 |
| 58 | HANDLE thread; |
| 59 | #else |
| 60 | pthread_t id; |
| 61 | #endif |
| 62 | }; |
| 63 | |
| 64 | struct slock |
| 65 | { |
| 66 | #ifdef _WIN32 |
| 67 | HANDLE lock; |
| 68 | #else |
| 69 | pthread_mutex_t lock; |
| 70 | #endif |
| 71 | }; |
| 72 | |
| 73 | struct scond |
| 74 | { |
| 75 | #ifdef _WIN32 |
| 76 | HANDLE event; |
| 77 | #else |
| 78 | pthread_cond_t cond; |
| 79 | #endif |
| 80 | }; |
| 81 | |
| 82 | #ifdef _WIN32 |
| 83 | static DWORD CALLBACK thread_wrap(void *data_) |
| 84 | #else |
| 85 | static void *thread_wrap(void *data_) |
| 86 | #endif |
| 87 | { |
| 88 | struct thread_data *data = (struct thread_data*)data_; |
| 89 | if (!data) |
| 90 | return 0; |
| 91 | data->func(data->userdata); |
| 92 | free(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_t *sthread_create(void (*thread_func)(void*), void *userdata) |
| 107 | { |
| 108 | bool thread_created = false; |
| 109 | struct thread_data *data = NULL; |
| 110 | sthread_t *thread = (sthread_t*)calloc(1, sizeof(*thread)); |
| 111 | |
| 112 | if (!thread) |
| 113 | return NULL; |
| 114 | |
| 115 | data = (struct thread_data*)calloc(1, sizeof(*data)); |
| 116 | if (!data) |
| 117 | goto error; |
| 118 | |
| 119 | data->func = thread_func; |
| 120 | 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_create(&thread->id, NULL, thread_wrap, data) == 0; |
| 127 | #endif |
| 128 | |
| 129 | if (!thread_created) |
| 130 | goto error; |
| 131 | |
| 132 | return thread; |
| 133 | |
| 134 | error: |
| 135 | if (data) |
| 136 | free(data); |
| 137 | free(thread); |
| 138 | return NULL; |
| 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_detach(sthread_t *thread) |
| 153 | { |
| 154 | #ifdef _WIN32 |
| 155 | CloseHandle(thread->thread); |
| 156 | free(thread); |
| 157 | return 0; |
| 158 | #else |
| 159 | return pthread_detach(thread->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_join(sthread_t *thread) |
| 175 | { |
| 176 | #ifdef _WIN32 |
| 177 | WaitForSingleObject(thread->thread, INFINITE); |
| 178 | CloseHandle(thread->thread); |
| 179 | #else |
| 180 | pthread_join(thread->id, NULL); |
| 181 | #endif |
| 182 | free(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_t *slock_new(void) |
| 194 | { |
| 195 | bool mutex_created = false; |
| 196 | slock_t *lock = (slock_t*)calloc(1, sizeof(*lock)); |
| 197 | if (!lock) |
| 198 | return NULL; |
| 199 | |
| 200 | #ifdef _WIN32 |
| 201 | lock->lock = CreateMutex(NULL, FALSE, NULL); |
| 202 | mutex_created = lock->lock; |
| 203 | #else |
| 204 | mutex_created = (pthread_mutex_init(&lock->lock, NULL) == 0); |
| 205 | #endif |
| 206 | |
| 207 | if (!mutex_created) |
| 208 | goto error; |
| 209 | |
| 210 | return lock; |
| 211 | |
| 212 | error: |
| 213 | free(lock); |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * slock_free: |
| 219 | * @lock : pointer to mutex object |
| 220 | * |
| 221 | * Frees a mutex. |
| 222 | **/ |
| 223 | void slock_free(slock_t *lock) |
| 224 | { |
| 225 | if (!lock) |
| 226 | return; |
| 227 | |
| 228 | #ifdef _WIN32 |
| 229 | CloseHandle(lock->lock); |
| 230 | #else |
| 231 | pthread_mutex_destroy(&lock->lock); |
| 232 | #endif |
| 233 | free(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_lock(slock_t *lock) |
| 245 | { |
| 246 | #ifdef _WIN32 |
| 247 | WaitForSingleObject(lock->lock, INFINITE); |
| 248 | #else |
| 249 | pthread_mutex_lock(&lock->lock); |
| 250 | #endif |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * slock_unlock: |
| 255 | * @lock : pointer to mutex object |
| 256 | * |
| 257 | * Unlocks a mutex. |
| 258 | **/ |
| 259 | void slock_unlock(slock_t *lock) |
| 260 | { |
| 261 | #ifdef _WIN32 |
| 262 | ReleaseMutex(lock->lock); |
| 263 | #else |
| 264 | pthread_mutex_unlock(&lock->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_t *scond_new(void) |
| 278 | { |
| 279 | bool event_created = false; |
| 280 | scond_t *cond = (scond_t*)calloc(1, sizeof(*cond)); |
| 281 | |
| 282 | if (!cond) |
| 283 | return NULL; |
| 284 | |
| 285 | #ifdef _WIN32 |
| 286 | cond->event = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 287 | event_created = cond->event; |
| 288 | #else |
| 289 | event_created = (pthread_cond_init(&cond->cond, NULL) == 0); |
| 290 | #endif |
| 291 | |
| 292 | if (!event_created) |
| 293 | goto error; |
| 294 | |
| 295 | return cond; |
| 296 | |
| 297 | error: |
| 298 | free(cond); |
| 299 | return NULL; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * scond_free: |
| 304 | * @cond : pointer to condition variable object |
| 305 | * |
| 306 | * Frees a condition variable. |
| 307 | **/ |
| 308 | void scond_free(scond_t *cond) |
| 309 | { |
| 310 | if (!cond) |
| 311 | return; |
| 312 | |
| 313 | #ifdef _WIN32 |
| 314 | CloseHandle(cond->event); |
| 315 | #else |
| 316 | pthread_cond_destroy(&cond->cond); |
| 317 | #endif |
| 318 | free(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_wait(scond_t *cond, 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_wait(&cond->cond, &lock->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_broadcast(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_broadcast(&cond->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_signal(scond_t *cond) |
| 367 | { |
| 368 | #ifdef _WIN32 |
| 369 | SetEvent(cond->event); |
| 370 | #else |
| 371 | pthread_cond_signal(&cond->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 | bool scond_wait_timeout(scond_t *cond, slock_t *lock, 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_t seconds, remainder; |
| 401 | struct 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_gettime(CLOCK_REALTIME, &now); |
| 429 | #endif |
| 430 | |
| 431 | seconds = timeout_us / INT64_C(1000000); |
| 432 | remainder = timeout_us % INT64_C(1000000); |
| 433 | |
| 434 | now.tv_sec += seconds; |
| 435 | now.tv_nsec += remainder * INT64_C(1000); |
| 436 | |
| 437 | ret = pthread_cond_timedwait(&cond->cond, &lock->lock, &now); |
| 438 | return (ret == 0); |
| 439 | #endif |
| 440 | } |