src/osd/sdl/sdlsync.c
| r1082 | r1083 | |
| 31 | 31 | #include <os2.h> |
| 32 | 32 | #endif |
| 33 | 33 | |
| 34 | #ifdef SDLMAME_MACOSX |
| 35 | #include <mach/mach.h> |
| 36 | #endif |
| 37 | |
| 34 | 38 | // standard C headers |
| 35 | 39 | #include <math.h> |
| 36 | 40 | #include <unistd.h> |
| r1082 | r1083 | |
| 38 | 42 | // MAME headers |
| 39 | 43 | #include "osdcore.h" |
| 40 | 44 | #include "mame.h" |
| 45 | #include "sdlsync.h" |
| 41 | 46 | |
| 42 | 47 | #ifndef SDLMAME_WIN32 |
| 43 | 48 | #ifndef SDLMAME_OS2 |
| 44 | 49 | #include <pthread.h> |
| 45 | 50 | #include <errno.h> |
| 51 | #include <sys/time.h> |
| 46 | 52 | |
| 47 | | typedef struct _hidden_mutex_t hidden_mutex_t; |
| 48 | | struct _hidden_mutex_t { |
| 49 | | pthread_mutex_t id; |
| 53 | struct _osd_lock { |
| 54 | pthread_mutex_t id; |
| 55 | }; |
| 56 | |
| 57 | struct _osd_event { |
| 58 | pthread_mutex_t mutex; |
| 59 | pthread_cond_t cond; |
| 60 | int autoreset; |
| 61 | volatile int signalled; |
| 50 | 62 | }; |
| 51 | 63 | |
| 64 | struct _osd_thread { |
| 65 | pthread_t thread; |
| 66 | }; |
| 67 | |
| 68 | static osd_lock *atomic_lck = NULL; |
| 69 | |
| 52 | 70 | //============================================================ |
| 53 | 71 | // osd_lock_alloc |
| 54 | 72 | //============================================================ |
| 55 | 73 | |
| 56 | 74 | osd_lock *osd_lock_alloc(void) |
| 57 | 75 | { |
| 58 | | hidden_mutex_t *mutex; |
| 76 | osd_lock *mutex; |
| 59 | 77 | pthread_mutexattr_t mtxattr; |
| 60 | 78 | |
| 61 | | mutex = (hidden_mutex_t *)calloc(1, sizeof(hidden_mutex_t)); |
| 79 | mutex = (osd_lock *)calloc(1, sizeof(osd_lock)); |
| 62 | 80 | |
| 63 | 81 | pthread_mutexattr_init(&mtxattr); |
| 64 | 82 | pthread_mutexattr_settype(&mtxattr, PTHREAD_MUTEX_RECURSIVE); |
| 65 | 83 | pthread_mutex_init(&mutex->id, &mtxattr); |
| 66 | 84 | |
| 67 | | return (osd_lock *)mutex; |
| 85 | return mutex; |
| 68 | 86 | } |
| 69 | 87 | |
| 70 | 88 | //============================================================ |
| r1082 | r1083 | |
| 73 | 91 | |
| 74 | 92 | void osd_lock_acquire(osd_lock *lock) |
| 75 | 93 | { |
| 76 | | hidden_mutex_t *mutex = (hidden_mutex_t *) lock; |
| 77 | 94 | int r; |
| 78 | | |
| 79 | | r = pthread_mutex_lock(&mutex->id); |
| 95 | |
| 96 | r = pthread_mutex_lock(&lock->id); |
| 80 | 97 | if (r==0) |
| 81 | 98 | return; |
| 82 | | mame_printf_error("Error on lock: %d: %s\n", r, strerror(r)); |
| 99 | printf("Error on lock: %d: %s\n", r, strerror(r)); |
| 83 | 100 | } |
| 84 | 101 | |
| 85 | 102 | //============================================================ |
| r1082 | r1083 | |
| 88 | 105 | |
| 89 | 106 | int osd_lock_try(osd_lock *lock) |
| 90 | 107 | { |
| 91 | | hidden_mutex_t *mutex = (hidden_mutex_t *) lock; |
| 92 | 108 | int r; |
| 93 | 109 | |
| 94 | | r = pthread_mutex_trylock(&mutex->id); |
| 110 | r = pthread_mutex_trylock(&lock->id); |
| 95 | 111 | if (r==0) |
| 96 | 112 | return 1; |
| 97 | 113 | if (r!=EBUSY) |
| 98 | | mame_printf_error("Error on trylock: %d: %s\n", r, strerror(r)); |
| 114 | printf("Error on trylock: %d: %s\n", r, strerror(r)); |
| 99 | 115 | return 0; |
| 100 | 116 | } |
| 101 | 117 | |
| r1082 | r1083 | |
| 105 | 121 | |
| 106 | 122 | void osd_lock_release(osd_lock *lock) |
| 107 | 123 | { |
| 108 | | hidden_mutex_t *mutex = (hidden_mutex_t *) lock; |
| 109 | | |
| 110 | | pthread_mutex_unlock(&mutex->id); |
| 124 | pthread_mutex_unlock(&lock->id); |
| 111 | 125 | } |
| 112 | 126 | |
| 113 | 127 | //============================================================ |
| r1082 | r1083 | |
| 116 | 130 | |
| 117 | 131 | void osd_lock_free(osd_lock *lock) |
| 118 | 132 | { |
| 119 | | hidden_mutex_t *mutex = (hidden_mutex_t *) lock; |
| 133 | pthread_mutex_unlock(&lock->id); |
| 134 | pthread_mutex_destroy(&lock->id); |
| 135 | free(lock); |
| 136 | } |
| 120 | 137 | |
| 121 | | pthread_mutex_unlock(&mutex->id); |
| 122 | | pthread_mutex_destroy(&mutex->id); |
| 123 | | free(mutex); |
| 138 | |
| 139 | //============================================================ |
| 140 | // osd_num_processors |
| 141 | //============================================================ |
| 142 | |
| 143 | int osd_num_processors(void) |
| 144 | { |
| 145 | int processors = 1; |
| 146 | |
| 147 | #ifdef SDLMAME_MACOSX |
| 148 | { |
| 149 | struct host_basic_info host_basic_info; |
| 150 | unsigned int count; |
| 151 | kern_return_t r; |
| 152 | mach_port_t my_mach_host_self; |
| 153 | |
| 154 | count = HOST_BASIC_INFO_COUNT; |
| 155 | my_mach_host_self = mach_host_self(); |
| 156 | if ( ( r = host_info(my_mach_host_self, HOST_BASIC_INFO, (host_info_t)(&host_basic_info), &count)) == KERN_SUCCESS ) |
| 157 | { |
| 158 | processors = host_basic_info.avail_cpus; |
| 159 | } |
| 160 | mach_port_deallocate(mach_task_self(), my_mach_host_self); |
| 161 | } |
| 162 | #elif defined(_SC_NPROCESSORS_ONLN) |
| 163 | processors = sysconf(_SC_NPROCESSORS_ONLN); |
| 164 | #endif |
| 165 | return processors; |
| 124 | 166 | } |
| 167 | |
| 168 | //============================================================ |
| 169 | // osd_event_alloc |
| 170 | //============================================================ |
| 171 | |
| 172 | osd_event *osd_event_alloc(int manualreset, int initialstate) |
| 173 | { |
| 174 | osd_event *ev; |
| 175 | pthread_mutexattr_t mtxattr; |
| 176 | |
| 177 | ev = (osd_event *)calloc(1, sizeof(osd_event)); |
| 178 | |
| 179 | pthread_mutexattr_init(&mtxattr); |
| 180 | pthread_mutex_init(&ev->mutex, &mtxattr); |
| 181 | pthread_cond_init(&ev->cond, NULL); |
| 182 | ev->signalled = initialstate; |
| 183 | ev->autoreset = !manualreset; |
| 184 | |
| 185 | return ev; |
| 186 | } |
| 187 | |
| 188 | //============================================================ |
| 189 | // osd_event_free |
| 190 | //============================================================ |
| 191 | |
| 192 | void osd_event_free(osd_event *event) |
| 193 | { |
| 194 | pthread_mutex_destroy(&event->mutex); |
| 195 | pthread_cond_destroy(&event->cond); |
| 196 | free(event); |
| 197 | } |
| 198 | |
| 199 | //============================================================ |
| 200 | // osd_event_set |
| 201 | //============================================================ |
| 202 | |
| 203 | void osd_event_set(osd_event *event) |
| 204 | { |
| 205 | pthread_mutex_lock(&event->mutex); |
| 206 | event->signalled = TRUE; |
| 207 | pthread_cond_broadcast(&event->cond); |
| 208 | pthread_mutex_unlock(&event->mutex); |
| 209 | } |
| 210 | |
| 211 | //============================================================ |
| 212 | // osd_event_reset |
| 213 | //============================================================ |
| 214 | |
| 215 | void osd_event_reset(osd_event *event) |
| 216 | { |
| 217 | pthread_mutex_lock(&event->mutex); |
| 218 | event->signalled = FALSE; |
| 219 | pthread_mutex_unlock(&event->mutex); |
| 220 | } |
| 221 | |
| 222 | //============================================================ |
| 223 | // osd_event_wait |
| 224 | //============================================================ |
| 225 | |
| 226 | int osd_event_wait(osd_event *event, osd_ticks_t timeout) |
| 227 | { |
| 228 | pthread_mutex_lock(&event->mutex); |
| 229 | if (!timeout) |
| 230 | { |
| 231 | if (!event->signalled) |
| 232 | { |
| 233 | pthread_cond_wait(&event->cond, &event->mutex); |
| 234 | } |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | struct timespec ts; |
| 239 | struct timeval tp; |
| 240 | osd_ticks_t t; |
| 241 | osd_ticks_t msec = timeout * 1000 / osd_ticks_per_second(); |
| 242 | |
| 243 | gettimeofday(&tp, NULL); |
| 244 | |
| 245 | ts.tv_sec = tp.tv_sec; |
| 246 | ts.tv_nsec = tp.tv_usec * 1000 + (msec * 1000000); |
| 247 | t = ts.tv_nsec / 1000000000; |
| 248 | ts.tv_nsec = t % 1000000000; |
| 249 | ts.tv_sec += t; |
| 250 | |
| 251 | if (!event->signalled) |
| 252 | if ( pthread_cond_timedwait(&event->cond, &event->mutex, &ts) != 0 ) |
| 253 | { |
| 254 | pthread_mutex_unlock(&event->mutex); |
| 255 | return FALSE; |
| 256 | } |
| 257 | } |
| 258 | if (event->autoreset) |
| 259 | event->signalled = 0; |
| 260 | pthread_mutex_unlock(&event->mutex); |
| 261 | return TRUE; |
| 262 | } |
| 263 | |
| 264 | //============================================================ |
| 265 | // osd_event_wait_multiple |
| 266 | //============================================================ |
| 267 | |
| 268 | int osd_event_wait_multiple(int num, osd_event *events[], osd_ticks_t timeout) |
| 269 | { |
| 270 | int i; |
| 271 | int ret; |
| 272 | |
| 273 | //FIXME: ugly and inefficient and ignores timeout |
| 274 | do |
| 275 | { |
| 276 | for (i=0;i<num;i++) |
| 277 | { |
| 278 | ret = osd_event_wait(events[i], 10); |
| 279 | if (ret == TRUE) |
| 280 | return (i); |
| 281 | } |
| 282 | osd_sleep(20); |
| 283 | } |
| 284 | while (TRUE); |
| 285 | return -1; |
| 286 | } |
| 287 | |
| 288 | //============================================================ |
| 289 | // osd_atomic_lock |
| 290 | //============================================================ |
| 291 | |
| 292 | void osd_atomic_lock(void) |
| 293 | { |
| 294 | if (!atomic_lck) |
| 295 | atomic_lck = osd_lock_alloc(); |
| 296 | osd_lock_acquire(atomic_lck); |
| 297 | } |
| 298 | |
| 299 | //============================================================ |
| 300 | // osd_atomic_unlock |
| 301 | //============================================================ |
| 302 | |
| 303 | void osd_atomic_unlock(void) |
| 304 | { |
| 305 | osd_lock_release(atomic_lck); |
| 306 | } |
| 307 | |
| 308 | //============================================================ |
| 309 | // osd_thread_create |
| 310 | //============================================================ |
| 311 | |
| 312 | osd_thread *osd_thread_create(osd_thread_callback callback, void *cbparam) |
| 313 | { |
| 314 | osd_thread *thread; |
| 315 | |
| 316 | thread = (osd_thread *)calloc(1, sizeof(osd_thread)); |
| 317 | if ( pthread_create(&thread->thread, NULL, callback, cbparam) != 0 ) |
| 318 | { |
| 319 | free(thread); |
| 320 | return NULL; |
| 321 | } |
| 322 | return thread; |
| 323 | } |
| 324 | |
| 325 | //============================================================ |
| 326 | // osd_thread_adjust_priority |
| 327 | //============================================================ |
| 328 | |
| 329 | int osd_thread_adjust_priority(osd_thread *thread, int adjust) |
| 330 | { |
| 331 | struct sched_param sched; |
| 332 | int policy; |
| 333 | |
| 334 | if ( pthread_getschedparam( thread->thread, &policy, &sched ) == 0 ) |
| 335 | { |
| 336 | sched.sched_priority += adjust; |
| 337 | if ( pthread_setschedparam(thread->thread, policy, &sched ) == 0) |
| 338 | return TRUE; |
| 339 | else |
| 340 | return FALSE; |
| 341 | } |
| 342 | else |
| 343 | return FALSE; |
| 344 | } |
| 345 | |
| 346 | //============================================================ |
| 347 | // osd_thread_wait_free |
| 348 | //============================================================ |
| 349 | |
| 350 | void osd_thread_wait_free(osd_thread *thread) |
| 351 | { |
| 352 | pthread_join(thread->thread, NULL); |
| 353 | free(thread); |
| 354 | } |
| 355 | |
| 356 | //============================================================ |
| 357 | // osd_compare_exchange32 |
| 358 | //============================================================ |
| 359 | |
| 360 | #if defined(__i386__) || defined(__x86_64__) |
| 361 | |
| 362 | INT32 osd_compare_exchange32(INT32 volatile *ptr, INT32 compare, INT32 exchange) |
| 363 | { |
| 364 | register INT32 ret; |
| 365 | __asm__ __volatile__ ( |
| 366 | " lock ; cmpxchg %[exchange], %[ptr] ;" |
| 367 | : [ptr] "+m" (*ptr) |
| 368 | , [ret] "=a" (ret) |
| 369 | : [compare] "1" (compare) |
| 370 | , [exchange] "q" (exchange) |
| 371 | : "%cc" |
| 372 | ); |
| 373 | return ret; |
| 374 | } |
| 375 | |
| 376 | #elif defined(__ppc__) || defined(__PPC__) || defined(__ppc64__) || defined(__PPC64__) |
| 377 | |
| 378 | INT32 osd_compare_exchange32(INT32 volatile *ptr, INT32 compare, INT32 exchange) |
| 379 | { |
| 380 | register INT32 ret; |
| 381 | __asm__ __volatile__ ( |
| 382 | "1: lwarx %[ret], 0, %[ptr] \n" |
| 383 | " cmpw %[compare], %[ret] \n" |
| 384 | " bne 2f \n" |
| 385 | " sync \n" |
| 386 | " stwcx. %[exchange], 0, %[ptr] \n" |
| 387 | " bne- 1b \n" |
| 388 | "2: " |
| 389 | : [ret] "=&r" (ret) |
| 390 | : [ptr] "r" (ptr) |
| 391 | , [exchange] "r" (exchange) |
| 392 | , [compare] "r" (compare) |
| 393 | : "cr0" |
| 394 | ); |
| 395 | return ret; |
| 396 | } |
| 397 | #else |
| 398 | |
| 399 | INT32 osd_compare_exchange32(INT32 volatile *ptr, INT32 compare, INT32 exchange) |
| 400 | { |
| 401 | INT32 ret; |
| 402 | osd_atomic_lock(); |
| 403 | |
| 404 | ret = *ptr; |
| 405 | |
| 406 | if ( *ptr == compare ) |
| 407 | { |
| 408 | *ptr = exchange; |
| 409 | } |
| 410 | |
| 411 | osd_atomic_unlock(); |
| 412 | return ret; |
| 413 | } |
| 414 | |
| 415 | #endif |
| 416 | |
| 417 | //============================================================ |
| 418 | // osd_compare_exchange64 |
| 419 | //============================================================ |
| 420 | |
| 421 | #if defined(__x86_64__) |
| 422 | |
| 423 | INT64 osd_compare_exchange64(INT64 volatile *ptr, INT64 compare, INT64 exchange) |
| 424 | { |
| 425 | register INT64 ret; |
| 426 | __asm__ __volatile__ ( |
| 427 | " lock ; cmpxchg %[exchange], %[ptr] ;" |
| 428 | : [ptr] "+m" (*ptr) |
| 429 | , [ret] "=a" (ret) |
| 430 | : [compare] "1" (compare) |
| 431 | , [exchange] "q" (exchange) |
| 432 | : "%cc" |
| 433 | ); |
| 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | #elif defined(__ppc64__) || defined(__PPC64__) |
| 438 | |
| 439 | INT64 osd_compare_exchange64(INT64 volatile *ptr, INT64 compare, INT64 exchange) |
| 440 | { |
| 441 | register INT64 ret; |
| 442 | __asm__ __volatile__ ( |
| 443 | "1: ldarx %[ret], 0, %[ptr] \n" |
| 444 | " cmpd %[compare], %[ret] \n" |
| 445 | " bne 2f \n" |
| 446 | " stdcx. %[exchange], 0, %[ptr] \n" |
| 447 | " bne-- 1b \n" |
| 448 | "2: " |
| 449 | : [ret] "=&r" (ret) |
| 450 | : [ptr] "r" (ptr) |
| 451 | , [exchange] "r" (exchange) |
| 452 | , [compare] "r" (compare) |
| 453 | : "cr0" |
| 454 | ); |
| 455 | return ret; |
| 456 | } |
| 457 | #else |
| 458 | |
| 459 | #ifdef PTR64 |
| 460 | INT64 osd_compare_exchange64(INT64 volatile *ptr, INT64 compare, INT64 exchange) |
| 461 | { |
| 462 | INT64 ret; |
| 463 | osd_atomic_lock(); |
| 464 | |
| 465 | ret = *ptr; |
| 466 | |
| 467 | if ( *ptr == compare ) |
| 468 | { |
| 469 | *ptr = exchange; |
| 470 | } |
| 471 | |
| 472 | osd_atomic_unlock(); |
| 473 | return ret; |
| 474 | } |
| 475 | #endif |
| 476 | |
| 477 | #endif |
| 478 | |
| 125 | 479 | #else // SDLMAME_OS2 |
| 126 | 480 | |
| 127 | 481 | struct _osd_lock |
src/osd/sdl/sdlsync.h
| r0 | r1083 | |
| 1 | //============================================================ |
| 2 | // |
| 3 | // sdlsync.h - SDL core synchronization functions |
| 4 | // |
| 5 | // Copyright (c) 1996-2007, Nicola Salmoria and the MAME Team. |
| 6 | // Visit http://mamedev.org for licensing and usage restrictions. |
| 7 | // |
| 8 | // SDLMAME by Olivier Galibert and R. Belmont |
| 9 | // |
| 10 | //============================================================ |
| 11 | |
| 12 | #ifndef __SDL_SYNC__ |
| 13 | #define __SDL_SYNC__ |
| 14 | |
| 15 | #ifndef SDLMAME_WIN32 |
| 16 | |
| 17 | /*************************************************************************** |
| 18 | SYNCHRONIZATION INTERFACES - Events |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | /* osd_event is an opaque type which represents a setable/resetable event */ |
| 22 | |
| 23 | typedef struct _osd_event osd_event; |
| 24 | |
| 25 | /*----------------------------------------------------------------------------- |
| 26 | osd_lock_event_alloc: allocate a new event |
| 27 | |
| 28 | Parameters: |
| 29 | |
| 30 | manualreset - boolean. If true, the event will be automatically set |
| 31 | to non-signalled after a thread successfully waited for |
| 32 | it. |
| 33 | initialstate - boolean. If true, the event is signalled initially. |
| 34 | |
| 35 | Return value: |
| 36 | |
| 37 | A pointer to the allocated event. |
| 38 | -----------------------------------------------------------------------------*/ |
| 39 | osd_event *osd_event_alloc(int manualreset, int initialstate); |
| 40 | |
| 41 | /*----------------------------------------------------------------------------- |
| 42 | osd_event_wait: wait for an event to be signalled |
| 43 | |
| 44 | Parameters: |
| 45 | |
| 46 | event - The event to wait for. If the event is in signalled state, the |
| 47 | function returns immediately. If not it will wait for the event |
| 48 | to become signalled. |
| 49 | timeout - timeout in osd_ticks |
| 50 | |
| 51 | Return value: |
| 52 | |
| 53 | TRUE: The event was signalled |
| 54 | FALSE: A timeout occurred |
| 55 | -----------------------------------------------------------------------------*/ |
| 56 | int osd_event_wait(osd_event *event, osd_ticks_t timeout); |
| 57 | |
| 58 | //FIXME: description |
| 59 | /*----------------------------------------------------------------------------- |
| 60 | osd_event_wait_multiple: wait for multiple events |
| 61 | |
| 62 | Parameters: |
| 63 | |
| 64 | event - The event to set to non-signalled state |
| 65 | |
| 66 | Return value: |
| 67 | |
| 68 | None |
| 69 | -----------------------------------------------------------------------------*/ |
| 70 | |
| 71 | int osd_event_wait_multiple(int num, osd_event **events, osd_ticks_t timeout); |
| 72 | |
| 73 | /*----------------------------------------------------------------------------- |
| 74 | osd_event_reset: reset an event to non-signalled state |
| 75 | |
| 76 | Parameters: |
| 77 | |
| 78 | event - The event to set to non-signalled state |
| 79 | |
| 80 | Return value: |
| 81 | |
| 82 | None |
| 83 | -----------------------------------------------------------------------------*/ |
| 84 | void osd_event_reset(osd_event *event); |
| 85 | |
| 86 | /*----------------------------------------------------------------------------- |
| 87 | osd_event_reset: reset an event to non-signalled state |
| 88 | |
| 89 | Parameters: |
| 90 | |
| 91 | event - The event to set to non-signalled state |
| 92 | |
| 93 | Return value: |
| 94 | |
| 95 | None |
| 96 | -----------------------------------------------------------------------------*/ |
| 97 | |
| 98 | int osd_event_wait_multiple(int num, osd_event *events[], osd_ticks_t timeout); |
| 99 | |
| 100 | /*----------------------------------------------------------------------------- |
| 101 | osd_event_set: set an event to signalled state |
| 102 | |
| 103 | Parameters: |
| 104 | |
| 105 | event - The event to set to signalled state |
| 106 | |
| 107 | Return value: |
| 108 | |
| 109 | None |
| 110 | |
| 111 | Notes: |
| 112 | |
| 113 | All threads waiting for the event will be signalled. |
| 114 | -----------------------------------------------------------------------------*/ |
| 115 | void osd_event_set(osd_event *event); |
| 116 | |
| 117 | |
| 118 | /*----------------------------------------------------------------------------- |
| 119 | osd_event_free: free the memory and resources associated with an osd_event |
| 120 | |
| 121 | Parameters: |
| 122 | |
| 123 | event - a pointer to a previously allocated osd_event. |
| 124 | |
| 125 | Return value: |
| 126 | |
| 127 | None. |
| 128 | -----------------------------------------------------------------------------*/ |
| 129 | void osd_event_free(osd_event *event); |
| 130 | |
| 131 | |
| 132 | /*************************************************************************** |
| 133 | SYNCHRONIZATION INTERFACES - Threads |
| 134 | ***************************************************************************/ |
| 135 | |
| 136 | /* osd_thread is an opaque type which represents a thread */ |
| 137 | typedef struct _osd_thread osd_thread; |
| 138 | |
| 139 | /* osd_thread_callback is a callback function that will be called from the thread */ |
| 140 | typedef void *(*osd_thread_callback)(void *param); |
| 141 | |
| 142 | |
| 143 | /*----------------------------------------------------------------------------- |
| 144 | osd_thread_create: create a new thread |
| 145 | |
| 146 | Parameters: |
| 147 | |
| 148 | callback - The callback function to be called once the thread is up |
| 149 | cbparam - The parameter to pass to the callback function. |
| 150 | |
| 151 | Return value: |
| 152 | |
| 153 | A pointer to the created thread. |
| 154 | -----------------------------------------------------------------------------*/ |
| 155 | osd_thread *osd_thread_create(osd_thread_callback callback, void *cbparam); |
| 156 | |
| 157 | |
| 158 | /*----------------------------------------------------------------------------- |
| 159 | osd_thread_adjust_priority: adjust priority of a thread |
| 160 | |
| 161 | Parameters: |
| 162 | |
| 163 | thread - A pointer to a previously created thread. |
| 164 | adjust - signed integer to add to the thread priority |
| 165 | |
| 166 | Return value: |
| 167 | |
| 168 | TRUE on success, FALSE on failure |
| 169 | -----------------------------------------------------------------------------*/ |
| 170 | int osd_thread_adjust_priority(osd_thread *thread, int adjust); |
| 171 | |
| 172 | |
| 173 | /*----------------------------------------------------------------------------- |
| 174 | osd_thread_wait_free: wait for thread to finish and free resources |
| 175 | |
| 176 | Parameters: |
| 177 | |
| 178 | thread - A pointer to a previously created thread. |
| 179 | |
| 180 | Return value: |
| 181 | |
| 182 | None. |
| 183 | -----------------------------------------------------------------------------*/ |
| 184 | void osd_thread_wait_free(osd_thread *thread); |
| 185 | |
| 186 | |
| 187 | /*----------------------------------------------------------------------------- |
| 188 | osd_num_processors: return the number of processors |
| 189 | |
| 190 | Parameters: |
| 191 | |
| 192 | None. |
| 193 | |
| 194 | Return value: |
| 195 | |
| 196 | Number of processors |
| 197 | -----------------------------------------------------------------------------*/ |
| 198 | int osd_num_processors(void); |
| 199 | |
| 200 | |
| 201 | /*----------------------------------------------------------------------------- |
| 202 | osd_atomic_lock: block other processes |
| 203 | |
| 204 | Parameters: |
| 205 | |
| 206 | None. |
| 207 | |
| 208 | Return value: |
| 209 | |
| 210 | None. |
| 211 | |
| 212 | Notes: |
| 213 | This will be used on certain platforms to emulate atomic operations |
| 214 | Please see osinclude.h |
| 215 | -----------------------------------------------------------------------------*/ |
| 216 | void osd_atomic_lock(void); |
| 217 | |
| 218 | |
| 219 | /*----------------------------------------------------------------------------- |
| 220 | osd_atomic_unlock: unblock other processes |
| 221 | |
| 222 | Parameters: |
| 223 | |
| 224 | None. |
| 225 | |
| 226 | Return value: |
| 227 | |
| 228 | None. |
| 229 | |
| 230 | Notes: |
| 231 | This will be used on certain platforms to emulate atomic operations |
| 232 | Please see osinclude.h |
| 233 | -----------------------------------------------------------------------------*/ |
| 234 | void osd_atomic_unlock(void); |
| 235 | |
| 236 | |
| 237 | //============================================================ |
| 238 | // INLINE FUNCTIONS |
| 239 | //============================================================ |
| 240 | |
| 241 | #ifndef SDLMAME_WIN32 |
| 242 | #ifdef X86_ASM |
| 243 | |
| 244 | INLINE void * osd_atomic_cmpxchg(void * volatile *ptr, void *exchange, void *compare) |
| 245 | { |
| 246 | long prev; |
| 247 | __asm__ __volatile__("lock; cmpxchgl %1,%2" |
| 248 | : "=a"(prev) |
| 249 | : "r"((long) exchange), "m"(*ptr), "0"((long) compare) |
| 250 | : "memory"); |
| 251 | return (void *) prev; |
| 252 | } |
| 253 | |
| 254 | INLINE long osd_atomic_increment(long volatile *addend) |
| 255 | { |
| 256 | __asm__ __volatile__( |
| 257 | "lock;" "incl %0" |
| 258 | :"=m" (*addend) |
| 259 | :"m" (*addend)); |
| 260 | return *addend; |
| 261 | } |
| 262 | |
| 263 | |
| 264 | INLINE long osd_atomic_decrement(long volatile *addend) |
| 265 | { |
| 266 | __asm__ __volatile__( |
| 267 | "lock;" "decl %0" |
| 268 | :"=m" (*addend) |
| 269 | :"m" (*addend)); |
| 270 | return *addend; |
| 271 | } |
| 272 | #else |
| 273 | |
| 274 | INLINE void * osd_atomic_cmpxchg(void * volatile *ptr, void *exchange, void *compare) |
| 275 | { |
| 276 | void *ret = NULL; |
| 277 | |
| 278 | osd_atomic_lock(); |
| 279 | |
| 280 | #ifdef PTR64 |
| 281 | ret = (void*)(*((UINT64*)ptr)); |
| 282 | |
| 283 | if ( *((UINT64*)ptr) == (UINT64)compare ) |
| 284 | { |
| 285 | *((UINT64*)ptr) = (UINT64)exchange; |
| 286 | } |
| 287 | #else |
| 288 | ret = (void*)(*((UINT32*)ptr)); |
| 289 | |
| 290 | if ( *((UINT32*)ptr) == (UINT32)compare ) |
| 291 | { |
| 292 | *((UINT32*)ptr) = (UINT32)exchange; |
| 293 | } |
| 294 | #endif |
| 295 | |
| 296 | osd_atomic_unlock(); |
| 297 | |
| 298 | return ret; |
| 299 | } |
| 300 | |
| 301 | INLINE long osd_atomic_increment(long volatile *addend) |
| 302 | { |
| 303 | long ret; |
| 304 | osd_atomic_lock(); |
| 305 | ret = (++(*addend)); |
| 306 | osd_atomic_unlock(); |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | |
| 311 | INLINE long osd_atomic_decrement(long volatile *addend) |
| 312 | { |
| 313 | long ret; |
| 314 | osd_atomic_lock(); |
| 315 | ret = (--(*addend)); |
| 316 | osd_atomic_unlock(); |
| 317 | return ret; |
| 318 | } |
| 319 | #endif |
| 320 | |
| 321 | #else //SDLMAME_WIN32 |
| 322 | |
| 323 | INLINE PVOID osd_atomic_cmpxchg(PVOID volatile *ptr, PVOID exchange, PVOID compare) |
| 324 | { |
| 325 | // the mingw headers don't put the volatile keyword on the first parameter |
| 326 | // gcc also can't handle casting the result of a function |
| 327 | #ifdef __GNUC__ |
| 328 | #ifdef PTR64 |
| 329 | UINT64 result = InterlockedCompareExchange64((UINT64)ptr, (UINT64)exchange, (UINT64)compare); |
| 330 | return (PVOID)result; |
| 331 | #else |
| 332 | LONG result = InterlockedCompareExchange((LPLONG)ptr, (LONG)exchange, (LONG)compare); |
| 333 | return (PVOID)result; |
| 334 | #endif |
| 335 | #else |
| 336 | return InterlockedCompareExchangePointer(ptr, exchange, compare); |
| 337 | #endif |
| 338 | } |
| 339 | |
| 340 | |
| 341 | INLINE LONG osd_atomic_increment(LONG volatile *addend) |
| 342 | { |
| 343 | // the mingw headers don't put the volatile keyword on the first parameter |
| 344 | #ifdef __GNUC__ |
| 345 | return InterlockedIncrement((LPLONG)addend); |
| 346 | #else |
| 347 | return InterlockedIncrement(addend); |
| 348 | #endif |
| 349 | } |
| 350 | |
| 351 | |
| 352 | INLINE LONG osd_atomic_decrement(LONG volatile *addend) |
| 353 | { |
| 354 | // the mingw headers don't put the volatile keyword on the first parameter |
| 355 | #ifdef __GNUC__ |
| 356 | return InterlockedDecrement((LPLONG)addend); |
| 357 | #else |
| 358 | return InterlockedDecrement(addend); |
| 359 | #endif |
| 360 | } |
| 361 | |
| 362 | #endif |
| 363 | |
| 364 | #endif /* SDLMAME_WIN32 */ |
| 365 | #endif /* __SDL_SYNC__ */ |
src/osd/sdl/sdlwork.c
| r1082 | r1083 | |
| 1 | | //============================================================ |
| 2 | 1 | // |
| 3 | 2 | // sdlwork.c - SDL OSD core work item functions |
| 4 | 3 | // |
| r1082 | r1083 | |
| 29 | 28 | #include <mach/mach.h> |
| 30 | 29 | #include "osxutils.h" |
| 31 | 30 | #endif |
| 31 | #include "sdlsync.h" |
| 32 | 32 | |
| 33 | 33 | |
| 34 | 34 | //============================================================ |
| 35 | | // TYPE DEFINITIONS |
| 35 | // DEBUGGING |
| 36 | 36 | //============================================================ |
| 37 | 37 | |
| 38 | | #define QUEUE_STATE_DOWORK 0 |
| 39 | | #define QUEUE_STATE_WORK_COMPLETE 1 |
| 40 | | #define QUEUE_STATE_EXIT 2 |
| 38 | #define KEEP_STATISTICS (0) |
| 41 | 39 | |
| 42 | | struct _osd_work_queue |
| 43 | | { |
| 44 | | pthread_mutex_t critsect; // mutex protecting the queue (everything except for the state) |
| 45 | | osd_work_item * volatile list; // list of items in the queue |
| 46 | | osd_work_item ** volatile tailptr; // pointer to the tail pointer of work items in the queue |
| 47 | | osd_work_item * volatile free; // free list of work items |
| 48 | | volatile long items; // items in the queue |
| 49 | | volatile long livethreads; // number of live threads |
| 50 | | UINT32 threads; // number of threads in this queue |
| 51 | | pthread_t * thread; // array of thread handles |
| 52 | | volatile int state; // QUEUE_STATE_* above |
| 53 | | pthread_mutex_t statelock; // lock for state data |
| 54 | | pthread_cond_t statecond; // condition variable to wait for state change |
| 55 | | }; |
| 56 | 40 | |
| 57 | 41 | |
| 58 | | #define WORK_STATE_INCOMPLETE 0 |
| 59 | | #define WORK_STATE_COMPLETE 1 |
| 60 | | |
| 61 | | struct _osd_work_item |
| 62 | | { |
| 63 | | osd_work_item * next; // pointer to next item |
| 64 | | osd_work_queue * queue; // pointer back to the owning queue |
| 65 | | osd_work_callback callback; // callback function |
| 66 | | void * param; // callback parameter |
| 67 | | void * result; // callback result |
| 68 | | volatile int state; // WORK_STATE_* above |
| 69 | | pthread_mutex_t statelock; // lock for state data |
| 70 | | pthread_cond_t statecond; // condition variable to wait for state change |
| 71 | | UINT32 flags; // creation flags |
| 72 | | UINT32 complete; // are we finished yet? |
| 73 | | }; |
| 74 | | |
| 75 | | |
| 76 | | |
| 77 | 42 | //============================================================ |
| 78 | | // FUNCTION PROTOTYPES |
| 43 | // MACROS |
| 79 | 44 | //============================================================ |
| 80 | 45 | |
| 81 | | static void * worker_thread_entry(void *param); |
| 82 | | static int execute_work_item(osd_work_item *item); |
| 46 | #if KEEP_STATISTICS |
| 47 | #define add_to_stat(v,x) do { osd_sync_add((v), (x)); } while (0) |
| 48 | #else |
| 49 | #define add_to_stat(v,x) do { } while (0) |
| 50 | #endif |
| 83 | 51 | |
| 52 | #ifndef YieldProcessor |
| 84 | 53 | |
| 85 | | |
| 86 | | //============================================================ |
| 87 | | // INLINE FUNCTIONS |
| 88 | | //============================================================ |
| 89 | | |
| 90 | 54 | #if defined(__i386__) || defined(__x86_64__) |
| 91 | 55 | |
| 92 | | INLINE void * compare_exchange_pointer(void * volatile *ptr, void *exchange, void *compare) |
| 56 | INLINE void YieldProcessor(void) |
| 93 | 57 | { |
| 94 | | register void *ret; |
| 95 | | __asm__ __volatile__ ( |
| 96 | | " lock ; cmpxchg %[exchange], %[ptr] ;" |
| 97 | | : [ptr] "+m" (*ptr) |
| 98 | | , [ret] "=a" (ret) |
| 99 | | : [compare] "1" (compare) |
| 100 | | , [exchange] "q" (exchange) |
| 101 | | : "%cc" |
| 102 | | ); |
| 103 | | return ret; |
| 58 | __asm__ __volatile__ ( " rep ; nop ;" ); |
| 104 | 59 | } |
| 105 | 60 | |
| 106 | | INLINE long interlocked_increment(long volatile *addend) |
| 107 | | { |
| 108 | | register long ret; |
| 109 | | __asm__ __volatile__ ( |
| 110 | | " mov $1, %[ret] ;" |
| 111 | | " lock ; xadd %[ret], %[addend] ;" |
| 112 | | " inc %[ret] ;" |
| 113 | | : [addend] "+m" (*addend) |
| 114 | | , [ret] "=&a" (ret) |
| 115 | | : |
| 116 | | : "%cc" |
| 117 | | ); |
| 118 | | return ret; |
| 119 | | } |
| 61 | #elif defined(__ppc__) || defined (__PPC__) || defined(__ppc64__) || defined(__PPC64__) |
| 120 | 62 | |
| 121 | | INLINE long interlocked_decrement(long volatile *addend) |
| 63 | INLINE void YieldProcessor(void) |
| 122 | 64 | { |
| 123 | | register long ret; |
| 124 | | __asm__ __volatile__ ( |
| 125 | | " mov $-1, %[ret] ;" |
| 126 | | " lock ; xadd %[ret], %[addend] ;" |
| 127 | | " dec %[ret] ;" |
| 128 | | : [addend] "+m" (*addend) |
| 129 | | , [ret] "=&a" (ret) |
| 130 | | : |
| 131 | | : "%cc" |
| 132 | | ); |
| 133 | | return ret; |
| 65 | __asm__ __volatile__ ( " nop \n" ); |
| 134 | 66 | } |
| 135 | 67 | |
| 136 | | #elif defined(__ppc64__) || defined(__PPC64__) |
| 68 | #endif |
| 137 | 69 | |
| 138 | | INLINE void * compare_exchange_pointer(void * volatile *ptr, void *exchange, void *compare) |
| 139 | | { |
| 140 | | register void *ret; |
| 141 | | __asm__ __volatile__ ( |
| 142 | | "1: sync \n" |
| 143 | | " ldarx %[ret], 0, %[ptr] \n" |
| 144 | | " cmpd %[compare], %[ret] \n" |
| 145 | | " bne 2f \n" |
| 146 | | " stdcx. %[exchange], 0, %[ptr] \n" |
| 147 | | " bne-- 1b \n" |
| 148 | | " sync \n" |
| 149 | | "2: " |
| 150 | | : [ret] "=&r" (ret) |
| 151 | | : [ptr] "r" (ptr) |
| 152 | | , [exchange] "r" (exchange) |
| 153 | | , [compare] "r" (compare) |
| 154 | | : "%cc" |
| 155 | | ); |
| 156 | | return ret; |
| 157 | | } |
| 70 | #endif |
| 158 | 71 | |
| 159 | | INLINE long interlocked_increment(long volatile *addend) |
| 160 | | { |
| 161 | | register long ret; |
| 162 | | __asm__ __volatile__ ( |
| 163 | | "1: sync \n" |
| 164 | | " ldarx %[ret], 0, %[addend] \n" |
| 165 | | " addi %[ret], %[ret], 1 \n" |
| 166 | | " stdcx. %[ret], 0, %[addend] \n" |
| 167 | | " bne-- 1b \n" |
| 168 | | " sync \n" |
| 169 | | : [ret] "=&b" (ret) |
| 170 | | : [addend] "r" (addend) |
| 171 | | : "%cc" |
| 172 | | ); |
| 173 | | return ret; |
| 174 | | } |
| 175 | 72 | |
| 176 | | INLINE long interlocked_decrement(long volatile *addend) |
| 177 | | { |
| 178 | | register long ret; |
| 179 | | __asm__ __volatile__ ( |
| 180 | | "1: sync \n" |
| 181 | | " ldarx %[ret], 0, %[addend] \n" |
| 182 | | " addi %[ret], %[ret], -1 \n" |
| 183 | | " stdcx. %[ret], 0, %[addend] \n" |
| 184 | | " bne-- 1b \n" |
| 185 | | " sync \n" |
| 186 | | : [ret] "=&b" (ret) |
| 187 | | : [addend] "r" (addend) |
| 188 | | : "%cc" |
| 189 | | ); |
| 190 | | return ret; |
| 191 | | } |
| 192 | 73 | |
| 193 | | #elif defined(__ppc__) || defined(__PPC__) |
| 74 | //============================================================ |
| 75 | // TYPE DEFINITIONS |
| 76 | //============================================================ |
| 194 | 77 | |
| 195 | | INLINE void * compare_exchange_pointer(void * volatile *ptr, void *exchange, void *compare) |
| 78 | typedef struct _mame_thread_info mame_thread_info; |
| 79 | struct _mame_thread_info |
| 196 | 80 | { |
| 197 | | register void *ret; |
| 198 | | __asm__ __volatile__ ( |
| 199 | | "1: sync \n" |
| 200 | | " lwarx %[ret], 0, %[ptr] \n" |
| 201 | | " cmpw %[compare], %[ret] \n" |
| 202 | | " bne 2f \n" |
| 203 | | " stwcx. %[exchange], 0, %[ptr] \n" |
| 204 | | " bne- 1b \n" |
| 205 | | " sync \n" |
| 206 | | "2: " |
| 207 | | : [ret] "=&r" (ret) |
| 208 | | : [ptr] "r" (ptr) |
| 209 | | , [exchange] "r" (exchange) |
| 210 | | , [compare] "r" (compare) |
| 211 | | : "%cc" |
| 212 | | ); |
| 213 | | return ret; |
| 214 | | } |
| 81 | osd_work_queue * queue; // pointer back to the queue |
| 82 | osd_thread * handle; // handle to the thread |
| 83 | osd_event * wakeevent; // wake event for the thread |
| 84 | UINT8 active; // are we actively processing work? |
| 85 | }; |
| 215 | 86 | |
| 216 | | INLINE long interlocked_increment(long volatile *addend) |
| 217 | | { |
| 218 | | register long ret; |
| 219 | | __asm__ __volatile__ ( |
| 220 | | "1: sync \n" |
| 221 | | " lwarx %[ret], 0, %[addend] \n" |
| 222 | | " addi %[ret], %[ret], 1 \n" |
| 223 | | " stwcx. %[ret], 0, %[addend] \n" |
| 224 | | " bne- 1b \n" |
| 225 | | " sync \n" |
| 226 | | : [ret] "=&b" (ret) |
| 227 | | : [addend] "r" (addend) |
| 228 | | : "%cc" |
| 229 | | ); |
| 230 | | return ret; |
| 231 | | } |
| 232 | 87 | |
| 233 | | INLINE long interlocked_decrement(long volatile *addend) |
| 88 | struct _osd_work_queue |
| 234 | 89 | { |
| 235 | | register long ret; |
| 236 | | __asm__ __volatile__ ( |
| 237 | | "1: sync \n" |
| 238 | | " lwarx %[ret], 0, %[addend] \n" |
| 239 | | " addi %[ret], %[ret], -1 \n" |
| 240 | | " stwcx. %[ret], 0, %[addend] \n" |
| 241 | | " bne- 1b \n" |
| 242 | | " sync \n" |
| 243 | | : [ret] "=&b" (ret) |
| 244 | | : [addend] "r" (addend) |
| 245 | | : "%cc" |
| 246 | | ); |
| 247 | | return ret; |
| 248 | | } |
| 90 | osd_lock * critsect; // critical section protecting the queue |
| 91 | osd_work_item * volatile list; // list of items in the queue |
| 92 | osd_work_item ** volatile tailptr; // pointer to the tail pointer of work items in the queue |
| 93 | osd_work_item * volatile free; // free list of work items |
| 94 | volatile INT32 items; // items in the queue |
| 95 | volatile INT32 livethreads; // number of live threads |
| 96 | UINT32 threads; // number of threads in this queue |
| 97 | UINT32 flags; // creation flags |
| 98 | mame_thread_info * thread; // array of thread information |
| 99 | osd_event * workevent; // shared event signalled when work is available |
| 100 | osd_event * doneevent; // event signalled when work is complete |
| 101 | osd_event * exitevent; // event signalled when thread should exit |
| 249 | 102 | |
| 103 | #if KEEP_STATISTICS |
| 104 | volatile INT32 itemsqueued; // total items queued |
| 105 | volatile INT32 setevents; // number of times we called SetEvent |
| 106 | volatile INT32 extraitems; // how many extra items we got after the first in the queue loop |
| 107 | volatile INT32 spinloops; // how many times spinning bought us more items |
| 250 | 108 | #endif |
| 109 | }; |
| 251 | 110 | |
| 252 | 111 | |
| 112 | struct _osd_work_item |
| 113 | { |
| 114 | osd_work_item * next; // pointer to next item |
| 115 | osd_work_queue * queue; // pointer back to the owning queue |
| 116 | osd_work_callback callback; // callback function |
| 117 | void * param; // callback parameter |
| 118 | void * result; // callback result |
| 119 | osd_event * event; // event signalled when complete |
| 120 | UINT32 flags; // creation flags |
| 121 | volatile INT32 refcount; // reference count of threads processing |
| 122 | }; |
| 123 | |
| 124 | |
| 125 | |
| 253 | 126 | //============================================================ |
| 127 | // FUNCTION PROTOTYPES |
| 128 | //============================================================ |
| 129 | |
| 130 | static int effective_num_processors(void); |
| 131 | static void * worker_thread_entry(void *param); |
| 132 | static void worker_thread_process(osd_work_queue *queue, mame_thread_info *thread); |
| 133 | static void execute_work_item(osd_work_item *item); |
| 134 | |
| 135 | //============================================================ |
| 254 | 136 | // osd_work_queue_alloc |
| 255 | 137 | //============================================================ |
| 256 | 138 | |
| 257 | 139 | osd_work_queue *osd_work_queue_alloc(int flags) |
| 258 | 140 | { |
| 141 | int numprocs = effective_num_processors(); |
| 259 | 142 | osd_work_queue *queue; |
| 260 | 143 | int threadnum; |
| 261 | | int processors; |
| 262 | 144 | |
| 263 | 145 | // allocate a new queue |
| 264 | 146 | queue = malloc(sizeof(*queue)); |
| r1082 | r1083 | |
| 266 | 148 | goto error; |
| 267 | 149 | memset(queue, 0, sizeof(*queue)); |
| 268 | 150 | |
| 269 | | // allocate mutexes and conditions for the queue |
| 270 | | if ( pthread_mutex_init(&queue->statelock, NULL) != 0 ) |
| 151 | // initialize basic queue members |
| 152 | queue->tailptr = (osd_work_item **)&queue->list; |
| 153 | queue->flags = flags; |
| 154 | |
| 155 | // allocate events for the queue |
| 156 | queue->workevent = osd_event_alloc(FALSE, FALSE); // auto-reset, not signalled |
| 157 | queue->doneevent = osd_event_alloc(TRUE, TRUE); // manual reset, signalled |
| 158 | queue->exitevent = osd_event_alloc(TRUE, FALSE); // manual reset, not signalled |
| 159 | if (queue->workevent == NULL || queue->doneevent == NULL || queue->exitevent == NULL) |
| 271 | 160 | goto error; |
| 272 | | if ( pthread_cond_init(&queue->statecond, NULL) != 0 ) |
| 273 | | goto error; |
| 274 | 161 | |
| 275 | 162 | // initialize the critical section |
| 276 | | if ( pthread_mutex_init(&queue-> critsect, NULL) != 0 ) |
| 163 | if ( (queue->critsect = osd_lock_alloc()) == NULL ) |
| 277 | 164 | goto error; |
| 278 | 165 | |
| 279 | | queue->tailptr = (osd_work_item **)&queue->list; |
| 280 | | |
| 281 | | // determine how many threads to create |
| 282 | | processors = 1; |
| 283 | | |
| 284 | | #ifdef SDLMAME_DARWIN |
| 285 | | { |
| 286 | | struct host_basic_info host_basic_info; |
| 287 | | unsigned int count; |
| 288 | | kern_return_t r; |
| 289 | | mach_port_t my_mach_host_self; |
| 290 | | |
| 291 | | count = HOST_BASIC_INFO_COUNT; |
| 292 | | my_mach_host_self = mach_host_self(); |
| 293 | | if ( ( r = host_info(my_mach_host_self, HOST_BASIC_INFO, (host_info_t)(&host_basic_info), &count)) == KERN_SUCCESS ) |
| 294 | | { |
| 295 | | processors = host_basic_info.avail_cpus; |
| 296 | | } |
| 297 | | mach_port_deallocate(mach_task_self(), my_mach_host_self); |
| 298 | | } |
| 299 | | #elif defined(_SC_NPROCESSORS_ONLN) |
| 300 | | processors = sysconf(_SC_NPROCESSORS_ONLN); |
| 301 | | #endif |
| 302 | | |
| 303 | | if (processors == 1) |
| 166 | // determine how many threads to create... |
| 167 | // on a single-CPU system, create 1 thread for I/O queues, and 0 threads for everything else |
| 168 | if (numprocs == 1) |
| 304 | 169 | queue->threads = (flags & WORK_QUEUE_FLAG_IO) ? 1 : 0; |
| 170 | |
| 171 | // on an n-CPU system, create (n-1) threads for multi queues, and 1 thread for everything else |
| 305 | 172 | else |
| 306 | | queue->threads = (flags & WORK_QUEUE_FLAG_MULTI) ? processors : 1; |
| 173 | queue->threads = (flags & WORK_QUEUE_FLAG_MULTI) ? (numprocs - 1) : 1; |
| 307 | 174 | |
| 308 | | queue->state = QUEUE_STATE_WORK_COMPLETE; |
| 309 | | |
| 310 | 175 | // if we have threads, create them |
| 311 | 176 | if (queue->threads > 0) |
| 312 | 177 | { |
| r1082 | r1083 | |
| 319 | 184 | // iterate over threads |
| 320 | 185 | for (threadnum = 0; threadnum < queue->threads; threadnum++) |
| 321 | 186 | { |
| 187 | mame_thread_info *thread = &queue->thread[threadnum]; |
| 188 | |
| 189 | // set a pointer back to the queue |
| 190 | thread->queue = queue; |
| 191 | |
| 192 | // create the per-thread wake event |
| 193 | thread->wakeevent = osd_event_alloc(FALSE, FALSE); // auto-reset, not signalled |
| 194 | if (thread->wakeevent == NULL) |
| 195 | goto error; |
| 196 | |
| 322 | 197 | // create the thread |
| 323 | | if ( pthread_create(&queue->thread[threadnum], NULL, worker_thread_entry, queue) != 0 ) |
| 198 | thread->handle = osd_thread_create(worker_thread_entry, thread); |
| 199 | if (thread->handle == NULL) |
| 324 | 200 | goto error; |
| 325 | 201 | |
| 326 | | // set its priority |
| 202 | // set its priority: I/O threads get high priority because they are assumed to be |
| 203 | // blocked most of the time; other threads just match the creator's priority |
| 327 | 204 | if (flags & WORK_QUEUE_FLAG_IO) |
| 328 | | { |
| 329 | | struct sched_param sched; |
| 330 | | int policy; |
| 331 | | |
| 332 | | if ( pthread_getschedparam( queue->thread[threadnum], &policy, &sched ) == 0 ) |
| 333 | | { |
| 334 | | sched.sched_priority++; |
| 335 | | pthread_setschedparam( queue->thread[threadnum], policy, &sched ); |
| 336 | | } |
| 337 | | } |
| 338 | | |
| 339 | | |
| 205 | osd_thread_adjust_priority(thread->handle, 0); // TODO: specify appropriate priority |
| 206 | else |
| 207 | osd_thread_adjust_priority(thread->handle, 0); // TODO: specify appropriate priority |
| 340 | 208 | } |
| 341 | 209 | } |
| 342 | 210 | return queue; |
| r1082 | r1083 | |
| 368 | 236 | if (queue->threads == 0) |
| 369 | 237 | return TRUE; |
| 370 | 238 | |
| 371 | | // wait for the collection to be signalled |
| 372 | | if ( pthread_mutex_lock(&queue->statelock) != 0 ) |
| 373 | | return FALSE; |
| 239 | // if no items, we're done |
| 240 | if (queue->items == 0) |
| 241 | return TRUE; |
| 374 | 242 | |
| 375 | | do |
| 243 | // if this is a multi queue, help out rather than doing nothing |
| 244 | if (queue->flags & WORK_QUEUE_FLAG_MULTI) |
| 376 | 245 | { |
| 377 | | struct timespec ts; |
| 378 | | struct timeval tp; |
| 246 | osd_ticks_t stopspin = osd_ticks() + timeout; |
| 379 | 247 | |
| 380 | | // usleep(1000); |
| 248 | // process what we can as a worker thread |
| 249 | worker_thread_process(queue, NULL); |
| 381 | 250 | |
| 382 | | if ( queue->state == QUEUE_STATE_WORK_COMPLETE ) |
| 383 | | { |
| 384 | | pthread_mutex_unlock(&queue->statelock); |
| 385 | | return TRUE; |
| 386 | | } |
| 251 | // spin until we hit 0 items and then return TRUE |
| 252 | while (queue->items != 0 && osd_ticks() < stopspin) |
| 253 | YieldProcessor(); |
| 254 | return TRUE; |
| 255 | } |
| 387 | 256 | |
| 388 | | gettimeofday(&tp, NULL); |
| 389 | | ts.tv_sec = tp.tv_sec; |
| 390 | | ts.tv_nsec = tp.tv_usec * 1000; |
| 391 | | ts.tv_sec += timeout / osd_ticks_per_second(); |
| 257 | // reset our done event and double-check the items before waiting |
| 258 | osd_event_reset(queue->doneevent); |
| 259 | if (queue->items == 0) |
| 260 | return TRUE; |
| 392 | 261 | |
| 393 | | if ( pthread_cond_timedwait(&queue->statecond, &queue->statelock, &ts) != 0 ) |
| 394 | | return FALSE; |
| 395 | | } while( 1 ); |
| 396 | | |
| 397 | | //Never get here |
| 398 | | //return FALSE; |
| 262 | // wait for the collection to be signalled |
| 263 | return (osd_event_wait(queue->doneevent, timeout)); |
| 399 | 264 | } |
| 400 | 265 | |
| 401 | 266 | |
| r1082 | r1083 | |
| 410 | 275 | { |
| 411 | 276 | int threadnum; |
| 412 | 277 | |
| 413 | | pthread_mutex_lock(&queue->statelock); |
| 414 | | queue->state = QUEUE_STATE_EXIT; |
| 415 | | pthread_mutex_unlock(&queue->statelock); |
| 416 | | |
| 417 | 278 | // signal all the threads to exit |
| 418 | | pthread_cond_broadcast(&queue->statecond); |
| 279 | osd_event_set(queue->exitevent); |
| 419 | 280 | |
| 420 | | // count the number of valid threads (we could be partially constructed) |
| 421 | | |
| 281 | // wait for all the threads to go away |
| 422 | 282 | for (threadnum = 0; threadnum < queue->threads; threadnum++) |
| 423 | 283 | { |
| 424 | | if (queue->thread[threadnum] != (pthread_t)NULL) |
| 284 | mame_thread_info *thread = &queue->thread[threadnum]; |
| 285 | |
| 286 | // block on the thread going away, then close the handle |
| 287 | if (thread->handle != NULL) |
| 425 | 288 | { |
| 426 | | // wait for all the threads to exit |
| 427 | | pthread_join(queue->thread[threadnum], NULL); |
| 289 | osd_thread_wait_free(thread->handle); |
| 428 | 290 | } |
| 291 | |
| 292 | // clean up the wake event |
| 293 | if (thread->wakeevent != NULL) |
| 294 | osd_event_free(thread->wakeevent); |
| 429 | 295 | } |
| 430 | 296 | |
| 431 | 297 | // free the list |
| 432 | 298 | free(queue->thread); |
| 433 | 299 | } |
| 434 | 300 | |
| 435 | | // free all the mutexes and conditions |
| 436 | | pthread_cond_destroy(&queue->statecond); |
| 437 | | pthread_mutex_unlock(&queue->statelock); |
| 438 | | pthread_mutex_destroy(&queue->statelock); |
| 301 | // free all the events |
| 302 | if (queue->workevent != NULL) |
| 303 | osd_event_free(queue->workevent); |
| 304 | if (queue->doneevent != NULL) |
| 305 | osd_event_free(queue->doneevent); |
| 306 | if (queue->exitevent != NULL) |
| 307 | osd_event_free(queue->exitevent); |
| 439 | 308 | |
| 440 | 309 | // free the critical section |
| 441 | | pthread_mutex_unlock(&queue->critsect); |
| 442 | | pthread_mutex_destroy(&queue->critsect); |
| 310 | osd_lock_free(queue->critsect); |
| 443 | 311 | |
| 444 | 312 | // free all items in the free list |
| 445 | 313 | while (queue->free != NULL) |
| 446 | 314 | { |
| 447 | 315 | osd_work_item *item = (osd_work_item *)queue->free; |
| 448 | 316 | queue->free = item->next; |
| 449 | | if (!(item->flags & WORK_ITEM_FLAG_AUTO_RELEASE)) |
| 450 | | { |
| 451 | | pthread_cond_destroy(&item->statecond); |
| 452 | | pthread_mutex_unlock(&item->statelock); |
| 453 | | pthread_mutex_destroy(&item->statelock); |
| 454 | | } |
| 317 | if (item->event != NULL) |
| 318 | osd_event_free(item->event); |
| 455 | 319 | free(item); |
| 456 | 320 | } |
| 457 | 321 | |
| r1082 | r1083 | |
| 460 | 324 | { |
| 461 | 325 | osd_work_item *item = (osd_work_item *)queue->list; |
| 462 | 326 | queue->list = item->next; |
| 463 | | pthread_cond_destroy(&item->statecond); |
| 464 | | pthread_mutex_unlock(&item->statelock); |
| 465 | | pthread_mutex_destroy(&item->statelock); |
| 327 | if (item->event != NULL) |
| 328 | osd_event_free(item->event); |
| 466 | 329 | free(item); |
| 467 | 330 | } |
| 468 | 331 | |
| 332 | #if KEEP_STATISTICS |
| 333 | printf("Items queued = %9d\n", queue->itemsqueued); |
| 334 | printf("SetEvent calls = %9d\n", queue->setevents); |
| 335 | printf("Extra items = %9d\n", queue->extraitems); |
| 336 | printf("Spin loops = %9d\n", queue->spinloops); |
| 337 | #endif |
| 338 | |
| 469 | 339 | // free the queue itself |
| 470 | 340 | free(queue); |
| 471 | 341 | } |
| r1082 | r1083 | |
| 483 | 353 | do |
| 484 | 354 | { |
| 485 | 355 | item = (osd_work_item *)queue->free; |
| 486 | | } while (item != NULL && compare_exchange_pointer((void * volatile *)&queue->free, item->next, item) != item); |
| 356 | } while (item != NULL && osd_compare_exchange_ptr((void * volatile *)&queue->free, item, item->next) != item); |
| 487 | 357 | |
| 488 | 358 | // if nothing, allocate something new |
| 489 | 359 | if (item == NULL) |
| r1082 | r1083 | |
| 492 | 362 | item = malloc(sizeof(*item)); |
| 493 | 363 | if (item == NULL) |
| 494 | 364 | return NULL; |
| 365 | item->event = NULL; |
| 366 | item->queue = queue; |
| 495 | 367 | } |
| 496 | 368 | |
| 497 | 369 | // fill in the basics |
| r1082 | r1083 | |
| 500 | 372 | item->param = param; |
| 501 | 373 | item->result = NULL; |
| 502 | 374 | item->flags = flags; |
| 503 | | item->queue = queue; |
| 504 | | item->complete = FALSE; |
| 505 | | item->state = WORK_STATE_INCOMPLETE; |
| 506 | | // allocate mutexes and conditions |
| 507 | | if ( pthread_mutex_init(&item->statelock, NULL) != 0 || pthread_cond_init(&item->statecond, NULL) != 0 ) |
| 508 | | { |
| 509 | | return NULL; |
| 510 | | } |
| 375 | item->refcount = 1; |
| 376 | if (item->event != NULL) |
| 377 | osd_event_reset(item->event); |
| 511 | 378 | |
| 512 | 379 | // if no threads, just run it now |
| 513 | 380 | if (queue->threads == 0) |
| 514 | | return execute_work_item(item) ? item : NULL; |
| 381 | { |
| 382 | // execute the callback and clear the refcount back to 0 |
| 383 | item->result = (*item->callback)(item->param); |
| 384 | item->refcount = 0; |
| 515 | 385 | |
| 516 | | // otherwise, enqueue it |
| 517 | | pthread_mutex_lock(&queue->critsect); |
| 518 | | *queue->tailptr = item; |
| 519 | | queue->tailptr = (osd_work_item **)&item->next; |
| 520 | | pthread_mutex_unlock(&queue->critsect); |
| 386 | // handle auto-release; make sure we return NULL in that case |
| 387 | if (flags & WORK_ITEM_FLAG_AUTO_RELEASE) |
| 388 | osd_work_item_release(item); |
| 389 | } |
| 521 | 390 | |
| 522 | | // if we're not full up, signal the event |
| 523 | | pthread_mutex_lock(&queue->statelock); |
| 524 | | if (interlocked_increment(&queue->items) == 1) |
| 525 | | queue->state = QUEUE_STATE_DOWORK; |
| 526 | | pthread_mutex_unlock(&queue->statelock); |
| 527 | | |
| 528 | | if (queue->livethreads < queue->threads) |
| 529 | | pthread_cond_broadcast(&queue->statecond); |
| 391 | // otherwise, enqueue the item and wake the threads |
| 392 | else |
| 393 | { |
| 394 | // enqueue within the critical section |
| 395 | osd_lock_acquire(queue->critsect); |
| 396 | *queue->tailptr = item; |
| 397 | queue->tailptr = (osd_work_item **)&item->next; |
| 398 | osd_lock_release(queue->critsect); |
| 530 | 399 | |
| 531 | | return item; |
| 400 | // increment the number of items in the queue |
| 401 | osd_sync_add(&queue->items, 1); |
| 402 | add_to_stat(&queue->itemsqueued, 1); |
| 403 | |
| 404 | // non-shared case |
| 405 | if (!(flags & WORK_ITEM_FLAG_SHARED)) |
| 406 | { |
| 407 | // if there are still some free threads, signal the shared work event |
| 408 | if (queue->livethreads < queue->threads) |
| 409 | { |
| 410 | osd_event_set(queue->workevent); |
| 411 | add_to_stat(&queue->setevents, 1); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | // shared case |
| 416 | else |
| 417 | { |
| 418 | int threadnum; |
| 419 | |
| 420 | // signal the wake event for all non-active threads |
| 421 | for (threadnum = 0; threadnum < queue->threads; threadnum++) |
| 422 | if (!queue->thread[threadnum].active) |
| 423 | { |
| 424 | osd_event_set(queue->thread[threadnum].wakeevent); |
| 425 | add_to_stat(&queue->setevents, 1); |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | // only return the item if it won't get released automatically |
| 431 | return (flags & WORK_ITEM_FLAG_AUTO_RELEASE) ? NULL : item; |
| 532 | 432 | } |
| 533 | 433 | |
| 534 | 434 | |
| r1082 | r1083 | |
| 539 | 439 | int osd_work_item_wait(osd_work_item *item, osd_ticks_t timeout) |
| 540 | 440 | { |
| 541 | 441 | // if we're done already, just return |
| 542 | | if (item->complete) |
| 442 | if (item->refcount == 0) |
| 543 | 443 | return TRUE; |
| 544 | 444 | |
| 545 | | // wait for the collection to be signalled |
| 546 | | if ( pthread_mutex_lock(&item->statelock) != 0 ) |
| 547 | | return FALSE; |
| 445 | // if we don't have an event, create one |
| 446 | if (item->event == NULL) |
| 447 | item->event = osd_event_alloc(TRUE, FALSE); // manual reset, not signalled |
| 548 | 448 | |
| 549 | | do |
| 449 | // if we don't have an event, we need to spin (shouldn't ever really happen) |
| 450 | if (item->event != NULL) |
| 550 | 451 | { |
| 551 | | struct timespec ts; |
| 552 | | struct timeval tp; |
| 452 | osd_ticks_t stopspin = osd_ticks() + timeout; |
| 453 | while (item->refcount != 0 && osd_ticks() < stopspin) |
| 454 | YieldProcessor(); |
| 455 | } |
| 553 | 456 | |
| 554 | | if ( item->complete ) |
| 555 | | { |
| 556 | | pthread_mutex_unlock(&item->statelock); |
| 557 | | return TRUE; |
| 558 | | } |
| 457 | // otherwise, block on the event until done |
| 458 | else if (item->refcount != 0) |
| 459 | osd_event_wait(item->event, timeout); |
| 559 | 460 | |
| 560 | | gettimeofday(&tp, NULL); |
| 561 | | ts.tv_sec = tp.tv_sec; |
| 562 | | ts.tv_nsec = tp.tv_usec * 1000; |
| 563 | | ts.tv_sec += timeout / osd_ticks_per_second(); |
| 564 | | |
| 565 | | if ( pthread_cond_timedwait(&item->statecond, &item->statelock, &ts) != 0 ) |
| 566 | | return FALSE; |
| 567 | | } while( 1 ); |
| 568 | | |
| 569 | | // We never get here |
| 570 | | // return item->complete; |
| 461 | // return TRUE if the refcount actually hit 0 |
| 462 | return (item->refcount == 0); |
| 571 | 463 | } |
| 572 | 464 | |
| 573 | 465 | |
| r1082 | r1083 | |
| 592 | 484 | // make sure we're done first |
| 593 | 485 | osd_work_item_wait(item, 100 * osd_ticks_per_second()); |
| 594 | 486 | |
| 595 | | // release the mutexes and conditions |
| 596 | | pthread_cond_destroy(&item->statecond); |
| 597 | | pthread_mutex_unlock(&item->statelock); |
| 598 | | pthread_mutex_destroy(&item->statelock); |
| 599 | | |
| 600 | 487 | // add us to the free list on our queue |
| 601 | 488 | do |
| 602 | 489 | { |
| 603 | 490 | next = (osd_work_item *)item->queue->free; |
| 604 | 491 | item->next = next; |
| 605 | | } while (compare_exchange_pointer((void * volatile *)&item->queue->free, item, next) != next); |
| 492 | } while (osd_compare_exchange_ptr((void * volatile *)&item->queue->free, next, item) != next); |
| 606 | 493 | } |
| 607 | 494 | |
| 608 | 495 | |
| 609 | 496 | //============================================================ |
| 497 | // effective_num_processors |
| 498 | //============================================================ |
| 499 | |
| 500 | static int effective_num_processors(void) |
| 501 | { |
| 502 | char *procsoverride; |
| 503 | int numprocs = 0; |
| 504 | |
| 505 | // if the OSDPROCESSORS environment variable is set, use that value if valid |
| 506 | procsoverride = getenv("OSDPROCESSORS"); |
| 507 | if (procsoverride != NULL && sscanf(procsoverride, "%d", &numprocs) == 1 && numprocs > 0) |
| 508 | return numprocs; |
| 509 | |
| 510 | // otherwise, fetch the info from the system |
| 511 | return osd_num_processors(); |
| 512 | } |
| 513 | |
| 514 | |
| 515 | //============================================================ |
| 610 | 516 | // worker_thread_entry |
| 611 | 517 | //============================================================ |
| 612 | 518 | |
| 613 | 519 | static void *worker_thread_entry(void *param) |
| 614 | 520 | { |
| 615 | | osd_work_queue *queue = (osd_work_queue *)param; |
| 521 | mame_thread_info *thread = param; |
| 522 | osd_work_queue *queue = thread->queue; |
| 523 | osd_event *hlist[3]; |
| 616 | 524 | |
| 525 | // fill in our handle list |
| 526 | hlist[0] = queue->workevent; |
| 527 | hlist[1] = thread->wakeevent; |
| 528 | hlist[2] = queue->exitevent; |
| 529 | |
| 617 | 530 | // loop until we exit |
| 618 | 531 | for ( ;; ) |
| 619 | 532 | { |
| 620 | 533 | // block waiting for work or exit |
| 621 | | pthread_mutex_lock(&queue->statelock); |
| 622 | | pthread_cond_wait(&queue->statecond, &queue->statelock); |
| 623 | | pthread_mutex_unlock(&queue->statelock); |
| 534 | int result = 0; |
| 624 | 535 | |
| 625 | | // bail on exit |
| 626 | | if ( queue->state == QUEUE_STATE_EXIT ) |
| 536 | // bail on exit, and only wait if there are no pending items in queue |
| 537 | if (queue->items == 0) |
| 538 | result = osd_event_wait_multiple(ARRAY_LENGTH(hlist), hlist, 10000000); |
| 539 | if (result == 2) |
| 627 | 540 | break; |
| 628 | 541 | |
| 629 | | // loop until everything is processed |
| 630 | | while (queue->items != 0) |
| 542 | // indicate that we are live |
| 543 | thread->active = TRUE; |
| 544 | osd_sync_add(&queue->livethreads, 1); |
| 545 | |
| 546 | // process work items |
| 547 | for ( ;; ) |
| 631 | 548 | { |
| 632 | | osd_work_item *item; |
| 549 | osd_ticks_t stopspin; |
| 633 | 550 | |
| 634 | | // indicate that we are live |
| 635 | | interlocked_increment(&queue->livethreads); |
| 551 | // process as much as we can |
| 552 | worker_thread_process(queue, thread); |
| 636 | 553 | |
| 637 | | // pull an item off the head |
| 638 | | pthread_mutex_lock(&queue->critsect); |
| 554 | // spin for a while looking for more work |
| 555 | stopspin = osd_ticks() + osd_ticks_per_second() / 1000; |
| 556 | while (queue->items == 0 && osd_ticks() < stopspin) |
| 557 | YieldProcessor(); |
| 558 | |
| 559 | // if nothing more, release the processor |
| 560 | if (queue->items == 0) |
| 561 | break; |
| 562 | add_to_stat(&queue->spinloops, 1); |
| 563 | } |
| 564 | |
| 565 | // decrement the live thread count |
| 566 | thread->active = FALSE; |
| 567 | osd_sync_add(&queue->livethreads, -1); |
| 568 | } |
| 569 | //return 0; |
| 570 | return NULL; |
| 571 | } |
| 572 | |
| 573 | |
| 574 | //============================================================ |
| 575 | // worker_thread_process |
| 576 | //============================================================ |
| 577 | |
| 578 | static void worker_thread_process(osd_work_queue *queue, mame_thread_info *thread) |
| 579 | { |
| 580 | // loop until everything is processed |
| 581 | while (queue->items != 0) |
| 582 | { |
| 583 | int removed_item = FALSE; |
| 584 | osd_work_item *item; |
| 585 | |
| 586 | // use a critical section to synchronize the removal of items |
| 587 | osd_lock_acquire(queue->critsect); |
| 588 | { |
| 639 | 589 | item = (osd_work_item *)queue->list; |
| 640 | | if (item != NULL) |
| 590 | |
| 591 | // if it is a regular item, or if it is a shared item that is complete, remove it |
| 592 | if (item != NULL && (!(item->flags & WORK_ITEM_FLAG_SHARED) || item->refcount == 0)) |
| 641 | 593 | { |
| 594 | removed_item = TRUE; |
| 642 | 595 | queue->list = item->next; |
| 643 | 596 | if (item->next == NULL) |
| 644 | 597 | queue->tailptr = (osd_work_item **)&queue->list; |
| 645 | 598 | } |
| 646 | | pthread_mutex_unlock(&queue->critsect); |
| 599 | } |
| 600 | osd_lock_release(queue->critsect); |
| 647 | 601 | |
| 648 | | // call the callback and signal its complete |
| 649 | | if (item) |
| 650 | | { |
| 651 | | #ifdef SDLMAME_DARWIN |
| 652 | | void *pool = NewAutoreleasePool(); |
| 653 | | #endif |
| 654 | | // WARNING: if execute_work_item returns FALSE the item was |
| 655 | | // auto-destroyed and it's statecond is a bad thing to touch! |
| 656 | | if (execute_work_item(item)) |
| 657 | | { |
| 658 | | pthread_cond_broadcast(&item->statecond); |
| 659 | | } |
| 660 | | #ifdef SDLMAME_DARWIN |
| 661 | | ReleaseAutoreleasePool(pool); |
| 662 | | #endif |
| 663 | | } |
| 602 | // call the callback and signal its event |
| 603 | if (item != NULL && item->refcount != 0) |
| 604 | execute_work_item(item); |
| 664 | 605 | |
| 665 | | // decrement the count |
| 666 | | if (interlocked_decrement(&queue->items) == 0) |
| 667 | | { |
| 668 | | pthread_mutex_lock(&queue->statelock); |
| 669 | | queue->state = QUEUE_STATE_WORK_COMPLETE; |
| 670 | | pthread_mutex_unlock(&queue->statelock); |
| 671 | | pthread_cond_broadcast(&queue->statecond); |
| 672 | | } |
| 606 | // if we removed an item, decrement the item count and signal done if appropriate |
| 607 | if (removed_item && osd_sync_add(&queue->items, -1) == 0) |
| 608 | { |
| 609 | // we don't need to set the doneevent for multi queues because they spin |
| 610 | if (!(item->flags & WORK_QUEUE_FLAG_MULTI)) |
| 611 | osd_event_set(queue->doneevent); |
| 673 | 612 | |
| 674 | | // decrement the live thread count |
| 675 | | interlocked_decrement(&queue->livethreads); |
| 613 | // if it's an auto-release item, release it |
| 614 | if (item->flags & WORK_ITEM_FLAG_AUTO_RELEASE) |
| 615 | osd_work_item_release(item); |
| 676 | 616 | } |
| 677 | | } |
| 678 | 617 | |
| 679 | | return 0; |
| 618 | // if we removed an item and there's still work to do, bump the stats |
| 619 | if (removed_item && queue->items != 0) |
| 620 | add_to_stat(&queue->extraitems, 1); |
| 621 | } |
| 680 | 622 | } |
| 681 | 623 | |
| 624 | |
| 682 | 625 | //============================================================ |
| 683 | 626 | // execute_work_item |
| 684 | 627 | //============================================================ |
| 685 | 628 | |
| 686 | | static int execute_work_item(osd_work_item *item) |
| 629 | static void execute_work_item(osd_work_item *item) |
| 687 | 630 | { |
| 688 | | // call the callback and stash the result |
| 689 | | item->result = (*item->callback)(item->param); |
| 631 | void *result; |
| 690 | 632 | |
| 691 | | // mark it complete (and signal the event?) |
| 692 | | item->complete = TRUE; |
| 693 | | |
| 694 | | // if it's an auto-release item, release it |
| 695 | | if (item->flags & WORK_ITEM_FLAG_AUTO_RELEASE) |
| 633 | // add a claim to this work item; if it had already been completed, back off and exit |
| 634 | if (osd_sync_add(&item->refcount, 1) == 1) |
| 696 | 635 | { |
| 697 | | osd_work_item_release(item); |
| 698 | | return FALSE; |
| 636 | item->refcount = 0; |
| 637 | return; |
| 699 | 638 | } |
| 700 | 639 | |
| 701 | | return TRUE; |
| 640 | // call the callback and stash the result |
| 641 | result = (*item->callback)(item->param); |
| 642 | |
| 643 | // mark it complete and signal the event |
| 644 | if (osd_sync_add(&item->refcount, -1) == 1 && osd_compare_exchange32(&item->refcount, 1, 0) == 1) |
| 645 | { |
| 646 | // set the result and signal the event |
| 647 | item->result = result; |
| 648 | if (item->event != NULL) |
| 649 | osd_event_set(item->event); |
| 650 | } |
| 702 | 651 | } |
| 703 | 652 | |
| 704 | 653 | #endif // SDLMAME_WIN32 |