[asterisk-commits] rmudgett: branch rmudgett/bridge_tasks r402440 - in /team/rmudgett/bridge_tas...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sat Nov 2 00:02:48 CDT 2013


Author: rmudgett
Date: Sat Nov  2 00:02:45 2013
New Revision: 402440

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=402440
Log:
Resolve conflict.  Restart automerge.

Modified:
    team/rmudgett/bridge_tasks/   (props changed)
    team/rmudgett/bridge_tasks/apps/app_confbridge.c
    team/rmudgett/bridge_tasks/apps/confbridge/conf_state.c
    team/rmudgett/bridge_tasks/apps/confbridge/conf_state_inactive.c
    team/rmudgett/bridge_tasks/apps/confbridge/conf_state_multi.c
    team/rmudgett/bridge_tasks/apps/confbridge/conf_state_multi_marked.c
    team/rmudgett/bridge_tasks/apps/confbridge/conf_state_single.c
    team/rmudgett/bridge_tasks/apps/confbridge/conf_state_single_marked.c
    team/rmudgett/bridge_tasks/apps/confbridge/include/confbridge.h
    team/rmudgett/bridge_tasks/include/asterisk/lock.h
    team/rmudgett/bridge_tasks/include/asterisk/vector.h
    team/rmudgett/bridge_tasks/main/stasis.c
    team/rmudgett/bridge_tasks/main/stasis_message_router.c

Propchange: team/rmudgett/bridge_tasks/
------------------------------------------------------------------------------
    automerge = *

Propchange: team/rmudgett/bridge_tasks/
------------------------------------------------------------------------------
--- branch-12-merged (original)
+++ branch-12-merged Sat Nov  2 00:02:45 2013
@@ -1,1 +1,1 @@
-/branches/12:1-398558,398560-398577,398579-399305,399307-401390,401392-402367,402387,402398,402416
+/branches/12:1-398558,398560-398577,398579-399305,399307-401390,401392-402367,402387,402398,402416,402427,402429

Propchange: team/rmudgett/bridge_tasks/
------------------------------------------------------------------------------
--- bridge_phase-integrated (original)
+++ bridge_phase-integrated Sat Nov  2 00:02:45 2013
@@ -1,1 +1,1 @@
-/trunk:1-402421
+/trunk:1-402434

Propchange: team/rmudgett/bridge_tasks/
------------------------------------------------------------------------------
--- bridge_tasks-integrated (original)
+++ bridge_tasks-integrated Sat Nov  2 00:02:45 2013
@@ -1,1 +1,1 @@
-/team/rmudgett/bridge_phase:1-402422
+/team/rmudgett/bridge_phase:1-402439

Modified: team/rmudgett/bridge_tasks/apps/app_confbridge.c
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/bridge_tasks/apps/app_confbridge.c?view=diff&rev=402440&r1=402439&r2=402440
==============================================================================
--- team/rmudgett/bridge_tasks/apps/app_confbridge.c (original)
+++ team/rmudgett/bridge_tasks/apps/app_confbridge.c Sat Nov  2 00:02:45 2013
@@ -967,6 +967,32 @@
 	return 0;
 }
 
+void conf_update_user_mute(struct confbridge_user *user)
+{
+	int mute_user;
+	int mute_system;
+	int mute_effective;
+
+	/* User level mute request. */
+	mute_user = user->muted;
+
+	/* System level mute request. */
+	mute_system = user->playing_moh
+		/*
+		 * Do not allow waitmarked users to talk to anyone unless there
+		 * is a marked user present.
+		 */
+		|| (!user->conference->markedusers
+			&& ast_test_flag(&user->u_profile, USER_OPT_WAITMARKED));
+
+	mute_effective = mute_user || mute_system;
+
+	ast_debug(1, "User %s is %s: user:%d system:%d.\n",
+		ast_channel_name(user->chan), mute_effective ? "muted" : "unmuted",
+		mute_user, mute_system);
+	ast_bridge_mute_set(user->conference->bridge, user->chan, mute_effective);
+}
+
 void conf_moh_stop(struct confbridge_user *user)
 {
 	user->playing_moh = 0;
@@ -1112,9 +1138,7 @@
 	if (ast_test_flag(&first_user->u_profile, USER_OPT_MUSICONHOLD)) {
 		conf_moh_stop(first_user);
 	}
-	if (!ast_test_flag(&first_user->u_profile, USER_OPT_STARTMUTED)) {
-		ast_bridge_mute_set(conference->bridge, first_user->chan, 0);
-	}
+	conf_update_user_mute(first_user);
 }
 
 void conf_ended(struct confbridge_conference *conference)
