[asterisk-commits] file: branch 12 r404204 - in /branches/12: addons/ apps/ apps/confbridge/ cha...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Dec 18 13:20:15 CST 2013


Author: file
Date: Wed Dec 18 13:20:02 2013
New Revision: 404204

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=404204
Log:
channels: Return allocated channels locked.

This change makes ast_channel_alloc return allocated channels
locked. By doing so no other thread can acquire, lock, and manipulate
the channel before it is completely set up.

(closes issue AST-1256)

Review: https://reviewboard.asterisk.org/r/3067/

Modified:
    branches/12/addons/chan_mobile.c
    branches/12/addons/chan_ooh323.c
    branches/12/apps/app_meetme.c
    branches/12/apps/app_voicemail.c
    branches/12/apps/confbridge/conf_chan_record.c
    branches/12/channels/chan_alsa.c
    branches/12/channels/chan_console.c
    branches/12/channels/chan_dahdi.c
    branches/12/channels/chan_gtalk.c
    branches/12/channels/chan_h323.c
    branches/12/channels/chan_iax2.c
    branches/12/channels/chan_jingle.c
    branches/12/channels/chan_mgcp.c
    branches/12/channels/chan_misdn.c
    branches/12/channels/chan_motif.c
    branches/12/channels/chan_multicast_rtp.c
    branches/12/channels/chan_nbs.c
    branches/12/channels/chan_oss.c
    branches/12/channels/chan_phone.c
    branches/12/channels/chan_pjsip.c
    branches/12/channels/chan_sip.c
    branches/12/channels/chan_skinny.c
    branches/12/channels/chan_unistim.c
    branches/12/channels/chan_vpb.cc
    branches/12/include/asterisk/channel.h
    branches/12/main/channel.c
    branches/12/main/core_unreal.c
    branches/12/main/message.c
    branches/12/main/pbx.c
    branches/12/res/parking/parking_tests.c
    branches/12/res/res_calendar.c
    branches/12/res/res_stasis_snoop.c
    branches/12/tests/test_app.c
    branches/12/tests/test_cdr.c
    branches/12/tests/test_cel.c
    branches/12/tests/test_stasis_channels.c
    branches/12/tests/test_substitution.c
    branches/12/tests/test_voicemail_api.c

Modified: branches/12/addons/chan_mobile.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/addons/chan_mobile.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/addons/chan_mobile.c (original)
+++ branches/12/addons/chan_mobile.c Wed Dec 18 13:20:02 2013
@@ -878,6 +878,8 @@
 	if (pvt->sco_socket != -1) {
 		ast_channel_set_fd(chn, 0, pvt->sco_socket);
 	}
+
+	ast_channel_unlock(chn);
 
 	return chn;
 

Modified: branches/12/addons/chan_ooh323.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/addons/chan_ooh323.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/addons/chan_ooh323.c (original)
+++ branches/12/addons/chan_ooh323.c Wed Dec 18 13:20:02 2013
@@ -395,7 +395,6 @@
 	ast_mutex_lock(&i->lock);
 
 	if (ch) {
-		ast_channel_lock(ch);
 		ast_channel_tech_set(ch, &ooh323_tech);
 
 		if (cap)

Modified: branches/12/apps/app_meetme.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/apps/app_meetme.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/apps/app_meetme.c (original)
+++ branches/12/apps/app_meetme.c Wed Dec 18 13:20:02 2013
@@ -8140,6 +8140,8 @@
 		return AST_TEST_FAIL;
 	}
 
+	ast_channel_unlock(chan);
+
 	cnf = build_conf("9898", "", "1234", 1, 1, 1, chan, test);
 	if (!cnf) {
 		ast_test_status_update(test, "Build of test conference 9898 failed\n");

Modified: branches/12/apps/app_voicemail.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/apps/app_voicemail.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/apps/app_voicemail.c (original)
+++ branches/12/apps/app_voicemail.c Wed Dec 18 13:20:02 2013
@@ -13744,6 +13744,8 @@
 	ast_format_set(ast_channel_rawreadformat(test_channel1), AST_FORMAT_GSM, 0);
 	ast_channel_tech_set(test_channel1, &fake_tech);
 
+	ast_channel_unlock(test_channel1);
+
 	ast_test_status_update(test, "Test playing of extension when greeting is not available...\n");
 	snprintf(dir, sizeof(dir), "%s@%s", TEST_EXTENSION, TEST_CONTEXT); /* not a dir, don't get confused */
 	if (!(res = vmsayname_exec(test_channel1, dir))) {

Modified: branches/12/apps/confbridge/conf_chan_record.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/apps/confbridge/conf_chan_record.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/apps/confbridge/conf_chan_record.c (original)
+++ branches/12/apps/confbridge/conf_chan_record.c Wed Dec 18 13:20:02 2013
@@ -66,6 +66,7 @@
 		return NULL;
 	}
 	if (ast_channel_add_bridge_role(chan, "recorder")) {
+		ast_channel_unlock(chan);
 		ast_channel_release(chan);
 		return NULL;
 	}
@@ -76,6 +77,7 @@
 	ast_format_copy(ast_channel_rawwriteformat(chan), &format);
 	ast_format_copy(ast_channel_readformat(chan), &format);
 	ast_format_copy(ast_channel_rawreadformat(chan), &format);
+	ast_channel_unlock(chan);
 	return chan;
 }
 

