[asterisk-commits] russell: branch russell/smdi-1.4 r93093 - /team/russell/smdi-1.4/res/res_smdi.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Dec 14 15:39:12 CST 2007


Author: russell
Date: Fri Dec 14 15:39:10 2007
New Revision: 93093

URL: http://svn.digium.com/view/asterisk?view=rev&rev=93093
Log:
Combine the implementations of ast_smdi_md_message_wait() and mwi_message_wait(),
as the two functions were exactly the same except they operated on different queues

Modified:
    team/russell/smdi-1.4/res/res_smdi.c

Modified: team/russell/smdi-1.4/res/res_smdi.c
URL: http://svn.digium.com/view/asterisk/team/russell/smdi-1.4/res/res_smdi.c?view=diff&rev=93093&r1=93092&r2=93093
==============================================================================
--- team/russell/smdi-1.4/res/res_smdi.c (original)
+++ team/russell/smdi-1.4/res/res_smdi.c Fri Dec 14 15:39:10 2007
@@ -297,6 +297,78 @@
 	return md_msg;
 }
 
+enum smdi_message_type {
+	SMDI_MWI,
+	SMDI_MD,
+};
+
+static inline int lock_msg_q(struct ast_smdi_interface *iface, enum smdi_message_type type)
+{
+	if (type == SMDI_MWI)
+		return ast_mutex_lock(&iface->mwi_q_lock);
+	
+	return ast_mutex_lock(&iface->md_q_lock);
+}
+
+static inline int unlock_msg_q(struct ast_smdi_interface *iface, enum smdi_message_type type)
+{
+	if (type == SMDI_MWI)
+		return ast_mutex_unlock(&iface->mwi_q_lock);
+
+	return ast_mutex_unlock(&iface->md_q_lock);
+}
+
+static inline void *smdi_msg_pop(struct ast_smdi_interface *iface, enum smdi_message_type type)
+{
+	if (type == SMDI_MWI)
+		return ast_smdi_mwi_message_pop(iface);
+
+	return ast_smdi_md_message_pop(iface);
+}
+
+static void *smdi_message_wait(struct ast_smdi_interface *iface, int timeout, enum smdi_message_type type)
+{
+	struct timeval start;
+	long diff = 0;
+	void *msg;
+
+	start = ast_tvnow();
+	while (diff < timeout) {
+		struct timespec ts = { 0, };
+		struct timeval tv;
+
+		lock_msg_q(iface, type);
+
+		if ((msg = smdi_msg_pop(iface, type))) {
+			unlock_msg_q(iface, type);
+			return msg;
+		}
+
+		tv = ast_tvadd(start, ast_tv(0, timeout));
+		ts.tv_sec = tv.tv_sec;
+		ts.tv_nsec = tv.tv_usec * 1000;
+
+		/* If there were no messages in the queue, then go to sleep until one
+		 * arrives. */
+
+		ast_cond_wait(&iface->md_q_cond, &iface->md_q_lock);
+
+		if ((msg = smdi_msg_pop(iface, type))) {
+			unlock_msg_q(iface, type);
+			return msg;
+		}
+
+		unlock_msg_q(iface, type);
+
+		/* check timeout */
+		diff = ast_tvdiff_ms(ast_tvnow(), start);
+	}
+
+	/* A timeout occurred, but try one last time ... */
+
+	return ast_smdi_md_message_pop(iface);
+}
+
 /*!
  * \brief Get the next SMDI message from the queue.
  * \param iface a pointer to the interface to use.
@@ -309,47 +381,9 @@
  * \return the next SMDI message, or NULL if there were no pending messages and
  * the timeout has expired.
  */
-extern struct ast_smdi_md_message *ast_smdi_md_message_wait(struct ast_smdi_interface *iface, int timeout)
-{
-	struct timeval start;
-	long diff = 0;
-	struct ast_smdi_md_message *msg;
-
-	start = ast_tvnow();
-	while (diff < timeout) {
-		struct timespec ts = { 0, };
-		struct timeval tv;
-
-		ast_mutex_lock(&iface->md_q_lock);
-
-		if ((msg = ast_smdi_md_message_pop(iface))) {
-			ast_mutex_unlock(&iface->md_q_lock);
-			return msg;
-		}
-
-		tv = ast_tvadd(start, ast_tv(0, timeout));
-		ts.tv_sec = tv.tv_sec;
-		ts.tv_nsec = tv.tv_usec * 1000;
-
-		/* If there were no messages in the queue, then go to sleep until one
-		 * arrives. */
-
-		ast_cond_wait(&iface->md_q_cond, &iface->md_q_lock);
-
-		if ((msg = ast_smdi_md_message_pop(iface))) {
-			ast_mutex_unlock(&iface->md_q_lock);
-			return msg;
-		}
-	
-		ast_mutex_unlock(&iface->md_q_lock);
-
-		/* check timeout */
-		diff = ast_tvdiff_ms(ast_tvnow(), start);
-	}
-
-	/* A timeout occurred, but try one last time ... */
-
-	return (ast_smdi_md_message_pop(iface));
+struct ast_smdi_md_message *ast_smdi_md_message_wait(struct ast_smdi_interface *iface, int timeout)
+{
+	return smdi_message_wait(iface, timeout, SMDI_MD);
 }
 
 /*!
@@ -374,7 +408,7 @@
 
 	/* purge old messages */
 	now = ast_tvnow();
-	while (mwi_msg)	{
+	while (mwi_msg) {
 		elapsed = ast_tvdiff_ms(now, mwi_msg->timestamp);
 
 		if (elapsed > iface->msg_expiry) {
@@ -386,8 +420,7 @@
 			ast_mutex_lock(&iface->mwi_q_lock);
 			mwi_msg = ASTOBJ_CONTAINER_UNLINK_START(&iface->mwi_q);
 			ast_mutex_unlock(&iface->mwi_q_lock);
-		}
-		else {
+		} else {
 			/* good message, return it */
 			break;
 		}
@@ -410,45 +443,7 @@
  */
 extern struct ast_smdi_mwi_message *ast_smdi_mwi_message_wait(struct ast_smdi_interface *iface, int timeout)
 {
-	struct timeval start;
-	long diff = 0;
-	struct ast_smdi_mwi_message *msg;
-
-	start = ast_tvnow();
-	while (diff < timeout) {
-		struct timespec ts = { 0, };
-		struct timeval tv;
-
-		ast_mutex_lock(&iface->mwi_q_lock);
-
-		if ((msg = ast_smdi_mwi_message_pop(iface))) {
-			ast_mutex_unlock(&iface->mwi_q_lock);
-			return msg;
-		}
-
-		tv = ast_tvadd(start, ast_tv(0, timeout));
-		ts.tv_sec = tv.tv_sec;
-		ts.tv_nsec = tv.tv_usec * 1000;
-
-		/* If there were no messages in the queue, then go to sleep until one
-		 * arrives. */
-
-		ast_cond_wait(&iface->mwi_q_cond, &iface->mwi_q_lock);
-
-		if ((msg = ast_smdi_mwi_message_pop(iface))) {
-			ast_mutex_unlock(&iface->mwi_q_lock);
-			return msg;
-		}
-
-		ast_mutex_unlock(&iface->mwi_q_lock);
-
-		/* check timeout */
-		diff = ast_tvdiff_ms(ast_tvnow(), start);
-	}
-
-	/* A timeout occurred, but try one last time ... */
-
-	return (ast_smdi_mwi_message_pop(iface));
+	return smdi_message_wait(iface, timeout, SMDI_MWI);
 }
 
 /*!




More information about the asterisk-commits mailing list