Lines Matching refs:tpool
38 delete_pool(tpool_t *tpool) in delete_pool() argument
42 ASSERT(tpool->tp_current == 0 && tpool->tp_active == NULL); in delete_pool()
48 if (thread_pools == tpool) in delete_pool()
49 thread_pools = tpool->tp_forw; in delete_pool()
50 if (thread_pools == tpool) in delete_pool()
53 tpool->tp_back->tp_forw = tpool->tp_forw; in delete_pool()
54 tpool->tp_forw->tp_back = tpool->tp_back; in delete_pool()
61 for (job = tpool->tp_head; job != NULL; job = tpool->tp_head) { in delete_pool()
62 tpool->tp_head = job->tpj_next; in delete_pool()
65 (void) pthread_attr_destroy(&tpool->tp_attr); in delete_pool()
66 free(tpool); in delete_pool()
75 tpool_t *tpool = (tpool_t *)arg; in worker_cleanup() local
77 if (--tpool->tp_current == 0 && in worker_cleanup()
78 (tpool->tp_flags & (TP_DESTROY | TP_ABANDON))) { in worker_cleanup()
79 if (tpool->tp_flags & TP_ABANDON) { in worker_cleanup()
80 pthread_mutex_unlock(&tpool->tp_mutex); in worker_cleanup()
81 delete_pool(tpool); in worker_cleanup()
84 if (tpool->tp_flags & TP_DESTROY) in worker_cleanup()
85 (void) pthread_cond_broadcast(&tpool->tp_busycv); in worker_cleanup()
87 pthread_mutex_unlock(&tpool->tp_mutex); in worker_cleanup()
91 notify_waiters(tpool_t *tpool) in notify_waiters() argument
93 if (tpool->tp_head == NULL && tpool->tp_active == NULL) { in notify_waiters()
94 tpool->tp_flags &= ~TP_WAIT; in notify_waiters()
95 (void) pthread_cond_broadcast(&tpool->tp_waitcv); in notify_waiters()
105 tpool_t *tpool = (tpool_t *)arg; in job_cleanup() local
111 pthread_mutex_lock(&tpool->tp_mutex); in job_cleanup()
112 for (activepp = &tpool->tp_active; ; activepp = &activep->tpa_next) { in job_cleanup()
119 if (tpool->tp_flags & TP_WAIT) in job_cleanup()
120 notify_waiters(tpool); in job_cleanup()
126 tpool_t *tpool = (tpool_t *)arg; in tpool_worker() local
132 pthread_mutex_lock(&tpool->tp_mutex); in tpool_worker()
133 pthread_cleanup_push(worker_cleanup, tpool); in tpool_worker()
142 tpool->tp_idle++; in tpool_worker()
143 if (tpool->tp_flags & TP_WAIT) in tpool_worker()
144 notify_waiters(tpool); in tpool_worker()
145 while ((tpool->tp_head == NULL || in tpool_worker()
146 (tpool->tp_flags & TP_SUSPEND)) && in tpool_worker()
147 !(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))) { in tpool_worker()
148 if (tpool->tp_current <= tpool->tp_minimum || in tpool_worker()
149 tpool->tp_linger == 0) { in tpool_worker()
150 (void) pthread_cond_wait(&tpool->tp_workcv, in tpool_worker()
151 &tpool->tp_mutex); in tpool_worker()
156 ts.tv_sec += tpool->tp_linger; in tpool_worker()
158 if (pthread_cond_timedwait(&tpool->tp_workcv, in tpool_worker()
159 &tpool->tp_mutex, &ts) != 0) { in tpool_worker()
165 tpool->tp_idle--; in tpool_worker()
166 if (tpool->tp_flags & TP_DESTROY) in tpool_worker()
168 if (tpool->tp_flags & TP_ABANDON) { in tpool_worker()
170 if (tpool->tp_flags & TP_SUSPEND) { in tpool_worker()
171 tpool->tp_flags &= ~TP_SUSPEND; in tpool_worker()
173 &tpool->tp_workcv); in tpool_worker()
175 if (tpool->tp_head == NULL) in tpool_worker()
178 if ((job = tpool->tp_head) != NULL && in tpool_worker()
179 !(tpool->tp_flags & TP_SUSPEND)) { in tpool_worker()
183 tpool->tp_head = job->tpj_next; in tpool_worker()
184 if (job == tpool->tp_tail) in tpool_worker()
185 tpool->tp_tail = NULL; in tpool_worker()
186 tpool->tp_njobs--; in tpool_worker()
187 active.tpa_next = tpool->tp_active; in tpool_worker()
188 tpool->tp_active = &active; in tpool_worker()
189 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_worker()
190 pthread_cleanup_push(job_cleanup, tpool); in tpool_worker()
212 if (elapsed && tpool->tp_current > tpool->tp_minimum) { in tpool_worker()
229 create_worker(tpool_t *tpool) in create_worker() argument
236 error = pthread_create(&thread, &tpool->tp_attr, tpool_worker, tpool); in create_worker()
325 tpool_t *tpool; in tpool_create() local
356 tpool = calloc(1, sizeof (*tpool)); in tpool_create()
357 if (tpool == NULL) { in tpool_create()
361 (void) pthread_mutex_init(&tpool->tp_mutex, NULL); in tpool_create()
362 (void) pthread_cond_init(&tpool->tp_busycv, NULL); in tpool_create()
363 (void) pthread_cond_init(&tpool->tp_workcv, NULL); in tpool_create()
364 (void) pthread_cond_init(&tpool->tp_waitcv, NULL); in tpool_create()
365 tpool->tp_minimum = min_threads; in tpool_create()
366 tpool->tp_maximum = max_threads; in tpool_create()
367 tpool->tp_linger = linger; in tpool_create()
376 error = pthread_attr_clone(&tpool->tp_attr, attr); in tpool_create()
378 free(tpool); in tpool_create()
384 (void) pthread_attr_setdetachstate(&tpool->tp_attr, in tpool_create()
390 tpool->tp_forw = tpool; in tpool_create()
391 tpool->tp_back = tpool; in tpool_create()
392 thread_pools = tpool; in tpool_create()
394 thread_pools->tp_back->tp_forw = tpool; in tpool_create()
395 tpool->tp_forw = thread_pools; in tpool_create()
396 tpool->tp_back = thread_pools->tp_back; in tpool_create()
397 thread_pools->tp_back = tpool; in tpool_create()
401 return (tpool); in tpool_create()
412 tpool_dispatch(tpool_t *tpool, void (*func)(void *), void *arg) in tpool_dispatch() argument
416 ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))); in tpool_dispatch()
424 pthread_mutex_lock(&tpool->tp_mutex); in tpool_dispatch()
426 if (tpool->tp_head == NULL) in tpool_dispatch()
427 tpool->tp_head = job; in tpool_dispatch()
429 tpool->tp_tail->tpj_next = job; in tpool_dispatch()
430 tpool->tp_tail = job; in tpool_dispatch()
431 tpool->tp_njobs++; in tpool_dispatch()
433 if (!(tpool->tp_flags & TP_SUSPEND)) { in tpool_dispatch()
434 if (tpool->tp_idle > 0) in tpool_dispatch()
435 (void) pthread_cond_signal(&tpool->tp_workcv); in tpool_dispatch()
436 else if (tpool->tp_current < tpool->tp_maximum && in tpool_dispatch()
437 create_worker(tpool) == 0) in tpool_dispatch()
438 tpool->tp_current++; in tpool_dispatch()
441 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_dispatch()
448 tpool_t *tpool = (tpool_t *)arg; in tpool_cleanup() local
450 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_cleanup()
459 tpool_destroy(tpool_t *tpool) in tpool_destroy() argument
463 ASSERT(!tpool_member(tpool)); in tpool_destroy()
464 ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))); in tpool_destroy()
466 pthread_mutex_lock(&tpool->tp_mutex); in tpool_destroy()
467 pthread_cleanup_push(tpool_cleanup, tpool); in tpool_destroy()
470 tpool->tp_flags |= TP_DESTROY; in tpool_destroy()
471 tpool->tp_flags &= ~TP_SUSPEND; in tpool_destroy()
472 (void) pthread_cond_broadcast(&tpool->tp_workcv); in tpool_destroy()
475 for (activep = tpool->tp_active; activep; activep = activep->tpa_next) in tpool_destroy()
479 while (tpool->tp_active != NULL) { in tpool_destroy()
480 tpool->tp_flags |= TP_WAIT; in tpool_destroy()
481 (void) pthread_cond_wait(&tpool->tp_waitcv, &tpool->tp_mutex); in tpool_destroy()
485 while (tpool->tp_current != 0) in tpool_destroy()
486 (void) pthread_cond_wait(&tpool->tp_busycv, &tpool->tp_mutex); in tpool_destroy()
489 delete_pool(tpool); in tpool_destroy()
497 tpool_abandon(tpool_t *tpool) in tpool_abandon() argument
499 ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))); in tpool_abandon()
501 pthread_mutex_lock(&tpool->tp_mutex); in tpool_abandon()
502 if (tpool->tp_current == 0) { in tpool_abandon()
504 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_abandon()
505 delete_pool(tpool); in tpool_abandon()
508 tpool->tp_flags |= TP_ABANDON; in tpool_abandon()
509 tpool->tp_flags &= ~TP_SUSPEND; in tpool_abandon()
510 (void) pthread_cond_broadcast(&tpool->tp_workcv); in tpool_abandon()
511 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_abandon()
520 tpool_wait(tpool_t *tpool) in tpool_wait() argument
522 ASSERT(!tpool_member(tpool)); in tpool_wait()
523 ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))); in tpool_wait()
525 pthread_mutex_lock(&tpool->tp_mutex); in tpool_wait()
526 pthread_cleanup_push(tpool_cleanup, tpool); in tpool_wait()
527 while (tpool->tp_head != NULL || tpool->tp_active != NULL) { in tpool_wait()
528 tpool->tp_flags |= TP_WAIT; in tpool_wait()
529 (void) pthread_cond_wait(&tpool->tp_waitcv, &tpool->tp_mutex); in tpool_wait()
530 ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))); in tpool_wait()
536 tpool_suspend(tpool_t *tpool) in tpool_suspend() argument
538 ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))); in tpool_suspend()
540 pthread_mutex_lock(&tpool->tp_mutex); in tpool_suspend()
541 tpool->tp_flags |= TP_SUSPEND; in tpool_suspend()
542 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_suspend()
546 tpool_suspended(tpool_t *tpool) in tpool_suspended() argument
550 ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))); in tpool_suspended()
552 pthread_mutex_lock(&tpool->tp_mutex); in tpool_suspended()
553 suspended = (tpool->tp_flags & TP_SUSPEND) != 0; in tpool_suspended()
554 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_suspended()
560 tpool_resume(tpool_t *tpool) in tpool_resume() argument
564 ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))); in tpool_resume()
566 pthread_mutex_lock(&tpool->tp_mutex); in tpool_resume()
567 if (!(tpool->tp_flags & TP_SUSPEND)) { in tpool_resume()
568 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_resume()
571 tpool->tp_flags &= ~TP_SUSPEND; in tpool_resume()
572 (void) pthread_cond_broadcast(&tpool->tp_workcv); in tpool_resume()
573 excess = tpool->tp_njobs - tpool->tp_idle; in tpool_resume()
574 while (excess-- > 0 && tpool->tp_current < tpool->tp_maximum) { in tpool_resume()
575 if (create_worker(tpool) != 0) in tpool_resume()
577 tpool->tp_current++; in tpool_resume()
579 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_resume()
583 tpool_member(tpool_t *tpool) in tpool_member() argument
588 ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))); in tpool_member()
590 pthread_mutex_lock(&tpool->tp_mutex); in tpool_member()
591 for (activep = tpool->tp_active; activep; activep = activep->tpa_next) { in tpool_member()
593 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_member()
597 pthread_mutex_unlock(&tpool->tp_mutex); in tpool_member()