/* * Called by a worker thread on return from a job. */ void job_cleanup(thr_pool_t *pool) { pthread_t my_tid = pthread_self(); active_t *activep; active_t **activepp; (void) pthread_mutex_lock(&pool->pool_mutex); for (activepp = &pool->pool_active; (activep = *activepp) != NULL; activepp = &activep->active_next) { if (activep->active_tid == my_tid) { *activepp = activep->active_next; break; } } if (pool->pool_flags & POOL_WAIT) notify_waiters(pool); }
/* * Called by a worker thread on return from a tpool_dispatch()d job. */ static void job_cleanup(tpool_t *tpool) { pthread_t my_tid = pthread_self(); tpool_active_t *activep; tpool_active_t **activepp; sig_mutex_lock(&tpool->tp_mutex); /* CSTYLED */ for (activepp = &tpool->tp_active;; activepp = &activep->tpa_next) { activep = *activepp; if (activep->tpa_tid == my_tid) { *activepp = activep->tpa_next; break; } } if (tpool->tp_flags & TP_WAIT) notify_waiters(tpool); }
static void * tpool_worker(void *arg) { tpool_t *tpool = (tpool_t *)arg; int elapsed; tpool_job_t *job; void (*func)(void *); tpool_active_t active; sigset_t maskset; pthread_mutex_lock(&tpool->tp_mutex); pthread_cleanup_push(worker_cleanup, tpool); /* * This is the worker's main loop. * It will only be left if a timeout or an error has occured. */ active.tpa_tid = pthread_self(); for (;;) { elapsed = 0; tpool->tp_idle++; if (tpool->tp_flags & TP_WAIT) notify_waiters(tpool); while ((tpool->tp_head == NULL || (tpool->tp_flags & TP_SUSPEND)) && !(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))) { if (tpool->tp_current <= tpool->tp_minimum || tpool->tp_linger == 0) { (void) pthread_cond_wait(&tpool->tp_workcv, &tpool->tp_mutex); } else { struct timespec timeout; clock_gettime(CLOCK_MONOTONIC, &timeout); timeout.tv_sec += tpool->tp_linger; if (pthread_cond_timedwait(&tpool->tp_workcv, &tpool->tp_mutex, &timeout) != 0) { elapsed = 1; break; } } } tpool->tp_idle--; if (tpool->tp_flags & TP_DESTROY) break; if (tpool->tp_flags & TP_ABANDON) { /* can't abandon a suspended pool */ if (tpool->tp_flags & TP_SUSPEND) { tpool->tp_flags &= ~TP_SUSPEND; (void) pthread_cond_broadcast(&tpool->tp_workcv); } if (tpool->tp_head == NULL) break; } if ((job = tpool->tp_head) != NULL && !(tpool->tp_flags & TP_SUSPEND)) { elapsed = 0; func = job->tpj_func; arg = job->tpj_arg; tpool->tp_head = job->tpj_next; if (job == tpool->tp_tail) tpool->tp_tail = NULL; tpool->tp_njobs--; active.tpa_next = tpool->tp_active; tpool->tp_active = &active; pthread_mutex_unlock(&tpool->tp_mutex); pthread_cleanup_push(job_cleanup, tpool); free(job); /* * Call the specified function. */ func(arg); /* * We don't know what this thread has been doing, * so we reset its signal mask and cancellation * state back to the initial values. */ sigfillset(&maskset); (void) pthread_sigmask(SIG_SETMASK, &maskset, NULL); (void) pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL); (void) pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); pthread_cleanup_pop(1); } if (elapsed && tpool->tp_current > tpool->tp_minimum) { /* * We timed out and there is no work to be done * and the number of workers exceeds the minimum. * Exit now to reduce the size of the pool. */ break; } } pthread_cleanup_pop(1); return (arg); }
void * worker_thread(void *arg) { thr_pool_t *pool = (thr_pool_t *)arg; int timedout; job_t *job; void *(*func)(void *); active_t active; struct timespec ts; /* * This is the worker's main loop. It will only be left * if a timeout occurs or if the pool is being destroyed. */ (void) pthread_mutex_lock(&pool->pool_mutex); pthread_cleanup_push(worker_cleanup, pool); active.active_tid = pthread_self(); for (;;) { /* * We don't know what this thread was doing during * its last job, so we reset its signal mask and * cancellation state back to the initial values. */ (void) pthread_sigmask(SIG_SETMASK, &fillset, NULL); (void) pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL); (void) pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); timedout = 0; pool->pool_idle++; if (pool->pool_flags & POOL_WAIT) notify_waiters(pool); while (pool->pool_head == NULL && !(pool->pool_flags & POOL_DESTROY)) { if (pool->pool_nthreads <= pool->pool_minimum) { (void) pthread_cond_wait(&pool->pool_workcv, &pool->pool_mutex); } else { (void) clock_gettime(CLOCK_REALTIME, &ts); ts.tv_sec += pool->pool_linger; if (pool->pool_linger == 0 || pthread_cond_timedwait(&pool->pool_workcv, &pool->pool_mutex, &ts) == ETIMEDOUT) { timedout = 1; break; } } } pool->pool_idle--; if (pool->pool_flags & POOL_DESTROY) break; if ((job = pool->pool_head) != NULL) { timedout = 0; func = job->job_func; arg = job->job_arg; pool->pool_head = job->job_next; if (job == pool->pool_tail) pool->pool_tail = NULL; active.active_next = pool->pool_active; pool->pool_active = &active; (void) pthread_mutex_unlock(&pool->pool_mutex); pthread_cleanup_push(job_cleanup, pool); free(job); /* * Call the specified job function. */ (void) func(arg); /* * If the job function calls pthread_exit(), the thread * calls job_cleanup(pool) and worker_cleanup(pool); * the integrity of the pool is thereby maintained. */ pthread_cleanup_pop(1); /* job_cleanup(pool) */ } if (timedout && pool->pool_nthreads > pool->pool_minimum) { /* * We timed out and there is no work to be done * and the number of workers exceeds the minimum. * Exit now to reduce the size of the pool. */ break; } } pthread_cleanup_pop(1); /* worker_cleanup(pool) */ return (NULL); }