Lines Matching refs:tpool
39 delete_pool(tpool_t *tpool) in delete_pool() argument
43 ASSERT(tpool->tp_current == 0 && tpool->tp_active == NULL); in delete_pool()
49 if (thread_pools == tpool) in delete_pool()
50 thread_pools = tpool->tp_forw; in delete_pool()
51 if (thread_pools == tpool) in delete_pool()
54 tpool->tp_back->tp_forw = tpool->tp_forw; in delete_pool()
55 tpool->tp_forw->tp_back = tpool->tp_back; in delete_pool()
62 for (job = tpool->tp_head; job != NULL; job = tpool->tp_head) { in delete_pool()
63 tpool->tp_head = job->tpj_next; in delete_pool()
66 (void) pthread_attr_destroy(&tpool->tp_attr); in delete_pool()
67 free(tpool); in delete_pool()
76 tpool_t *tpool = (tpool_t *)arg; in worker_cleanup() local
78 if (--tpool->tp_current == 0 && in worker_cleanup()
79 (tpool->tp_flags & (TP_DESTROY | TP_ABANDON))) { in worker_cleanup()
80 if (tpool->tp_flags & TP_ABANDON) { in worker_cleanup()
81 pthread_mutex_unlock(&tpool->tp_mutex); in worker_cleanup()
82 delete_pool(tpool); in worker_cleanup()
85 if (tpool->tp_flags & TP_DESTROY) in worker_cleanup()
86 (void) pthread_cond_broadcast(&tpool->tp_busycv); in worker_cleanup()
88 pthread_mutex_unlock(&tpool->tp_mutex); in worker_cleanup()
92 notify_waiters(tpool_t *tpool) in notify_waiters() argument
94 if (tpool->tp_head == NULL && tpool->tp_active == NULL) { in notify_waiters()
95 tpool->tp_flags &= ~TP_WAIT; in notify_waiters()
96 (void) pthread_cond_broadcast(&tpool->tp_waitcv); in notify_waiters()
106 tpool_t *tpool = (tpool_t *)arg; in job_cleanup() local
112 pthread_mutex_lock(&tpool->tp_mutex); in job_cleanup()
113 for (activepp = &tpool->tp_active; ; activepp = &activep->tpa_next) { in job_cleanup()
120 if (tpool->tp_flags & TP_WAIT) in job_cleanup()
121 notify_waiters(tpool); in job_cleanup()
127 tpool_t *tpool = (tpool_t *)arg; in tpool_worker() local
133 pthread_mutex_lock(&tpool->tp_mutex); in tpool_worker()
134 pthread_cleanup_push(worker_cleanup, tpool); in tpool_worker()
143 tpool->tp_idle++; in tpool_worker()
144 if (tpool->tp_flags & TP_WAIT) in tpool_worker()
145 notify_waiters(tpool); in tpool_worker()
146 while ((tpool->tp_head == NULL || in tpool_worker()
147 (tpool->tp_flags & TP_SUSPEND)) && in tpool_worker()
148 !(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))) { in tpool_worker()
149 if (tpool->tp_current <= tpool->tp_minimum || in tpool_worker()
150 tpool->tp_linger == 0) { in tpool_worker()
151 (void) pthread_cond_wait(&tpool->tp_workcv, in tpool_worker()
152 &tpool->tp_mutex); in tpool_worker()
157 ts.tv_sec += tpool->tp_linger; in tpool_worker()
159 if (pthread_cond_timedwait(&tpool->tp_workcv, in tpool_worker()
160 &tpool->tp_mutex, &ts) != 0) { in tpool_worker()
166 tpool->tp_idle--; in tpool_worker()
167 if (tpool->tp_flags & TP_DESTROY) in tpool_worker()
169 if (tpool->tp_flags & TP_ABANDON) { in tpool_worker()
171 if (tpool->tp_flags & TP_SUSPEND) { in tpool_worker()
172 tpool->tp_flags &= ~TP_SUSPEND; in tpool_worker()
174 &tpool->tp_workcv); in tpool_worker()
176 if (tpool->tp_head == NULL) in tpool_worker()
179 if ((job = tpool->tp_head) != NULL && in tpool_worker()
180 !(tpool->tp_flags & TP_SUSPEND)) { in tpool_worker()
184 tpool->tp_head = job->tpj_next; in tpool_worker()
185 if (job == tpool->tp_tail) in tpool_worker()
186 tpool->tp_tail = NULL; in tpool_worker()
187 tpool->tp_njobs--; in tpool_worker()
188 active.tpa_next = tpool->tp_active; in tpool_worker()
189 tpool->tp_active = &active; in tpool_worker()
190 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_worker()
191 pthread_cleanup_push(job_cleanup, tpool); in tpool_worker()
213 if (elapsed && tpool->tp_current > tpool->tp_minimum) { in tpool_worker()
230 create_worker(tpool_t *tpool) in create_worker() argument
237 error = pthread_create(&thread, &tpool->tp_attr, tpool_worker, tpool); in create_worker()
326 tpool_t *tpool; in tpool_create() local
357 tpool = calloc(1, sizeof (*tpool)); in tpool_create()
358 if (tpool == NULL) { in tpool_create()
362 (void) pthread_mutex_init(&tpool->tp_mutex, NULL); in tpool_create()
363 (void) pthread_cond_init(&tpool->tp_busycv, NULL); in tpool_create()
364 (void) pthread_cond_init(&tpool->tp_workcv, NULL); in tpool_create()
365 (void) pthread_cond_init(&tpool->tp_waitcv, NULL); in tpool_create()
366 tpool->tp_minimum = min_threads; in tpool_create()
367 tpool->tp_maximum = max_threads; in tpool_create()
368 tpool->tp_linger = linger; in tpool_create()
377 error = pthread_attr_clone(&tpool->tp_attr, attr); in tpool_create()
379 free(tpool); in tpool_create()
385 (void) pthread_attr_setdetachstate(&tpool->tp_attr, in tpool_create()
391 tpool->tp_forw = tpool; in tpool_create()
392 tpool->tp_back = tpool; in tpool_create()
393 thread_pools = tpool; in tpool_create()
395 thread_pools->tp_back->tp_forw = tpool; in tpool_create()
396 tpool->tp_forw = thread_pools; in tpool_create()
397 tpool->tp_back = thread_pools->tp_back; in tpool_create()
398 thread_pools->tp_back = tpool; in tpool_create()
402 return (tpool); in tpool_create()
413 tpool_dispatch(tpool_t *tpool, void (*func)(void *), void *arg) in tpool_dispatch() argument
417 ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))); in tpool_dispatch()
425 pthread_mutex_lock(&tpool->tp_mutex); in tpool_dispatch()
427 if (!(tpool->tp_flags & TP_SUSPEND)) { in tpool_dispatch()
428 if (tpool->tp_idle > 0) in tpool_dispatch()
429 (void) pthread_cond_signal(&tpool->tp_workcv); in tpool_dispatch()
430 else if (tpool->tp_current >= tpool->tp_maximum) { in tpool_dispatch()
433 if (create_worker(tpool) == 0) { in tpool_dispatch()
435 tpool->tp_current++; in tpool_dispatch()
436 } else if (tpool->tp_current > 0) { in tpool_dispatch()
440 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_dispatch()
447 if (tpool->tp_head == NULL) in tpool_dispatch()
448 tpool->tp_head = job; in tpool_dispatch()
450 tpool->tp_tail->tpj_next = job; in tpool_dispatch()
451 tpool->tp_tail = job; in tpool_dispatch()
452 tpool->tp_njobs++; in tpool_dispatch()
454 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_dispatch()
461 tpool_t *tpool = (tpool_t *)arg; in tpool_cleanup() local
463 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_cleanup()
472 tpool_destroy(tpool_t *tpool) in tpool_destroy() argument
476 ASSERT(!tpool_member(tpool)); in tpool_destroy()
477 ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))); in tpool_destroy()
479 pthread_mutex_lock(&tpool->tp_mutex); in tpool_destroy()
480 pthread_cleanup_push(tpool_cleanup, tpool); in tpool_destroy()
483 tpool->tp_flags |= TP_DESTROY; in tpool_destroy()
484 tpool->tp_flags &= ~TP_SUSPEND; in tpool_destroy()
485 (void) pthread_cond_broadcast(&tpool->tp_workcv); in tpool_destroy()
488 for (activep = tpool->tp_active; activep; activep = activep->tpa_next) in tpool_destroy()
492 while (tpool->tp_active != NULL) { in tpool_destroy()
493 tpool->tp_flags |= TP_WAIT; in tpool_destroy()
494 (void) pthread_cond_wait(&tpool->tp_waitcv, &tpool->tp_mutex); in tpool_destroy()
498 while (tpool->tp_current != 0) in tpool_destroy()
499 (void) pthread_cond_wait(&tpool->tp_busycv, &tpool->tp_mutex); in tpool_destroy()
502 delete_pool(tpool); in tpool_destroy()
510 tpool_abandon(tpool_t *tpool) in tpool_abandon() argument
512 ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))); in tpool_abandon()
514 pthread_mutex_lock(&tpool->tp_mutex); in tpool_abandon()
515 if (tpool->tp_current == 0) { in tpool_abandon()
517 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_abandon()
518 delete_pool(tpool); in tpool_abandon()
521 tpool->tp_flags |= TP_ABANDON; in tpool_abandon()
522 tpool->tp_flags &= ~TP_SUSPEND; in tpool_abandon()
523 (void) pthread_cond_broadcast(&tpool->tp_workcv); in tpool_abandon()
524 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_abandon()
533 tpool_wait(tpool_t *tpool) in tpool_wait() argument
535 ASSERT(!tpool_member(tpool)); in tpool_wait()
536 ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))); in tpool_wait()
538 pthread_mutex_lock(&tpool->tp_mutex); in tpool_wait()
539 pthread_cleanup_push(tpool_cleanup, tpool); in tpool_wait()
540 while (tpool->tp_head != NULL || tpool->tp_active != NULL) { in tpool_wait()
541 tpool->tp_flags |= TP_WAIT; in tpool_wait()
542 (void) pthread_cond_wait(&tpool->tp_waitcv, &tpool->tp_mutex); in tpool_wait()
543 ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))); in tpool_wait()
549 tpool_suspend(tpool_t *tpool) in tpool_suspend() argument
551 ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))); in tpool_suspend()
553 pthread_mutex_lock(&tpool->tp_mutex); in tpool_suspend()
554 tpool->tp_flags |= TP_SUSPEND; in tpool_suspend()
555 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_suspend()
559 tpool_suspended(tpool_t *tpool) in tpool_suspended() argument
563 ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))); in tpool_suspended()
565 pthread_mutex_lock(&tpool->tp_mutex); in tpool_suspended()
566 suspended = (tpool->tp_flags & TP_SUSPEND) != 0; in tpool_suspended()
567 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_suspended()
573 tpool_resume(tpool_t *tpool) in tpool_resume() argument
577 ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))); in tpool_resume()
579 pthread_mutex_lock(&tpool->tp_mutex); in tpool_resume()
580 if (!(tpool->tp_flags & TP_SUSPEND)) { in tpool_resume()
581 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_resume()
584 tpool->tp_flags &= ~TP_SUSPEND; in tpool_resume()
585 (void) pthread_cond_broadcast(&tpool->tp_workcv); in tpool_resume()
586 excess = tpool->tp_njobs - tpool->tp_idle; in tpool_resume()
587 while (excess-- > 0 && tpool->tp_current < tpool->tp_maximum) { in tpool_resume()
588 if (create_worker(tpool) != 0) in tpool_resume()
590 tpool->tp_current++; in tpool_resume()
592 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_resume()
596 tpool_member(tpool_t *tpool) in tpool_member() argument
601 ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))); in tpool_member()
603 pthread_mutex_lock(&tpool->tp_mutex); in tpool_member()
604 for (activep = tpool->tp_active; activep; activep = activep->tpa_next) { in tpool_member()
606 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_member()
610 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_member()