Modified: branches/12/channels/chan_alsa.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/channels/chan_alsa.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/channels/chan_alsa.c (original)
+++ branches/12/channels/chan_alsa.c Wed Dec 18 13:20:02 2013
@@ -602,6 +602,8 @@
 
 	ast_channel_stage_snapshot_done(tmp);
 
+	ast_channel_unlock(tmp);
+
 	if (state != AST_STATE_DOWN) {
 		if (ast_pbx_start(tmp)) {
 			ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(tmp));

Modified: branches/12/channels/chan_console.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/channels/chan_console.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/channels/chan_console.c (original)
+++ branches/12/channels/chan_console.c Wed Dec 18 13:20:02 2013
@@ -445,6 +445,8 @@
 
 	ast_channel_stage_snapshot_done(chan);
 
+	ast_channel_unlock(chan);
+
 	if (state != AST_STATE_DOWN) {
 		if (ast_pbx_start(chan)) {
 			ast_channel_hangupcause_set(chan, AST_CAUSE_SWITCH_CONGESTION);

Modified: branches/12/channels/chan_dahdi.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/channels/chan_dahdi.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/channels/chan_dahdi.c (original)
+++ branches/12/channels/chan_dahdi.c Wed Dec 18 13:20:02 2013
@@ -9087,6 +9087,8 @@
 		pbx_builtin_setvar_helper(tmp, v->name, v->value);
 
 	ast_channel_stage_snapshot_done(tmp);
+
+	ast_channel_unlock(tmp);
 
 	ast_module_ref(ast_module_info->self);
 

Modified: branches/12/channels/chan_gtalk.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/channels/chan_gtalk.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/channels/chan_gtalk.c (original)
+++ branches/12/channels/chan_gtalk.c Wed Dec 18 13:20:02 2013
@@ -1226,6 +1226,8 @@
 		ast_jb_configure(tmp, &global_jbconf);
 
 	ast_channel_stage_snapshot_done(tmp);
+
+	ast_channel_unlock(tmp);
 
 	if (state != AST_STATE_DOWN && ast_pbx_start(tmp)) {
 		ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(tmp));

Modified: branches/12/channels/chan_h323.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/channels/chan_h323.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/channels/chan_h323.c (original)
+++ branches/12/channels/chan_h323.c Wed Dec 18 13:20:02 2013
@@ -1139,6 +1139,7 @@
 		}
 		if (pvt->cd.transfer_capability >= 0)
 			ast_channel_transfercapability_set(ch, pvt->cd.transfer_capability);
+		ast_channel_unlock(ch);
 		if (state != AST_STATE_DOWN) {
 			if (ast_pbx_start(ch)) {
 				ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(ch));

Modified: branches/12/channels/chan_iax2.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/channels/chan_iax2.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/channels/chan_iax2.c (original)
+++ branches/12/channels/chan_iax2.c Wed Dec 18 13:20:02 2013
@@ -5694,6 +5694,7 @@
 		if (tmp) {
 			/* unlock and relock iaxsl[callno] to preserve locking order */
 			ast_mutex_unlock(&iaxsl[callno]);
+			ast_channel_unlock(tmp);
 			tmp = ast_channel_release(tmp);
 			ast_mutex_lock(&iaxsl[callno]);
 		}
@@ -5803,6 +5804,8 @@
 	}
 
 	ast_channel_stage_snapshot_done(tmp);
+
+	ast_channel_unlock(tmp);
 
 	if (state != AST_STATE_DOWN) {
 		if (ast_pbx_start(tmp)) {

Modified: branches/12/channels/chan_jingle.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/channels/chan_jingle.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/channels/chan_jingle.c (original)
+++ branches/12/channels/chan_jingle.c Wed Dec 18 13:20:02 2013
@@ -941,6 +941,8 @@
 		ast_jb_configure(tmp, &global_jbconf);
 
 	ast_channel_stage_snapshot_done(tmp);
+
+	ast_channel_unlock(tmp);
 
 	if (state != AST_STATE_DOWN && ast_pbx_start(tmp)) {
 		ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(tmp));

Modified: branches/12/channels/chan_mgcp.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/channels/chan_mgcp.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/channels/chan_mgcp.c (original)
+++ branches/12/channels/chan_mgcp.c Wed Dec 18 13:20:02 2013
@@ -1571,6 +1571,8 @@
 
 		ast_channel_stage_snapshot_done(tmp);
 
+		ast_channel_unlock(tmp);
+
 		if (state != AST_STATE_DOWN) {
 			if (ast_pbx_start(tmp)) {
 				ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(tmp));

Modified: branches/12/channels/chan_misdn.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/channels/chan_misdn.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/channels/chan_misdn.c (original)
+++ branches/12/channels/chan_misdn.c Wed Dec 18 13:20:02 2013
@@ -8226,6 +8226,8 @@
 		ast_channel_rings_set(tmp, (state == AST_STATE_RING) ? 1 : 0);
 
 		ast_jb_configure(tmp, misdn_get_global_jbconf());
+
+		ast_channel_unlock(tmp);
 	} else {
 		chan_misdn_log(-1, 0, "Unable to allocate channel structure\n");
 	}
@@ -8948,6 +8950,8 @@
 	ast_channel_priority_set(chan, notify->priority);
 	ast_free(ast_channel_dialed(chan)->number.str);
 	ast_channel_dialed(chan)->number.str = ast_strdup(notify->exten);
+
+	ast_channel_unlock(chan);
 
 	if (ast_pbx_start(chan)) {
 		ast_log(LOG_WARNING, "Unable to start pbx channel %s!\n", ast_channel_name(chan));

Modified: branches/12/channels/chan_motif.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/channels/chan_motif.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/channels/chan_motif.c (original)
+++ branches/12/channels/chan_motif.c Wed Dec 18 13:20:02 2013
@@ -853,6 +853,8 @@
 
 	ast_channel_stage_snapshot_done(chan);
 
+	ast_channel_unlock(chan);
+
 	return chan;
 }
 

Modified: branches/12/channels/chan_multicast_rtp.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/channels/chan_multicast_rtp.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/channels/chan_multicast_rtp.c (original)
+++ branches/12/channels/chan_multicast_rtp.c Wed Dec 18 13:20:02 2013
@@ -166,6 +166,8 @@
 
 	ast_channel_tech_pvt_set(chan, instance);
 
+	ast_channel_unlock(chan);
+
 	return chan;
 
 failure:

Modified: branches/12/channels/chan_nbs.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/channels/chan_nbs.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/channels/chan_nbs.c (original)
+++ branches/12/channels/chan_nbs.c Wed Dec 18 13:20:02 2013
@@ -239,6 +239,7 @@
 		ast_channel_language_set(tmp, "");
 		i->owner = tmp;
 		i->u = ast_module_user_add(tmp);
+		ast_channel_unlock(tmp);
 		if (state != AST_STATE_DOWN) {
 			if (ast_pbx_start(tmp)) {
 				ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(tmp));

Modified: branches/12/channels/chan_oss.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/channels/chan_oss.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/channels/chan_oss.c (original)
+++ branches/12/channels/chan_oss.c Wed Dec 18 13:20:02 2013
@@ -829,6 +829,7 @@
 	o->owner = c;
 	ast_module_ref(ast_module_info->self);
 	ast_jb_configure(c, &global_jbconf);
+	ast_channel_unlock(c);
 	if (state != AST_STATE_DOWN) {
 		if (ast_pbx_start(c)) {
 			ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(c));

Modified: branches/12/channels/chan_phone.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/channels/chan_phone.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/channels/chan_phone.c (original)
+++ branches/12/channels/chan_phone.c Wed Dec 18 13:20:02 2013
@@ -901,6 +901,7 @@
 
 		i->owner = tmp;
 		ast_module_ref(ast_module_info->self);
+		ast_channel_unlock(tmp);
 		if (state != AST_STATE_DOWN) {
 			if (state == AST_STATE_RING) {
 				ioctl(ast_channel_fd(tmp, 0), PHONE_RINGBACK);

Modified: branches/12/channels/chan_pjsip.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/channels/chan_pjsip.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/channels/chan_pjsip.c (original)
+++ branches/12/channels/chan_pjsip.c Wed Dec 18 13:20:02 2013
@@ -359,18 +359,63 @@
 	ast_channel_tech_set(chan, &chan_pjsip_tech);
 
 	if (!(channel = ast_sip_channel_pvt_alloc(pvt, session))) {
+		ast_channel_unlock(chan);
 		ast_hangup(chan);
 		return NULL;
 	}
 
 	ast_channel_stage_snapshot(chan);
+
+	ast_channel_tech_pvt_set(chan, channel);
+
+	if (ast_format_cap_is_empty(session->req_caps) || !ast_format_cap_has_joint(session->req_caps, session->endpoint->media.codecs)) {
+		ast_format_cap_copy(ast_channel_nativeformats(chan), session->endpoint->media.codecs);
+	} else {
+		ast_format_cap_copy(ast_channel_nativeformats(chan), session->req_caps);
+	}
+
+	ast_codec_choose(&session->endpoint->media.prefs, ast_channel_nativeformats(chan), 1, &fmt);
+	ast_format_copy(ast_channel_writeformat(chan), &fmt);
+	ast_format_copy(ast_channel_rawwriteformat(chan), &fmt);
+	ast_format_copy(ast_channel_readformat(chan), &fmt);
+	ast_format_copy(ast_channel_rawreadformat(chan), &fmt);
+
+	if (state == AST_STATE_RING) {
+		ast_channel_rings_set(chan, 1);
+	}
+
+	ast_channel_adsicpe_set(chan, AST_ADSI_UNAVAILABLE);
+
+	ast_channel_context_set(chan, session->endpoint->context);
+	ast_channel_exten_set(chan, S_OR(exten, "s"));
+	ast_channel_priority_set(chan, 1);
+
+	ast_channel_callgroup_set(chan, session->endpoint->pickup.callgroup);
+	ast_channel_pickupgroup_set(chan, session->endpoint->pickup.pickupgroup);
+
+	ast_channel_named_callgroups_set(chan, session->endpoint->pickup.named_callgroups);
+	ast_channel_named_pickupgroups_set(chan, session->endpoint->pickup.named_pickupgroups);
+
+	if (!ast_strlen_zero(session->endpoint->language)) {
+		ast_channel_language_set(chan, session->endpoint->language);
+	}
+
+	if (!ast_strlen_zero(session->endpoint->zone)) {
+		struct ast_tone_zone *zone = ast_get_indication_zone(session->endpoint->zone);
+		if (!zone) {
+			ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", session->endpoint->zone);
+		}
+		ast_channel_zone_set(chan, zone);
+	}
+
+	ast_channel_stage_snapshot_done(chan);
+	ast_channel_unlock(chan);
 
 	/* If res_pjsip_session is ever updated to create/destroy ast_sip_session_media
 	 * during a call such as if multiple same-type stream support is introduced,
 	 * these will need to be recaptured as well */
 	pvt->media[SIP_MEDIA_AUDIO] = ao2_find(session->media, "audio", OBJ_KEY);
 	pvt->media[SIP_MEDIA_VIDEO] = ao2_find(session->media, "video", OBJ_KEY);
-	ast_channel_tech_pvt_set(chan, channel);
 	if (pvt->media[SIP_MEDIA_AUDIO] && pvt->media[SIP_MEDIA_AUDIO]->rtp) {
 		ast_rtp_instance_set_channel_id(pvt->media[SIP_MEDIA_AUDIO]->rtp, ast_channel_uniqueid(chan));
 	}
@@ -378,49 +423,7 @@
 		ast_rtp_instance_set_channel_id(pvt->media[SIP_MEDIA_VIDEO]->rtp, ast_channel_uniqueid(chan));
 	}
 
-	if (ast_format_cap_is_empty(session->req_caps) || !ast_format_cap_has_joint(session->req_caps, session->endpoint->media.codecs)) {
-		ast_format_cap_copy(ast_channel_nativeformats(chan), session->endpoint->media.codecs);
-	} else {
-		ast_format_cap_copy(ast_channel_nativeformats(chan), session->req_caps);
-	}
-
-	ast_codec_choose(&session->endpoint->media.prefs, ast_channel_nativeformats(chan), 1, &fmt);
-	ast_format_copy(ast_channel_writeformat(chan), &fmt);
-	ast_format_copy(ast_channel_rawwriteformat(chan), &fmt);
-	ast_format_copy(ast_channel_readformat(chan), &fmt);
-	ast_format_copy(ast_channel_rawreadformat(chan), &fmt);
-
-	if (state == AST_STATE_RING) {
-		ast_channel_rings_set(chan, 1);
-	}
-
-	ast_channel_adsicpe_set(chan, AST_ADSI_UNAVAILABLE);
-
-	ast_channel_context_set(chan, session->endpoint->context);
-	ast_channel_exten_set(chan, S_OR(exten, "s"));
-	ast_channel_priority_set(chan, 1);
-
-	ast_channel_callgroup_set(chan, session->endpoint->pickup.callgroup);
-	ast_channel_pickupgroup_set(chan, session->endpoint->pickup.pickupgroup);
-
-	ast_channel_named_callgroups_set(chan, session->endpoint->pickup.named_callgroups);
-	ast_channel_named_pickupgroups_set(chan, session->endpoint->pickup.named_pickupgroups);
-
-	if (!ast_strlen_zero(session->endpoint->language)) {
-		ast_channel_language_set(chan, session->endpoint->language);
-	}
-
-	if (!ast_strlen_zero(session->endpoint->zone)) {
-		struct ast_tone_zone *zone = ast_get_indication_zone(session->endpoint->zone);
-		if (!zone) {
-			ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", session->endpoint->zone);
-		}
-		ast_channel_zone_set(chan, zone);
-	}
-
 	ast_endpoint_add_channel(session->endpoint->persistent, chan);
-
-	ast_channel_stage_snapshot_done(chan);
 
 	return chan;
 }

Modified: branches/12/channels/chan_sip.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/channels/chan_sip.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/channels/chan_sip.c (original)
+++ branches/12/channels/chan_sip.c Wed Dec 18 13:20:02 2013
@@ -8100,6 +8100,7 @@
 
 	if (i->relatedpeer && i->relatedpeer->endpoint) {
 		if (ast_endpoint_add_channel(i->relatedpeer->endpoint, tmp)) {
+			ast_channel_unlock(tmp);
 			ast_channel_unref(tmp);
 			sip_pvt_lock(i);
 			return NULL;
@@ -8113,7 +8114,6 @@
 		ast_channel_callid_set(tmp, callid);
 	}
 
-	ast_channel_lock(tmp);
 	sip_pvt_lock(i);
 	ast_channel_cc_params_init(tmp, i->cc_params);
 	ast_channel_caller(tmp)->id.tag = ast_strdup(i->cid_tag);

Modified: branches/12/channels/chan_skinny.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/channels/chan_skinny.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/channels/chan_skinny.c (original)
+++ branches/12/channels/chan_skinny.c Wed Dec 18 13:20:02 2013
@@ -5393,6 +5393,8 @@
 		sub = ast_calloc(1, sizeof(*sub));
 		if (!sub) {
 			ast_log(LOG_WARNING, "Unable to allocate Skinny subchannel\n");
+			ast_channel_unlock(tmp);
+			ast_channel_unref(tmp);
 			return NULL;
 		} else {
 			ast_mutex_init(&sub->lock);
@@ -5499,6 +5501,8 @@
 			pbx_builtin_setvar_helper(tmp, v->name, v->value);
 
 		ast_channel_stage_snapshot_done(tmp);
+
+		ast_channel_unlock(tmp);
 
 		if (state != AST_STATE_DOWN) {
 			if (ast_pbx_start(tmp)) {

Modified: branches/12/channels/chan_unistim.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/channels/chan_unistim.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/channels/chan_unistim.c (original)
+++ branches/12/channels/chan_unistim.c Wed Dec 18 13:20:02 2013
@@ -5628,6 +5628,8 @@
 
 	ast_channel_stage_snapshot_done(tmp);
 
+	ast_channel_unlock(tmp);
+
 	if (state != AST_STATE_DOWN) {
 		if (unistimdebug) {
 			ast_verb(0, "Starting pbx in unistim_new\n");

Modified: branches/12/channels/chan_vpb.cc
URL: http://svnview.digium.com/svn/asterisk/branches/12/channels/chan_vpb.cc?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/channels/chan_vpb.cc (original)
+++ branches/12/channels/chan_vpb.cc Wed Dec 18 13:20:02 2013
@@ -2472,6 +2472,8 @@
 		if (!ast_strlen_zero(me->language))
 			ast_channel_language_set(tmp, me->language);
 
+		ast_channel_unlock(tmp);
+
 		me->owner = tmp;
 
 		me->bridge = NULL;

Modified: branches/12/include/asterisk/channel.h
URL: http://svnview.digium.com/svn/asterisk/branches/12/include/asterisk/channel.h?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/include/asterisk/channel.h (original)
+++ branches/12/include/asterisk/channel.h Wed Dec 18 13:20:02 2013
@@ -791,7 +791,8 @@
  * active channels in the system.  The hash key is based on the channel name.  Because
  * of this, if you want to change the name, you _must_ use ast_change_name(), not change
  * the name field directly.  When ast_channel_alloc() returns a channel pointer, you now
- * hold a reference to that channel.  In most cases this reference is given to ast_pbx_run().
+ * hold both a reference to that channel and a lock on the channel. Once the channel has
+ * been set up the lock can be released. In most cases the reference is given to ast_pbx_run().
  *
  * \par Channel Locking
  * There is a lock associated with every ast_channel.  It is allocated internally via astobj2.
@@ -1122,6 +1123,7 @@
  * \note Absolutely _NO_ channel locks should be held before calling this function.
  * \note By default, new channels are set to the "s" extension
  *       and "default" context.
+ * \note Since 12.0.0 this function returns with the newly created channel locked.
  */
 struct ast_channel * attribute_malloc __attribute__((format(printf, 13, 14)))
 	__ast_channel_alloc(int needqueue, int state, const char *cid_num,
@@ -1140,6 +1142,7 @@
  * \note Absolutely _NO_ channel locks should be held before calling this function.
  * \note By default, new channels are set to the "s" extension
  *       and "default" context.
+ * \note Since 12.0.0 this function returns with the newly created channel locked.
  */
 #define ast_channel_alloc(needqueue, state, cid_num, cid_name, acctcode, exten, context, linkedid, amaflag, ...) \
 	__ast_channel_alloc(needqueue, state, cid_num, cid_name, acctcode, exten, context, linkedid, amaflag, \

Modified: branches/12/main/channel.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/main/channel.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/main/channel.c (original)
+++ branches/12/main/channel.c Wed Dec 18 13:20:02 2013
@@ -1017,6 +1017,12 @@
 	ast_channel_internal_finalize(tmp);
 
 	ast_atomic_fetchadd_int(&chancount, +1);
+
+	/* You might scream "locking inversion" at seeing this but it is actually perfectly fine.
+	 * Since the channel was just created nothing can know about it yet or even acquire it.
+	 */
+	ast_channel_lock(tmp);
+
 	ao2_link(channels, tmp);
 
 	/*

Modified: branches/12/main/core_unreal.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/main/core_unreal.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/main/core_unreal.c (original)
+++ branches/12/main/core_unreal.c Wed Dec 18 13:20:02 2013
@@ -906,57 +906,72 @@
 	 */
 	if (!(owner = ast_channel_alloc(1, semi1_state, NULL, NULL, NULL,
 			exten, context, linkedid, 0,
-			"%s/%s-%08x;1", tech->type, p->name, generated_seqno))
-		|| !(chan = ast_channel_alloc(1, semi2_state, NULL, NULL, NULL,
-			exten, context, ast_channel_linkedid(owner), 0,
-			"%s/%s-%08x;2", tech->type, p->name, generated_seqno))) {
-		if (owner) {
-			owner = ast_channel_release(owner);
-		}
-		ast_log(LOG_WARNING, "Unable to allocate channel structure(s)\n");
+			"%s/%s-%08x;1", tech->type, p->name, generated_seqno))) {
+		ast_log(LOG_WARNING, "Unable to allocate owner channel structure\n");
 		return NULL;
 	}
 
 	if (callid) {
 		ast_channel_callid_set(owner, callid);
-		ast_channel_callid_set(chan, callid);
 	}
 
 	ast_channel_tech_set(owner, tech);
-	ast_channel_tech_set(chan, tech);
+	ao2_ref(p, +1);
 	ast_channel_tech_pvt_set(owner, p);
-	ast_channel_tech_pvt_set(chan, p);
 
 	ast_format_cap_copy(ast_channel_nativeformats(owner), p->reqcap);
-	ast_format_cap_copy(ast_channel_nativeformats(chan), p->reqcap);
 
 	/* Determine our read/write format and set it on each channel */
 	ast_best_codec(p->reqcap, &fmt);
 	ast_format_copy(ast_channel_writeformat(owner), &fmt);
-	ast_format_copy(ast_channel_writeformat(chan), &fmt);
 	ast_format_copy(ast_channel_rawwriteformat(owner), &fmt);
-	ast_format_copy(ast_channel_rawwriteformat(chan), &fmt);
 	ast_format_copy(ast_channel_readformat(owner), &fmt);
-	ast_format_copy(ast_channel_readformat(chan), &fmt);
 	ast_format_copy(ast_channel_rawreadformat(owner), &fmt);
-	ast_format_copy(ast_channel_rawreadformat(chan), &fmt);
 
 	ast_set_flag(ast_channel_flags(owner), AST_FLAG_DISABLE_DEVSTATE_CACHE);
-	ast_set_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE);
 
 	ast_jb_configure(owner, &p->jb_conf);
 
 	if (ast_channel_cc_params_init(owner, requestor
 		? ast_channel_get_cc_config_params((struct ast_channel *) requestor) : NULL)) {
+		ao2_ref(p, -1);
+		ast_channel_unlock(owner);
 		ast_channel_release(owner);
-		ast_channel_release(chan);
 		return NULL;
 	}
 
-	/* Give the private a ref for each channel. */
-	ao2_ref(p, +2);
 	p->owner = owner;
+	ast_channel_unlock(owner);
+
+	if (!(chan = ast_channel_alloc(1, semi2_state, NULL, NULL, NULL,
+			exten, context, ast_channel_linkedid(owner), 0,
+			"%s/%s-%08x;2", tech->type, p->name, generated_seqno))) {
+		ast_log(LOG_WARNING, "Unable to allocate chan channel structure\n");
+		ao2_ref(p, -1);
+		ast_channel_release(owner);
+		return NULL;
+	}
+
+	if (callid) {
+		ast_channel_callid_set(chan, callid);
+	}
+
+	ast_channel_tech_set(chan, tech);
+	ao2_ref(p, +1);
+	ast_channel_tech_pvt_set(chan, p);
+
+	ast_format_cap_copy(ast_channel_nativeformats(chan), p->reqcap);
+
+	/* Format was already determined when setting up owner */
+	ast_format_copy(ast_channel_writeformat(chan), &fmt);
+	ast_format_copy(ast_channel_rawwriteformat(chan), &fmt);
+	ast_format_copy(ast_channel_readformat(chan), &fmt);
+	ast_format_copy(ast_channel_rawreadformat(chan), &fmt);
+
+	ast_set_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE);
+
 	p->chan = chan;
+	ast_channel_unlock(chan);
 
 	return owner;
 }

Modified: branches/12/main/message.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/main/message.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/main/message.c (original)
+++ branches/12/main/message.c Wed Dec 18 13:20:02 2013
@@ -678,9 +678,9 @@
 		return NULL;
 	}
 
+	ast_channel_tech_set(chan, &msg_chan_tech_hack);
+	ast_channel_unlock(chan);
 	ast_channel_unlink(chan);
-
-	ast_channel_tech_set(chan, &msg_chan_tech_hack);
 
 	if (!(ds = ast_datastore_alloc(&msg_datastore, NULL))) {
 		ast_hangup(chan);

Modified: branches/12/main/pbx.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/main/pbx.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/main/pbx.c (original)
+++ branches/12/main/pbx.c Wed Dec 18 13:20:02 2013
@@ -10352,6 +10352,8 @@
 			snprintf(failed_reason, sizeof(failed_reason), "%d", ast_dial_reason(outgoing->dial, 0));
 			pbx_builtin_setvar_helper(failed, "REASON", failed_reason);
 
+			ast_channel_unlock(failed);
+
 			if (ast_pbx_run(failed)) {
 				ast_log(LOG_ERROR, "Unable to run PBX on '%s'\n", ast_channel_name(failed));
 				ast_hangup(failed);

Modified: branches/12/res/parking/parking_tests.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/parking/parking_tests.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/res/parking/parking_tests.c (original)
+++ branches/12/res/parking/parking_tests.c Wed Dec 18 13:20:02 2013
@@ -63,6 +63,8 @@
 
 	ast_channel_set_caller(alice, &alice_callerid, NULL);
 
+	ast_channel_unlock(alice);
+
 	return alice;
 }
 

Modified: branches/12/res/res_calendar.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/res_calendar.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/res/res_calendar.c (original)
+++ branches/12/res/res_calendar.c Wed Dec 18 13:20:02 2013
@@ -766,6 +766,8 @@
 	/* clear native formats and set to slinear. write format is signlear so just use that to set it */
 	ast_format_cap_set(ast_channel_nativeformats(chan), ast_channel_writeformat(chan));
 
+	ast_channel_unlock(chan);
+
 	if (!(datastore = ast_datastore_alloc(&event_notification_datastore, NULL))) {
 		ast_log(LOG_ERROR, "Could not allocate datastore, notification not being sent!\n");
 		goto notify_cleanup;

Modified: branches/12/res/res_stasis_snoop.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/res_stasis_snoop.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/res/res_stasis_snoop.c (original)
+++ branches/12/res/res_stasis_snoop.c Wed Dec 18 13:20:02 2013
@@ -274,12 +274,21 @@
 	return ast_audiohook_attach(chan, audiohook);
 }
 
+/*! \brief Helper function which gets the format for a Snoop channel based on the channel being snooped on */
+static void snoop_determine_format(struct ast_channel *chan, struct stasis_app_snoop *snoop)
+{
+	SCOPED_CHANNELLOCK(lock, chan);
+	unsigned int rate = MAX(ast_format_rate(ast_channel_rawwriteformat(chan)),
+		ast_format_rate(ast_channel_rawreadformat(chan)));
+
+	ast_format_set(&snoop->spy_format, ast_format_slin_by_rate(rate), 0);
+}
+
 struct ast_channel *stasis_app_control_snoop(struct ast_channel *chan,
 	enum stasis_app_snoop_direction spy, enum stasis_app_snoop_direction whisper,
 	const char *app, const char *app_args)
 {
 	RAII_VAR(struct stasis_app_snoop *, snoop, NULL, ao2_cleanup);
-	unsigned int rate;
 	pthread_t thread;
 
 	if (spy == STASIS_SNOOP_DIRECTION_NONE &&
@@ -309,6 +318,9 @@
 		return NULL;
 	}
 	ast_timer_set_rate(snoop->timer, 1000 / SNOOP_INTERVAL);
+
+	/* Determine which signed linear format should be used */
+	snoop_determine_format(chan, snoop);
 
 	/* Allocate a Snoop channel and set up various parameters */
 	snoop->chan = ast_channel_alloc(1, AST_STATE_UP, "", "", "", "", "", "", 0, "Snoop/%s-%08x", ast_channel_uniqueid(chan),
@@ -327,39 +339,33 @@
 	ast_channel_tech_pvt_set(snoop->chan, snoop);
 	ast_channel_set_fd(snoop->chan, 0, ast_timer_fd(snoop->timer));
 
-	{
-		SCOPED_CHANNELLOCK(lock, chan);
-
-		/* Determine the "best" signed linear format capable by the channel we are snooping on */
-		rate = MAX(ast_format_rate(ast_channel_rawwriteformat(chan)), ast_format_rate(ast_channel_rawreadformat(chan)));
-		ast_format_set(&snoop->spy_format, ast_format_slin_by_rate(rate), 0);
-
-		/* The format on the Snoop channel will be this signed linear format, and it will never change */
-		ast_format_cap_set(ast_channel_nativeformats(snoop->chan), &snoop->spy_format);
-		ast_format_copy(ast_channel_writeformat(snoop->chan), &snoop->spy_format);
-		ast_format_copy(ast_channel_rawwriteformat(snoop->chan), &snoop->spy_format);
-		ast_format_copy(ast_channel_readformat(snoop->chan), &snoop->spy_format);
-		ast_format_copy(ast_channel_rawreadformat(snoop->chan), &snoop->spy_format);
-
-		if (spy != STASIS_SNOOP_DIRECTION_NONE) {
-			if (snoop_setup_audiohook(chan, AST_AUDIOHOOK_TYPE_SPY, spy, &snoop->spy_direction, &snoop->spy)) {
-				ast_hangup(snoop->chan);
-				return NULL;
-			}
-
-			snoop->spy_samples = rate / (1000 / SNOOP_INTERVAL);
-			snoop->spy_active = 1;
+	/* The format on the Snoop channel will be this signed linear format, and it will never change */
+	ast_format_cap_set(ast_channel_nativeformats(snoop->chan), &snoop->spy_format);
+	ast_format_copy(ast_channel_writeformat(snoop->chan), &snoop->spy_format);
+	ast_format_copy(ast_channel_rawwriteformat(snoop->chan), &snoop->spy_format);
+	ast_format_copy(ast_channel_readformat(snoop->chan), &snoop->spy_format);
+	ast_format_copy(ast_channel_rawreadformat(snoop->chan), &snoop->spy_format);
+
+	ast_channel_unlock(snoop->chan);
+
+	if (spy != STASIS_SNOOP_DIRECTION_NONE) {
+		if (snoop_setup_audiohook(chan, AST_AUDIOHOOK_TYPE_SPY, spy, &snoop->spy_direction, &snoop->spy)) {
+			ast_hangup(snoop->chan);
+			return NULL;
 		}
 
-		/* If whispering is enabled set up the audiohook */
-		if (whisper != STASIS_SNOOP_DIRECTION_NONE) {
-			if (snoop_setup_audiohook(chan, AST_AUDIOHOOK_TYPE_WHISPER, whisper, &snoop->whisper_direction, &snoop->whisper)) {
-				ast_hangup(snoop->chan);
-				return NULL;
-			}
-
-			snoop->whisper_active = 1;
+		snoop->spy_samples = ast_format_rate(&snoop->spy_format) / (1000 / SNOOP_INTERVAL);
+		snoop->spy_active = 1;
+	}
+
+	/* If whispering is enabled set up the audiohook */
+	if (whisper != STASIS_SNOOP_DIRECTION_NONE) {
+		if (snoop_setup_audiohook(chan, AST_AUDIOHOOK_TYPE_WHISPER, whisper, &snoop->whisper_direction, &snoop->whisper)) {
+			ast_hangup(snoop->chan);
+			return NULL;
 		}
+
+		snoop->whisper_active = 1;
 	}
 
 	/* Create the thread which services the Snoop channel */

Modified: branches/12/tests/test_app.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/tests/test_app.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/tests/test_app.c (original)
+++ branches/12/tests/test_app.c Wed Dec 18 13:20:02 2013
@@ -175,21 +175,25 @@
 		"'%s', '%s', '%s', '%s'\n", group1_full, group2_full, category1_full, category2_full);
 
 	if (!(test_channel1 = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
-        NULL, NULL, 0, 0, "TestChannel1"))) {
-		goto exit_group_test;
-	}
+		NULL, NULL, 0, 0, "TestChannel1"))) {
+		goto exit_group_test;
+	}
+	ast_channel_unlock(test_channel1);
 	if (!(test_channel2 = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
-        NULL, NULL, 0, 0, "TestChannel2"))) {
-		goto exit_group_test;
-	}
+		NULL, NULL, 0, 0, "TestChannel2"))) {
+		goto exit_group_test;
+	}
+	ast_channel_unlock(test_channel2);
 	if (!(test_channel3 = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
-        NULL, NULL, 0, 0, "TestChannel3"))) {
-		goto exit_group_test;
-	}
+		NULL, NULL, 0, 0, "TestChannel3"))) {
+		goto exit_group_test;
+	}
+	ast_channel_unlock(test_channel3);
 	if (!(test_channel4 = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
-        NULL, NULL, 0, 0, "TestChannel4"))) {
-		goto exit_group_test;
-	}
+		NULL, NULL, 0, 0, "TestChannel4"))) {
+		goto exit_group_test;
+	}
+	ast_channel_unlock(test_channel4);
 
 	ast_app_group_set_channel(test_channel1, group1_full);
 	ast_app_group_set_channel(test_channel2, group2_full);

