[asterisk-commits] russell: branch russell/smdi-1.4 r93114 - /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:54:59 CST 2007


Author: russell
Date: Fri Dec 14 15:54:58 2007
New Revision: 93114

URL: http://svn.digium.com/view/asterisk?view=rev&rev=93114
Log:
Combine implementations of the mwi/md message pop functions as they do
the same thing 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=93114&r1=93113&r2=93114
==============================================================================
--- team/russell/smdi-1.4/res/res_smdi.c (original)
+++ team/russell/smdi-1.4/res/res_smdi.c Fri Dec 14 15:54:58 2007
@@ -253,40 +253,99 @@
 	ast_mutex_unlock(&iface->mwi_q_lock);
 }
 
-/*! 
- * \brief Get the next SMDI message from the queue.
- * \param iface a pointer to the interface to use.
- *
- * This function pulls the first unexpired message from the SMDI message queue
- * on the specified interface.  It will purge all expired SMDI messages before
- * returning.
- *
- * \return the next SMDI message, or NULL if there were no pending messages.
- */
-struct ast_smdi_md_message *ast_smdi_md_message_pop(struct ast_smdi_interface *iface)
-{
-	struct ast_smdi_md_message *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)
+{
+	switch (type) {
+	case SMDI_MWI:
+		return ast_mutex_lock(&iface->mwi_q_lock);
+	case SMDI_MD:	
+		return ast_mutex_lock(&iface->md_q_lock);
+	}
+	
+	return -1;
+}
+
+static inline int unlock_msg_q(struct ast_smdi_interface *iface, enum smdi_message_type type)
+{
+	switch (type) {
+	case SMDI_MWI:
+		return ast_mutex_unlock(&iface->mwi_q_lock);
+	case SMDI_MD:
+		return ast_mutex_unlock(&iface->md_q_lock);
+	}
+
+	return -1;
+}
+
+static inline void *unlink_from_msg_q(struct ast_smdi_interface *iface, enum smdi_message_type type)
+{
+	switch (type) {
+	case SMDI_MWI:
+		return ASTOBJ_CONTAINER_UNLINK_START(&iface->mwi_q);
+	case SMDI_MD:
+		return ASTOBJ_CONTAINER_UNLINK_START(&iface->md_q);
+	}
+
+	return NULL;
+}
+
+static inline struct timeval msg_timestamp(void *msg, enum smdi_message_type type)
+{
+	struct ast_smdi_md_message *md_msg = msg;
+	struct ast_smdi_mwi_message *mwi_msg = msg;
+
+	switch (type) {
+	case SMDI_MWI:
+		return mwi_msg->timestamp;
+	case SMDI_MD:
+		return md_msg->timestamp;
+	}
+
+	return ast_tv(0, 0);
+}
+
+static inline void unref_msg(void *msg, enum smdi_message_type type)
+{
+	struct ast_smdi_md_message *md_msg = msg;
+	struct ast_smdi_mwi_message *mwi_msg = msg;
+
+	switch (type) {
+	case SMDI_MWI:
+		ASTOBJ_UNREF(mwi_msg, ast_smdi_mwi_message_destroy);
+	case SMDI_MD:
+		ASTOBJ_UNREF(md_msg, ast_smdi_md_message_destroy);
+	}
+}
+
+static inline void *smdi_msg_pop(struct ast_smdi_interface *iface, enum smdi_message_type type)
+{
 	struct timeval now;
 	long elapsed = 0;
-
-	ast_mutex_lock(&iface->md_q_lock);
-	md_msg = ASTOBJ_CONTAINER_UNLINK_START(&iface->md_q);
-	ast_mutex_unlock(&iface->md_q_lock);
+	void *msg;
+
+	lock_msg_q(iface, type);
+	msg = unlink_from_msg_q(iface, type);
+	unlock_msg_q(iface, type);
 
 	/* purge old messages */
 	now = ast_tvnow();
-	while (md_msg) {
-		elapsed = ast_tvdiff_ms(now, md_msg->timestamp);
+	while (msg) {
+		elapsed = ast_tvdiff_ms(now, msg_timestamp(msg, type));
 
 		if (elapsed > iface->msg_expiry) {
 			/* found an expired message */
-			ASTOBJ_UNREF(md_msg, ast_smdi_md_message_destroy);
+			unref_msg(msg, type);
 			ast_log(LOG_NOTICE, "Purged expired message from %s SMDI MD message queue.  Message was %ld milliseconds too old.\n",
 				iface->name, elapsed - iface->msg_expiry);
 
-			ast_mutex_lock(&iface->md_q_lock);
-			md_msg = ASTOBJ_CONTAINER_UNLINK_START(&iface->md_q);
-			ast_mutex_unlock(&iface->md_q_lock);
+			lock_msg_q(iface, type);
+			msg = unlink_from_msg_q(iface, type);
+			unlock_msg_q(iface, type);
 		}
 		else {
 			/* good message, return it */
@@ -294,36 +353,7 @@
 		}
 	}
 
-	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);
+	return msg;
 }
 
 static void *smdi_message_wait(struct ast_smdi_interface *iface, int timeout, enum smdi_message_type type)
