Previous 199869 Revisions Next

r34215 Tuesday 6th January, 2015 at 10:20:29 UTC by Oliver Stöneberg
osd_work.c: only allocate main threads when WORK_QUEUE_FLAG_MULTI / some logging of thread count / fixed clang warning (nw)
[src/osd/modules/sync]work_osd.c

trunk/src/osd/modules/sync/work_osd.c
r242726r242727
7676#endif
7777
7878// TODO: move this in a common place
79#if defined(OSD_WINDOWS)
7980#if __GNUC__ && defined(__i386__) && !defined(__x86_64)
8081#undef YieldProcessor
8182#endif
r242726r242727
9596#else
9697#define osd_yield_processor YieldProcessor
9798#endif
99#endif
98100
99101template<typename _PtrType>
100102static void spin_while(const volatile _PtrType * volatile ptr, const _PtrType val, const osd_ticks_t timeout, const int invert = 0)
r242726r242727
203205
204206osd_work_queue *osd_work_queue_alloc(int flags)
205207{
208   int threadnum;
206209   int numprocs = effective_num_processors();
207210   osd_work_queue *queue;
208   int threadnum;
211   int osdthreadnum = 0;
212   int allocthreadnum;
209213   char *osdworkqueuemaxthreads = osd_getenv("OSDWORKQUEUEMAXTHREADS");
210214
211215   // allocate a new queue
r242726r242727
231235   // determine how many threads to create...
232236   // on a single-CPU system, create 1 thread for I/O queues, and 0 threads for everything else
233237   if (numprocs == 1)
234      queue->threads = (flags & WORK_QUEUE_FLAG_IO) ? 1 : 0;
238      threadnum = (flags & WORK_QUEUE_FLAG_IO) ? 1 : 0;
235239   // TODO: chose either
236240#if defined(OSD_WINDOWS)
237241   // on an n-CPU system, create n threads for multi queues, and 1 thread for everything else
238242   else
239      queue->threads = (flags & WORK_QUEUE_FLAG_MULTI) ? numprocs : 1;
243      threadnum = (flags & WORK_QUEUE_FLAG_MULTI) ? numprocs : 1;
240244#else
241245   // on an n-CPU system, create (n-1) threads for multi queues, and 1 thread for everything else
242246   else
243      queue->threads = (flags & WORK_QUEUE_FLAG_MULTI) ? (numprocs - 1) : 1;
247      threadnum = (flags & WORK_QUEUE_FLAG_MULTI) ? (numprocs - 1) : 1;
244248#endif
245249
246   if (osdworkqueuemaxthreads != NULL && sscanf(osdworkqueuemaxthreads, "%d", &threadnum) == 1 && queue->threads > threadnum)
247      queue->threads = threadnum;
250   if (osdworkqueuemaxthreads != NULL && sscanf(osdworkqueuemaxthreads, "%d", &osdthreadnum) == 1 && threadnum > osdthreadnum)
251      threadnum = osdthreadnum;
248252
249253   // TODO: do we still have the scaling problems?
250254#if defined(OSD_WINDOWS)
251255   // multi-queues with high frequency items should top out at 4 for now
252256   // since we have scaling problems above that
253   if ((flags & WORK_QUEUE_FLAG_HIGH_FREQ) && queue->threads > 1)
254      queue->threads = MIN(queue->threads - 1, 4);
257   if ((flags & WORK_QUEUE_FLAG_HIGH_FREQ) && threadnum > 1)
258      threadnum = MIN(threadnum - 1, 4);
255259#endif
256260
257261   // clamp to the maximum
258   queue->threads = MIN(queue->threads, WORK_MAX_THREADS);
262   queue->threads = MIN(threadnum, WORK_MAX_THREADS);
259263
260   // allocate memory for thread array (+1 to count the calling thread)
261   queue->thread = (work_thread_info *)osd_malloc_array((queue->threads + 1) * sizeof(queue->thread[0]));
264   // allocate memory for thread array (+1 to count the calling thread if WORK_QUEUE_FLAG_MULTI)
265   if (flags & WORK_QUEUE_FLAG_MULTI)
266      allocthreadnum = queue->threads + 1;
267   else
268      allocthreadnum = queue->threads;
269
270   osd_printf_verbose("procs: %d threads: %d allocthreads: %d osdthreads: %d maxthreads: %d queuethreads: %d\n", osd_num_processors, threadnum, allocthreadnum, osdthreadnum, WORK_MAX_THREADS, queue->threads);
271
272   queue->thread = (work_thread_info *)osd_malloc_array(allocthreadnum * sizeof(queue->thread[0]));
262273   if (queue->thread == NULL)
263274      goto error;
264   memset(queue->thread, 0, (queue->threads + 1) * sizeof(queue->thread[0]));
275   memset(queue->thread, 0, allocthreadnum * sizeof(queue->thread[0]));
265276
266277   // iterate over threads
267278   for (threadnum = 0; threadnum < queue->threads; threadnum++)
r242726r242727
290301   }
291302
292303   // start a timer going for "waittime" on the main thread
293   begin_timing(queue->thread[queue->threads].waittime);
304   if (flags & WORK_QUEUE_FLAG_MULTI)
305   {
306      begin_timing(queue->thread[queue->threads].waittime);
307   }
294308   return queue;
295309
296310error:
r242726r242727
372386      int threadnum;
373387
374388      // stop the timer for "waittime" on the main thread
375      end_timing(queue->thread[queue->threads].waittime);
389      if (queue->flags & WORK_QUEUE_FLAG_MULTI)
390      {
391         end_timing(queue->thread[queue->threads].waittime);
392      }
376393
377394      // signal all the threads to exit
378395      atomic_exchange32(&queue->exiting, TRUE);
r242726r242727
400417      }
401418
402419#if KEEP_STATISTICS
420      int allocthreadnum;
421      if (queue->flags & WORK_QUEUE_FLAG_MULTI)
422         allocthreadnum = queue->threads + 1;
423      else
424         allocthreadnum = queue->threads;
425
403426      // output per-thread statistics
404      for (threadnum = 0; threadnum <= queue->threads; threadnum++)
427      for (threadnum = 0; threadnum <= allocthreadnum; threadnum++)
405428      {
406429         work_thread_info *thread = &queue->thread[threadnum];
407430         osd_ticks_t total = thread->runtime + thread->waittime + thread->spintime;


Previous 199869 Revisions Next


© 1997-2024 The MAME Team