Lines Matching refs:tpool
38 delete_pool(tpool_t *tpool) in delete_pool() argument
45 for (job = tpool->tp_head; job != NULL; job = tpool->tp_head) { in delete_pool()
46 tpool->tp_head = job->tpj_next; in delete_pool()
49 (void) pthread_attr_destroy(&tpool->tp_attr); in delete_pool()
50 free(tpool); in delete_pool()
59 tpool_t *tpool = arg; in worker_cleanup() local
61 if (--tpool->tp_current == 0 && in worker_cleanup()
62 (tpool->tp_flags & (TP_DESTROY | TP_ABANDON))) { in worker_cleanup()
63 if (tpool->tp_flags & TP_ABANDON) { in worker_cleanup()
64 pthread_mutex_unlock(&tpool->tp_mutex); in worker_cleanup()
65 delete_pool(tpool); in worker_cleanup()
68 if (tpool->tp_flags & TP_DESTROY) in worker_cleanup()
69 (void) pthread_cond_broadcast(&tpool->tp_busycv); in worker_cleanup()
71 pthread_mutex_unlock(&tpool->tp_mutex); in worker_cleanup()
75 notify_waiters(tpool_t *tpool) in notify_waiters() argument
77 if (tpool->tp_head == NULL && tpool->tp_active == NULL) { in notify_waiters()
78 tpool->tp_flags &= ~TP_WAIT; in notify_waiters()
79 (void) pthread_cond_broadcast(&tpool->tp_waitcv); in notify_waiters()
89 tpool_t *tpool = arg; in job_cleanup() local
94 pthread_mutex_lock(&tpool->tp_mutex); in job_cleanup()
96 for (activepp = &tpool->tp_active;; activepp = &activep->tpa_next) { in job_cleanup()
103 if (tpool->tp_flags & TP_WAIT) in job_cleanup()
104 notify_waiters(tpool); in job_cleanup()
110 tpool_t *tpool = (tpool_t *)arg; in tpool_worker() local
117 pthread_mutex_lock(&tpool->tp_mutex); in tpool_worker()
118 pthread_cleanup_push(worker_cleanup, tpool); in tpool_worker()
127 tpool->tp_idle++; in tpool_worker()
128 if (tpool->tp_flags & TP_WAIT) in tpool_worker()
129 notify_waiters(tpool); in tpool_worker()
130 while ((tpool->tp_head == NULL || in tpool_worker()
131 (tpool->tp_flags & TP_SUSPEND)) && in tpool_worker()
132 !(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))) { in tpool_worker()
133 if (tpool->tp_current <= tpool->tp_minimum || in tpool_worker()
134 tpool->tp_linger == 0) { in tpool_worker()
135 (void) pthread_cond_wait(&tpool->tp_workcv, in tpool_worker()
136 &tpool->tp_mutex); in tpool_worker()
141 timeout.tv_sec += tpool->tp_linger; in tpool_worker()
142 if (pthread_cond_timedwait(&tpool->tp_workcv, in tpool_worker()
143 &tpool->tp_mutex, &timeout) != 0) { in tpool_worker()
149 tpool->tp_idle--; in tpool_worker()
150 if (tpool->tp_flags & TP_DESTROY) in tpool_worker()
152 if (tpool->tp_flags & TP_ABANDON) { in tpool_worker()
154 if (tpool->tp_flags & TP_SUSPEND) { in tpool_worker()
155 tpool->tp_flags &= ~TP_SUSPEND; in tpool_worker()
156 (void) pthread_cond_broadcast(&tpool->tp_workcv); in tpool_worker()
158 if (tpool->tp_head == NULL) in tpool_worker()
161 if ((job = tpool->tp_head) != NULL && in tpool_worker()
162 !(tpool->tp_flags & TP_SUSPEND)) { in tpool_worker()
166 tpool->tp_head = job->tpj_next; in tpool_worker()
167 if (job == tpool->tp_tail) in tpool_worker()
168 tpool->tp_tail = NULL; in tpool_worker()
169 tpool->tp_njobs--; in tpool_worker()
170 active.tpa_next = tpool->tp_active; in tpool_worker()
171 tpool->tp_active = &active; in tpool_worker()
172 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_worker()
173 pthread_cleanup_push(job_cleanup, tpool); in tpool_worker()
192 if (elapsed && tpool->tp_current > tpool->tp_minimum) { in tpool_worker()
209 create_worker(tpool_t *tpool) in create_worker() argument
217 error = pthread_create(&thread, &tpool->tp_attr, tpool_worker, tpool); in create_worker()
226 tpool_t *tpool; in tpool_create() local
234 tpool = calloc(1, sizeof (*tpool)); in tpool_create()
235 if (tpool == NULL) { in tpool_create()
239 (void) pthread_mutex_init(&tpool->tp_mutex, NULL); in tpool_create()
240 (void) pthread_cond_init(&tpool->tp_busycv, NULL); in tpool_create()
241 (void) pthread_cond_init(&tpool->tp_workcv, NULL); in tpool_create()
242 (void) pthread_cond_init(&tpool->tp_waitcv, NULL); in tpool_create()
243 tpool->tp_minimum = min_threads; in tpool_create()
244 tpool->tp_maximum = max_threads; in tpool_create()
245 tpool->tp_linger = linger; in tpool_create()
248 (void) pthread_attr_init(&tpool->tp_attr); in tpool_create()
249 (void) pthread_attr_setdetachstate(&tpool->tp_attr, in tpool_create()
252 return (tpool); in tpool_create()
263 tpool_dispatch(tpool_t *tpool, void (*func)(void *), void *arg) in tpool_dispatch() argument
273 pthread_mutex_lock(&tpool->tp_mutex); in tpool_dispatch()
275 if (tpool->tp_head == NULL) in tpool_dispatch()
276 tpool->tp_head = job; in tpool_dispatch()
278 tpool->tp_tail->tpj_next = job; in tpool_dispatch()
279 tpool->tp_tail = job; in tpool_dispatch()
280 tpool->tp_njobs++; in tpool_dispatch()
282 if (!(tpool->tp_flags & TP_SUSPEND)) { in tpool_dispatch()
283 if (tpool->tp_idle > 0) in tpool_dispatch()
284 (void) pthread_cond_signal(&tpool->tp_workcv); in tpool_dispatch()
285 else if (tpool->tp_current < tpool->tp_maximum && in tpool_dispatch()
286 create_worker(tpool) == 0) in tpool_dispatch()
287 tpool->tp_current++; in tpool_dispatch()
290 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_dispatch()
300 tpool_destroy(tpool_t *tpool) in tpool_destroy() argument
304 pthread_mutex_lock(&tpool->tp_mutex); in tpool_destroy()
305 pthread_cleanup_push((_Voidfp)pthread_mutex_unlock, &tpool->tp_mutex); in tpool_destroy()
308 tpool->tp_flags |= TP_DESTROY; in tpool_destroy()
309 tpool->tp_flags &= ~TP_SUSPEND; in tpool_destroy()
310 (void) pthread_cond_broadcast(&tpool->tp_workcv); in tpool_destroy()
313 for (activep = tpool->tp_active; activep; activep = activep->tpa_next) in tpool_destroy()
317 while (tpool->tp_active != NULL) { in tpool_destroy()
318 tpool->tp_flags |= TP_WAIT; in tpool_destroy()
319 (void) pthread_cond_wait(&tpool->tp_waitcv, &tpool->tp_mutex); in tpool_destroy()
323 while (tpool->tp_current != 0) in tpool_destroy()
324 (void) pthread_cond_wait(&tpool->tp_busycv, &tpool->tp_mutex); in tpool_destroy()
327 delete_pool(tpool); in tpool_destroy()
335 tpool_abandon(tpool_t *tpool) in tpool_abandon() argument
338 pthread_mutex_lock(&tpool->tp_mutex); in tpool_abandon()
339 if (tpool->tp_current == 0) { in tpool_abandon()
341 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_abandon()
342 delete_pool(tpool); in tpool_abandon()
345 tpool->tp_flags |= TP_ABANDON; in tpool_abandon()
346 tpool->tp_flags &= ~TP_SUSPEND; in tpool_abandon()
347 (void) pthread_cond_broadcast(&tpool->tp_workcv); in tpool_abandon()
348 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_abandon()
357 tpool_wait(tpool_t *tpool) in tpool_wait() argument
360 pthread_mutex_lock(&tpool->tp_mutex); in tpool_wait()
361 pthread_cleanup_push((_Voidfp)pthread_mutex_unlock, &tpool->tp_mutex); in tpool_wait()
362 while (tpool->tp_head != NULL || tpool->tp_active != NULL) { in tpool_wait()
363 tpool->tp_flags |= TP_WAIT; in tpool_wait()
364 (void) pthread_cond_wait(&tpool->tp_waitcv, &tpool->tp_mutex); in tpool_wait()
370 tpool_suspend(tpool_t *tpool) in tpool_suspend() argument
373 pthread_mutex_lock(&tpool->tp_mutex); in tpool_suspend()
374 tpool->tp_flags |= TP_SUSPEND; in tpool_suspend()
375 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_suspend()
379 tpool_suspended(tpool_t *tpool) in tpool_suspended() argument
383 pthread_mutex_lock(&tpool->tp_mutex); in tpool_suspended()
384 suspended = (tpool->tp_flags & TP_SUSPEND) != 0; in tpool_suspended()
385 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_suspended()
391 tpool_resume(tpool_t *tpool) in tpool_resume() argument
395 pthread_mutex_lock(&tpool->tp_mutex); in tpool_resume()
396 if (!(tpool->tp_flags & TP_SUSPEND)) { in tpool_resume()
397 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_resume()
400 tpool->tp_flags &= ~TP_SUSPEND; in tpool_resume()
401 (void) pthread_cond_broadcast(&tpool->tp_workcv); in tpool_resume()
402 excess = tpool->tp_njobs - tpool->tp_idle; in tpool_resume()
403 while (excess-- > 0 && tpool->tp_current < tpool->tp_maximum) { in tpool_resume()
404 if (create_worker(tpool) != 0) in tpool_resume()
406 tpool->tp_current++; in tpool_resume()
408 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_resume()
412 tpool_member(tpool_t *tpool) in tpool_member() argument
417 pthread_mutex_lock(&tpool->tp_mutex); in tpool_member()
418 for (activep = tpool->tp_active; activep; activep = activep->tpa_next) { in tpool_member()
420 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_member()
424 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_member()