[Freeswitch-svn] [commit] r2709 - in freeswitch/trunk/src: . mod/endpoints/mod_sofia

Freeswitch SVN anthm at freeswitch.org
Fri Sep 15 20:43:59 EDT 2006


Author: anthm
Date: Fri Sep 15 20:43:58 2006
New Revision: 2709

Modified:
   freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c
   freeswitch/trunk/src/switch_channel.c
   freeswitch/trunk/src/switch_core.c

Log:
ok maybe this will fix the conditional mutext race we will readlock the session during any callbacks and cancel the operation if read_lock fails

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c	Fri Sep 15 20:43:58 2006
@@ -375,7 +375,7 @@
 		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 @@
 	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 @@
 						   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);
 	
@@ -1680,6 +1684,10 @@
 		//tl_print(stdout, "", tags);
 		break;
 
+	}
+
+	if (session) {
+		switch_core_session_rwunlock(session);
 	}
 }
 

Modified: freeswitch/trunk/src/switch_channel.c
==============================================================================
--- freeswitch/trunk/src/switch_channel.c	(original)
+++ freeswitch/trunk/src/switch_channel.c	Fri Sep 15 20:43:58 2006
@@ -381,8 +381,14 @@
 
 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 @@
 
 
 	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_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 @@
 			assert(0);
 		}
 	}
+ done:
+	switch_mutex_unlock(channel->flag_mutex);
 	return channel->state;
 }
 
@@ -854,6 +864,7 @@
 																   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 @@
 	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_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;
 }
 

Modified: freeswitch/trunk/src/switch_core.c
==============================================================================
--- freeswitch/trunk/src/switch_core.c	(original)
+++ freeswitch/trunk/src/switch_core.c	Fri Sep 15 20:43:58 2006
@@ -90,8 +90,8 @@
 	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 @@
 
 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 @@
 
 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 @@
 	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 @@
 			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 @@
 	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);



More information about the Freeswitch-svn mailing list