@@ -1638,7 +1662,8 @@
 
 	/* If the caller should be joined already muted, make it so */
 	if (ast_test_flag(&user.u_profile, USER_OPT_STARTMUTED)) {
-		user.features.mute = 1;
+		/* Set user level mute request. */
+		user.muted = 1;
 	}
 
 	if (ast_test_flag(&user.u_profile, USER_OPT_DROP_SILENCE)) {
@@ -1758,21 +1783,17 @@
 {
 	int mute;
 
-	mute = ast_bridge_mute_get(conference->bridge, user->chan);
-	if (mute < 0) {
-		return -1;
-	}
-
-	/* Mute or unmute yourself, note we only allow manipulation if they aren't waiting for a marked user or if marked users exist */
-	if (!ast_test_flag(&user->u_profile, USER_OPT_WAITMARKED) || conference->markedusers) {
-		mute = !mute;
-		ast_bridge_mute_set(conference->bridge, user->chan, mute);
-		if (mute) {
-			send_mute_event(chan, conference);
-		} else {
-			send_unmute_event(chan, conference);
-		}
-	}
+	/* Toggle user level mute request. */
+	mute = !user->muted;
+	user->muted = mute;
+
+	conf_update_user_mute(user);
+	if (mute) {
+		send_mute_event(chan, conference);
+	} else {
+		send_unmute_event(chan, conference);
+	}
+
 	return ast_stream_and_wait(chan, (mute ?
 		conf_get_sound(CONF_SOUND_MUTED, user->b_profile.sounds) :
 		conf_get_sound(CONF_SOUND_UNMUTED, user->b_profile.sounds)),
@@ -1783,21 +1804,26 @@
 {
 	struct confbridge_user *cur_user = NULL;
 	const char *sound_to_play;
+	int mute;
 
 	ao2_lock(conference);
 
-	/* If already muted, then unmute */
-	conference->muted = !conference->muted;
-	sound_to_play = conf_get_sound((conference->muted ? CONF_SOUND_PARTICIPANTS_MUTED : CONF_SOUND_PARTICIPANTS_UNMUTED),
-		user->b_profile.sounds);
+	/* Toggle bridge level mute request. */
+	mute = !conference->muted;
+	conference->muted = mute;
 
 	AST_LIST_TRAVERSE(&conference->active_list, cur_user, list) {
 		if (!ast_test_flag(&cur_user->u_profile, USER_OPT_ADMIN)) {
-			ast_bridge_mute_set(conference->bridge, cur_user->chan, conference->muted);
+			/* Set user level to bridge level mute request. */
+			cur_user->muted = mute;
+			conf_update_user_mute(cur_user);
 		}
 	}
 
 	ao2_unlock(conference);
+
+	sound_to_play = conf_get_sound((mute ? CONF_SOUND_PARTICIPANTS_MUTED : CONF_SOUND_PARTICIPANTS_UNMUTED),
+		user->b_profile.sounds);
 
 	/* The host needs to hear it seperately, as they don't get the audio from play_sound_helper */
 	ast_stream_and_wait(user->chan, sound_to_play, "");
@@ -2218,7 +2244,7 @@
 
 static void handle_cli_confbridge_list_item(struct ast_cli_args *a, struct confbridge_user *user, int waiting)
 {
-	char flag_str[5 + 1];/* Max flags + terminator */
+	char flag_str[6 + 1];/* Max flags + terminator */
 	int pos = 0;
 
 	/* Build flags column string. */
@@ -2234,18 +2260,22 @@
 	if (ast_test_flag(&user->u_profile, USER_OPT_ENDMARKED)) {
 		flag_str[pos++] = 'E';
 	}
+	if (user->muted) {
+		flag_str[pos++] = 'm';
+	}
 	if (waiting) {
 		flag_str[pos++] = 'w';
 	}
 	flag_str[pos] = '\0';
 
-	ast_cli(a->fd, "%-29s ", ast_channel_name(user->chan));
-	ast_cli(a->fd, "%-5s ", flag_str);
-	ast_cli(a->fd, "%-17s", user->u_profile.name);
-	ast_cli(a->fd, "%-17s", user->b_profile.name);
-	ast_cli(a->fd, "%-17s", user->menu_name);
-	ast_cli(a->fd, "%-17s", S_COR(ast_channel_caller(user->chan)->id.number.valid, ast_channel_caller(user->chan)->id.number.str, "<unknown>"));
-	ast_cli(a->fd, "\n");
+	ast_cli(a->fd, "%-30s %-6s %-16s %-16s %-16s %s\n",
+		ast_channel_name(user->chan),
+		flag_str,
+		user->u_profile.name,
+		user->b_profile.name,
+		user->menu_name,
+		S_COR(ast_channel_caller(user->chan)->id.number.valid,
+			ast_channel_caller(user->chan)->id.number.str, "<unknown>"));
 }
 
 static char *handle_cli_confbridge_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
@@ -2267,6 +2297,7 @@
 			"         M - The user is a marked user\n"
 			"         W - The user must wait for a marked user to join\n"
 			"         E - The user will be kicked after the last marked user leaves the conference\n"
+			"         m - The user is muted\n"
 			"         w - The user is waiting for a marked user to join\n";
 		return NULL;
 	case CLI_GENERATE:
@@ -2298,8 +2329,8 @@
 			ast_cli(a->fd, "No conference bridge named '%s' found!\n", a->argv[2]);
 			return CLI_SUCCESS;
 		}
-		ast_cli(a->fd, "Channel                       Flags User Profile     Bridge Profile   Menu             CallerID\n");
-		ast_cli(a->fd, "============================= ===== ================ ================ ================ ================\n");
+		ast_cli(a->fd, "Channel                        Flags  User Profile     Bridge Profile   Menu             CallerID\n");
+		ast_cli(a->fd, "============================== ====== ================ ================ ================ ================\n");
 		ao2_lock(conference);
 		AST_LIST_TRAVERSE(&conference->active_list, user, list) {
 			handle_cli_confbridge_list_item(a, user, 0);
@@ -2363,15 +2394,14 @@
 		}
 	}
 	if (user) {
-		res = ast_bridge_mute_set(conference->bridge, user->chan, mute);
-		if (!res) {
-			ast_channel_lock(user->chan);
-			if (mute) {
-				send_mute_event(user->chan, conference);
-			} else {
-				send_unmute_event(user->chan, conference);
-			}
-			ast_channel_unlock(user->chan);
+		/* Set user level mute request. */
+		user->muted = mute ? 1 : 0;
+
+		conf_update_user_mute(user);
+		if (mute) {
+			send_mute_event(user->chan, conference);
+		} else {
+			send_unmute_event(user->chan, conference);
 		}
 	} else {
 		res = -2;;
@@ -2631,6 +2661,7 @@
 		"WaitMarked: %s\r\n"
 		"EndMarked: %s\r\n"
 		"Waiting: %s\r\n"
+		"Muted: %s\r\n"
 		"\r\n",
 		id_text,
 		conference->name,
@@ -2641,7 +2672,8 @@
 		ast_test_flag(&user->u_profile, USER_OPT_MARKEDUSER) ? "Yes" : "No",
 		ast_test_flag(&user->u_profile, USER_OPT_WAITMARKED) ? "Yes" : "No",
 		ast_test_flag(&user->u_profile, USER_OPT_ENDMARKED) ? "Yes" : "No",
-		waiting ? "Yes" : "No");
+		waiting ? "Yes" : "No",
+		user->muted ? "Yes" : "No");
 }
 
 static int action_confbridgelist(struct mansession *s, const struct message *m)
@@ -3074,11 +3106,11 @@
 {
 	struct confbridge_user *only_user = AST_LIST_FIRST(&conference->active_list);
 
-	/* Turn on MOH/mute if the single participant is set up for it */
+	/* Turn on MOH if the single participant is set up for it */
 	if (ast_test_flag(&only_user->u_profile, USER_OPT_MUSICONHOLD)) {
-		ast_bridge_mute_set(conference->bridge, only_user->chan, 1);
 		conf_moh_start(only_user);
 	}
+	conf_update_user_mute(only_user);
 }
 
 void conf_remove_user_waiting(struct confbridge_conference *conference, struct confbridge_user *user)

Modified: team/rmudgett/bridge_tasks/apps/confbridge/conf_state.c
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/bridge_tasks/apps/confbridge/conf_state.c?view=diff&rev=402440&r1=402439&r2=402440
==============================================================================
--- team/rmudgett/bridge_tasks/apps/confbridge/conf_state.c (original)
+++ team/rmudgett/bridge_tasks/apps/confbridge/conf_state.c Sat Nov  2 00:02:45 2013
@@ -57,12 +57,11 @@
  */
 static void conf_mute_moh_inactive_waitmarked(struct confbridge_user *user)
 {
-	/* Be sure we are muted so we can't talk to anybody else waiting */
-	ast_bridge_mute_set(user->conference->bridge, user->chan, 1);
 	/* Start music on hold if needed */
 	if (ast_test_flag(&user->u_profile, USER_OPT_MUSICONHOLD)) {
 		conf_moh_start(user);
 	}
+	conf_update_user_mute(user);
 }
 
 void conf_default_join_waitmarked(struct confbridge_user *user)

Modified: team/rmudgett/bridge_tasks/apps/confbridge/conf_state_inactive.c
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/bridge_tasks/apps/confbridge/conf_state_inactive.c?view=diff&rev=402440&r1=402439&r2=402440
==============================================================================
--- team/rmudgett/bridge_tasks/apps/confbridge/conf_state_inactive.c (original)
+++ team/rmudgett/bridge_tasks/apps/confbridge/conf_state_inactive.c Sat Nov  2 00:02:45 2013
@@ -59,6 +59,7 @@
 static void join_marked(struct confbridge_user *user)
 {
 	conf_add_user_marked(user->conference, user);
+	conf_update_user_mute(user);
 
 	conf_change_state(user, CONF_STATE_MULTI_MARKED);
 }

Modified: team/rmudgett/bridge_tasks/apps/confbridge/conf_state_multi.c
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/bridge_tasks/apps/confbridge/conf_state_multi.c?view=diff&rev=402440&r1=402439&r2=402440
==============================================================================
--- team/rmudgett/bridge_tasks/apps/confbridge/conf_state_multi.c (original)
+++ team/rmudgett/bridge_tasks/apps/confbridge/conf_state_multi.c Sat Nov  2 00:02:45 2013
@@ -52,11 +52,13 @@
 static void join_unmarked(struct confbridge_user *user)
 {
 	conf_add_user_active(user->conference, user);
+	conf_update_user_mute(user);
 }
 
 static void join_marked(struct confbridge_user *user)
 {
 	conf_add_user_marked(user->conference, user);
+	conf_update_user_mute(user);
 
 	conf_change_state(user, CONF_STATE_MULTI_MARKED);
 }

Modified: team/rmudgett/bridge_tasks/apps/confbridge/conf_state_multi_marked.c
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/bridge_tasks/apps/confbridge/conf_state_multi_marked.c?view=diff&rev=402440&r1=402439&r2=402440
==============================================================================
--- team/rmudgett/bridge_tasks/apps/confbridge/conf_state_multi_marked.c (original)
+++ team/rmudgett/bridge_tasks/apps/confbridge/conf_state_multi_marked.c Sat Nov  2 00:02:45 2013
@@ -60,11 +60,13 @@
 static void join_active(struct confbridge_user *user)
 {
 	conf_add_user_active(user->conference, user);
+	conf_update_user_mute(user);
 }
 
 static void join_marked(struct confbridge_user *user)
 {
 	conf_add_user_marked(user->conference, user);
+	conf_update_user_mute(user);
 }
 
 static void leave_active(struct confbridge_user *user)
@@ -95,8 +97,8 @@
 		AST_LIST_TRAVERSE_SAFE_BEGIN(&user->conference->active_list, user_iter, list) {
 			/* Kick ENDMARKED user_iters */
 			if (ast_test_flag(&user_iter->u_profile, USER_OPT_ENDMARKED)) {
-				if (ast_test_flag(&user_iter->u_profile, USER_OPT_WAITMARKED) &&
-						  !ast_test_flag(&user_iter->u_profile, USER_OPT_MARKEDUSER)) {
+				if (ast_test_flag(&user_iter->u_profile, USER_OPT_WAITMARKED)
+					&& !ast_test_flag(&user_iter->u_profile, USER_OPT_MARKEDUSER)) {
 					AST_LIST_REMOVE_CURRENT(list);
 					user_iter->conference->activeusers--;
 					AST_LIST_INSERT_TAIL(&user_iter->conference->waiting_list, user_iter, list);
@@ -104,15 +106,16 @@
 				}
 				user_iter->kicked = 1;
 				ast_bridge_remove(user_iter->conference->bridge, user_iter->chan);
-			} else if (ast_test_flag(&user_iter->u_profile, USER_OPT_WAITMARKED) &&
-					!ast_test_flag(&user_iter->u_profile, USER_OPT_MARKEDUSER)) {
+			} else if (ast_test_flag(&user_iter->u_profile, USER_OPT_WAITMARKED)
+				&& !ast_test_flag(&user_iter->u_profile, USER_OPT_MARKEDUSER)) {
 				conf_remove_user_active(user_iter->conference, user_iter);
 				conf_add_user_waiting(user_iter->conference, user_iter);
-				/* Handle muting/moh of user_iter if necessary */
+
+				/* Handle moh of user_iter if necessary */
 				if (ast_test_flag(&user_iter->u_profile, USER_OPT_MUSICONHOLD)) {
-					ast_bridge_mute_set(user_iter->conference->bridge, user_iter->chan, 1);
 					conf_moh_start(user_iter);
 				}
+				conf_update_user_mute(user_iter);
 			}
 		}
 		AST_LIST_TRAVERSE_SAFE_END;
@@ -168,7 +171,7 @@
 		conf_handle_first_marked_common(user);
 	}
 
-	/* Move all waiting users to active, stopping MOH and umuting if necessary */
+	/* Move all waiting users to active, stopping MOH and unmuting if necessary */
 	AST_LIST_TRAVERSE_SAFE_BEGIN(&user->conference->waiting_list, user_iter, list) {
 		AST_LIST_REMOVE_CURRENT(list);
 		user->conference->waitingusers--;
@@ -177,10 +180,7 @@
 		if (user_iter->playing_moh) {
 			conf_moh_stop(user_iter);
 		}
-		/* only unmute them if they are not supposed to start muted */
-		if (!ast_test_flag(&user_iter->u_profile, USER_OPT_STARTMUTED)) {
-			ast_bridge_mute_set(user_iter->conference->bridge, user_iter->chan, 0);
-		}
+		conf_update_user_mute(user_iter);
 	}
 	AST_LIST_TRAVERSE_SAFE_END;
 }

