FreeBSD/cddl/compat/opensolaris/misc/thread_pool.c
/*
 * Dispatch a work request to the thread pool.
 * If there are idle workers, awaken one.
 * Else, if the maximum number of workers has
 * not been reached, spawn a new worker thread.
 * Else just return with the job added to the queue.
 */
Dispatch a work request to the thread pool.
If there are idle workers, awaken one.
Else, if the maximum number of workers has
not been reached, spawn a new worker thread.
Else just return with the job added to the queue.
Dispatch a work request to the thread pool . If there are idle workers awaken one . Else if the maximum number of workers has not been reached spawn a new worker thread . Else just return with the job added to the queue .
int
tpool_dispatch(tpool_t *tpool, void (*func)(void *), void *arg)
{
	tpool_job_t *job;
	if ((job = calloc(1, sizeof (*job))) == NULL)
		return (-1);
	job->tpj_next = NULL;
	job->tpj_func = func;
	job->tpj_arg = arg;
	pthread_mutex_lock(&tpool->tp_mutex);
	if (tpool->tp_head == NULL)
		tpool->tp_head = job;
	else
		tpool->tp_tail->tpj_next = job;
	tpool->tp_tail = job;
	tpool->tp_njobs++;
	if (!(tpool->tp_flags & TP_SUSPEND)) {
		if (tpool->tp_idle > 0)
			(void) pthread_cond_signal(&tpool->tp_workcv);
		else if (tpool->tp_current < tpool->tp_maximum &&
		    create_worker(tpool) == 0)
			tpool->tp_current++;
	}
	pthread_mutex_unlock(&tpool->tp_mutex);
	return (0);
}
int tpool_dispatch ( tpool_t * tpool , void ( * func ) ( void * ) , void * arg ) { tpool_job_t * job ; if ( ( job = calloc ( 1 , sizeof ( * job ) ) ) == NULL ) return ( -1 ) ; job -> tpj_next = NULL ; job -> tpj_func = func ; job -> tpj_arg = arg ; pthread_mutex_lock ( & tpool -> tp_mutex ) ; if ( tpool -> tp_head == NULL ) tpool -> tp_head = job ; else tpool -> tp_tail -> tpj_next = job ; tpool -> tp_tail = job ; tpool -> tp_njobs ++ ; if ( ! ( tpool -> tp_flags & TP_SUSPEND ) ) { if ( tpool -> tp_idle > 0 ) ( void ) pthread_cond_signal ( & tpool -> tp_workcv ) ; else if ( tpool -> tp_current < tpool -> tp_maximum && create_worker ( tpool ) == 0 ) tpool -> tp_current ++ ; } pthread_mutex_unlock ( & tpool -> tp_mutex ) ; return ( 0 ) ; }
Dispatch a work request to the thread pool.
Dispatch a work request to the thread pool .