@@ -366,7 +396,22 @@
 
 	/* A timeout occurred, but try one last time ... */
 
-	return ast_smdi_md_message_pop(iface);
+	return smdi_msg_pop(iface, type);
+}
+
+/*! 
+ * \brief Get the next SMDI message from the queue.
+ * \param iface a pointer to the interface to use.
+ *
+ * This function pulls the first unexpired message from the SMDI message queue
+ * on the specified interface.  It will purge all expired SMDI messages before
+ * returning.
+ *
+ * \return the next SMDI message, or NULL if there were no pending messages.
+ */
+struct ast_smdi_md_message *ast_smdi_md_message_pop(struct ast_smdi_interface *iface)
+{
+	return smdi_msg_pop(iface, SMDI_MD);
 }
 
 /*!
@@ -396,37 +441,9 @@
  *
  * \return the next SMDI message, or NULL if there were no pending messages.
  */
-extern struct ast_smdi_mwi_message *ast_smdi_mwi_message_pop(struct ast_smdi_interface *iface)
-{
-	struct ast_smdi_mwi_message *mwi_msg;
-	struct timeval now;
-	long elapsed = 0;
-
-	ast_mutex_lock(&iface->mwi_q_lock);
-	mwi_msg = ASTOBJ_CONTAINER_UNLINK_START(&iface->mwi_q);
-	ast_mutex_unlock(&iface->mwi_q_lock);
-
-	/* purge old messages */
-	now = ast_tvnow();
-	while (mwi_msg) {
-		elapsed = ast_tvdiff_ms(now, mwi_msg->timestamp);
-
-		if (elapsed > iface->msg_expiry) {
-			/* found an expired message */
-			ASTOBJ_UNREF(mwi_msg, ast_smdi_mwi_message_destroy);
-			ast_log(LOG_NOTICE, "Purged expired message from %s SMDI MWI message queue.  Message was %ld milliseconds too old.\n",
-				iface->name, elapsed - iface->msg_expiry);
-
-			ast_mutex_lock(&iface->mwi_q_lock);
-			mwi_msg = ASTOBJ_CONTAINER_UNLINK_START(&iface->mwi_q);
-			ast_mutex_unlock(&iface->mwi_q_lock);
-		} else {
-			/* good message, return it */
-			break;
-		}
-	}
-
-	return mwi_msg;
+struct ast_smdi_mwi_message *ast_smdi_mwi_message_pop(struct ast_smdi_interface *iface)
+{
+	return smdi_msg_pop(iface, SMDI_MWI);
 }
 
 /*!




More information about the asterisk-commits mailing list