Modified: team/rmudgett/bridge_tasks/apps/confbridge/conf_state_single.c
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/bridge_tasks/apps/confbridge/conf_state_single.c?view=diff&rev=402440&r1=402439&r2=402440
==============================================================================
--- team/rmudgett/bridge_tasks/apps/confbridge/conf_state_single.c (original)
+++ team/rmudgett/bridge_tasks/apps/confbridge/conf_state_single.c Sat Nov  2 00:02:45 2013
@@ -55,6 +55,7 @@
 {
 	conf_add_user_active(user->conference, user);
 	conf_handle_second_active(user->conference);
+	conf_update_user_mute(user);
 
 	conf_change_state(user, CONF_STATE_MULTI);
 }
@@ -63,6 +64,7 @@
 {
 	conf_add_user_marked(user->conference, user);
 	conf_handle_second_active(user->conference);
+	conf_update_user_mute(user);
 
 	conf_change_state(user, CONF_STATE_MULTI_MARKED);
 }

Modified: team/rmudgett/bridge_tasks/apps/confbridge/conf_state_single_marked.c
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/bridge_tasks/apps/confbridge/conf_state_single_marked.c?view=diff&rev=402440&r1=402439&r2=402440
==============================================================================
--- team/rmudgett/bridge_tasks/apps/confbridge/conf_state_single_marked.c (original)
+++ team/rmudgett/bridge_tasks/apps/confbridge/conf_state_single_marked.c Sat Nov  2 00:02:45 2013
@@ -54,6 +54,7 @@
 {
 	conf_add_user_active(user->conference, user);
 	conf_handle_second_active(user->conference);
+	conf_update_user_mute(user);
 
 	conf_change_state(user, CONF_STATE_MULTI_MARKED);
 }
