src/osd/sdl/sdlwork.c
| r1299 | r1300 | |
| 16 | 16 | #include "os2work.c" |
| 17 | 17 | #else |
| 18 | 18 | |
| 19 | | // standard headers |
| 20 | | #include <time.h> |
| 21 | | #if defined(SDLMAME_UNIX) && !defined(SDLMAME_DARWIN) |
| 22 | | #include <sys/time.h> |
| 23 | | #endif |
| 24 | | #include <pthread.h> |
| 25 | | #include <unistd.h> |
| 26 | 19 | #include "osdcore.h" |
| 27 | 20 | #include "osinline.h" |
| 28 | 21 | |
| r1299 | r1300 | |
| 48 | 41 | //============================================================ |
| 49 | 42 | |
| 50 | 43 | #define INFINITE (osd_ticks_per_second()*10000) |
| 51 | | #define MAX_THREADS (16) |
| 52 | 44 | #define SPIN_LOOP_TIME (osd_ticks_per_second() / 1000) |
| 53 | 45 | |
| 54 | 46 | |
| r1299 | r1300 | |
| 58 | 50 | //============================================================ |
| 59 | 51 | |
| 60 | 52 | #if KEEP_STATISTICS |
| 61 | | #define add_to_stat(v,x) do { atomic_add32((v), (x)); } while (0) |
| 53 | #define add_to_stat(v,x) do { interlocked_add((v), (x)); } while (0) |
| 62 | 54 | #define begin_timing(v) do { (v) -= osd_profiling_ticks(); } while (0) |
| 63 | 55 | #define end_timing(v) do { (v) += osd_profiling_ticks(); } while (0) |
| 64 | 56 | #else |
| r1299 | r1300 | |
| 79 | 71 | struct |
| 80 | 72 | { |
| 81 | 73 | volatile INT32 haslock; // do we have the lock? |
| 82 | | UINT8 filler[60]; // assumes a 64-byte cache line |
| 83 | | } slot[MAX_THREADS]; // one slot per thread |
| 74 | INT32 filler[64/4-1]; // assumes a 64-byte cache line |
| 75 | } slot[WORK_MAX_THREADS]; // one slot per thread |
| 84 | 76 | volatile INT32 nextindex; // index of next slot to use |
| 85 | 77 | }; |
| 86 | 78 | |
| 87 | 79 | |
| 88 | | typedef struct _mame_thread_info mame_thread_info; |
| 89 | | struct _mame_thread_info |
| 80 | typedef struct _work_thread_info work_thread_info; |
| 81 | struct _work_thread_info |
| 90 | 82 | { |
| 91 | 83 | osd_work_queue * queue; // pointer back to the queue |
| 92 | 84 | osd_thread * handle; // handle to the thread |
| r1299 | r1300 | |
| 115 | 107 | volatile UINT8 exiting; // should the threads exit on their next opportunity? |
| 116 | 108 | UINT32 threads; // number of threads in this queue |
| 117 | 109 | UINT32 flags; // creation flags |
| 118 | | mame_thread_info * thread; // array of thread information |
| 110 | work_thread_info * thread; // array of thread information |
| 119 | 111 | osd_event * doneevent; // event signalled when work is complete |
| 120 | 112 | |
| 121 | 113 | #if KEEP_STATISTICS |
| r1299 | r1300 | |
| 139 | 131 | volatile INT32 done; // is the item done? |
| 140 | 132 | }; |
| 141 | 133 | |
| 134 | typedef void *PVOID; |
| 142 | 135 | |
| 143 | | |
| 144 | 136 | //============================================================ |
| 145 | 137 | // FUNCTION PROTOTYPES |
| 146 | 138 | //============================================================ |
| 147 | 139 | |
| 148 | 140 | static int effective_num_processors(void); |
| 149 | 141 | static void * worker_thread_entry(void *param); |
| 150 | | static void worker_thread_process(osd_work_queue *queue, mame_thread_info *thread); |
| 142 | static void worker_thread_process(osd_work_queue *queue, work_thread_info *thread); |
| 151 | 143 | |
| 152 | 144 | //============================================================ |
| 153 | 145 | // INLINE FUNCTIONS |
| 154 | 146 | //============================================================ |
| 155 | 147 | |
| 156 | | #ifndef osd_interlocked_increment |
| 157 | | INLINE INT32 osd_interlocked_increment(INT32 volatile *ptr) |
| 148 | INLINE INT32 interlocked_exchange32(INT32 volatile *ptr, INT32 value) |
| 158 | 149 | { |
| 159 | | return atomic_add32(ptr, 1); |
| 150 | return atomic_exchange32(ptr, value); |
| 160 | 151 | } |
| 161 | | #endif |
| 162 | 152 | |
| 163 | | #ifndef osd_interlocked_decrement |
| 164 | | INLINE INT32 osd_interlocked_decrement(INT32 volatile *ptr) |
| 153 | INLINE INT32 interlocked_increment(INT32 volatile *ptr) |
| 165 | 154 | { |
| 166 | | return atomic_add32(ptr, -1); |
| 155 | return atomic_increment32(ptr); |
| 167 | 156 | } |
| 168 | | #endif |
| 169 | 157 | |
| 158 | |
| 159 | INLINE INT32 interlocked_decrement(INT32 volatile *ptr) |
| 160 | { |
| 161 | return atomic_decrement32(ptr); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | INLINE INT32 interlocked_add(INT32 volatile *ptr, INT32 add) |
| 166 | { |
| 167 | return atomic_add32(ptr, add); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | |
| 172 | //============================================================ |
| 173 | // Scalable Locks |
| 174 | //============================================================ |
| 175 | |
| 170 | 176 | INLINE void scalable_lock_init(scalable_lock *lock) |
| 171 | 177 | { |
| 172 | 178 | memset(lock, 0, sizeof(*lock)); |
| r1299 | r1300 | |
| 176 | 182 | |
| 177 | 183 | INLINE INT32 scalable_lock_acquire(scalable_lock *lock) |
| 178 | 184 | { |
| 179 | | INT32 myslot = (osd_interlocked_increment(&lock->nextindex) - 1) & (MAX_THREADS - 1); |
| 185 | INT32 myslot = (atomic_increment32(&lock->nextindex) - 1) & (WORK_MAX_THREADS - 1); |
| 180 | 186 | |
| 181 | 187 | #if defined(__i386__) || defined(__x86_64__) |
| 182 | 188 | register INT32 tmp; |
| r1299 | r1300 | |
| 230 | 236 | return myslot; |
| 231 | 237 | } |
| 232 | 238 | |
| 239 | |
| 233 | 240 | INLINE void scalable_lock_release(scalable_lock *lock, INT32 myslot) |
| 234 | 241 | { |
| 235 | 242 | #if defined(__i386__) || defined(__x86_64__) |
| 236 | 243 | register INT32 tmp = TRUE; |
| 237 | 244 | __asm__ __volatile__ ( |
| 238 | 245 | " xchg %[haslock], %[tmp] ;" |
| 239 | | : [haslock] "+m" (lock->slot[(myslot + 1) & (MAX_THREADS - 1)].haslock) |
| 246 | : [haslock] "+m" (lock->slot[(myslot + 1) & (WORK_MAX_THREADS - 1)].haslock) |
| 240 | 247 | , [tmp] "+r" (tmp) |
| 241 | 248 | : |
| 242 | 249 | ); |
| 243 | 250 | #elif defined(__ppc__) || defined (__PPC__) || defined(__ppc64__) || defined(__PPC64__) |
| 244 | | lock->slot[(myslot + 1) & (MAX_THREADS - 1)].haslock = TRUE; |
| 251 | lock->slot[(myslot + 1) & (WORK_MAX_THREADS - 1)].haslock = TRUE; |
| 245 | 252 | __asm__ __volatile__ ( " eieio " : : ); |
| 246 | 253 | #else |
| 247 | | osd_exchange32(&lock->slot[(myslot + 1) & (MAX_THREADS - 1)].haslock, TRUE); |
| 254 | osd_exchange32(&lock->slot[(myslot + 1) & (WORK_MAX_THREADS - 1)].haslock, TRUE); |
| 248 | 255 | #endif |
| 249 | 256 | } |
| 250 | 257 | |
| r1299 | r1300 | |
| 287 | 294 | queue->threads = (flags & WORK_QUEUE_FLAG_MULTI) ? (numprocs - 1) : 1; |
| 288 | 295 | |
| 289 | 296 | // clamp to the maximum |
| 290 | | queue->threads = MIN(queue->threads, MAX_THREADS); |
| 297 | queue->threads = MIN(queue->threads, WORK_MAX_THREADS); |
| 291 | 298 | |
| 292 | 299 | // allocate memory for thread array (+1 to count the calling thread) |
| 293 | 300 | queue->thread = malloc((queue->threads + 1) * sizeof(queue->thread[0])); |
| r1299 | r1300 | |
| 298 | 305 | // iterate over threads |
| 299 | 306 | for (threadnum = 0; threadnum < queue->threads; threadnum++) |
| 300 | 307 | { |
| 301 | | mame_thread_info *thread = &queue->thread[threadnum]; |
| 308 | work_thread_info *thread = &queue->thread[threadnum]; |
| 302 | 309 | |
| 303 | 310 | // set a pointer back to the queue |
| 304 | 311 | thread->queue = queue; |
| r1299 | r1300 | |
| 359 | 366 | // if this is a multi queue, help out rather than doing nothing |
| 360 | 367 | if (queue->flags & WORK_QUEUE_FLAG_MULTI) |
| 361 | 368 | { |
| 362 | | mame_thread_info *thread = &queue->thread[queue->threads]; |
| 369 | work_thread_info *thread = &queue->thread[queue->threads]; |
| 363 | 370 | osd_ticks_t stopspin = osd_ticks() + timeout; |
| 364 | 371 | |
| 365 | 372 | end_timing(thread->waittime); |
| r1299 | r1300 | |
| 367 | 374 | // process what we can as a worker thread |
| 368 | 375 | worker_thread_process(queue, thread); |
| 369 | 376 | |
| 370 | | // spin until we're done |
| 371 | | begin_timing(thread->spintime); |
| 372 | | while (queue->items != 0 && osd_ticks() < stopspin) |
| 373 | | osd_yield_processor(); |
| 374 | | end_timing(thread->spintime); |
| 377 | // if we're a high frequency queue, spin until done |
| 378 | if (queue->flags & WORK_QUEUE_FLAG_HIGH_FREQ) |
| 379 | { |
| 380 | // spin until we're done |
| 381 | begin_timing(thread->spintime); |
| 382 | while (queue->items != 0 && osd_ticks() < stopspin) |
| 383 | osd_yield_processor(); |
| 384 | end_timing(thread->spintime); |
| 375 | 385 | |
| 386 | begin_timing(thread->waittime); |
| 387 | return (queue->items == 0); |
| 388 | } |
| 376 | 389 | begin_timing(thread->waittime); |
| 377 | | return (queue->items == 0); |
| 378 | 390 | } |
| 379 | 391 | |
| 380 | 392 | // reset our done event and double-check the items before waiting |
| 381 | 393 | osd_event_reset(queue->doneevent); |
| 382 | | atomic_exchange32(&queue->waiting, TRUE); |
| 394 | interlocked_exchange32(&queue->waiting, TRUE); |
| 383 | 395 | if (queue->items != 0) |
| 384 | 396 | osd_event_wait(queue->doneevent, timeout); |
| 385 | | atomic_exchange32(&queue->waiting, FALSE); |
| 397 | interlocked_exchange32(&queue->waiting, FALSE); |
| 386 | 398 | |
| 387 | 399 | // return TRUE if we actually hit 0 |
| 388 | 400 | return (queue->items == 0); |
| r1299 | r1300 | |
| 407 | 419 | queue->exiting = TRUE; |
| 408 | 420 | for (threadnum = 0; threadnum < queue->threads; threadnum++) |
| 409 | 421 | { |
| 410 | | mame_thread_info *thread = &queue->thread[threadnum]; |
| 422 | work_thread_info *thread = &queue->thread[threadnum]; |
| 411 | 423 | if (thread->wakeevent != NULL) |
| 412 | 424 | osd_event_set(thread->wakeevent); |
| 413 | 425 | } |
| r1299 | r1300 | |
| 415 | 427 | // wait for all the threads to go away |
| 416 | 428 | for (threadnum = 0; threadnum < queue->threads; threadnum++) |
| 417 | 429 | { |
| 418 | | mame_thread_info *thread = &queue->thread[threadnum]; |
| 430 | work_thread_info *thread = &queue->thread[threadnum]; |
| 419 | 431 | |
| 420 | 432 | // block on the thread going away, then close the handle |
| 421 | 433 | if (thread->handle != NULL) |
| r1299 | r1300 | |
| 432 | 444 | // output per-thread statistics |
| 433 | 445 | for (threadnum = 0; threadnum <= queue->threads; threadnum++) |
| 434 | 446 | { |
| 435 | | mame_thread_info *thread = &queue->thread[threadnum]; |
| 447 | work_thread_info *thread = &queue->thread[threadnum]; |
| 436 | 448 | osd_ticks_t total = thread->runtime + thread->waittime + thread->spintime; |
| 437 | 449 | printf("Thread %d: items=%9d run=%5.2f%% (%5.2f%%) spin=%5.2f%% wait/other=%5.2f%% total=%9d\n", |
| 438 | 450 | threadnum, thread->itemsdone, |
| r1299 | r1300 | |
| 504 | 516 | do |
| 505 | 517 | { |
| 506 | 518 | item = (osd_work_item *)queue->free; |
| 507 | | } while (item != NULL && compare_exchange_ptr((void * volatile *)&queue->free, item, item->next) != item); |
| 519 | } while (item != NULL && compare_exchange_ptr((PVOID volatile *)&queue->free, item, item->next) != item); |
| 508 | 520 | |
| 509 | 521 | // if nothing, allocate something new |
| 510 | 522 | if (item == NULL) |
| r1299 | r1300 | |
| 537 | 549 | scalable_lock_release(&queue->lock, lockslot); |
| 538 | 550 | |
| 539 | 551 | // increment the number of items in the queue |
| 540 | | atomic_add32(&queue->items, numitems); |
| 552 | interlocked_add(&queue->items, numitems); |
| 541 | 553 | add_to_stat(&queue->itemsqueued, numitems); |
| 542 | 554 | |
| 543 | 555 | // look for free threads to do the work |
| r1299 | r1300 | |
| 548 | 560 | // iterate over all the threads |
| 549 | 561 | for (threadnum = 0; threadnum < queue->threads; threadnum++) |
| 550 | 562 | { |
| 551 | | mame_thread_info *thread = &queue->thread[threadnum]; |
| 563 | work_thread_info *thread = &queue->thread[threadnum]; |
| 552 | 564 | |
| 553 | 565 | // if this thread is not active, wake him up |
| 554 | 566 | if (!thread->active) |
| r1299 | r1300 | |
| 634 | 646 | { |
| 635 | 647 | next = (osd_work_item *)item->queue->free; |
| 636 | 648 | item->next = next; |
| 637 | | } while (compare_exchange_ptr((void * volatile *)&item->queue->free, next, item) != next); |
| 649 | } while (compare_exchange_ptr((PVOID volatile *)&item->queue->free, next, item) != next); |
| 638 | 650 | } |
| 639 | 651 | |
| 640 | 652 | |
| r1299 | r1300 | |
| 663 | 675 | |
| 664 | 676 | static void *worker_thread_entry(void *param) |
| 665 | 677 | { |
| 666 | | mame_thread_info *thread = param; |
| 678 | work_thread_info *thread = param; |
| 667 | 679 | osd_work_queue *queue = thread->queue; |
| 668 | 680 | |
| 669 | 681 | // loop until we exit |
| r1299 | r1300 | |
| 681 | 693 | break; |
| 682 | 694 | |
| 683 | 695 | // indicate that we are live |
| 684 | | atomic_exchange32(&thread->active, TRUE); |
| 685 | | osd_interlocked_increment(&queue->livethreads); |
| 696 | interlocked_exchange32(&thread->active, TRUE); |
| 697 | interlocked_increment(&queue->livethreads); |
| 686 | 698 | |
| 687 | 699 | // process work items |
| 688 | 700 | for ( ;; ) |
| r1299 | r1300 | |
| 692 | 704 | // process as much as we can |
| 693 | 705 | worker_thread_process(queue, thread); |
| 694 | 706 | |
| 695 | | // spin for a while looking for more work |
| 707 | // if we're a high frequency queue, spin for a while before giving up |
| 696 | 708 | if (queue->flags & WORK_QUEUE_FLAG_HIGH_FREQ) |
| 697 | 709 | { |
| 710 | // spin for a while looking for more work |
| 698 | 711 | begin_timing(thread->spintime); |
| 699 | 712 | stopspin = osd_ticks() + SPIN_LOOP_TIME; |
| 700 | 713 | while (queue->list == NULL && osd_ticks() < stopspin) |
| 701 | 714 | osd_yield_processor(); |
| 702 | 715 | end_timing(thread->spintime); |
| 703 | 716 | } |
| 717 | |
| 704 | 718 | // if nothing more, release the processor |
| 705 | 719 | if (queue->list == NULL) |
| 706 | 720 | break; |
| r1299 | r1300 | |
| 708 | 722 | } |
| 709 | 723 | |
| 710 | 724 | // decrement the live thread count |
| 711 | | atomic_exchange32(&thread->active, FALSE); |
| 712 | | osd_interlocked_decrement(&queue->livethreads); |
| 725 | interlocked_exchange32(&thread->active, FALSE); |
| 726 | interlocked_decrement(&queue->livethreads); |
| 713 | 727 | } |
| 714 | 728 | return NULL; |
| 715 | 729 | } |
| r1299 | r1300 | |
| 719 | 733 | // worker_thread_process |
| 720 | 734 | //============================================================ |
| 721 | 735 | |
| 722 | | static void worker_thread_process(osd_work_queue *queue, mame_thread_info *thread) |
| 736 | static void worker_thread_process(osd_work_queue *queue, work_thread_info *thread) |
| 723 | 737 | { |
| 724 | 738 | int threadid = thread - queue->thread; |
| 725 | 739 | |
| r1299 | r1300 | |
| 754 | 768 | end_timing(thread->actruntime); |
| 755 | 769 | |
| 756 | 770 | // decrement the item count after we are done |
| 757 | | osd_interlocked_decrement(&queue->items); |
| 758 | | item->done = TRUE; |
| 759 | | |
| 771 | interlocked_decrement(&queue->items); |
| 772 | interlocked_exchange32(&item->done, TRUE); |
| 760 | 773 | add_to_stat(&thread->itemsdone, 1); |
| 761 | 774 | |
| 762 | 775 | // if it's an auto-release item, release it |