thread pool was not releasing surplus threads after timeout

This commit is contained in:
Anthony Minessale 2012-11-14 18:43:22 -06:00
parent a47321f0fd
commit f8aa3777e9
2 changed files with 21 additions and 6 deletions

View File

@ -285,6 +285,7 @@ struct switch_session_manager {
int ready; int ready;
int running; int running;
int busy; int busy;
int nuking;
}; };
extern struct switch_session_manager session_manager; extern struct switch_session_manager session_manager;

View File

@ -1530,13 +1530,15 @@ static void *SWITCH_THREAD_FUNC switch_core_session_thread_pool_worker(switch_th
while(session_manager.ready) { while(session_manager.ready) {
switch_status_t check_status; switch_status_t check_status;
pop = NULL;
if (check) { if (check) {
check_status = switch_queue_trypop(session_manager.thread_queue, &pop); check_status = switch_queue_trypop(session_manager.thread_queue, &pop);
} else { } else {
check_status = switch_queue_pop(session_manager.thread_queue, &pop); check_status = switch_queue_pop(session_manager.thread_queue, &pop);
} }
if (check_status == SWITCH_STATUS_SUCCESS) { if (check_status == SWITCH_STATUS_SUCCESS && pop) {
switch_thread_data_t *td = (switch_thread_data_t *) pop; switch_thread_data_t *td = (switch_thread_data_t *) pop;
if (!td) break; if (!td) break;
@ -1609,12 +1611,11 @@ static switch_status_t check_queue(void)
int x = 0; int x = 0;
switch_mutex_lock(session_manager.mutex); switch_mutex_lock(session_manager.mutex);
ttl = switch_queue_size(session_manager.thread_queue); ttl = switch_queue_size(session_manager.thread_queue) - session_manager.nuking;
x = (session_manager.running - session_manager.busy); x = (session_manager.running - session_manager.busy);
switch_mutex_unlock(session_manager.mutex); switch_mutex_unlock(session_manager.mutex);
while (x < ttl) { while (x < ttl) {
switch_thread_t *thread; switch_thread_t *thread;
switch_threadattr_t *thd_attr; switch_threadattr_t *thd_attr;
@ -1653,9 +1654,22 @@ static void *SWITCH_THREAD_FUNC switch_core_session_thread_pool_manager(switch_t
switch_yield(100000); switch_yield(100000);
if (++x == 300) { if (++x == 300) {
switch_queue_interrupt_all(session_manager.thread_queue); switch_mutex_lock(session_manager.mutex);
session_manager.nuking = (session_manager.running - session_manager.busy);
switch_mutex_unlock(session_manager.mutex);
if (session_manager.nuking) {
int i = 0;
for (i = 0; i < session_manager.nuking; i++) {
switch_queue_push(session_manager.thread_queue, NULL);
}
x--;
} else {
x = 0; x = 0;
} }
}
check_queue(); check_queue();
} }