@@ -62,6 +63,7 @@
 {
 	conf_add_user_marked(user->conference, user);
 	conf_handle_second_active(user->conference);
+	conf_update_user_mute(user);
 
 	conf_change_state(user, CONF_STATE_MULTI_MARKED);
 }

Modified: team/rmudgett/bridge_tasks/apps/confbridge/include/confbridge.h
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/bridge_tasks/apps/confbridge/include/confbridge.h?view=diff&rev=402440&r1=402439&r2=402440
==============================================================================
--- team/rmudgett/bridge_tasks/apps/confbridge/include/confbridge.h (original)
+++ team/rmudgett/bridge_tasks/apps/confbridge/include/confbridge.h Sat Nov  2 00:02:45 2013
@@ -242,6 +242,7 @@
 	struct ast_bridge_features features;         /*!< Bridge features structure */
 	struct ast_bridge_tech_optimizations tech_args; /*!< Bridge technology optimizations for talk detection */
 	unsigned int suspended_moh;                  /*!< Count of active suspended MOH actions. */
+	unsigned int muted:1;                        /*!< Has the user requested to be muted? */
 	unsigned int kicked:1;                       /*!< User has been kicked from the conference */
 	unsigned int playing_moh:1;                  /*!< MOH is currently being played to the user */
 	AST_LIST_HEAD_NOLOCK(, post_join_action) post_join_list; /*!< List of sounds to play after joining */;
@@ -385,6 +386,15 @@
 void conf_ended(struct confbridge_conference *conference);
 
 /*!
+ * \brief Update the actual mute status of the user and set it on the bridge.
+ *
+ * \param user User to update the mute status.
+ *
+ * \return Nothing
+ */
+void conf_update_user_mute(struct confbridge_user *user);
+
+/*!
  * \brief Stop MOH for the conference user.
  *
  * \param user Conference user to stop MOH on.

Modified: team/rmudgett/bridge_tasks/include/asterisk/lock.h
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/bridge_tasks/include/asterisk/lock.h?view=diff&rev=402440&r1=402439&r2=402440
==============================================================================
--- team/rmudgett/bridge_tasks/include/asterisk/lock.h (original)
+++ team/rmudgett/bridge_tasks/include/asterisk/lock.h Sat Nov  2 00:02:45 2013
@@ -339,6 +339,28 @@
  * used during deadlock avoidance, to preserve the original location where
  * a lock was originally acquired.
  */
