src/osd/sdl/sdlsync.c
| r1112 | r1113 | |
| 13 | 13 | #ifndef _GNU_SOURCE |
| 14 | 14 | #define _GNU_SOURCE // for PTHREAD_MUTEX_RECURSIVE; needs to be here before other glibc headers are included |
| 15 | 15 | #endif |
| 16 | | #endif |
| 17 | 16 | |
| 18 | 17 | #ifdef SDLMAME_MACOSX |
| 19 | 18 | #include "SDL/SDL.h" |
| r1112 | r1113 | |
| 21 | 20 | #include "SDL.h" |
| 22 | 21 | #endif |
| 23 | 22 | |
| 24 | | #ifdef SDLMAME_WIN32 |
| 25 | | #define WIN32_LEAN_AND_MEAN |
| 26 | | #include <windows.h> |
| 27 | | #endif |
| 28 | | |
| 29 | 23 | #ifdef SDLMAME_OS2 |
| 30 | 24 | #define INCL_DOS |
| 31 | 25 | #include <os2.h> |
| r1112 | r1113 | |
| 44 | 38 | #include "mame.h" |
| 45 | 39 | #include "sdlsync.h" |
| 46 | 40 | |
| 47 | | #ifndef SDLMAME_WIN32 |
| 48 | 41 | #ifndef SDLMAME_OS2 |
| 49 | 42 | #include <pthread.h> |
| 50 | 43 | #include <errno.h> |
| r1112 | r1113 | |
| 57 | 50 | struct _osd_event { |
| 58 | 51 | pthread_mutex_t mutex; |
| 59 | 52 | pthread_cond_t cond; |
| 60 | | int autoreset; |
| 61 | | volatile int signalled; |
| 53 | volatile INT32 autoreset; |
| 54 | volatile INT32 signalled; |
| 62 | 55 | }; |
| 63 | 56 | |
| 64 | 57 | struct _osd_thread { |
| 65 | 58 | pthread_t thread; |
| 66 | 59 | }; |
| 67 | 60 | |
| 68 | | static osd_lock *atomic_lck = NULL; |
| 61 | static osd_lock *atomic_lck = NULL; |
| 69 | 62 | |
| 70 | 63 | //============================================================ |
| 71 | 64 | // osd_lock_alloc |
| r1112 | r1113 | |
| 181 | 174 | pthread_cond_init(&ev->cond, NULL); |
| 182 | 175 | ev->signalled = initialstate; |
| 183 | 176 | ev->autoreset = !manualreset; |
| 184 | | |
| 177 | |
| 185 | 178 | return ev; |
| 186 | 179 | } |
| 187 | 180 | |
| r1112 | r1113 | |
| 202 | 195 | |
| 203 | 196 | void osd_event_set(osd_event *event) |
| 204 | 197 | { |
| 198 | #if 0 |
| 199 | if (osd_compare_exchange32(&event->signalled, FALSE, TRUE) == FALSE) |
| 200 | { |
| 201 | pthread_mutex_lock(&event->mutex); |
| 202 | if (event->autoreset) |
| 203 | pthread_cond_signal(&event->cond); |
| 204 | else |
| 205 | pthread_cond_broadcast(&event->cond); |
| 206 | pthread_mutex_unlock(&event->mutex); |
| 207 | } |
| 208 | #endif |
| 205 | 209 | pthread_mutex_lock(&event->mutex); |
| 206 | | event->signalled = TRUE; |
| 207 | | pthread_cond_broadcast(&event->cond); |
| 210 | if (event->signalled == FALSE) |
| 211 | { |
| 212 | event->signalled = TRUE; |
| 213 | if (event->autoreset) |
| 214 | pthread_cond_signal(&event->cond); |
| 215 | else |
| 216 | pthread_cond_broadcast(&event->cond); |
| 217 | } |
| 208 | 218 | pthread_mutex_unlock(&event->mutex); |
| 209 | 219 | } |
| 210 | 220 | |
| r1112 | r1113 | |
| 230 | 240 | { |
| 231 | 241 | if (!event->signalled) |
| 232 | 242 | { |
| 233 | | pthread_cond_wait(&event->cond, &event->mutex); |
| 243 | pthread_mutex_unlock(&event->mutex); |
| 244 | return FALSE; |
| 234 | 245 | } |
| 235 | | } |
| 246 | } |
| 236 | 247 | else |
| 237 | 248 | { |
| 238 | 249 | struct timespec ts; |
| r1112 | r1113 | |
| 249 | 260 | ts.tv_sec += t; |
| 250 | 261 | |
| 251 | 262 | if (!event->signalled) |
| 263 | { |
| 252 | 264 | if ( pthread_cond_timedwait(&event->cond, &event->mutex, &ts) != 0 ) |
| 253 | 265 | { |
| 254 | 266 | pthread_mutex_unlock(&event->mutex); |
| 255 | 267 | return FALSE; |
| 256 | 268 | } |
| 269 | } |
| 257 | 270 | } |
| 271 | |
| 258 | 272 | if (event->autoreset) |
| 259 | 273 | event->signalled = 0; |
| 274 | |
| 260 | 275 | pthread_mutex_unlock(&event->mutex); |
| 276 | |
| 261 | 277 | return TRUE; |
| 262 | 278 | } |
| 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 | 279 | |
| 288 | 280 | //============================================================ |
| 289 | 281 | // osd_atomic_lock |
| r1112 | r1113 | |
| 312 | 304 | osd_thread *osd_thread_create(osd_thread_callback callback, void *cbparam) |
| 313 | 305 | { |
| 314 | 306 | osd_thread *thread; |
| 307 | pthread_attr_t attr; |
| 315 | 308 | |
| 316 | 309 | thread = (osd_thread *)calloc(1, sizeof(osd_thread)); |
| 317 | | if ( pthread_create(&thread->thread, NULL, callback, cbparam) != 0 ) |
| 310 | pthread_attr_init(&attr); |
| 311 | pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); |
| 312 | if ( pthread_create(&thread->thread, &attr, callback, cbparam) != 0 ) |
| 318 | 313 | { |
| 319 | 314 | free(thread); |
| 320 | 315 | return NULL; |
| r1112 | r1113 | |
| 357 | 352 | // osd_compare_exchange32 |
| 358 | 353 | //============================================================ |
| 359 | 354 | |
| 360 | | #if defined(__i386__) || defined(__x86_64__) |
| 355 | #ifndef osd_compare_exchange32 |
| 361 | 356 | |
| 362 | 357 | INT32 osd_compare_exchange32(INT32 volatile *ptr, INT32 compare, INT32 exchange) |
| 363 | 358 | { |
| 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 | 359 | INT32 ret; |
| 402 | 360 | osd_atomic_lock(); |
| 403 | 361 | |
| r1112 | r1113 | |
| 418 | 376 | // osd_compare_exchange64 |
| 419 | 377 | //============================================================ |
| 420 | 378 | |
| 421 | | #if defined(__x86_64__) |
| 379 | #ifndef osd_compare_exchange64 |
| 422 | 380 | |
| 423 | 381 | INT64 osd_compare_exchange64(INT64 volatile *ptr, INT64 compare, INT64 exchange) |
| 424 | 382 | { |
| 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 | 383 | INT64 ret; |
| 463 | 384 | osd_atomic_lock(); |
| 464 | 385 | |
| r1112 | r1113 | |
| 472 | 393 | osd_atomic_unlock(); |
| 473 | 394 | return ret; |
| 474 | 395 | } |
| 475 | | #endif |
| 476 | 396 | |
| 477 | 397 | #endif |
| 478 | 398 | |
src/osd/sdl/osinline.h
| r1112 | r1113 | |
| 1 | 1 | //============================================================ |
| 2 | 2 | // |
| 3 | | // osinline.h - Win32 inline functions |
| 3 | // osinline.h - GNU C inline functions |
| 4 | 4 | // |
| 5 | 5 | //============================================================ |
| 6 | 6 | |
| r1112 | r1113 | |
| 13 | 13 | // INLINE FUNCTIONS |
| 14 | 14 | //============================================================ |
| 15 | 15 | |
| 16 | //#define YieldProcessor() sched_yield() |
| 17 | //#define YieldProcessor() do {} while (0) |
| 16 | 18 | |
| 19 | #ifndef YieldProcessor |
| 20 | |
| 21 | #if defined(__i386__) || defined(__x86_64__) |
| 22 | |
| 23 | INLINE void osd_yield_processor(void) |
| 24 | { |
| 25 | __asm__ __volatile__ ( " rep ; nop ;" ); |
| 26 | } |
| 27 | |
| 28 | #elif defined(__ppc__) || defined (__PPC__) || defined(__ppc64__) || defined(__PPC64__) |
| 29 | |
| 30 | INLINE void osd_yield_processor(void) |
| 31 | { |
| 32 | __asm__ __volatile__ ( " nop \n" ); |
| 33 | } |
| 34 | |
| 35 | #endif |
| 36 | |
| 37 | #else |
| 38 | #define osd_yield_processor() YieldProcessor() |
| 39 | #endif |
| 40 | |
| 41 | #ifdef __GNUC__ |
| 42 | |
| 43 | #if defined(__i386__) || defined(__x86_64__) |
| 44 | |
| 45 | |
| 46 | //============================================================ |
| 47 | // osd_compare_exchange32 |
| 48 | //============================================================ |
| 49 | #ifndef SDLMAME_WIN32 |
| 50 | ATTR_UNUSED |
| 51 | INLINE INT32 _osd_compare_exchange32(INT32 volatile *ptr, INT32 compare, INT32 exchange) |
| 52 | { |
| 53 | register INT32 ret; |
| 54 | __asm__ __volatile__ ( |
| 55 | " lock ; cmpxchg %[exchange], %[ptr] ;" |
| 56 | : [ptr] "+m" (*ptr) |
| 57 | , [ret] "=a" (ret) |
| 58 | : [compare] "1" (compare) |
| 59 | , [exchange] "q" (exchange) |
| 60 | : "%cc" |
| 61 | ); |
| 62 | return ret; |
| 63 | } |
| 64 | #define osd_compare_exchange32 _osd_compare_exchange32 |
| 65 | |
| 66 | |
| 67 | //============================================================ |
| 68 | // osd_exchange32 |
| 69 | //============================================================ |
| 70 | |
| 71 | ATTR_UNUSED |
| 72 | INLINE INT32 _osd_exchange32(INT32 volatile *ptr, INT32 exchange) |
| 73 | { |
| 74 | register INT32 ret; |
| 75 | __asm__ __volatile__ ( |
| 76 | " lock ; xchg %[exchange], %[ptr] ;" |
| 77 | : [ptr] "+m" (*ptr) |
| 78 | , [ret] "=r" (ret) |
| 79 | : [exchange] "1" (exchange) |
| 80 | ); |
| 81 | return ret; |
| 82 | } |
| 83 | #define osd_exchange32 _osd_exchange32 |
| 84 | #endif |
| 85 | |
| 86 | //============================================================ |
| 87 | // osd_sync_add |
| 88 | //============================================================ |
| 89 | |
| 90 | ATTR_UNUSED |
| 91 | INLINE INT32 _osd_sync_add(INT32 volatile *ptr, INT32 delta) |
| 92 | { |
| 93 | register INT32 ret; |
| 94 | __asm __volatile__ ( |
| 95 | " mov %[delta],%[ret] ;" |
| 96 | " lock ; xadd %[ret],%[ptr] ;" |
| 97 | " add %[delta],%[ret] ;" |
| 98 | : [ptr] "+m" (*ptr) |
| 99 | , [ret] "=&r" (ret) |
| 100 | : [delta] "rmi" (delta) |
| 101 | : "%cc" |
| 102 | ); |
| 103 | return ret; |
| 104 | } |
| 105 | #define osd_sync_add _osd_sync_add |
| 106 | |
| 107 | |
| 108 | //============================================================ |
| 109 | // osd_interlocked_increment |
| 110 | //============================================================ |
| 111 | |
| 112 | ATTR_UNUSED |
| 113 | INLINE INT32 _osd_interlocked_increment(INT32 volatile *ptr) |
| 114 | { |
| 115 | register INT32 ret; |
| 116 | __asm__ __volatile__( |
| 117 | " mov $1,%[ret] ;" |
| 118 | " lock ; xadd %[ret],%[ptr] ;" |
| 119 | " inc %[ret] ;" |
| 120 | : [ptr] "+m" (*ptr) |
| 121 | , [ret] "=&r" (ret) |
| 122 | : |
| 123 | : "%cc" |
| 124 | ); |
| 125 | return ret; |
| 126 | } |
| 127 | #define osd_interlocked_increment _osd_interlocked_increment |
| 128 | |
| 129 | |
| 130 | //============================================================ |
| 131 | // osd_interlocked_decrement |
| 132 | //============================================================ |
| 133 | |
| 134 | ATTR_UNUSED |
| 135 | INLINE INT32 _osd_interlocked_decrement(INT32 volatile *ptr) |
| 136 | { |
| 137 | register INT32 ret; |
| 138 | __asm__ __volatile__( |
| 139 | " mov $-1,%[ret] ;" |
| 140 | " lock ; xadd %[ret],%[ptr] ;" |
| 141 | " dec %[ret] ;" |
| 142 | : [ptr] "+m" (*ptr) |
| 143 | , [ret] "=&r" (ret) |
| 144 | : |
| 145 | : "%cc" |
| 146 | ); |
| 147 | return ret; |
| 148 | } |
| 149 | #define osd_interlocked_decrement _osd_interlocked_decrement |
| 150 | |
| 151 | |
| 152 | #if defined(__x86_64__) |
| 153 | |
| 154 | //============================================================ |
| 155 | // osd_compare_exchange64 |
| 156 | //============================================================ |
| 157 | |
| 158 | ATTR_UNUSED |
| 159 | INLINE INT64 _osd_compare_exchange64(INT64 volatile *ptr, INT64 compare, INT64 exchange) |
| 160 | { |
| 161 | register INT64 ret; |
| 162 | __asm__ __volatile__ ( |
| 163 | " lock ; cmpxchg %[exchange], %[ptr] ;" |
| 164 | : [ptr] "+m" (*ptr) |
| 165 | , [ret] "=a" (ret) |
| 166 | : [compare] "1" (compare) |
| 167 | , [exchange] "q" (exchange) |
| 168 | : "%cc" |
| 169 | ); |
| 170 | return ret; |
| 171 | } |
| 172 | #define osd_compare_exchange64 _osd_compare_exchange64 |
| 173 | |
| 174 | #endif /* __x86_64__ */ |
| 175 | |
| 176 | |
| 177 | #elif defined(__ppc__) || defined(__PPC__) || defined(__ppc64__) || defined(__PPC64__) |
| 178 | |
| 179 | |
| 180 | //============================================================ |
| 181 | // osd_compare_exchange32 |
| 182 | //============================================================ |
| 183 | |
| 184 | ATTR_UNUSED |
| 185 | INLINE INT32 _osd_compare_exchange32(INT32 volatile *ptr, INT32 compare, INT32 exchange) |
| 186 | { |
| 187 | register INT32 ret; |
| 188 | __asm__ __volatile__ ( |
| 189 | "1: lwarx %[ret], 0, %[ptr] \n" |
| 190 | " cmpw %[compare], %[ret] \n" |
| 191 | " bne 2f \n" |
| 192 | " sync \n" |
| 193 | " stwcx. %[exchange], 0, %[ptr] \n" |
| 194 | " bne- 1b \n" |
| 195 | "2: " |
| 196 | : [ret] "=&r" (ret) |
| 197 | : [ptr] "r" (ptr) |
| 198 | , [exchange] "r" (exchange) |
| 199 | , [compare] "r" (compare) |
| 200 | : "cr0" |
| 201 | ); |
| 202 | return ret; |
| 203 | } |
| 204 | #define osd_compare_exchange32 _osd_compare_exchange32 |
| 205 | |
| 206 | |
| 207 | //============================================================ |
| 208 | // osd_exchange32 |
| 209 | //============================================================ |
| 210 | |
| 211 | ATTR_UNUSED |
| 212 | INLINE INT32 _osd_exchange32(INT32 volatile *ptr, INT32 exchange) |
| 213 | { |
| 214 | register INT32 ret; |
| 215 | __asm__ __volatile__ ( |
| 216 | "1: lwarx %[ret], 0, %[ptr] \n" |
| 217 | " sync \n" |
| 218 | " stwcx. %[exchange], 0, %[ptr] \n" |
| 219 | " bne- 1b \n" |
| 220 | : [ret] "=&r" (ret) |
| 221 | : [ptr] "r" (ptr) |
| 222 | , [exchange] "r" (exchange) |
| 223 | : "cr0" |
| 224 | ); |
| 225 | return ret; |
| 226 | } |
| 227 | #define osd_exchange32 _osd_exchange32 |
| 228 | |
| 229 | |
| 230 | //============================================================ |
| 231 | // osd_sync_add |
| 232 | //============================================================ |
| 233 | |
| 234 | ATTR_UNUSED |
| 235 | INLINE INT32 _osd_sync_add(INT32 volatile *ptr, INT32 delta) |
| 236 | { |
| 237 | register INT32 ret; |
| 238 | __asm __volatile__ ( |
| 239 | "1: lwarx %[ret], 0, %[ptr] \n" |
| 240 | " add %[ret], %[ret], %[delta] \n" |
| 241 | " sync \n" |
| 242 | " stwcx. %[ret], 0, %[ptr] \n" |
| 243 | " bne- 1b \n" |
| 244 | : [ret] "=&b" (ret) |
| 245 | : [ptr] "r" (ptr) |
| 246 | , [delta] "r" (delta) |
| 247 | : "cr0" |
| 248 | ); |
| 249 | return ret; |
| 250 | } |
| 251 | #define osd_sync_add _osd_sync_add |
| 252 | |
| 253 | |
| 254 | //============================================================ |
| 255 | // osd_interlocked_increment |
| 256 | //============================================================ |
| 257 | |
| 258 | ATTR_UNUSED |
| 259 | INLINE INT32 _osd_interlocked_increment(INT32 volatile *ptr) |
| 260 | { |
| 261 | register INT32 ret; |
| 262 | __asm__ __volatile__( |
| 263 | "1: lwarx %[ret], 0, %[ptr] \n" |
| 264 | " addi %[ret], %[ret], 1 \n" |
| 265 | " sync \n" |
| 266 | " stwcx. %[ret], 0, %[ptr] \n" |
| 267 | " bne- 1b \n" |
| 268 | : [ret] "=&b" (ret) |
| 269 | : [ptr] "r" (ptr) |
| 270 | : "cr0" |
| 271 | ); |
| 272 | return ret; |
| 273 | } |
| 274 | #define osd_interlocked_increment _osd_interlocked_increment |
| 275 | |
| 276 | |
| 277 | //============================================================ |
| 278 | // osd_interlocked_decrement |
| 279 | //============================================================ |
| 280 | |
| 281 | ATTR_UNUSED |
| 282 | INLINE INT32 _osd_interlocked_decrement(INT32 volatile *ptr) |
| 283 | { |
| 284 | register INT32 ret; |
| 285 | __asm__ __volatile__( |
| 286 | "1: lwarx %[ret], 0, %[ptr] \n" |
| 287 | " addi %[ret], %[ret], -1 \n" |
| 288 | " sync \n" |
| 289 | " stwcx. %[ret], 0, %[ptr] \n" |
| 290 | " bne- 1b \n" |
| 291 | : [ret] "=&b" (ret) |
| 292 | : [ptr] "r" (ptr) |
| 293 | : "cr0" |
| 294 | ); |
| 295 | return ret; |
| 296 | } |
| 297 | #define osd_interlocked_decrement _osd_interlocked_decrement |
| 298 | |
| 299 | |
| 300 | #if defined(__ppc64__) || defined(__PPC64__) |
| 301 | |
| 302 | //============================================================ |
| 303 | // osd_compare_exchange64 |
| 304 | //============================================================ |
| 305 | |
| 306 | ATTR_UNUSED |
| 307 | INLINE INT64 _osd_compare_exchange64(INT64 volatile *ptr, INT64 compare, INT64 exchange) |
| 308 | { |
| 309 | register INT64 ret; |
| 310 | __asm__ __volatile__ ( |
| 311 | "1: ldarx %[ret], 0, %[ptr] \n" |
| 312 | " cmpd %[compare], %[ret] \n" |
| 313 | " bne 2f \n" |
| 314 | " stdcx. %[exchange], 0, %[ptr] \n" |
| 315 | " bne-- 1b \n" |
| 316 | "2: " |
| 317 | : [ret] "=&r" (ret) |
| 318 | : [ptr] "r" (ptr) |
| 319 | , [exchange] "r" (exchange) |
| 320 | , [compare] "r" (compare) |
| 321 | : "cr0" |
| 322 | ); |
| 323 | return ret; |
| 324 | } |
| 325 | #define osd_compare_exchange64 _osd_compare_exchange64 |
| 326 | |
| 327 | #endif /* __ppc64__ || __PPC64__ */ |
| 328 | |
| 329 | |
| 330 | #endif |
| 331 | |
| 332 | #endif /* __GNUC__ */ |
| 333 | |
| 334 | |
| 17 | 335 | #endif /* __OSINLINE__ */ |
src/osd/sdl/sdlwork.c
| r1112 | r1113 | |
| 1 | //============================================================ |
| 1 | 2 | // |
| 2 | 3 | // sdlwork.c - SDL OSD core work item functions |
| 3 | 4 | // |
| r1112 | r1113 | |
| 23 | 24 | #include <pthread.h> |
| 24 | 25 | #include <unistd.h> |
| 25 | 26 | #include "osdcore.h" |
| 27 | #include "osinline.h" |
| 26 | 28 | |
| 27 | 29 | #ifdef SDLMAME_DARWIN |
| 28 | 30 | #include <mach/mach.h> |
| r1112 | r1113 | |
| 40 | 42 | |
| 41 | 43 | |
| 42 | 44 | //============================================================ |
| 45 | // PARAMETERS |
| 46 | //============================================================ |
| 47 | |
| 48 | #define INFINITE (osd_ticks_per_second()*10000) |
| 49 | #define MAX_THREADS (16) |
| 50 | #define SPIN_LOOP_TIME (osd_ticks_per_second() / 1000) |
| 51 | |
| 52 | |
| 53 | |
| 54 | //============================================================ |
| 43 | 55 | // MACROS |
| 44 | 56 | //============================================================ |
| 45 | 57 | |
| 46 | 58 | #if KEEP_STATISTICS |
| 47 | | #define add_to_stat(v,x) do { osd_sync_add((v), (x)); } while (0) |
| 59 | #define add_to_stat(v,x) do { osd_interlocked_add((v), (x)); } while (0) |
| 60 | #define begin_timing(v) do { (v) -= osd_profiling_ticks(); } while (0) |
| 61 | #define end_timing(v) do { (v) += osd_profiling_ticks(); } while (0) |
| 48 | 62 | #else |
| 49 | 63 | #define add_to_stat(v,x) do { } while (0) |
| 64 | #define begin_timing(v) do { } while (0) |
| 65 | #define end_timing(v) do { } while (0) |
| 50 | 66 | #endif |
| 51 | 67 | |
| 52 | | #ifndef YieldProcessor |
| 53 | 68 | |
| 54 | | #if defined(__i386__) || defined(__x86_64__) |
| 55 | 69 | |
| 56 | | INLINE void YieldProcessor(void) |
| 57 | | { |
| 58 | | __asm__ __volatile__ ( " rep ; nop ;" ); |
| 59 | | } |
| 70 | //============================================================ |
| 71 | // TYPE DEFINITIONS |
| 72 | //============================================================ |
| 60 | 73 | |
| 61 | | #elif defined(__ppc__) || defined (__PPC__) || defined(__ppc64__) || defined(__PPC64__) |
| 62 | | |
| 63 | | INLINE void YieldProcessor(void) |
| 74 | typedef struct _scalable_lock scalable_lock; |
| 75 | struct _scalable_lock |
| 64 | 76 | { |
| 65 | | __asm__ __volatile__ ( " nop \n" ); |
| 66 | | } |
| 77 | struct |
| 78 | { |
| 79 | volatile INT32 haslock; // do we have the lock? |
| 80 | UINT8 filler[60]; // assumes a 64-byte cache line |
| 81 | } slot[MAX_THREADS]; // one slot per thread |
| 82 | volatile INT32 nextindex; // index of next slot to use |
| 83 | }; |
| 67 | 84 | |
| 68 | | #endif |
| 69 | 85 | |
| 70 | | #endif |
| 71 | | |
| 72 | | |
| 73 | | |
| 74 | | //============================================================ |
| 75 | | // TYPE DEFINITIONS |
| 76 | | //============================================================ |
| 77 | | |
| 78 | 86 | typedef struct _mame_thread_info mame_thread_info; |
| 79 | 87 | struct _mame_thread_info |
| 80 | 88 | { |
| 81 | 89 | osd_work_queue * queue; // pointer back to the queue |
| 82 | 90 | osd_thread * handle; // handle to the thread |
| 83 | 91 | osd_event * wakeevent; // wake event for the thread |
| 84 | | UINT8 active; // are we actively processing work? |
| 92 | volatile UINT8 active; // are we actively processing work? |
| 93 | |
| 94 | #if KEEP_STATISTICS |
| 95 | osd_ticks_t runtime; |
| 96 | osd_ticks_t spintime; |
| 97 | osd_ticks_t waittime; |
| 98 | #endif |
| 85 | 99 | }; |
| 86 | 100 | |
| 87 | 101 | |
| 88 | 102 | struct _osd_work_queue |
| 89 | 103 | { |
| 90 | | osd_lock * critsect; // critical section protecting the queue |
| 104 | scalable_lock lock; // lock for protecting the queue |
| 91 | 105 | osd_work_item * volatile list; // list of items in the queue |
| 92 | 106 | osd_work_item ** volatile tailptr; // pointer to the tail pointer of work items in the queue |
| 93 | 107 | osd_work_item * volatile free; // free list of work items |
| 94 | 108 | volatile INT32 items; // items in the queue |
| 95 | 109 | volatile INT32 livethreads; // number of live threads |
| 110 | volatile UINT8 waiting; // is someone waiting on the queue to complete? |
| 111 | volatile UINT8 exiting; // should the threads exit on their next opportunity? |
| 96 | 112 | UINT32 threads; // number of threads in this queue |
| 97 | 113 | UINT32 flags; // creation flags |
| 98 | 114 | 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 |
| 115 | osd_event * doneevent; // event signalled when work is complete |
| 102 | 116 | |
| 103 | 117 | #if KEEP_STATISTICS |
| 104 | 118 | volatile INT32 itemsqueued; // total items queued |
| r1112 | r1113 | |
| 118 | 132 | void * result; // callback result |
| 119 | 133 | osd_event * event; // event signalled when complete |
| 120 | 134 | UINT32 flags; // creation flags |
| 121 | | volatile INT32 refcount; // reference count of threads processing |
| 135 | volatile UINT8 done; // is the item done? |
| 122 | 136 | }; |
| 123 | 137 | |
| 124 | 138 | |
| r1112 | r1113 | |
| 130 | 144 | static int effective_num_processors(void); |
| 131 | 145 | static void * worker_thread_entry(void *param); |
| 132 | 146 | static void worker_thread_process(osd_work_queue *queue, mame_thread_info *thread); |
| 133 | | static void execute_work_item(osd_work_item *item); |
| 134 | 147 | |
| 135 | 148 | //============================================================ |
| 149 | // INLINE FUNCTIONS |
| 150 | //============================================================ |
| 151 | |
| 152 | #ifndef osd_exchange32 |
| 153 | INLINE INT32 osd_exchange32(INT32 volatile *ptr, INT32 exchange) |
| 154 | { |
| 155 | INT32 origvalue; |
| 156 | do { |
| 157 | origvalue = *ptr; |
| 158 | } while (osd_compare_exchange32(ptr, origvalue, exchange) != origvalue); |
| 159 | return origvalue; |
| 160 | } |
| 161 | #endif |
| 162 | |
| 163 | #ifndef osd_interlocked_increment |
| 164 | INLINE INT32 osd_interlocked_increment(INT32 volatile *ptr) |
| 165 | { |
| 166 | return osd_sync_add(ptr, 1); |
| 167 | } |
| 168 | #endif |
| 169 | |
| 170 | #ifndef osd_interlocked_decrement |
| 171 | INLINE INT32 osd_interlocked_decrement(INT32 volatile *ptr) |
| 172 | { |
| 173 | return osd_sync_add(ptr, -1); |
| 174 | } |
| 175 | #endif |
| 176 | |
| 177 | #ifndef osd_interlocked_add |
| 178 | INLINE INT32 osd_interlocked_add(INT32 volatile *ptr, INT32 add) |
| 179 | { |
| 180 | return osd_sync_add(ptr, add); |
| 181 | } |
| 182 | #endif |
| 183 | |
| 184 | INLINE void scalable_lock_init(scalable_lock *lock) |
| 185 | { |
| 186 | memset(lock, 0, sizeof(*lock)); |
| 187 | lock->slot[0].haslock = TRUE; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | INT32 scalable_lock_acquire(scalable_lock *lock) |
| 192 | { |
| 193 | INT32 myslot = (osd_interlocked_increment(&lock->nextindex) - 1) & (MAX_THREADS - 1); |
| 194 | INT32 backoff = 1; |
| 195 | |
| 196 | while (!osd_compare_exchange32(&lock->slot[myslot].haslock, TRUE, FALSE)) |
| 197 | { |
| 198 | INT32 backcount; |
| 199 | for (backcount = 0; backcount < backoff; backcount++) |
| 200 | osd_yield_processor(); |
| 201 | backoff <<= 1; |
| 202 | } |
| 203 | return myslot; |
| 204 | } |
| 205 | |
| 206 | void scalable_lock_release(scalable_lock *lock, INT32 myslot) |
| 207 | { |
| 208 | osd_exchange32(&lock->slot[(myslot + 1) & (MAX_THREADS - 1)].haslock, TRUE); |
| 209 | } |
| 210 | |
| 211 | |
| 212 | //============================================================ |
| 136 | 213 | // osd_work_queue_alloc |
| 137 | 214 | //============================================================ |
| 138 | 215 | |
| r1112 | r1113 | |
| 153 | 230 | queue->flags = flags; |
| 154 | 231 | |
| 155 | 232 | // allocate events for the queue |
| 156 | | queue->workevent = osd_event_alloc(FALSE, FALSE); // auto-reset, not signalled |
| 157 | 233 | 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) |
| 234 | if (queue->doneevent == NULL) |
| 160 | 235 | goto error; |
| 161 | 236 | |
| 162 | 237 | // initialize the critical section |
| 163 | | if ( (queue->critsect = osd_lock_alloc()) == NULL ) |
| 164 | | goto error; |
| 238 | scalable_lock_init(&queue->lock); |
| 165 | 239 | |
| 166 | 240 | // determine how many threads to create... |
| 167 | 241 | // on a single-CPU system, create 1 thread for I/O queues, and 0 threads for everything else |
| r1112 | r1113 | |
| 172 | 246 | else |
| 173 | 247 | queue->threads = (flags & WORK_QUEUE_FLAG_MULTI) ? (numprocs - 1) : 1; |
| 174 | 248 | |
| 175 | | // if we have threads, create them |
| 176 | | if (queue->threads > 0) |
| 249 | // clamp to the maximum |
| 250 | queue->threads = MIN(queue->threads, MAX_THREADS); |
| 251 | |
| 252 | // allocate memory for thread array (+1 to count the calling thread) |
| 253 | queue->thread = malloc((queue->threads + 1) * sizeof(queue->thread[0])); |
| 254 | if (queue->thread == NULL) |
| 255 | goto error; |
| 256 | memset(queue->thread, 0, (queue->threads + 1) * sizeof(queue->thread[0])); |
| 257 | |
| 258 | // iterate over threads |
| 259 | for (threadnum = 0; threadnum < queue->threads; threadnum++) |
| 177 | 260 | { |
| 178 | | // allocate memory for thread array |
| 179 | | queue->thread = malloc(queue->threads * sizeof(queue->thread[0])); |
| 180 | | if (queue->thread == NULL) |
| 181 | | goto error; |
| 182 | | memset(queue->thread, 0, queue->threads * sizeof(queue->thread[0])); |
| 261 | mame_thread_info *thread = &queue->thread[threadnum]; |
| 183 | 262 | |
| 184 | | // iterate over threads |
| 185 | | for (threadnum = 0; threadnum < queue->threads; threadnum++) |
| 186 | | { |
| 187 | | mame_thread_info *thread = &queue->thread[threadnum]; |
| 263 | // set a pointer back to the queue |
| 264 | thread->queue = queue; |
| 188 | 265 | |
| 189 | | // set a pointer back to the queue |
| 190 | | thread->queue = queue; |
| 266 | // create the per-thread wake event |
| 267 | thread->wakeevent = osd_event_alloc(FALSE, FALSE); // auto-reset, not signalled |
| 268 | if (thread->wakeevent == NULL) |
| 269 | goto error; |
| 191 | 270 | |
| 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; |
| 271 | // create the thread |
| 272 | thread->handle = osd_thread_create(worker_thread_entry, thread); |
| 273 | if (thread->handle == NULL) |
| 274 | goto error; |
| 196 | 275 | |
| 197 | | // create the thread |
| 198 | | thread->handle = osd_thread_create(worker_thread_entry, thread); |
| 199 | | if (thread->handle == NULL) |
| 200 | | goto error; |
| 276 | // set its priority: I/O threads get high priority because they are assumed to be |
| 277 | // blocked most of the time; other threads just match the creator's priority |
| 278 | if (flags & WORK_QUEUE_FLAG_IO) |
| 279 | osd_thread_adjust_priority(thread->handle, 0); // TODO: specify appropriate priority |
| 280 | else |
| 281 | osd_thread_adjust_priority(thread->handle, 0); // TODO: specify appropriate priority |
| 282 | } |
| 201 | 283 | |
| 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 |
| 204 | | if (flags & WORK_QUEUE_FLAG_IO) |
| 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 |
| 208 | | } |
| 209 | | } |
| 284 | // start a timer going for "waittime" on the main thread |
| 285 | begin_timing(queue->thread[queue->threads].waittime); |
| 210 | 286 | return queue; |
| 211 | 287 | |
| 212 | 288 | error: |
| r1112 | r1113 | |
| 243 | 319 | // if this is a multi queue, help out rather than doing nothing |
| 244 | 320 | if (queue->flags & WORK_QUEUE_FLAG_MULTI) |
| 245 | 321 | { |
| 246 | | osd_ticks_t stopspin = osd_ticks() + timeout; |
| 322 | mame_thread_info *thread = &queue->thread[queue->threads]; |
| 247 | 323 | |
| 324 | end_timing(thread->waittime); |
| 325 | |
| 248 | 326 | // process what we can as a worker thread |
| 249 | | worker_thread_process(queue, NULL); |
| 327 | worker_thread_process(queue, thread); |
| 250 | 328 | |
| 251 | | // spin until we hit 0 items and then return TRUE |
| 252 | | while (queue->items != 0 && osd_ticks() < stopspin) |
| 253 | | YieldProcessor(); |
| 329 | begin_timing(thread->waittime); |
| 254 | 330 | return TRUE; |
| 255 | 331 | } |
| 256 | 332 | |
| 257 | 333 | // reset our done event and double-check the items before waiting |
| 258 | 334 | osd_event_reset(queue->doneevent); |
| 259 | | if (queue->items == 0) |
| 260 | | return TRUE; |
| 335 | queue->waiting = TRUE; |
| 336 | if (queue->items != 0) |
| 337 | { |
| 338 | osd_event_wait(queue->doneevent, timeout); |
| 339 | } |
| 340 | queue->waiting = FALSE; |
| 261 | 341 | |
| 262 | | // wait for the collection to be signalled |
| 263 | | return (osd_event_wait(queue->doneevent, timeout)); |
| 342 | // return TRUE if we actually hit 0 |
| 343 | return (queue->items == 0); |
| 264 | 344 | } |
| 265 | 345 | |
| 266 | 346 | |
| r1112 | r1113 | |
| 275 | 355 | { |
| 276 | 356 | int threadnum; |
| 277 | 357 | |
| 358 | // stop the timer for "waittime" on the main thread |
| 359 | end_timing(queue->thread[queue->threads].waittime); |
| 360 | |
| 278 | 361 | // signal all the threads to exit |
| 279 | | osd_event_set(queue->exitevent); |
| 362 | //osd_work_queue_wait(queue, osd_ticks_per_second()*10); |
| 363 | queue->exiting = TRUE; |
| 364 | for (threadnum = 0; threadnum < queue->threads; threadnum++) |
| 365 | { |
| 366 | mame_thread_info *thread = &queue->thread[threadnum]; |
| 367 | if (thread->wakeevent != NULL) |
| 368 | osd_event_set(thread->wakeevent); |
| 369 | } |
| 280 | 370 | |
| 281 | 371 | // wait for all the threads to go away |
| 282 | 372 | for (threadnum = 0; threadnum < queue->threads; threadnum++) |
| r1112 | r1113 | |
| 294 | 384 | osd_event_free(thread->wakeevent); |
| 295 | 385 | } |
| 296 | 386 | |
| 387 | #if KEEP_STATISTICS |
| 388 | // output per-thread statistics |
| 389 | for (threadnum = 0; threadnum <= queue->threads; threadnum++) |
| 390 | { |
| 391 | mame_thread_info *thread = &queue->thread[threadnum]; |
| 392 | osd_ticks_t total = thread->runtime + thread->waittime + thread->spintime; |
| 393 | printf("Thread %d: run=%5.2f%% spin=%5.2f%% wait/other=%5.2f%%\n", |
| 394 | threadnum, |
| 395 | (double)thread->runtime * 100.0 / (double)total, |
| 396 | (double)thread->spintime * 100.0 / (double)total, |
| 397 | (double)thread->waittime * 100.0 / (double)total); |
| 398 | } |
| 399 | #endif |
| 400 | |
| 297 | 401 | // free the list |
| 298 | 402 | free(queue->thread); |
| 299 | 403 | } |
| 300 | 404 | |
| 301 | 405 | // free all the events |
| 302 | | if (queue->workevent != NULL) |
| 303 | | osd_event_free(queue->workevent); |
| 304 | 406 | if (queue->doneevent != NULL) |
| 305 | 407 | osd_event_free(queue->doneevent); |
| 306 | | if (queue->exitevent != NULL) |
| 307 | | osd_event_free(queue->exitevent); |
| 308 | 408 | |
| 309 | | // free the critical section |
| 310 | | osd_lock_free(queue->critsect); |
| 311 | | |
| 312 | 409 | // free all items in the free list |
| 313 | 410 | while (queue->free != NULL) |
| 314 | 411 | { |
| r1112 | r1113 | |
| 342 | 439 | |
| 343 | 440 | |
| 344 | 441 | //============================================================ |
| 345 | | // osd_work_item_queue |
| 442 | // osd_work_item_queue_multiple |
| 346 | 443 | //============================================================ |
| 347 | 444 | |
| 348 | | osd_work_item *osd_work_item_queue(osd_work_queue *queue, osd_work_callback callback, void *param, UINT32 flags) |
| 445 | osd_work_item *osd_work_item_queue_multiple(osd_work_queue *queue, osd_work_callback callback, INT32 numitems, void *parambase, INT32 paramstep, UINT32 flags) |
| 349 | 446 | { |
| 350 | | osd_work_item *item; |
| 447 | osd_work_item *itemlist = NULL; |
| 448 | osd_work_item **item_tailptr = &itemlist; |
| 449 | INT32 lockslot; |
| 450 | int itemnum; |
| 351 | 451 | |
| 352 | | // first allocate a new work item; try the free list first |
| 353 | | do |
| 452 | // loop over items, building up a local list of work |
| 453 | for (itemnum = 0; itemnum < numitems; itemnum++) |
| 354 | 454 | { |
| 355 | | item = (osd_work_item *)queue->free; |
| 356 | | } while (item != NULL && osd_compare_exchange_ptr((void * volatile *)&queue->free, item, item->next) != item); |
| 455 | osd_work_item *item; |
| 357 | 456 | |
| 358 | | // if nothing, allocate something new |
| 359 | | if (item == NULL) |
| 360 | | { |
| 361 | | // allocate the item |
| 362 | | item = malloc(sizeof(*item)); |
| 457 | // first allocate a new work item; try the free list first |
| 458 | do |
| 459 | { |
| 460 | item = (osd_work_item *)queue->free; |
| 461 | } while (item != NULL && osd_compare_exchange_ptr((void * volatile *)&queue->free, item, item->next) != item); |
| 462 | |
| 463 | // if nothing, allocate something new |
| 363 | 464 | if (item == NULL) |
| 364 | | return NULL; |
| 365 | | item->event = NULL; |
| 366 | | item->queue = queue; |
| 465 | { |
| 466 | // allocate the item |
| 467 | item = malloc(sizeof(*item)); |
| 468 | if (item == NULL) |
| 469 | return NULL; |
| 470 | item->event = NULL; |
| 471 | item->queue = queue; |
| 472 | } |
| 473 | |
| 474 | // fill in the basics |
| 475 | item->next = NULL; |
| 476 | item->callback = callback; |
| 477 | item->param = parambase; |
| 478 | item->result = NULL; |
| 479 | item->flags = flags; |
| 480 | item->done = FALSE; |
| 481 | |
| 482 | // advance to the next |
| 483 | *item_tailptr = item; |
| 484 | item_tailptr = &item->next; |
| 485 | parambase = (UINT8 *)parambase + paramstep; |
| 367 | 486 | } |
| 368 | 487 | |
| 369 | | // fill in the basics |
| 370 | | item->next = NULL; |
| 371 | | item->callback = callback; |
| 372 | | item->param = param; |
| 373 | | item->result = NULL; |
| 374 | | item->flags = flags; |
| 375 | | item->refcount = 1; |
| 376 | | if (item->event != NULL) |
| 377 | | osd_event_reset(item->event); |
| 488 | // enqueue the whole thing within the critical section |
| 489 | lockslot = scalable_lock_acquire(&queue->lock); |
| 490 | *queue->tailptr = itemlist; |
| 491 | queue->tailptr = item_tailptr; |
| 492 | scalable_lock_release(&queue->lock, lockslot); |
| 378 | 493 | |
| 379 | | // if no threads, just run it now |
| 380 | | if (queue->threads == 0) |
| 381 | | { |
| 382 | | // execute the callback and clear the refcount back to 0 |
| 383 | | item->result = (*item->callback)(item->param); |
| 384 | | item->refcount = 0; |
| 494 | // increment the number of items in the queue |
| 495 | osd_interlocked_add(&queue->items, numitems); |
| 496 | add_to_stat(&queue->itemsqueued, numitems); |
| 385 | 497 | |
| 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 | | } |
| 390 | | |
| 391 | | // otherwise, enqueue the item and wake the threads |
| 392 | | else |
| 498 | // look for free threads to do the work |
| 499 | if (queue->livethreads < queue->threads) |
| 393 | 500 | { |
| 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); |
| 501 | int threadnum; |
| 399 | 502 | |
| 400 | | // increment the number of items in the queue |
| 401 | | osd_sync_add(&queue->items, 1); |
| 402 | | add_to_stat(&queue->itemsqueued, 1); |
| 503 | // iterate over all the threads |
| 504 | for (threadnum = 0; threadnum < queue->threads; threadnum++) |
| 505 | { |
| 506 | mame_thread_info *thread = &queue->thread[threadnum]; |
| 403 | 507 | |
| 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) |
| 508 | // if this thread is not active, wake him up |
| 509 | if (!thread->active) |
| 409 | 510 | { |
| 410 | | osd_event_set(queue->workevent); |
| 511 | osd_event_set(thread->wakeevent); |
| 411 | 512 | add_to_stat(&queue->setevents, 1); |
| 513 | |
| 514 | // for non-shared, the first one we find is good enough |
| 515 | if (--numitems == 0) |
| 516 | break; |
| 412 | 517 | } |
| 413 | 518 | } |
| 519 | } |
| 414 | 520 | |
| 415 | | // shared case |
| 416 | | else |
| 417 | | { |
| 418 | | int threadnum; |
| 521 | // if no threads, run the queue now on this thread |
| 522 | if (queue->threads == 0) |
| 523 | worker_thread_process(queue, &queue->thread[0]); |
| 419 | 524 | |
| 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 | 525 | // only return the item if it won't get released automatically |
| 431 | | return (flags & WORK_ITEM_FLAG_AUTO_RELEASE) ? NULL : item; |
| 526 | //return (flags & WORK_ITEM_FLAG_AUTO_RELEASE) ? NULL : *item_tailptr; |
| 527 | return (flags & WORK_ITEM_FLAG_AUTO_RELEASE) ? NULL : itemlist; |
| 432 | 528 | } |
| 433 | 529 | |
| 434 | 530 | |
| r1112 | r1113 | |
| 439 | 535 | int osd_work_item_wait(osd_work_item *item, osd_ticks_t timeout) |
| 440 | 536 | { |
| 441 | 537 | // if we're done already, just return |
| 442 | | if (item->refcount == 0) |
| 538 | if (item->done) |
| 443 | 539 | return TRUE; |
| 444 | 540 | |
| 445 | 541 | // if we don't have an event, create one |
| 446 | 542 | if (item->event == NULL) |
| 447 | 543 | item->event = osd_event_alloc(TRUE, FALSE); // manual reset, not signalled |
| 544 | else |
| 545 | osd_event_reset(item->event); |
| 448 | 546 | |
| 449 | 547 | // if we don't have an event, we need to spin (shouldn't ever really happen) |
| 450 | | if (item->event != NULL) |
| 548 | if (item->event == NULL) |
| 451 | 549 | { |
| 452 | 550 | osd_ticks_t stopspin = osd_ticks() + timeout; |
| 453 | | while (item->refcount != 0 && osd_ticks() < stopspin) |
| 454 | | YieldProcessor(); |
| 551 | while (!item->done && osd_ticks() < stopspin) |
| 552 | osd_yield_processor(); |
| 455 | 553 | } |
| 456 | 554 | |
| 457 | 555 | // otherwise, block on the event until done |
| 458 | | else if (item->refcount != 0) |
| 556 | else if (!item->done) |
| 459 | 557 | osd_event_wait(item->event, timeout); |
| 460 | 558 | |
| 461 | 559 | // return TRUE if the refcount actually hit 0 |
| 462 | | return (item->refcount == 0); |
| 560 | return item->done; |
| 463 | 561 | } |
| 464 | 562 | |
| 465 | 563 | |
| r1112 | r1113 | |
| 520 | 618 | { |
| 521 | 619 | mame_thread_info *thread = param; |
| 522 | 620 | osd_work_queue *queue = thread->queue; |
| 523 | | osd_event *hlist[3]; |
| 524 | 621 | |
| 525 | | // fill in our handle list |
| 526 | | hlist[0] = queue->workevent; |
| 527 | | hlist[1] = thread->wakeevent; |
| 528 | | hlist[2] = queue->exitevent; |
| 529 | | |
| 530 | 622 | // loop until we exit |
| 531 | 623 | for ( ;; ) |
| 532 | 624 | { |
| 533 | 625 | // block waiting for work or exit |
| 534 | | int result = 0; |
| 535 | | |
| 536 | 626 | // 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 | if (!queue->exiting && queue->items == 0) |
| 628 | { |
| 629 | begin_timing(thread->waittime); |
| 630 | osd_event_wait(thread->wakeevent, INFINITE); |
| 631 | end_timing(thread->waittime); |
| 632 | } |
| 633 | if (queue->exiting) |
| 540 | 634 | break; |
| 541 | 635 | |
| 542 | 636 | // indicate that we are live |
| 543 | 637 | thread->active = TRUE; |
| 544 | | osd_sync_add(&queue->livethreads, 1); |
| 638 | osd_interlocked_increment(&queue->livethreads); |
| 545 | 639 | |
| 546 | 640 | // process work items |
| 547 | 641 | for ( ;; ) |
| r1112 | r1113 | |
| 552 | 646 | worker_thread_process(queue, thread); |
| 553 | 647 | |
| 554 | 648 | // spin for a while looking for more work |
| 555 | | stopspin = osd_ticks() + osd_ticks_per_second() / 1000; |
| 649 | begin_timing(thread->spintime); |
| 650 | stopspin = osd_ticks() + SPIN_LOOP_TIME; |
| 556 | 651 | while (queue->items == 0 && osd_ticks() < stopspin) |
| 557 | | YieldProcessor(); |
| 652 | osd_yield_processor(); |
| 653 | end_timing(thread->spintime); |
| 558 | 654 | |
| 559 | 655 | // if nothing more, release the processor |
| 560 | 656 | if (queue->items == 0) |
| r1112 | r1113 | |
| 564 | 660 | |
| 565 | 661 | // decrement the live thread count |
| 566 | 662 | thread->active = FALSE; |
| 567 | | osd_sync_add(&queue->livethreads, -1); |
| 663 | osd_interlocked_decrement(&queue->livethreads); |
| 568 | 664 | } |
| 569 | | //return 0; |
| 570 | 665 | return NULL; |
| 571 | 666 | } |
| 572 | 667 | |
| r1112 | r1113 | |
| 577 | 672 | |
| 578 | 673 | static void worker_thread_process(osd_work_queue *queue, mame_thread_info *thread) |
| 579 | 674 | { |
| 675 | begin_timing(thread->runtime); |
| 676 | |
| 580 | 677 | // loop until everything is processed |
| 581 | 678 | while (queue->items != 0) |
| 582 | 679 | { |
| 583 | | int removed_item = FALSE; |
| 584 | 680 | osd_work_item *item; |
| 681 | INT32 lockslot; |
| 585 | 682 | |
| 586 | 683 | // use a critical section to synchronize the removal of items |
| 587 | | osd_lock_acquire(queue->critsect); |
| 684 | lockslot = scalable_lock_acquire(&queue->lock); |
| 588 | 685 | { |
| 686 | // pull the item from the queue |
| 589 | 687 | item = (osd_work_item *)queue->list; |
| 590 | 688 | |
| 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)) |
| 689 | if (item != NULL) |
| 593 | 690 | { |
| 594 | | removed_item = TRUE; |
| 595 | 691 | queue->list = item->next; |
| 596 | | if (item->next == NULL) |
| 692 | if (queue->list == NULL) |
| 597 | 693 | queue->tailptr = (osd_work_item **)&queue->list; |
| 598 | 694 | } |
| 599 | 695 | } |
| 600 | | osd_lock_release(queue->critsect); |
| 696 | scalable_lock_release(&queue->lock, lockslot); |
| 601 | 697 | |
| 602 | | // call the callback and signal its event |
| 603 | | if (item != NULL && item->refcount != 0) |
| 604 | | execute_work_item(item); |
| 605 | | |
| 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) |
| 698 | // process non-NULL items |
| 699 | if (item != NULL) |
| 608 | 700 | { |
| 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); |
| 701 | // call the callback and stash the result |
| 702 | item->result = (*item->callback)(item->param); |
| 703 | osd_interlocked_decrement(&queue->items); |
| 704 | item->done = TRUE; |
| 612 | 705 | |
| 613 | 706 | // if it's an auto-release item, release it |
| 614 | 707 | if (item->flags & WORK_ITEM_FLAG_AUTO_RELEASE) |
| 615 | 708 | osd_work_item_release(item); |
| 709 | |
| 710 | // set the result and signal the event |
| 711 | else if (item->event != NULL) |
| 712 | { |
| 713 | osd_event_set(item->event); |
| 714 | add_to_stat(&item->queue->setevents, 1); |
| 715 | } |
| 716 | |
| 717 | // if we removed an item and there's still work to do, bump the stats |
| 718 | if (queue->items != 0) |
| 719 | add_to_stat(&queue->extraitems, 1); |
| 616 | 720 | } |
| 617 | | |
| 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 | 721 | } |
| 622 | | } |
| 623 | 722 | |
| 624 | | |
| 625 | | //============================================================ |
| 626 | | // execute_work_item |
| 627 | | //============================================================ |
| 628 | | |
| 629 | | static void execute_work_item(osd_work_item *item) |
| 630 | | { |
| 631 | | void *result; |
| 632 | | |
| 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) |
| 723 | // we don't need to set the doneevent for multi queues because they spin |
| 724 | if (queue->waiting) |
| 635 | 725 | { |
| 636 | | item->refcount = 0; |
| 637 | | return; |
| 726 | osd_event_set(queue->doneevent); |
| 727 | add_to_stat(&queue->setevents, 1); |
| 638 | 728 | } |
| 639 | 729 | |
| 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 | | } |
| 730 | end_timing(thread->runtime); |
| 651 | 731 | } |
| 652 | 732 | |
| 653 | 733 | #endif // SDLMAME_WIN32 |