thread/m_threadpool.c
/*! Function implementing an individual thread.  Loops looking for and
 *  performing tasks until the threadpool is shutdown
 * \param arg is the initialized threadpool handle
 * \return always returns NULL, return value is meaningless
 */
Function implementing an individual thread.  Loops looking for and
performing tasks until the threadpool is shutdown
\param arg is the initialized threadpool handle
\return always returns NULL, return value is meaningless
Function implementing an individual thread . Loops looking for and performing tasks until the threadpool is shutdown \ param arg is the initialized threadpool handle \ return always returns NULL return value is meaningless
static void *M_threadpool_thread(void *arg)
{
	M_threadpool_t       *pool = arg;
	M_threadpool_queue_t  task;
	while (1) {
		if (!M_threadpool_queue_fetch(pool, &task))
			break;
		task.task(task.task_arg);
		if (task.finished)
			task.finished(task.task_arg);
		M_thread_mutex_lock(task.parent->lock);
		task.parent->tasks_remaining--;
		if (task.parent->is_waiting && task.parent->tasks_remaining == 0) {
			M_thread_cond_broadcast(task.parent->cond);
		}
		M_thread_mutex_unlock(task.parent->lock);
	}
	M_thread_mutex_lock(pool->queue_lock);
	pool->num_threads--;
	if (!pool->up && pool->num_threads == 0)
		M_thread_cond_broadcast(pool->queue_icond);
	M_thread_mutex_unlock(pool->queue_lock);
	return NULL;
}
static void * M_threadpool_thread ( void * arg ) { M_threadpool_t * pool = arg ; M_threadpool_queue_t task ; while ( 1 ) { if ( ! M_threadpool_queue_fetch ( pool , & task ) ) break ; task . task ( task . task_arg ) ; if ( task . finished ) task . finished ( task . task_arg ) ; M_thread_mutex_lock ( task . parent -> lock ) ; task . parent -> tasks_remaining -- ; if ( task . parent -> is_waiting && task . parent -> tasks_remaining == 0 ) { M_thread_cond_broadcast ( task . parent -> cond ) ; } M_thread_mutex_unlock ( task . parent -> lock ) ; } M_thread_mutex_lock ( pool -> queue_lock ) ; pool -> num_threads -- ; if ( ! pool -> up && pool -> num_threads == 0 ) M_thread_cond_broadcast ( pool -> queue_icond ) ; M_thread_mutex_unlock ( pool -> queue_lock ) ; return NULL ; }
Function implementing an individual thread.
Function implementing an individual thread .
/* The only reason this would fail is on shutdown or idle timeout */ /* Perform task */ /* Notify on completion */ /* Tell the parent the task is done, and wake them up if we were the
		 * last task left */ /* use broadcast instead of signal incase multiple threads are
			 * calling the parent wait function even though it isn't recommended */ /* On M_threadpool_destroy() it will block on queue_icond until woken up
	 * with the thread count at 0 */