+#define AO2_DEADLOCK_AVOIDANCE(obj) \
+	do { \
+		char __filename[80], __func[80], __mutex_name[80]; \
+		int __lineno; \
+		int __res = ast_find_lock_info(ao2_object_get_lockaddr(obj), __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
+		int __res2 = ao2_unlock(obj); \
+		usleep(1); \
+		if (__res < 0) { /* Could happen if the ao2 object does not have a mutex. */ \
+			if (__res2) { \
+				ast_log(LOG_WARNING, "Could not unlock ao2 object '%s': %s and no lock info found!  I will NOT try to relock.\n", #obj, strerror(__res2)); \
+			} else { \
+				ao2_lock(obj); \
+			} \
+		} else { \
+			if (__res2) { \
+				ast_log(LOG_WARNING, "Could not unlock ao2 object '%s': %s.  {{{Originally locked at %s line %d: (%s) '%s'}}}  I will NOT try to relock.\n", #obj, strerror(__res2), __filename, __lineno, __func, __mutex_name); \
+			} else { \
+				__ao2_lock(obj, AO2_LOCK_REQ_MUTEX, __filename, __func, __lineno, __mutex_name); \
+			} \
+		} \
+	} while (0)
+
 #define CHANNEL_DEADLOCK_AVOIDANCE(chan) \
 	do { \
 		char __filename[80], __func[80], __mutex_name[80]; \
@@ -493,12 +515,17 @@
 
 #else /* !DEBUG_THREADS */
 
-#define	CHANNEL_DEADLOCK_AVOIDANCE(chan) \
+#define AO2_DEADLOCK_AVOIDANCE(obj) \
+	ao2_unlock(obj); \
+	usleep(1); \
+	ao2_lock(obj);
+
+#define CHANNEL_DEADLOCK_AVOIDANCE(chan) \
 	ast_channel_unlock(chan); \
 	usleep(1); \
 	ast_channel_lock(chan);
 
-#define	DEADLOCK_AVOIDANCE(lock) \
+#define DEADLOCK_AVOIDANCE(lock) \
 	do { \
 		int __res; \
 		if (!(__res = ast_mutex_unlock(lock))) { \

Modified: team/rmudgett/bridge_tasks/include/asterisk/vector.h
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/bridge_tasks/include/asterisk/vector.h?view=diff&rev=402440&r1=402439&r2=402440
==============================================================================
--- team/rmudgett/bridge_tasks/include/asterisk/vector.h (original)
+++ team/rmudgett/bridge_tasks/include/asterisk/vector.h Sat Nov  2 00:02:45 2013
@@ -33,9 +33,14 @@
  * \since 12
  */
 
-/*! \brief Define a vector structure */
-#define ast_vector(type)			\
-	struct {				\
+/*!
+ * \brief Define a vector structure
+ *
+ * \param name Optional vector struct name.
+ * \param type Vector element type.
+ */
+#define ast_vector(name, type)			\
+	struct name {				\
 		type *elems;			\
 		size_t max;			\
 		size_t current;			\
@@ -55,15 +60,15 @@
  */
 #define ast_vector_init(vec, size) ({					\
 	size_t __size = (size);						\
-	size_t alloc_size = __size * sizeof(*(vec).elems);		\
-	(vec).elems = alloc_size ? ast_malloc(alloc_size) : NULL;	\
-	(vec).current = 0;						\
-	if ((vec).elems) {						\
-		(vec).max = __size;					\
+	size_t alloc_size = __size * sizeof(*((vec)->elems));		\
+	(vec)->elems = alloc_size ? ast_malloc(alloc_size) : NULL;	\
+	(vec)->current = 0;						\
+	if ((vec)->elems) {						\
+		(vec)->max = __size;					\
 	} else {							\
-		(vec).max = 0;						\
+		(vec)->max = 0;						\
 	}								\
-	alloc_size == 0 || (vec).elems != NULL ? 0 : -1;		\
+	(alloc_size == 0 || (vec)->elems != NULL) ? 0 : -1;		\
 })
 
 /*!
@@ -75,10 +80,10 @@
  * \param vec Vector to deallocate.
  */
 #define ast_vector_free(vec) do {		\
-	ast_free((vec).elems);			\
-	(vec).elems = NULL;			\
-	(vec).max = 0;				\
-	(vec).current = 0;			\
+	ast_free((vec)->elems);			\
+	(vec)->elems = NULL;			\
+	(vec)->max = 0;				\
+	(vec)->current = 0;			\
 } while (0)
 
 /*!
@@ -90,25 +95,24 @@
  * \return 0 on success.
  * \return Non-zero on failure.
  */
-#define ast_vector_append(vec, elem) ({					\
-	int res = 0;							\
-									\
-	if ((vec).current + 1 > (vec).max) {				\
-		size_t new_max = (vec).max ? 2 * (vec).max : 1;		\
-		typeof((vec).elems) new_elems = ast_realloc(		\
-			(vec).elems, new_max * sizeof(*new_elems));	\
-		if (new_elems) {					\
-			(vec).elems = new_elems;				\
-			(vec).max = new_max;				\
-		} else {						\
-			res = -1;					\
-		}							\
-	}								\
-									\
-	if (res == 0) {							\
-		(vec).elems[(vec).current++] = (elem);			\
-	}								\
-	res;								\
+#define ast_vector_append(vec, elem) ({						\
+	int res = 0;								\
+	do {									\
+		if ((vec)->current + 1 > (vec)->max) {				\
+			size_t new_max = (vec)->max ? 2 * (vec)->max : 1;	\
+			typeof((vec)->elems) new_elems = ast_realloc(		\
+				(vec)->elems, new_max * sizeof(*new_elems));	\
+			if (new_elems) {					\
+				(vec)->elems = new_elems;			\
+				(vec)->max = new_max;				\
+			} else {						\
+				res = -1;					\
+				break;						\
+			}							\
+		}								\
+		(vec)->elems[(vec)->current++] = (elem);			\
+	} while (0);								\
+	res;									\
 })
 
 /*!
@@ -122,11 +126,11 @@
  * \return The element that was removed.
  */
 #define ast_vector_remove_unordered(vec, idx) ({		\
-	typeof((vec).elems[0]) res;				\
+	typeof((vec)->elems[0]) res;				\
 	size_t __idx = (idx);					\
-	ast_assert(__idx < (vec).current);			\
-	res = (vec).elems[__idx];				\
-	(vec).elems[__idx] = (vec).elems[--(vec).current];	\
+	ast_assert(__idx < (vec)->current);			\
+	res = (vec)->elems[__idx];				\
+	(vec)->elems[__idx] = (vec)->elems[--(vec)->current];	\
 	res;							\
 })
 
@@ -137,15 +141,18 @@
  * \param vec Vector to remove from.
  * \param value Value to pass into comparator.
  * \param cmp Comparator function/macros (called as \c cmp(elem, value))
+ * \param cleanup How to cleanup a removed element macro/function.
+ *
  * \return 0 if element was removed.
  * \return Non-zero if element was not in the vector.
  */
-#define ast_vector_remove_cmp_unordered(vec, value, cmp) ({		\
+#define ast_vector_remove_cmp_unordered(vec, value, cmp, cleanup) ({	\
 	int res = -1;							\
 	size_t idx;							\
 	typeof(value) __value = (value);				\
-	for (idx = 0; idx < (vec).current; ++idx) {			\
-		if (cmp((vec).elems[idx], __value)) {			\
+	for (idx = 0; idx < (vec)->current; ++idx) {			\
+		if (cmp((vec)->elems[idx], __value)) {			\
+			cleanup((vec)->elems[idx]);			\
 			ast_vector_remove_unordered((vec), idx);	\
 			res = 0;					\
 			break;						\
@@ -154,20 +161,39 @@
 	res;								\
 })
 
-/*! \brief Default comparator for ast_vector_remove_elem_unordered() */
-#define AST_VECTOR_DEFAULT_CMP(a, b) ((a) == (b))
+/*!
+ * \brief Default comparator for ast_vector_remove_elem_unordered()
+ *
+ * \param elem Element to compare against
+ * \param value Value to compare with the vector element.
+ *
+ * \return 0 if element does not match.
+ * \return Non-zero if element matches.
+ */
+#define AST_VECTOR_ELEM_DEFAULT_CMP(elem, value) ((elem) == (value))
+
+/*!
+ * \brief Vector element cleanup that does nothing.
+ *
+ * \param elem Element to cleanup
+ *
+ * \return Nothing
+ */
+#define AST_VECTOR_ELEM_CLEANUP_NOOP(elem)
 
 /*!
  * \brief Remove an element from a vector.
  *
  * \param vec Vector to remove from.
  * \param elem Element to remove
+ * \param cleanup How to cleanup a removed element macro/function.
+ *
  * \return 0 if element was removed.
  * \return Non-zero if element was not in the vector.
  */
-#define ast_vector_remove_elem_unordered(vec, elem) ({	\
-	ast_vector_remove_cmp_unordered((vec), (elem),	\
-		AST_VECTOR_DEFAULT_CMP);		\
+#define ast_vector_remove_elem_unordered(vec, elem, cleanup) ({	\
+	ast_vector_remove_cmp_unordered((vec), (elem),		\
+		AST_VECTOR_ELEM_DEFAULT_CMP, cleanup);		\
 })
 
 /*!
@@ -176,7 +202,19 @@
  * \param vec Vector to query.
  * \return Number of elements in the vector.
  */
-#define ast_vector_size(vec) (vec).current
+#define ast_vector_size(vec) (vec)->current
+
+/*!
+ * \brief Get an address of element in a vector.
+ *
+ * \param vec Vector to query.
+ * \param idx Index of the element to get address of.
+ */
+#define ast_vector_get_addr(vec, idx) ({	\
+	size_t __idx = (idx);			\
+	ast_assert(__idx < (vec)->current);	\
+	&(vec)->elems[__idx];			\
+})
 
 /*!
  * \brief Get an element from a vector.
@@ -186,8 +224,8 @@
  */
 #define ast_vector_get(vec, idx) ({		\
 	size_t __idx = (idx);			\