Modified: branches/12/tests/test_cdr.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/tests/test_cdr.c?view=diff&rev=404204&r1=404203&r2=404204
==============================================================================
--- branches/12/tests/test_cdr.c (original)
+++ branches/12/tests/test_cdr.c Wed Dec 18 13:20:02 2013
@@ -238,6 +238,7 @@
 	ast_channel_set_caller((channel_var), (caller_id), NULL); \
 	ast_copy_string((expected_record)->uniqueid, ast_channel_uniqueid((channel_var)), sizeof((expected_record)->uniqueid)); \
 	ast_copy_string((expected_record)->linkedid, ast_channel_linkedid((channel_var)), sizeof((expected_record)->linkedid)); \
+	ast_channel_unlock((channel_var)); \
 	} while (0)
 
 /*! \brief Create a \ref test_cdr_chan_tech for Bob, and set the expected
@@ -247,6 +248,7 @@
 	ast_channel_set_caller((channel_var), (caller_id), NULL); \
 	ast_copy_string((expected_record)->uniqueid, ast_channel_uniqueid((channel_var)), sizeof((expected_record)->uniqueid)); \
 	ast_copy_string((expected_record)->linkedid, ast_channel_linkedid((channel_var)), sizeof((expected_record)->linkedid)); \
+	ast_channel_unlock((channel_var)); \
 	} while (0)
 
 /*! \brief Create a \ref test_cdr_chan_tech for Charlie, and set the expected
@@ -256,6 +258,7 @@
 	ast_channel_set_caller((channel_var), (caller_id), NULL); \
 	ast_copy_string((expected_record)->uniqueid, ast_channel_uniqueid((channel_var)), sizeof((expected_record)->uniqueid)); \
 	ast_copy_string((expected_record)->linkedid, ast_channel_linkedid((channel_var)), sizeof((expected_record)->linkedid)); \
+	ast_channel_unlock((channel_var)); \
 	} while (0)
 
 /*! \brief Create a \ref test_cdr_chan_tech for Charlie, and set the expected
@@ -265,6 +268,7 @@
 	ast_channel_set_caller((channel_var), (caller_id), NULL); \
 	ast_copy_string((expected_record)->uniqueid, ast_channel_uniqueid((channel_var)), sizeof((expected_record)->uniqueid)); \
 	ast_copy_string((expected_record)->linkedid, ast_channel_linkedid((channel_var)), sizeof((expected_record)->linkedid)); \
+	ast_channel_unlock((channel_var)); \
 	} while (0)
 
 /*! \brief Emulate a channel entering into an application */
