FreeBSD/cddl/compat/opensolaris/misc/thread_pool.c
/*
 * Assumes: by the time tpool_destroy() is called no one will use this
 * thread pool in any way and no one will try to dispatch entries to it.
 * Calling tpool_destroy() from a job in the pool will cause deadlock.
 */
by the time tpool_destroy() is called no one will use this
thread pool in any way and no one will try to dispatch entries to it.
Calling tpool_destroy() from a job in the pool will cause deadlock.
by the time tpool_destroy () is called no one will use this thread pool in any way and no one will try to dispatch entries to it . Calling tpool_destroy () from a job in the pool will cause deadlock .
void
tpool_destroy(tpool_t *tpool)
{
	tpool_active_t *activep;
	pthread_mutex_lock(&tpool->tp_mutex);
	pthread_cleanup_push((_Voidfp)pthread_mutex_unlock, &tpool->tp_mutex);
	tpool->tp_flags |= TP_DESTROY;
	tpool->tp_flags &= ~TP_SUSPEND;
	(void) pthread_cond_broadcast(&tpool->tp_workcv);
	for (activep = tpool->tp_active; activep; activep = activep->tpa_next)
		(void) pthread_cancel(activep->tpa_tid);
	while (tpool->tp_active != NULL) {
		tpool->tp_flags |= TP_WAIT;
		(void) pthread_cond_wait(&tpool->tp_waitcv, &tpool->tp_mutex);
	}
	while (tpool->tp_current != 0)
		(void) pthread_cond_wait(&tpool->tp_busycv, &tpool->tp_mutex);
	pthread_cleanup_pop(1);	
	delete_pool(tpool);
}
void tpool_destroy ( tpool_t * tpool ) { tpool_active_t * activep ; pthread_mutex_lock ( & tpool -> tp_mutex ) ; pthread_cleanup_push ( ( _Voidfp ) pthread_mutex_unlock , & tpool -> tp_mutex ) ; tpool -> tp_flags |= TP_DESTROY ; tpool -> tp_flags &= ~ TP_SUSPEND ; ( void ) pthread_cond_broadcast ( & tpool -> tp_workcv ) ; for ( activep = tpool -> tp_active ; activep ; activep = activep -> tpa_next ) ( void ) pthread_cancel ( activep -> tpa_tid ) ; while ( tpool -> tp_active != NULL ) { tpool -> tp_flags |= TP_WAIT ; ( void ) pthread_cond_wait ( & tpool -> tp_waitcv , & tpool -> tp_mutex ) ; } while ( tpool -> tp_current != 0 ) ( void ) pthread_cond_wait ( & tpool -> tp_busycv , & tpool -> tp_mutex ) ; pthread_cleanup_pop ( 1 ) ; delete_pool ( tpool ) ; }
Assumes: by the time tpool_destroy() is called no one will use this
 thread pool in any way and no one will try to dispatch entries to it.
Assumes : by the time tpool_destroy () is called no one will use this thread pool in any way and no one will try to dispatch entries to it .
/* mark the pool as being destroyed; wakeup idle workers */ /* cancel all active workers */ /* wait for all active workers to finish */ /* the last worker to terminate will wake us up */ /* pthread_mutex_unlock(&tpool->tp_mutex); */