diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.c b/src/mod/endpoints/mod_sofia/mod_sofia.c index 250c68bdc2..ca6a899222 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.c +++ b/src/mod/endpoints/mod_sofia/mod_sofia.c @@ -375,7 +375,7 @@ static void terminate_session(switch_core_session_t **session, switch_call_cause switch_channel_t *channel = switch_core_session_get_channel(*session); switch_channel_state_t state = switch_channel_get_state(channel); struct private_object *tech_pvt = NULL; - + tech_pvt = switch_core_session_get_private(*session); if (tech_pvt) { @@ -456,7 +456,6 @@ static void do_invite(switch_core_session_t *session) switch_set_flag_locked(tech_pvt, TFLAG_READY); tech_pvt->nh = nua_handle(tech_pvt->profile->nua, NULL, SIPTAG_TO_STR(tech_pvt->dest), TAG_END()); - nua_handle_bind(tech_pvt->nh, session); nua_invite(tech_pvt->nh, SOATAG_USER_SDP_STR(tech_pvt->local_sdp_str), @@ -1560,7 +1559,12 @@ static void event_callback(nua_event_t event, sip_t const *sip, tagi_t tags[]) { - + if (session) { + if (switch_core_session_read_lock(session) != SWITCH_STATUS_SUCCESS) { + /* too late */ + return; + } + } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "channel [%s] event [%s] status [%d] [%s]\n", session ? switch_channel_get_name(switch_core_session_get_channel(session)) : "null",nua_event_name (event), status, phrase); @@ -1681,6 +1685,10 @@ static void event_callback(nua_event_t event, break; } + + if (session) { + switch_core_session_rwunlock(session); + } } static void *SWITCH_THREAD_FUNC profile_thread_run(switch_thread_t *thread, void *obj) diff --git a/src/switch_channel.c b/src/switch_channel.c index 1655863286..ad909c7587 100644 --- a/src/switch_channel.c +++ b/src/switch_channel.c @@ -381,8 +381,14 @@ SWITCH_DECLARE(void) switch_channel_clear_flag(switch_channel_t *channel, switch SWITCH_DECLARE(switch_channel_state_t) switch_channel_get_state(switch_channel_t *channel) { + switch_channel_state_t state; assert(channel != NULL); - return channel->state; + + switch_mutex_lock(channel->flag_mutex); + state = channel->state; + switch_mutex_unlock(channel->flag_mutex); + + return state; } SWITCH_DECLARE(unsigned int) switch_channel_ready(switch_channel_t *channel) @@ -433,16 +439,18 @@ SWITCH_DECLARE(switch_channel_state_t) switch_channel_perform_set_state(switch_c assert(channel != NULL); + switch_mutex_lock(channel->flag_mutex); + last_state = channel->state; if (last_state == state) { - return state; + goto done; } if (last_state >= CS_HANGUP && state < last_state) { - return last_state; + goto done; } - + /* STUB for more dev case CS_INIT: switch(state) { @@ -589,11 +597,11 @@ SWITCH_DECLARE(switch_channel_state_t) switch_channel_perform_set_state(switch_c switch_event_fire(&event); } } - /* + if (state < CS_DONE) { switch_core_session_signal_state_change(channel->session); } - */ + } else { switch_log_printf(SWITCH_CHANNEL_ID_LOG, (char *) file, func, line, SWITCH_LOG_WARNING, "%s Invalid State Change %s -> %s\n", channel->name, @@ -606,6 +614,8 @@ SWITCH_DECLARE(switch_channel_state_t) switch_channel_perform_set_state(switch_c assert(0); } } + done: + switch_mutex_unlock(channel->flag_mutex); return channel->state; } @@ -854,6 +864,7 @@ SWITCH_DECLARE(switch_channel_state_t) switch_channel_perform_hangup(switch_chan switch_call_cause_t hangup_cause) { assert(channel != NULL); + switch_mutex_lock(channel->flag_mutex); if (channel->times && !channel->times->hungup) { channel->times->hungup = switch_time_now(); @@ -862,9 +873,7 @@ SWITCH_DECLARE(switch_channel_state_t) switch_channel_perform_hangup(switch_chan if (channel->state < CS_HANGUP) { switch_event_t *event; switch_channel_state_t last_state = channel->state; - switch_mutex_lock(channel->flag_mutex); channel->state = CS_HANGUP; - switch_mutex_unlock(channel->flag_mutex); channel->hangup_cause = hangup_cause; switch_log_printf(SWITCH_CHANNEL_ID_LOG, (char *) file, func, line, SWITCH_LOG_NOTICE, "Hangup %s [%s] [%s]\n", channel->name, @@ -876,8 +885,10 @@ SWITCH_DECLARE(switch_channel_state_t) switch_channel_perform_hangup(switch_chan } switch_core_session_kill_channel(channel->session, SWITCH_SIG_KILL); - //switch_core_session_signal_state_change(channel->session); + switch_core_session_signal_state_change(channel->session); } + + switch_mutex_unlock(channel->flag_mutex); return channel->state; } diff --git a/src/switch_core.c b/src/switch_core.c index d91091a5bf..065fc9c274 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -90,8 +90,8 @@ struct switch_core_session { switch_audio_resampler_t *read_resampler; switch_audio_resampler_t *write_resampler; - //switch_mutex_t *mutex; - //switch_thread_cond_t *cond; + switch_mutex_t *mutex; + switch_thread_cond_t *cond; switch_thread_rwlock_t *rwlock; @@ -2489,13 +2489,13 @@ static void switch_core_standard_on_hold(switch_core_session_t *session) SWITCH_DECLARE(void) switch_core_session_signal_state_change(switch_core_session_t *session) { - return; - /* If trylock fails the signal is already awake so we needn't bother + + /* If trylock fails the signal is already awake so we needn't bother */ if (switch_mutex_trylock(session->mutex) == SWITCH_STATUS_SUCCESS) { switch_thread_cond_signal(session->cond); switch_mutex_unlock(session->mutex); } - */ + } SWITCH_DECLARE(unsigned int) switch_core_session_runing(switch_core_session_t *session) @@ -2555,7 +2555,7 @@ static int handle_fatality(int sig) SWITCH_DECLARE(void) switch_core_session_run(switch_core_session_t *session) { - switch_channel_state_t state = CS_NEW, laststate = CS_HANGUP, midstate = CS_DONE; + switch_channel_state_t state = CS_NEW, laststate = CS_HANGUP, midstate = CS_DONE, endstate; const switch_endpoint_interface_t *endpoint_interface; const switch_state_handler_table_t *driver_state_handler = NULL; const switch_state_handler_table_t *application_state_handler = NULL; @@ -2607,7 +2607,7 @@ SWITCH_DECLARE(void) switch_core_session_run(switch_core_session_t *session) driver_state_handler = endpoint_interface->state_handler; assert(driver_state_handler != NULL); - //switch_mutex_lock(session->mutex); + switch_mutex_lock(session->mutex); while ((state = switch_channel_get_state(session->channel)) != CS_DONE) { if (state != laststate) { @@ -2886,20 +2886,20 @@ SWITCH_DECLARE(void) switch_core_session_run(switch_core_session_t *session) laststate = midstate; } - if (state >= CS_HANGUP) { + + endstate = switch_channel_get_state(session->channel); + + if (endstate >= CS_HANGUP) { goto done; } - if (midstate == switch_channel_get_state(session->channel)) { - //switch_thread_cond_wait(session->cond, session->mutex); - switch_yield(10000); - } else { - switch_yield(1000); + if (midstate == endstate) { + switch_thread_cond_wait(session->cond, session->mutex); } } done: - //switch_mutex_unlock(session->mutex); + switch_mutex_unlock(session->mutex); #ifdef CRASH_PROT apr_hash_set(runtime.stack_table, &thread_id, sizeof(thread_id), NULL); @@ -3151,9 +3151,9 @@ SWITCH_DECLARE(switch_core_session_t *) switch_core_session_request(const switch session->enc_read_frame.data = session->enc_read_buf; session->enc_read_frame.buflen = sizeof(session->enc_read_buf); - //switch_mutex_init(&session->mutex, SWITCH_MUTEX_NESTED, session->pool); + switch_mutex_init(&session->mutex, SWITCH_MUTEX_NESTED, session->pool); switch_thread_rwlock_create(&session->bug_rwlock, session->pool); - //switch_thread_cond_create(&session->cond, session->pool); + switch_thread_cond_create(&session->cond, session->pool); switch_thread_rwlock_create(&session->rwlock, session->pool); switch_mutex_lock(runtime.session_table_mutex);