-	ast_assert(__idx < (vec).current);	\
-	(vec).elems[__idx];			\
+	ast_assert(__idx < (vec)->current);	\
+	(vec)->elems[__idx];			\
 })
 
 #endif /* _ASTERISK_VECTOR_H */

Modified: team/rmudgett/bridge_tasks/main/stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/bridge_tasks/main/stasis.c?view=diff&rev=402440&r1=402439&r2=402440
==============================================================================
--- team/rmudgett/bridge_tasks/main/stasis.c (original)
+++ team/rmudgett/bridge_tasks/main/stasis.c Sat Nov  2 00:02:45 2013
@@ -140,10 +140,10 @@
 struct stasis_topic {
 	char *name;
 	/*! Variable length array of the subscribers */
-	ast_vector(struct stasis_subscription *) subscribers;
+	ast_vector(, struct stasis_subscription *) subscribers;
 
 	/*! Topics forwarding into this topic */
-	ast_vector(struct stasis_topic *) upstream_topics;
+	ast_vector(, struct stasis_topic *) upstream_topics;
 };
 
 /* Forward declarations for the tightly-coupled subscription object */
@@ -152,18 +152,28 @@
 
 static int topic_remove_subscription(struct stasis_topic *topic, struct stasis_subscription *sub);
 
+/*! \brief Lock two topics. */
+#define topic_lock_both(topic1, topic2) \
+	do { \
+		ao2_lock(topic1); \
+		while (ao2_trylock(topic2)) { \
+			AO2_DEADLOCK_AVOIDANCE(topic1); \
+		} \
+	} while (0)
+
 static void topic_dtor(void *obj)
 {
 	struct stasis_topic *topic = obj;
 
 	/* Subscribers hold a reference to topics, so they should all be
 	 * unsubscribed before we get here. */
-	ast_assert(ast_vector_size(topic->subscribers) == 0);
+	ast_assert(ast_vector_size(&topic->subscribers) == 0);
+
 	ast_free(topic->name);
 	topic->name = NULL;
 
-	ast_vector_free(topic->subscribers);
-	ast_vector_free(topic->upstream_topics);
+	ast_vector_free(&topic->subscribers);
+	ast_vector_free(&topic->upstream_topics);
 }
 
 struct stasis_topic *stasis_topic_create(const char *name)
@@ -182,8 +192,8 @@
 		return NULL;
 	}
 
