diff --git a/src/include/switch_ivr.h b/src/include/switch_ivr.h index 20e1cdfa45..155ba583fb 100644 --- a/src/include/switch_ivr.h +++ b/src/include/switch_ivr.h @@ -60,6 +60,7 @@ SWITCH_BEGIN_EXTERN_C struct switch_unicast_conninfo { int type; int transport; int stream_id; + switch_thread_t *thread; }; typedef struct switch_unicast_conninfo switch_unicast_conninfo_t; diff --git a/src/switch_core_memory.c b/src/switch_core_memory.c index 74e0fa70e0..7b1ae8f1c2 100644 --- a/src/switch_core_memory.c +++ b/src/switch_core_memory.c @@ -662,7 +662,6 @@ switch_memory_pool_t *switch_core_memory_init(void) switch_queue_create(&memory_manager.pool_recycle_queue, 50000, memory_manager.memory_pool); switch_threadattr_create(&thd_attr, memory_manager.memory_pool); - switch_threadattr_detach_set(thd_attr, 0); switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE); switch_thread_create(&pool_thread_p, thd_attr, pool_thread, NULL, memory_manager.memory_pool); diff --git a/src/switch_ivr.c b/src/switch_ivr.c index ad9b6b1139..17289cec2e 100644 --- a/src/switch_ivr.c +++ b/src/switch_ivr.c @@ -348,14 +348,12 @@ static void *SWITCH_THREAD_FUNC unicast_thread_run(switch_thread_t *thread, void static void unicast_thread_launch(switch_unicast_conninfo_t *conninfo) { - switch_thread_t *thread; switch_threadattr_t *thd_attr = NULL; switch_threadattr_create(&thd_attr, switch_core_session_get_pool(conninfo->session)); - switch_threadattr_detach_set(thd_attr, 1); switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE); switch_set_flag_locked(conninfo, SUF_THREAD_RUNNING); - switch_thread_create(&thread, thd_attr, unicast_thread_run, conninfo, switch_core_session_get_pool(conninfo->session)); + switch_thread_create(&conninfo->thread, thd_attr, unicast_thread_run, conninfo, switch_core_session_get_pool(conninfo->session)); } SWITCH_DECLARE(switch_status_t) switch_ivr_deactivate_unicast(switch_core_session_t *session) @@ -369,9 +367,13 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_deactivate_unicast(switch_core_sessio } if ((conninfo = switch_channel_get_private(channel, "unicast"))) { + switch_status_t st; + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Shutting down unicast connection\n"); switch_clear_flag_locked(conninfo, SUF_READY); switch_socket_shutdown(conninfo->socket, SWITCH_SHUTDOWN_READWRITE); + switch_thread_join(&st, conninfo->thread); + while (switch_test_flag(conninfo, SUF_THREAD_RUNNING)) { switch_yield(10000); if (++sanity >= 10000) { diff --git a/src/switch_ivr_async.c b/src/switch_ivr_async.c index 67cd095474..bc4d724a50 100644 --- a/src/switch_ivr_async.c +++ b/src/switch_ivr_async.c @@ -4012,6 +4012,7 @@ struct speech_thread_handle { switch_mutex_t *mutex; switch_thread_cond_t *cond; switch_memory_pool_t *pool; + switch_thread_t *thread; int ready; }; @@ -4175,23 +4176,29 @@ static switch_bool_t speech_callback(switch_media_bug_t *bug, void *user_data, s frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE; switch (type) { - case SWITCH_ABC_TYPE_INIT:{ - switch_thread_t *thread; + case SWITCH_ABC_TYPE_INIT: + { switch_threadattr_t *thd_attr = NULL; - + switch_threadattr_create(&thd_attr, sth->pool); - switch_threadattr_detach_set(thd_attr, 1); switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE); - switch_thread_create(&thread, thd_attr, speech_thread, sth, sth->pool); + switch_thread_create(&sth->thread, thd_attr, speech_thread, sth, sth->pool); } break; - case SWITCH_ABC_TYPE_CLOSE:{ + case SWITCH_ABC_TYPE_CLOSE: + { + switch_status_t st; + switch_core_asr_close(sth->ah, &flags); if (sth->mutex && sth->cond && sth->ready) { - switch_mutex_lock(sth->mutex); - switch_thread_cond_signal(sth->cond); - switch_mutex_unlock(sth->mutex); + if (switch_mutex_trylock(sth->mutex) == SWITCH_STATUS_SUCCESS) { + switch_thread_cond_signal(sth->cond); + switch_mutex_unlock(sth->mutex); + } } + + switch_thread_join(&st, sth->thread); + } break; case SWITCH_ABC_TYPE_READ: diff --git a/src/switch_log.c b/src/switch_log.c index 00ae7c0ef1..c512b2d0c5 100644 --- a/src/switch_log.c +++ b/src/switch_log.c @@ -512,8 +512,6 @@ SWITCH_DECLARE(switch_status_t) switch_log_init(switch_memory_pool_t *pool, swit LOG_POOL = pool; switch_threadattr_create(&thd_attr, LOG_POOL); - switch_threadattr_detach_set(thd_attr, 1); - switch_queue_create(&LOG_QUEUE, SWITCH_CORE_QUEUE_LEN, LOG_POOL); #ifdef SWITCH_LOG_RECYCLE diff --git a/src/switch_nat.c b/src/switch_nat.c index d1ab09eba1..e18ad261f7 100644 --- a/src/switch_nat.c +++ b/src/switch_nat.c @@ -365,7 +365,6 @@ SWITCH_DECLARE(void) switch_nat_thread_start(void) } switch_threadattr_create(&thd_attr, nat_globals_perm.pool); - switch_threadattr_detach_set(thd_attr, 1); switch_thread_create(&nat_thread_p, thd_attr, switch_nat_multicast_runtime, NULL, nat_globals_perm.pool); } diff --git a/src/switch_scheduler.c b/src/switch_scheduler.c index e734bad087..16fc59df40 100644 --- a/src/switch_scheduler.c +++ b/src/switch_scheduler.c @@ -335,7 +335,6 @@ SWITCH_DECLARE(void) switch_scheduler_task_thread_start(void) switch_mutex_init(&globals.task_mutex, SWITCH_MUTEX_NESTED, globals.memory_pool); switch_queue_create(&globals.event_queue, 250000, globals.memory_pool); - switch_threadattr_detach_set(thd_attr, 1); switch_thread_create(&task_thread_p, thd_attr, switch_scheduler_task_thread, NULL, globals.memory_pool); }