src/osd/sdl/os2work.c
| r3074 | r3075 | |
| 1 | | //============================================================ |
| 2 | | // |
| 3 | | // os2work.c - OS/2 OSD core work item 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 | | #define INCL_DOS |
| 13 | | #include <os2.h> |
| 14 | | |
| 15 | | #include <stdlib.h> |
| 16 | | #include <unistd.h> |
| 17 | | |
| 18 | | // MAME headers |
| 19 | | #include "osdcore.h" |
| 20 | | #include "eminline.h" |
| 21 | | |
| 22 | | |
| 23 | | //============================================================ |
| 24 | | // DEBUGGING |
| 25 | | //============================================================ |
| 26 | | |
| 27 | | #define KEEP_STATISTICS (0) |
| 28 | | |
| 29 | | |
| 30 | | |
| 31 | | //============================================================ |
| 32 | | // PARAMETERS |
| 33 | | //============================================================ |
| 34 | | |
| 35 | | #define SPIN_LOOP_TIME (osd_ticks_per_second() / 1000) |
| 36 | | |
| 37 | | |
| 38 | | |
| 39 | | //============================================================ |
| 40 | | // MACROS |
| 41 | | //============================================================ |
| 42 | | |
| 43 | | #if KEEP_STATISTICS |
| 44 | | #define add_to_stat(v,x) do { interlocked_add((v), (x)); } while (0) |
| 45 | | #define begin_timing(v) do { (v) -= osd_profiling_ticks(); } while (0) |
| 46 | | #define end_timing(v) do { (v) += osd_profiling_ticks(); } while (0) |
| 47 | | #else |
| 48 | | #define add_to_stat(v,x) do { } while (0) |
| 49 | | #define begin_timing(v) do { } while (0) |
| 50 | | #define end_timing(v) do { } while (0) |
| 51 | | #endif |
| 52 | | |
| 53 | | #ifndef YieldProcessor |
| 54 | | INLINE void YieldProcessor(void) |
| 55 | | { |
| 56 | | __asm__ __volatile__ ( "rep; nop" ); |
| 57 | | } |
| 58 | | #endif |
| 59 | | |
| 60 | | |
| 61 | | //============================================================ |
| 62 | | // TYPE DEFINITIONS |
| 63 | | //============================================================ |
| 64 | | |
| 65 | | typedef struct _scalable_lock scalable_lock; |
| 66 | | struct _scalable_lock |
| 67 | | { |
| 68 | | struct |
| 69 | | { |
| 70 | | volatile INT32 haslock; // do we have the lock? |
| 71 | | INT32 filler[64/4-1]; // assumes a 64-byte cache line |
| 72 | | } slot[WORK_MAX_THREADS]; // one slot per thread |
| 73 | | volatile INT32 nextindex; // index of next slot to use |
| 74 | | }; |
| 75 | | |
| 76 | | |
| 77 | | typedef struct _work_thread_info work_thread_info; |
| 78 | | struct _work_thread_info |
| 79 | | { |
| 80 | | osd_work_queue * queue; // pointer back to the queue |
| 81 | | TID handle; // handle to the thread |
| 82 | | HEV wakeevent; // wake event for the thread |
| 83 | | volatile INT32 active; // are we actively processing work? |
| 84 | | |
| 85 | | #if KEEP_STATISTICS |
| 86 | | INT32 itemsdone; |
| 87 | | osd_ticks_t actruntime; |
| 88 | | osd_ticks_t runtime; |
| 89 | | osd_ticks_t spintime; |
| 90 | | osd_ticks_t waittime; |
| 91 | | #endif |
| 92 | | }; |
| 93 | | |
| 94 | | |
| 95 | | struct _osd_work_queue |
| 96 | | { |
| 97 | | scalable_lock lock; // lock for protecting the queue |
| 98 | | osd_work_item * volatile list; // list of items in the queue |
| 99 | | osd_work_item ** volatile tailptr;// pointer to the tail pointer of work items in the queue |
| 100 | | osd_work_item * volatile free; // free list of work items |
| 101 | | volatile INT32 items; // items in the queue |
| 102 | | volatile INT32 livethreads; // number of live threads |
| 103 | | volatile INT32 waiting; // is someone waiting on the queue to complete? |
| 104 | | volatile UINT8 exiting; // should the threads exit on their next opportunity? |
| 105 | | UINT32 threads; // number of threads in this queue |
| 106 | | UINT32 flags; // creation flags |
| 107 | | work_thread_info * thread; // array of thread information |
| 108 | | HEV doneevent; // event signalled when work is complete |
| 109 | | |
| 110 | | #if KEEP_STATISTICS |
| 111 | | volatile INT32 itemsqueued; // total items queued |
| 112 | | volatile INT32 setevents; // number of times we called SetEvent |
| 113 | | volatile INT32 extraitems; // how many extra items we got after the first in the queue loop |
| 114 | | volatile INT32 spinloops; // how many times spinning bought us more items |
| 115 | | #endif |
| 116 | | }; |
| 117 | | |
| 118 | | |
| 119 | | struct _osd_work_item |
| 120 | | { |
| 121 | | osd_work_item * next; // pointer to next item |
| 122 | | osd_work_queue *queue; // pointer back to the owning queue |
| 123 | | osd_work_callback callback; // callback function |
| 124 | | void * param; // callback parameter |
| 125 | | void * result; // callback result |
| 126 | | HEV event; // event signalled when complete |
| 127 | | UINT32 flags; // creation flags |
| 128 | | volatile INT32 done; // is the item done? |
| 129 | | }; |
| 130 | | |
| 131 | | |
| 132 | | |
| 133 | | //============================================================ |
| 134 | | // FUNCTION PROTOTYPES |
| 135 | | //============================================================ |
| 136 | | |
| 137 | | static int effective_num_processors(void); |
| 138 | | static void worker_thread_entry(void *param); |
| 139 | | static void worker_thread_process(osd_work_queue *queue, work_thread_info *thread); |
| 140 | | |
| 141 | | |
| 142 | | |
| 143 | | //============================================================ |
| 144 | | // INLINE FUNCTIONS |
| 145 | | //============================================================ |
| 146 | | |
| 147 | | INLINE INT32 interlocked_exchange32(INT32 volatile *ptr, INT32 value) |
| 148 | | { |
| 149 | | return atomic_exchange32(ptr, value); |
| 150 | | } |
| 151 | | |
| 152 | | INLINE INT32 interlocked_increment(INT32 volatile *ptr) |
| 153 | | { |
| 154 | | return atomic_increment32(ptr); |
| 155 | | } |
| 156 | | |
| 157 | | INLINE INT32 interlocked_decrement(INT32 volatile *ptr) |
| 158 | | { |
| 159 | | return atomic_decrement32(ptr); |
| 160 | | } |
| 161 | | |
| 162 | | INLINE INT32 interlocked_add(INT32 volatile *ptr, INT32 add) |
| 163 | | { |
| 164 | | return atomic_add32(ptr, add); |
| 165 | | } |
| 166 | | |
| 167 | | |
| 168 | | |
| 169 | | //============================================================ |
| 170 | | // Scalable Locks |
| 171 | | //============================================================ |
| 172 | | |
| 173 | | INLINE void scalable_lock_init(scalable_lock *lock) |
| 174 | | { |
| 175 | | memset(lock, 0, sizeof(*lock)); |
| 176 | | lock->slot[0].haslock = TRUE; |
| 177 | | } |
| 178 | | |
| 179 | | |
| 180 | | INLINE INT32 scalable_lock_acquire(scalable_lock *lock) |
| 181 | | { |
| 182 | | INT32 myslot = (atomic_increment32(&lock->nextindex) - 1) & (WORK_MAX_THREADS - 1); |
| 183 | | |
| 184 | | #if defined(__i386__) || defined(__x86_64__) |
| 185 | | register INT32 tmp; |
| 186 | | __asm__ __volatile__ ( |
| 187 | | "1: clr %[tmp] ;" |
| 188 | | " xchg %[haslock], %[tmp] ;" |
| 189 | | " test %[tmp], %[tmp] ;" |
| 190 | | " jne 3f ;" |
| 191 | | "2: mov %[haslock], %[tmp] ;" |
| 192 | | " test %[tmp], %[tmp] ;" |
| 193 | | " jne 1b ;" |
| 194 | | " pause ;" |
| 195 | | " jmp 2b ;" |
| 196 | | "3: " |
| 197 | | : [haslock] "+m" (lock->slot[myslot].haslock) |
| 198 | | , [tmp] "=&r" (tmp) |
| 199 | | : |
| 200 | | : "%cc" |
| 201 | | ); |
| 202 | | #elif defined(__ppc__) || defined (__PPC__) || defined(__ppc64__) || defined(__PPC64__) |
| 203 | | register INT32 tmp; |
| 204 | | __asm__ __volatile__ ( |
| 205 | | "1: lwarx %[tmp], 0, %[haslock] \n" |
| 206 | | " cmpwi %[tmp], 0 \n" |
| 207 | | " bne 3f \n" |
| 208 | | "2: lwzx %[tmp], 0, %[haslock] \n" |
| 209 | | " cmpwi %[tmp], 0 \n" |
| 210 | | " bne 1b \n" |
| 211 | | " nop \n" |
| 212 | | " nop \n" |
| 213 | | " b 2b \n" |
| 214 | | "3: li %[tmp], 0 \n" |
| 215 | | " sync \n" |
| 216 | | " stwcx. %[tmp], 0, %[haslock] \n" |
| 217 | | " bne- 1b \n" |
| 218 | | " eieio \n" |
| 219 | | : [tmp] "=&r" (tmp) |
| 220 | | : [haslock] "r" (&lock->slot[myslot].haslock) |
| 221 | | : "cr0" |
| 222 | | ); |
| 223 | | #else |
| 224 | | INT32 backoff = 1; |
| 225 | | while (!osd_compare_exchange32(&lock->slot[myslot].haslock, TRUE, FALSE)) |
| 226 | | { |
| 227 | | INT32 backcount; |
| 228 | | for (backcount = 0; backcount < backoff; backcount++) |
| 229 | | osd_yield_processor(); |
| 230 | | backoff <<= 1; |
| 231 | | } |
| 232 | | #endif |
| 233 | | return myslot; |
| 234 | | } |
| 235 | | |
| 236 | | |
| 237 | | INLINE void scalable_lock_release(scalable_lock *lock, INT32 myslot) |
| 238 | | { |
| 239 | | #if defined(__i386__) || defined(__x86_64__) |
| 240 | | register INT32 tmp = TRUE; |
| 241 | | __asm__ __volatile__ ( |
| 242 | | " xchg %[haslock], %[tmp] ;" |
| 243 | | : [haslock] "+m" (lock->slot[(myslot + 1) & (WORK_MAX_THREADS - 1)].haslock) |
| 244 | | , [tmp] "+r" (tmp) |
| 245 | | : |
| 246 | | ); |
| 247 | | #elif defined(__ppc__) || defined (__PPC__) || defined(__ppc64__) || defined(__PPC64__) |
| 248 | | lock->slot[(myslot + 1) & (WORK_MAX_THREADS - 1)].haslock = TRUE; |
| 249 | | __asm__ __volatile__ ( " eieio " : : ); |
| 250 | | #else |
| 251 | | osd_exchange32(&lock->slot[(myslot + 1) & (WORK_MAX_THREADS - 1)].haslock, TRUE); |
| 252 | | #endif |
| 253 | | } |
| 254 | | |
| 255 | | |
| 256 | | //============================================================ |
| 257 | | // osd_work_queue_alloc |
| 258 | | //============================================================ |
| 259 | | |
| 260 | | osd_work_queue *osd_work_queue_alloc(int flags) |
| 261 | | { |
| 262 | | int numprocs = effective_num_processors(); |
| 263 | | osd_work_queue *queue; |
| 264 | | int threadnum; |
| 265 | | |
| 266 | | // allocate a new queue |
| 267 | | queue = malloc(sizeof(*queue)); |
| 268 | | if (queue == NULL) |
| 269 | | goto error; |
| 270 | | memset(queue, 0, sizeof(*queue)); |
| 271 | | |
| 272 | | // initialize basic queue members |
| 273 | | queue->tailptr = (osd_work_item **)&queue->list; |
| 274 | | queue->flags = flags; |
| 275 | | |
| 276 | | // allocate events for the queue |
| 277 | | DosCreateEventSem( NULL, &queue->doneevent, 0, TRUE ); // manual reset, signalled |
| 278 | | if (queue->doneevent == 0) |
| 279 | | goto error; |
| 280 | | |
| 281 | | // initialize the critical section |
| 282 | | scalable_lock_init(&queue->lock); |
| 283 | | |
| 284 | | // determine how many threads to create... |
| 285 | | // on a single-CPU system, create 1 thread for I/O queues, and 0 threads for everything else |
| 286 | | if (numprocs == 1) |
| 287 | | queue->threads = (flags & WORK_QUEUE_FLAG_IO) ? 1 : 0; |
| 288 | | |
| 289 | | // on an n-CPU system, create (n-1) threads for multi queues, and 1 thread for everything else |
| 290 | | else |
| 291 | | queue->threads = (flags & WORK_QUEUE_FLAG_MULTI) ? (numprocs - 1) : 1; |
| 292 | | |
| 293 | | // clamp to the maximum |
| 294 | | queue->threads = MIN(queue->threads, WORK_MAX_THREADS); |
| 295 | | |
| 296 | | // allocate memory for thread array (+1 to count the calling thread) |
| 297 | | queue->thread = malloc((queue->threads + 1) * sizeof(queue->thread[0])); |
| 298 | | if (queue->thread == NULL) |
| 299 | | goto error; |
| 300 | | memset(queue->thread, 0, (queue->threads + 1) * sizeof(queue->thread[0])); |
| 301 | | |
| 302 | | // iterate over threads |
| 303 | | for (threadnum = 0; threadnum < queue->threads; threadnum++) |
| 304 | | { |
| 305 | | work_thread_info *thread = &queue->thread[threadnum]; |
| 306 | | int handle; |
| 307 | | |
| 308 | | // set a pointer back to the queue |
| 309 | | thread->queue = queue; |
| 310 | | |
| 311 | | // create the per-thread wake event |
| 312 | | thread->wakeevent = 0; |
| 313 | | DosCreateEventSem( NULL, &thread->wakeevent, DCE_POSTONE, FALSE ); // auto-reset, not signalled |
| 314 | | if (thread->wakeevent == 0) |
| 315 | | goto error; |
| 316 | | |
| 317 | | // create the thread |
| 318 | | handle = _beginthread( worker_thread_entry, NULL, 256 * 1024, thread ), |
| 319 | | thread->handle = ( TID )handle; |
| 320 | | if (thread->handle == -1) |
| 321 | | goto error; |
| 322 | | |
| 323 | | // set its priority: I/O threads get high priority because they are assumed to be |
| 324 | | // blocked most of the time; other threads just match the creator's priority |
| 325 | | if (flags & WORK_QUEUE_FLAG_IO) |
| 326 | | DosSetPriority( PRTYS_THREAD, PRTYC_NOCHANGE, +1, thread->handle); |
| 327 | | #if 0 |
| 328 | | else |
| 329 | | { |
| 330 | | PTIB ptib; |
| 331 | | ULONG ulClass; |
| 332 | | LONG lDelta; |
| 333 | | |
| 334 | | DosGetInfoBlocks( &ptib, NULL ); |
| 335 | | ulClass = HIBYTE( ptib->tib_ptib2->tib2_ulpri ); |
| 336 | | lDelta = LOBYTE( ptib->tib_ptib2->tib2_ulpri ); |
| 337 | | |
| 338 | | DosSetPriority( PRTYS_THREAD, ulClass, lDelta, thread->handle); |
| 339 | | } |
| 340 | | #endif |
| 341 | | } |
| 342 | | |
| 343 | | // start a timer going for "waittime" on the main thread |
| 344 | | begin_timing(queue->thread[queue->threads].waittime); |
| 345 | | return queue; |
| 346 | | |
| 347 | | error: |
| 348 | | osd_work_queue_free(queue); |
| 349 | | return NULL; |
| 350 | | } |
| 351 | | |
| 352 | | |
| 353 | | //============================================================ |
| 354 | | // osd_work_queue_items |
| 355 | | //============================================================ |
| 356 | | |
| 357 | | int osd_work_queue_items(osd_work_queue *queue) |
| 358 | | { |
| 359 | | // return the number of items currently in the queue |
| 360 | | return queue->items; |
| 361 | | } |
| 362 | | |
| 363 | | |
| 364 | | //============================================================ |
| 365 | | // osd_work_queue_wait |
| 366 | | //============================================================ |
| 367 | | |
| 368 | | int osd_work_queue_wait(osd_work_queue *queue, osd_ticks_t timeout) |
| 369 | | { |
| 370 | | ULONG ulPost; |
| 371 | | |
| 372 | | // if no threads, no waiting |
| 373 | | if (queue->threads == 0) |
| 374 | | return TRUE; |
| 375 | | |
| 376 | | // if no items, we're done |
| 377 | | if (queue->items == 0) |
| 378 | | return TRUE; |
| 379 | | |
| 380 | | // if this is a multi queue, help out rather than doing nothing |
| 381 | | if (queue->flags & WORK_QUEUE_FLAG_MULTI) |
| 382 | | { |
| 383 | | work_thread_info *thread = &queue->thread[queue->threads]; |
| 384 | | osd_ticks_t stopspin = osd_ticks() + timeout; |
| 385 | | |
| 386 | | end_timing(thread->waittime); |
| 387 | | |
| 388 | | // process what we can as a worker thread |
| 389 | | worker_thread_process(queue, thread); |
| 390 | | |
| 391 | | // if we're a high frequency queue, spin until done |
| 392 | | if (queue->flags & WORK_QUEUE_FLAG_HIGH_FREQ) |
| 393 | | { |
| 394 | | // spin until we're done |
| 395 | | begin_timing(thread->spintime); |
| 396 | | while (queue->items != 0 && osd_ticks() < stopspin) |
| 397 | | YieldProcessor(); |
| 398 | | end_timing(thread->spintime); |
| 399 | | |
| 400 | | begin_timing(thread->waittime); |
| 401 | | return (queue->items == 0); |
| 402 | | } |
| 403 | | begin_timing(thread->waittime); |
| 404 | | } |
| 405 | | |
| 406 | | // reset our done event and double-check the items before waiting |
| 407 | | DosResetEventSem( queue->doneevent, &ulPost ); |
| 408 | | interlocked_exchange32(&queue->waiting, TRUE); |
| 409 | | if (queue->items != 0) |
| 410 | | DosWaitEventSem(queue->doneevent, timeout * 1000 / osd_ticks_per_second()); |
| 411 | | interlocked_exchange32(&queue->waiting, FALSE); |
| 412 | | |
| 413 | | // return TRUE if we actually hit 0 |
| 414 | | return (queue->items == 0); |
| 415 | | } |
| 416 | | |
| 417 | | |
| 418 | | //============================================================ |
| 419 | | // osd_work_queue_free |
| 420 | | //============================================================ |
| 421 | | |
| 422 | | void osd_work_queue_free(osd_work_queue *queue) |
| 423 | | { |
| 424 | | // if we have threads, clean them up |
| 425 | | if (queue->threads > 0 && queue->thread != NULL) |
| 426 | | { |
| 427 | | int threadnum; |
| 428 | | |
| 429 | | // stop the timer for "waittime" on the main thread |
| 430 | | end_timing(queue->thread[queue->threads].waittime); |
| 431 | | |
| 432 | | // signal all the threads to exit |
| 433 | | queue->exiting = TRUE; |
| 434 | | for (threadnum = 0; threadnum < queue->threads; threadnum++) |
| 435 | | { |
| 436 | | work_thread_info *thread = &queue->thread[threadnum]; |
| 437 | | if (thread->wakeevent != 0) |
| 438 | | DosPostEventSem(thread->wakeevent); |
| 439 | | } |
| 440 | | |
| 441 | | // wait for all the threads to go away |
| 442 | | for (threadnum = 0; threadnum < queue->threads; threadnum++) |
| 443 | | { |
| 444 | | work_thread_info *thread = &queue->thread[threadnum]; |
| 445 | | |
| 446 | | // block on the thread going away, then close the handle |
| 447 | | if (thread->handle != ( TID )-1) |
| 448 | | DosWaitThread( &thread->handle, DCWW_WAIT ); |
| 449 | | |
| 450 | | // clean up the wake event |
| 451 | | if (thread->wakeevent != 0) |
| 452 | | DosCloseEventSem(thread->wakeevent); |
| 453 | | } |
| 454 | | |
| 455 | | #if KEEP_STATISTICS |
| 456 | | // output per-thread statistics |
| 457 | | for (threadnum = 0; threadnum <= queue->threads; threadnum++) |
| 458 | | { |
| 459 | | work_thread_info *thread = &queue->thread[threadnum]; |
| 460 | | osd_ticks_t total = thread->runtime + thread->waittime + thread->spintime; |
| 461 | | printf("Thread %d: items=%9d run=%5.2f%% (%5.2f%%) spin=%5.2f%% wait/other=%5.2f%%\n", |
| 462 | | threadnum, thread->itemsdone, |
| 463 | | (double)thread->runtime * 100.0 / (double)total, |
| 464 | | (double)thread->actruntime * 100.0 / (double)total, |
| 465 | | (double)thread->spintime * 100.0 / (double)total, |
| 466 | | (double)thread->waittime * 100.0 / (double)total); |
| 467 | | } |
| 468 | | #endif |
| 469 | | |
| 470 | | // free the list |
| 471 | | free(queue->thread); |
| 472 | | } |
| 473 | | |
| 474 | | // free all the events |
| 475 | | if (queue->doneevent != 0) |
| 476 | | DosCloseEventSem(queue->doneevent); |
| 477 | | |
| 478 | | // free all items in the free list |
| 479 | | while (queue->free != NULL) |
| 480 | | { |
| 481 | | osd_work_item *item = (osd_work_item *)queue->free; |
| 482 | | queue->free = item->next; |
| 483 | | if (item->event != 0) |
| 484 | | DosCloseEventSem(item->event); |
| 485 | | free(item); |
| 486 | | } |
| 487 | | |
| 488 | | // free all items in the active list |
| 489 | | while (queue->list != NULL) |
| 490 | | { |
| 491 | | osd_work_item *item = (osd_work_item *)queue->list; |
| 492 | | queue->list = item->next; |
| 493 | | if (item->event != 0) |
| 494 | | DosCloseEventSem(item->event); |
| 495 | | free(item); |
| 496 | | } |
| 497 | | |
| 498 | | #if KEEP_STATISTICS |
| 499 | | printf("Items queued = %9d\n", queue->itemsqueued); |
| 500 | | printf("SetEvent calls = %9d\n", queue->setevents); |
| 501 | | printf("Extra items = %9d\n", queue->extraitems); |
| 502 | | printf("Spin loops = %9d\n", queue->spinloops); |
| 503 | | #endif |
| 504 | | |
| 505 | | // free the queue itself |
| 506 | | free(queue); |
| 507 | | } |
| 508 | | |
| 509 | | |
| 510 | | //============================================================ |
| 511 | | // osd_Twork_item_queue_multiple |
| 512 | | //============================================================ |
| 513 | | |
| 514 | | osd_work_item *osd_work_item_queue_multiple(osd_work_queue *queue, osd_work_callback callback, INT32 numitems, void *parambase, INT32 paramstep, UINT32 flags) |
| 515 | | { |
| 516 | | osd_work_item *itemlist = NULL; |
| 517 | | osd_work_item **item_tailptr = &itemlist; |
| 518 | | INT32 lockslot; |
| 519 | | int itemnum; |
| 520 | | |
| 521 | | // loop over items, building up a local list of work |
| 522 | | for (itemnum = 0; itemnum < numitems; itemnum++) |
| 523 | | { |
| 524 | | osd_work_item *item; |
| 525 | | |
| 526 | | // first allocate a new work item; try the free list first |
| 527 | | do |
| 528 | | { |
| 529 | | item = (osd_work_item *)queue->free; |
| 530 | | } while (item != NULL && compare_exchange_ptr((PVOID volatile *)&queue->free, item, item->next) != item); |
| 531 | | |
| 532 | | // if nothing, allocate something new |
| 533 | | if (item == NULL) |
| 534 | | { |
| 535 | | // allocate the item |
| 536 | | item = malloc(sizeof(*item)); |
| 537 | | if (item == NULL) |
| 538 | | return NULL; |
| 539 | | item->event = 0; |
| 540 | | item->queue = queue; |
| 541 | | } |
| 542 | | |
| 543 | | // fill in the basics |
| 544 | | item->next = NULL; |
| 545 | | item->callback = callback; |
| 546 | | item->param = parambase; |
| 547 | | item->result = NULL; |
| 548 | | item->flags = flags; |
| 549 | | item->done = FALSE; |
| 550 | | |
| 551 | | // advance to the next |
| 552 | | *item_tailptr = item; |
| 553 | | item_tailptr = &item->next; |
| 554 | | parambase = (UINT8 *)parambase + paramstep; |
| 555 | | } |
| 556 | | |
| 557 | | // enqueue the whole thing within the critical section |
| 558 | | lockslot = scalable_lock_acquire(&queue->lock); |
| 559 | | *queue->tailptr = itemlist; |
| 560 | | queue->tailptr = item_tailptr; |
| 561 | | scalable_lock_release(&queue->lock, lockslot); |
| 562 | | |
| 563 | | // increment the number of items in the queue |
| 564 | | interlocked_add(&queue->items, numitems); |
| 565 | | add_to_stat(&queue->itemsqueued, numitems); |
| 566 | | |
| 567 | | // look for free threads to do the work |
| 568 | | if (queue->livethreads < queue->threads) |
| 569 | | { |
| 570 | | int threadnum; |
| 571 | | |
| 572 | | // iterate over all the threads |
| 573 | | for (threadnum = 0; threadnum < queue->threads; threadnum++) |
| 574 | | { |
| 575 | | work_thread_info *thread = &queue->thread[threadnum]; |
| 576 | | |
| 577 | | // if this thread is not active, wake him up |
| 578 | | if (!thread->active) |
| 579 | | { |
| 580 | | DosPostEventSem(thread->wakeevent); |
| 581 | | add_to_stat(&queue->setevents, 1); |
| 582 | | |
| 583 | | // for non-shared, the first one we find is good enough |
| 584 | | if (--numitems == 0) |
| 585 | | break; |
| 586 | | } |
| 587 | | } |
| 588 | | } |
| 589 | | |
| 590 | | // if no threads, run the queue now on this thread |
| 591 | | if (queue->threads == 0) |
| 592 | | worker_thread_process(queue, &queue->thread[0]); |
| 593 | | |
| 594 | | // only return the item if it won't get released automatically |
| 595 | | return (flags & WORK_ITEM_FLAG_AUTO_RELEASE) ? NULL : itemlist; |
| 596 | | } |
| 597 | | |
| 598 | | |
| 599 | | //============================================================ |
| 600 | | // osd_work_item_wait |
| 601 | | //============================================================ |
| 602 | | |
| 603 | | int osd_work_item_wait(osd_work_item *item, osd_ticks_t timeout) |
| 604 | | { |
| 605 | | // if we're done already, just return |
| 606 | | if (item->done) |
| 607 | | return TRUE; |
| 608 | | |
| 609 | | // if we don't have an event, create one |
| 610 | | if (item->event == 0) |
| 611 | | DosCreateEventSem( NULL, &item->event, 0, FALSE ); // manual reset, not signalled |
| 612 | | else |
| 613 | | { |
| 614 | | ULONG ulPost; |
| 615 | | |
| 616 | | DosResetEventSem( item->event, &ulPost ); |
| 617 | | } |
| 618 | | |
| 619 | | // if we don't have an event, we need to spin (shouldn't ever really happen) |
| 620 | | if (item->event != 0) |
| 621 | | { |
| 622 | | osd_ticks_t stopspin = osd_ticks() + timeout; |
| 623 | | while (!item->done && osd_ticks() < stopspin) |
| 624 | | YieldProcessor(); |
| 625 | | } |
| 626 | | |
| 627 | | // otherwise, block on the event until done |
| 628 | | else if (!item->done) |
| 629 | | DosWaitEventSem(item->event, timeout * 1000 / osd_ticks_per_second()); |
| 630 | | |
| 631 | | // return TRUE if the refcount actually hit 0 |
| 632 | | return item->done; |
| 633 | | } |
| 634 | | |
| 635 | | |
| 636 | | //============================================================ |
| 637 | | // osd_work_item_result |
| 638 | | //============================================================ |
| 639 | | |
| 640 | | void *osd_work_item_result(osd_work_item *item) |
| 641 | | { |
| 642 | | return item->result; |
| 643 | | } |
| 644 | | |
| 645 | | |
| 646 | | //============================================================ |
| 647 | | // osd_work_item_release |
| 648 | | //============================================================ |
| 649 | | |
| 650 | | void osd_work_item_release(osd_work_item *item) |
| 651 | | { |
| 652 | | osd_work_item *next; |
| 653 | | |
| 654 | | // make sure we're done first |
| 655 | | osd_work_item_wait(item, 100 * osd_ticks_per_second()); |
| 656 | | |
| 657 | | // add us to the free list on our queue |
| 658 | | do |
| 659 | | { |
| 660 | | next = (osd_work_item *)item->queue->free; |
| 661 | | item->next = next; |
| 662 | | } while (compare_exchange_ptr((PVOID volatile *)&item->queue->free, next, item) != next); |
| 663 | | } |
| 664 | | |
| 665 | | |
| 666 | | //============================================================ |
| 667 | | // effective_num_processors |
| 668 | | //============================================================ |
| 669 | | |
| 670 | | static int effective_num_processors(void) |
| 671 | | { |
| 672 | | char *procsoverride; |
| 673 | | ULONG numprocs = 0; |
| 674 | | |
| 675 | | // if the OSDPROCESSORS environment variable is set, use that value if valid |
| 676 | | procsoverride = getenv(SDLENV_PROCESSORS); |
| 677 | | if (procsoverride != NULL && sscanf(procsoverride, "%lu", &numprocs) == 1 && numprocs > 0) |
| 678 | | return numprocs; |
| 679 | | |
| 680 | | DosQuerySysInfo( QSV_NUMPROCESSORS, QSV_NUMPROCESSORS, &numprocs, sizeof( numprocs )); |
| 681 | | |
| 682 | | // max out at 4 for now since scaling above that seems to do poorly |
| 683 | | return MIN( numprocs, 4); |
| 684 | | } |
| 685 | | |
| 686 | | |
| 687 | | //============================================================ |
| 688 | | // worker_thread_entry |
| 689 | | //============================================================ |
| 690 | | |
| 691 | | static void worker_thread_entry(void *param) |
| 692 | | { |
| 693 | | work_thread_info *thread = param; |
| 694 | | osd_work_queue *queue = thread->queue; |
| 695 | | |
| 696 | | // loop until we exit |
| 697 | | for ( ;; ) |
| 698 | | { |
| 699 | | // block waiting for work or exit |
| 700 | | |
| 701 | | // bail on exit, and only wait if there are no pending items in queue |
| 702 | | if (!queue->exiting && queue->list == NULL) |
| 703 | | { |
| 704 | | begin_timing(thread->waittime); |
| 705 | | DosWaitEventSem(thread->wakeevent, SEM_INDEFINITE_WAIT ); |
| 706 | | end_timing(thread->waittime); |
| 707 | | } |
| 708 | | |
| 709 | | if (queue->exiting) |
| 710 | | break; |
| 711 | | |
| 712 | | // indicate that we are live |
| 713 | | interlocked_exchange32(&thread->active, TRUE); |
| 714 | | interlocked_increment(&queue->livethreads); |
| 715 | | |
| 716 | | // process work items |
| 717 | | for ( ;; ) |
| 718 | | { |
| 719 | | osd_ticks_t stopspin; |
| 720 | | |
| 721 | | // process as much as we can |
| 722 | | worker_thread_process(queue, thread); |
| 723 | | |
| 724 | | // if we're a high frequency queue, spin for a while before giving up |
| 725 | | if (queue->flags & WORK_QUEUE_FLAG_HIGH_FREQ) |
| 726 | | { |
| 727 | | // spin for a while looking for more work |
| 728 | | begin_timing(thread->spintime); |
| 729 | | stopspin = osd_ticks() + SPIN_LOOP_TIME; |
| 730 | | while (queue->list == NULL && osd_ticks() < stopspin) |
| 731 | | YieldProcessor(); |
| 732 | | end_timing(thread->spintime); |
| 733 | | } |
| 734 | | |
| 735 | | // if nothing more, release the processor |
| 736 | | if (queue->list == NULL) |
| 737 | | break; |
| 738 | | add_to_stat(&queue->spinloops, 1); |
| 739 | | } |
| 740 | | |
| 741 | | // decrement the live thread count |
| 742 | | interlocked_exchange32(&thread->active, FALSE); |
| 743 | | interlocked_decrement(&queue->livethreads); |
| 744 | | } |
| 745 | | } |
| 746 | | |
| 747 | | |
| 748 | | //============================================================ |
| 749 | | // worker_thread_process |
| 750 | | //============================================================ |
| 751 | | |
| 752 | | static void worker_thread_process(osd_work_queue *queue, work_thread_info *thread) |
| 753 | | { |
| 754 | | int threadid = thread - queue->thread; |
| 755 | | |
| 756 | | begin_timing(thread->runtime); |
| 757 | | |
| 758 | | // loop until everything is processed |
| 759 | | while (queue->list != NULL) |
| 760 | | { |
| 761 | | osd_work_item *item; |
| 762 | | INT32 lockslot; |
| 763 | | |
| 764 | | // use a critical section to synchronize the removal of items |
| 765 | | lockslot = scalable_lock_acquire(&queue->lock); |
| 766 | | { |
| 767 | | // pull the item from the queue |
| 768 | | item = (osd_work_item *)queue->list; |
| 769 | | |
| 770 | | if (item != NULL) |
| 771 | | { |
| 772 | | queue->list = item->next; |
| 773 | | if (queue->list == NULL) |
| 774 | | queue->tailptr = (osd_work_item **)&queue->list; |
| 775 | | } |
| 776 | | } |
| 777 | | scalable_lock_release(&queue->lock, lockslot); |
| 778 | | |
| 779 | | // process non-NULL items |
| 780 | | if (item != NULL) |
| 781 | | { |
| 782 | | // call the callback and stash the result |
| 783 | | begin_timing(thread->actruntime); |
| 784 | | item->result = (*item->callback)(item->param, threadid); |
| 785 | | end_timing(thread->actruntime); |
| 786 | | |
| 787 | | // decrement the item count after we are done |
| 788 | | interlocked_decrement(&queue->items); |
| 789 | | interlocked_exchange32(&item->done, TRUE); |
| 790 | | add_to_stat(&thread->itemsdone, 1); |
| 791 | | |
| 792 | | // if it's an auto-release item, release it |
| 793 | | if (item->flags & WORK_ITEM_FLAG_AUTO_RELEASE) |
| 794 | | osd_work_item_release(item); |
| 795 | | |
| 796 | | // set the result and signal the event |
| 797 | | else if (item->event != 0) |
| 798 | | { |
| 799 | | DosPostEventSem(item->event); |
| 800 | | add_to_stat(&item->queue->setevents, 1); |
| 801 | | } |
| 802 | | |
| 803 | | // if we removed an item and there's still work to do, bump the stats |
| 804 | | if (queue->list != NULL) |
| 805 | | add_to_stat(&queue->extraitems, 1); |
| 806 | | } |
| 807 | | } |
| 808 | | |
| 809 | | // we don't need to set the doneevent for multi queues because they spin |
| 810 | | if (queue->waiting) |
| 811 | | { |
| 812 | | DosPostEventSem(queue->doneevent); |
| 813 | | add_to_stat(&queue->setevents, 1); |
| 814 | | } |
| 815 | | |
| 816 | | end_timing(thread->runtime); |
| 817 | | } |
| 818 | | |