-	res |= ast_vector_init(topic->subscribers, INITIAL_SUBSCRIBERS_MAX);
-	res |= ast_vector_init(topic->upstream_topics, 0);
+	res |= ast_vector_init(&topic->subscribers, INITIAL_SUBSCRIBERS_MAX);
+	res |= ast_vector_init(&topic->upstream_topics, 0);
 
 	if (res != 0) {
 		return NULL;
@@ -279,6 +289,10 @@
 	int needs_mailbox)
 {
 	RAII_VAR(struct stasis_subscription *, sub, NULL, ao2_cleanup);
+
+	if (!topic) {
+		return NULL;
+	}
 
 	sub = ao2_alloc(sizeof(*sub), subscription_dtor);
 	if (!sub) {
@@ -414,8 +428,8 @@
 		struct stasis_topic *topic = sub->topic;
 		SCOPED_AO2LOCK(lock_topic, topic);
 
-		for (i = 0; i < ast_vector_size(topic->subscribers); ++i) {
-			if (ast_vector_get(topic->subscribers, i) == sub) {
+		for (i = 0; i < ast_vector_size(&topic->subscribers); ++i) {
+			if (ast_vector_get(&topic->subscribers, i) == sub) {
 				return 1;
 			}
 		}
@@ -466,11 +480,11 @@
 	 *
 	 * If we bumped the refcount here, the owner would have to unsubscribe
 	 * and cleanup, which is a bit awkward. */
-	ast_vector_append(topic->subscribers, sub);
-
-	for (idx = 0; idx < ast_vector_size(topic->upstream_topics); ++idx) {
+	ast_vector_append(&topic->subscribers, sub);
+
+	for (idx = 0; idx < ast_vector_size(&topic->upstream_topics); ++idx) {
 		topic_add_subscription(
-			ast_vector_get(topic->upstream_topics, idx), sub);
+			ast_vector_get(&topic->upstream_topics, idx), sub);
 	}
 
 	return 0;
@@ -481,12 +495,13 @@
 	size_t idx;
 	SCOPED_AO2LOCK(lock_topic, topic);
 
-	for (idx = 0; idx < ast_vector_size(topic->upstream_topics); ++idx) {
+	for (idx = 0; idx < ast_vector_size(&topic->upstream_topics); ++idx) {
 		topic_remove_subscription(
-			ast_vector_get(topic->upstream_topics, idx), sub);
-	}
-
-	return ast_vector_remove_elem_unordered(topic->subscribers, sub);
+			ast_vector_get(&topic->upstream_topics, idx), sub);
+	}
+
+	return ast_vector_remove_elem_unordered(&topic->subscribers, sub,
+		AST_VECTOR_ELEM_CLEANUP_NOOP);
 }
 
 /*!
@@ -512,7 +527,7 @@
 		ao2_bump(message);
 		if (ast_taskprocessor_push_local(sub->mailbox, dispatch_exec, message) != 0) {
 			/* Push failed; ugh. */
-			ast_log(LOG_DEBUG, "Dropping dispatch\n");
+			ast_log(LOG_ERROR, "Dropping dispatch\n");
 			ao2_cleanup(message);
 		}
 	} else {
@@ -521,26 +536,28 @@
 	}
 }
 