@@ -563,6 +567,7 @@
 	ast_test_validate(test, !ast_bridge_impart(bridge, chan_alice, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
 
 	chan_bob = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, "200", NULL, NULL, ast_channel_linkedid(chan_alice), 0, CHANNEL_TECH_NAME "/Bob");
+	ast_channel_unlock(chan_bob);
 	ast_copy_string(bob_expected.linkedid, ast_channel_linkedid(chan_bob), sizeof(bob_expected.linkedid));
 	ast_copy_string(bob_expected.uniqueid, ast_channel_uniqueid(chan_bob), sizeof(bob_expected.uniqueid));
 	ast_set_flag(ast_channel_flags(chan_bob), AST_FLAG_OUTGOING);
@@ -1133,6 +1138,7 @@
 	EMULATE_APP_DATA(chan_caller, 1, "Dial", "CDRTestChannel/Bob");
 
 	chan_callee = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, "200", NULL, NULL, ast_channel_linkedid(chan_caller), 0, CHANNEL_TECH_NAME "/Bob");
+	ast_channel_unlock(chan_callee);
 	ast_set_flag(ast_channel_flags(chan_callee), AST_FLAG_OUTGOING);
 	EMULATE_APP_DATA(chan_callee, 0, "AppDial", "(Outgoing Line)");
 
@@ -1194,6 +1200,7 @@
 	EMULATE_APP_DATA(chan_caller, 1, "Dial", CHANNEL_TECH_NAME "/Bob");
 
 	chan_callee = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, "200", NULL, NULL, ast_channel_linkedid(chan_caller), 0, CHANNEL_TECH_NAME "/Bob");
+	ast_channel_unlock(chan_callee);
 	ast_set_flag(ast_channel_flags(chan_callee), AST_FLAG_OUTGOING);
 	EMULATE_APP_DATA(chan_callee, 0, "AppDial", "(Outgoing Line)");
 
@@ -1254,6 +1261,7 @@

[... 196 lines stripped ...]



More information about the asterisk-commits mailing list