-void stasis_publish(struct stasis_topic *_topic, struct stasis_message *message)
+void stasis_publish(struct stasis_topic *topic, struct stasis_message *message)
 {
 	size_t i;
-	/* The topic may be unref'ed by the subscription invocation.
-	 * Make sure we hold onto a reference while dispatching. */
-	RAII_VAR(struct stasis_topic *, topic, ao2_bump(_topic),
-		ao2_cleanup);
-	SCOPED_AO2LOCK(lock, topic);
 
 	ast_assert(topic != NULL);
 	ast_assert(message != NULL);
 
-	for (i = 0; i < ast_vector_size(topic->subscribers); ++i) {
-		struct stasis_subscription *sub =
-			ast_vector_get(topic->subscribers, i);
+	/*
+	 * The topic may be unref'ed by the subscription invocation.
+	 * Make sure we hold onto a reference while dispatching.
+	 */
+	ao2_ref(topic, +1);
+	ao2_lock(topic);
+	for (i = 0; i < ast_vector_size(&topic->subscribers); ++i) {
+		struct stasis_subscription *sub = ast_vector_get(&topic->subscribers, i);
 
 		ast_assert(sub != NULL);
 
 		dispatch_message(sub, message);
 	}
+	ao2_unlock(topic);
+	ao2_ref(topic, -1);
 }
 
 /*!
@@ -570,23 +587,26 @@
 
 struct stasis_forward *stasis_forward_cancel(struct stasis_forward *forward)
 {
-	if (forward) {
-		int idx;
-
-		struct stasis_topic *from = forward->from_topic;
-		struct stasis_topic *to = forward->to_topic;
-
-		SCOPED_AO2LOCK(to_lock, to);
-
-		ast_vector_remove_elem_unordered(to->upstream_topics, from);
-
-		ao2_lock(from);
-		for (idx = 0; idx < ast_vector_size(to->subscribers); ++idx) {
-			topic_remove_subscription(
-				from, ast_vector_get(to->subscribers, idx));
-		}
-		ao2_unlock(from);
-	}
+	int idx;
+	struct stasis_topic *from;
+	struct stasis_topic *to;
+
+	if (!forward) {
+		return NULL;
+	}
+
+	from = forward->from_topic;
+	to = forward->to_topic;
+
+	topic_lock_both(to, from);
+	ast_vector_remove_elem_unordered(&to->upstream_topics, from,
+		AST_VECTOR_ELEM_CLEANUP_NOOP);
+
+	for (idx = 0; idx < ast_vector_size(&to->subscribers); ++idx) {
+		topic_remove_subscription(from, ast_vector_get(&to->subscribers, idx));
+	}
+	ao2_unlock(from);
+	ao2_unlock(to);
 
 	ao2_cleanup(forward);
 
@@ -596,6 +616,8 @@
 struct stasis_forward *stasis_forward_all(struct stasis_topic *from_topic,
 	struct stasis_topic *to_topic)
 {
+	int res;
+	size_t idx;
 	RAII_VAR(struct stasis_forward *, forward, NULL, ao2_cleanup);
 
 	if (!from_topic || !to_topic) {
@@ -610,23 +632,19 @@
 	forward->from_topic = ao2_bump(from_topic);
 	forward->to_topic = ao2_bump(to_topic);
 
-	{
-		SCOPED_AO2LOCK(lock, to_topic);
-		int res;
-
-		res = ast_vector_append(to_topic->upstream_topics, from_topic);
-		if (res != 0) {
-			return NULL;
-		}
-
-		{
-			SCOPED_AO2LOCK(lock, from_topic);
-			size_t idx;
-			for (idx = 0; idx < ast_vector_size(to_topic->subscribers); ++idx) {
-				topic_add_subscription(from_topic, ast_vector_get(to_topic->subscribers, idx));
-			}
-		}
-	}
+	topic_lock_both(to_topic, from_topic);
+	res = ast_vector_append(&to_topic->upstream_topics, from_topic);
+	if (res != 0) {
+		ao2_unlock(from_topic);
+		ao2_unlock(to_topic);
+		return NULL;
+	}
+
+	for (idx = 0; idx < ast_vector_size(&to_topic->subscribers); ++idx) {
+		topic_add_subscription(from_topic, ast_vector_get(&to_topic->subscribers, idx));
+	}
+	ao2_unlock(from_topic);
+	ao2_unlock(to_topic);
 
 	return ao2_bump(forward);
 }

Modified: team/rmudgett/bridge_tasks/main/stasis_message_router.c
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/bridge_tasks/main/stasis_message_router.c?view=diff&rev=402440&r1=402439&r2=402440
==============================================================================
--- team/rmudgett/bridge_tasks/main/stasis_message_router.c (original)
+++ team/rmudgett/bridge_tasks/main/stasis_message_router.c Sat Nov  2 00:02:45 2013
@@ -33,6 +33,7 @@
 
 #include "asterisk/astobj2.h"
 #include "asterisk/stasis_message_router.h"
+#include "asterisk/vector.h"
 
 /*! \internal */
 struct stasis_message_route {
@@ -44,19 +45,13 @@
 	void *data;
 };
 
-struct route_table {
-	/*! Current number of entries in the route table */
-	size_t current_size;
-	/*! Allocated number of entires in the route table */
-	size_t max_size;
-	/*! The route table itself */
-	struct stasis_message_route routes[];
-};
-
-static struct stasis_message_route *table_find_route(struct route_table *table,
+ast_vector(route_table, struct stasis_message_route);
+
+static struct stasis_message_route *route_table_find(struct route_table *table,
 	struct stasis_message_type *message_type)
 {
 	size_t idx;
+	struct stasis_message_route *route;
 
 	/* While a linear search for routes may seem very inefficient, most
 	 * route tables have six routes or less. For such small data, it's
@@ -64,59 +59,74 @@
 	 * tables, then we can look into containers with more efficient
 	 * lookups.
 	 */
-	for (idx = 0; idx < table->current_size; ++idx) {
-		if (table->routes[idx].message_type == message_type) {
-			return &table->routes[idx];
+	for (idx = 0; idx < ast_vector_size(table); ++idx) {
+		route = ast_vector_get_addr(table, idx);
+		if (route->message_type == message_type) {
+			return route;
 		}
 	}
 
 	return NULL;
 }
 
-static int table_add_route(struct route_table **table_ptr,
+/*!
+ * \brief route_table comparator for ast_vector_remove_cmp_unordered()
+ *
+ * \param elem Element to compare against
+ * \param value Value to compare with the vector element.
+ *
+ * \return 0 if element does not match.
+ * \return Non-zero if element matches.
+ */
+#define ROUTE_TABLE_ELEM_CMP(elem, value) ((elem).message_type == (value))
+
+/*!
+ * \brief route_table vector element cleanup.
+ *
+ * \param elem Element to cleanup
+ *
+ * \return Nothing
+ */
+#define ROUTE_TABLE_ELEM_CLEANUP(elem)  ao2_cleanup((elem).message_type)
+
+static int route_table_remove(struct route_table *table,
+	struct stasis_message_type *message_type)
+{
+	return ast_vector_remove_cmp_unordered(table, message_type, ROUTE_TABLE_ELEM_CMP,
+		ROUTE_TABLE_ELEM_CLEANUP);
+}
+
+static int route_table_add(struct route_table *table,
 	struct stasis_message_type *message_type,
 	stasis_subscription_cb callback, void *data)
 {
-	struct route_table *table = *table_ptr;
+	struct stasis_message_route route;
+	int res;
+
+	ast_assert(callback != NULL);
+	ast_assert(route_table_find(table, message_type) == NULL);
+
+	route.message_type = ao2_bump(message_type);
+	route.callback = callback;
+	route.data = data;
+
+	res = ast_vector_append(table, route);
+	if (res) {
+		ROUTE_TABLE_ELEM_CLEANUP(route);
+	}
+	return res;
+}
+
+static void route_table_dtor(struct route_table *table)
+{
+	size_t idx;
 	struct stasis_message_route *route;
 
-	ast_assert(table_find_route(table, message_type) == NULL);
-
-	if (table->current_size + 1 > table->max_size) {
-		size_t new_max_size = table->max_size ? table->max_size * 2 : 1;
-		struct route_table *new_table = ast_realloc(table,
-			sizeof(*new_table) +
-			sizeof(new_table->routes[0]) * new_max_size);
-		if (!new_table) {
-			return -1;
-		}
-		*table_ptr = table = new_table;
-		table->max_size = new_max_size;
-	}
-
-	route = &table->routes[table->current_size++];
-
-	route->message_type = ao2_bump(message_type);
-	route->callback = callback;
-	route->data = data;
-
-	return 0;
-}
-
-static int table_remove_route(struct route_table *table,
-	struct stasis_message_type *message_type)
-{
-	size_t idx;
-
-	for (idx = 0; idx < table->current_size; ++idx) {
-		if (table->routes[idx].message_type == message_type) {
-			ao2_cleanup(message_type);
-			table->routes[idx] =
-				table->routes[--table->current_size];
-			return 0;
-		}
-	}
-	return -1;
+	for (idx = 0; idx < ast_vector_size(table); ++idx) {
+		route = ast_vector_get_addr(table, idx);
+		ROUTE_TABLE_ELEM_CLEANUP(*route);
+	}
+	ast_vector_free(table);
 }
 
 /*! \internal */
@@ -124,9 +134,9 @@
 	/*! Subscription to the upstream topic */
 	struct stasis_subscription *subscription;
 	/*! Subscribed routes */
-	struct route_table *routes;

[... 157 lines stripped ...]



More information about the asterisk-commits mailing list