[asterisk-commits] russell: branch russell/chan_refcount r82305 - in /team/russell/chan_refcount...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Sep 12 18:20:08 CDT 2007


Author: russell
Date: Wed Sep 12 18:20:08 2007
New Revision: 82305

URL: http://svn.digium.com/view/asterisk?view=rev&rev=82305
Log:
fix up header files, add ao2_trylock(), fix various typos in channel.c so it will build

Modified:
    team/russell/chan_refcount/include/asterisk/astobj2.h
    team/russell/chan_refcount/include/asterisk/channel.h
    team/russell/chan_refcount/include/asterisk/lock.h
    team/russell/chan_refcount/main/astobj2.c
    team/russell/chan_refcount/main/channel.c

Modified: team/russell/chan_refcount/include/asterisk/astobj2.h
URL: http://svn.digium.com/view/asterisk/team/russell/chan_refcount/include/asterisk/astobj2.h?view=diff&rev=82305&r1=82304&r2=82305
==============================================================================
--- team/russell/chan_refcount/include/asterisk/astobj2.h (original)
+++ team/russell/chan_refcount/include/asterisk/astobj2.h Wed Sep 12 18:20:08 2007
@@ -191,6 +191,15 @@
  * \return 0 on success, other values on error.
  */
 int ao2_lock(void *a);
+
+/*!
+ * \brief Try to lock an object.
+ * 
+ * \param a A pointer to the object we want lock.
+ *
+ * \return 0 on success, other values on error.
+ */
+int ao2_trylock(void *a);
 
 /*!
  * Unlock an object.

Modified: team/russell/chan_refcount/include/asterisk/channel.h
URL: http://svn.digium.com/view/asterisk/team/russell/chan_refcount/include/asterisk/channel.h?view=diff&rev=82305&r1=82304&r2=82305
==============================================================================
--- team/russell/chan_refcount/include/asterisk/channel.h (original)
+++ team/russell/chan_refcount/include/asterisk/channel.h Wed Sep 12 18:20:08 2007
@@ -153,6 +153,8 @@
 #include "asterisk/linkedlists.h"
 #include "asterisk/stringfields.h"
 #include "asterisk/compiler.h"
+#include "asterisk/astobj2.h"
+
 #include <limits.h>
 
 #define DATASTORE_INHERIT_FOREVER	INT_MAX
@@ -692,8 +694,15 @@
 /*! \brief Change channel name */
 void ast_change_name(struct ast_channel *chan, char *newname);
 
-/*! \brief Free a channel structure */
-void  ast_channel_free(struct ast_channel *);
+/*!
+ * \brief Unlink and release reference to a channel
+ *
+ * This function will unlink the channel from the global channels container
+ * if it is still there and also release the current reference to the channel.
+ *
+ * \return NULL, convenient for clearing invalid pointers
+ */
+struct ast_channel *ast_channel_free(struct ast_channel *);
 
 /*! \brief Requests a channel 
  * \param type type of channel to request
@@ -1474,6 +1483,46 @@
  */
 const char *ast_channel_reason2str(int reason);
 
+#define ast_channel_lock(c) ao2_lock(c)
+#define ast_channel_unlock(c) ao2_unlock(c)
+#define ast_channel_trylock(c) ao2_trylock(c)
+
+static force_inline struct ast_channel *ast_channel_ref(struct ast_channel *chan)
+{
+	ao2_ref(chan, +1);
+	return chan;
+}
+
+static force_inline struct ast_channel *ast_channel_unref(struct ast_channel *chan)
+{
+	ao2_ref(chan, -1);
+	return NULL;
+}
+
+/* 
+ * \brief A channel iterator
+ *
+ * This is an opaque type.
+ */
+struct ast_channel_iterator;
+
+struct ast_channel_iterator *ast_channel_iterator_destroy(struct ast_channel_iterator *i);
+
+struct ast_channel_iterator *ast_channel_iterator_by_exten_new(const char *exten, 
+	const char *context);
+
+struct ast_channel_iterator *ast_channel_iterator_by_name_new(const char *name, 
+	size_t name_len);
+
+struct ast_channel_iterator *ast_channel_iterator_all_new(void);
+
+struct ast_channel *ast_channel_iterator_next(struct ast_channel_iterator *i);
+
+struct ast_channel *ast_channel_get_by_name(const char *name);
+
+struct ast_channel *ast_channel_get_by_name_prefix(const char *name, size_t name_len);
+
+struct ast_channel *ast_channel_get_by_exten(const char *exten, const char *context);
 
 #if defined(__cplusplus) || defined(c_plusplus)
 }

Modified: team/russell/chan_refcount/include/asterisk/lock.h
URL: http://svn.digium.com/view/asterisk/team/russell/chan_refcount/include/asterisk/lock.h?view=diff&rev=82305&r1=82304&r2=82305
==============================================================================
--- team/russell/chan_refcount/include/asterisk/lock.h (original)
+++ team/russell/chan_refcount/include/asterisk/lock.h Wed Sep 12 18:20:08 2007
@@ -839,32 +839,4 @@
 })
 #endif
 
-#ifndef DEBUG_CHANNEL_LOCKS
-/*! \brief Lock a channel. If DEBUG_CHANNEL_LOCKS is defined 
-	in the Makefile, print relevant output for debugging */
-#define ast_channel_lock(x)		ast_mutex_lock(&x->lock)
-/*! \brief Unlock a channel. If DEBUG_CHANNEL_LOCKS is defined 
-	in the Makefile, print relevant output for debugging */
-#define ast_channel_unlock(x)		ast_mutex_unlock(&x->lock)
-/*! \brief Try locking a channel. If DEBUG_CHANNEL_LOCKS is defined 
-	in the Makefile, print relevant output for debugging */
-#define ast_channel_trylock(x)		ast_mutex_trylock(&x->lock)
-#else
-
-struct ast_channel;
-
-/*! \brief Lock AST channel (and print debugging output)
-\note You need to enable DEBUG_CHANNEL_LOCKS for this function */
-int ast_channel_lock(struct ast_channel *chan);
-
-/*! \brief Unlock AST channel (and print debugging output)
-\note You need to enable DEBUG_CHANNEL_LOCKS for this function
-*/
-int ast_channel_unlock(struct ast_channel *chan);
-
-/*! \brief Lock AST channel (and print debugging output)
-\note   You need to enable DEBUG_CHANNEL_LOCKS for this function */
-int ast_channel_trylock(struct ast_channel *chan);
-#endif
-
 #endif /* _ASTERISK_LOCK_H */

Modified: team/russell/chan_refcount/main/astobj2.c
URL: http://svn.digium.com/view/asterisk/team/russell/chan_refcount/main/astobj2.c?view=diff&rev=82305&r1=82304&r2=82305
==============================================================================
--- team/russell/chan_refcount/main/astobj2.c (original)
+++ team/russell/chan_refcount/main/astobj2.c Wed Sep 12 18:20:08 2007
@@ -129,6 +129,22 @@
 	ast_atomic_fetchadd_int(&ao2.total_locked, 1);
 
 	return ast_mutex_lock(&p->priv_data.lock);
+}
+
+int ao2_trylock(void *user_data)
+{
+	struct astobj2 *p = INTERNAL_OBJ(user_data);
+	int res;
+
+	if (!p)
+		return -1;
+
+	res = ast_mutex_trylock(&p->priv_data.lock);
+
+	if (!res)
+		ast_atomic_fetchadd_int(&ao2.total_locked, 1);
+
+	return res;
 }
 
 int ao2_unlock(void *user_data)

Modified: team/russell/chan_refcount/main/channel.c
URL: http://svn.digium.com/view/asterisk/team/russell/chan_refcount/main/channel.c?view=diff&rev=82305&r1=82304&r2=82305
==============================================================================
--- team/russell/chan_refcount/main/channel.c (original)
+++ team/russell/chan_refcount/main/channel.c Wed Sep 12 18:20:08 2007
@@ -233,7 +233,7 @@
 	if (argc != 4)
 		return RESULT_SHOWUSAGE;
 	
-	AST_LIST_TRAVERSE(&backends);
+	AST_LIST_LOCK(&backends);
 	AST_LIST_TRAVERSE(&backends, cl, list) {
 		if (!strncasecmp(cl->tech->type, argv[3], strlen(cl->tech->type))) {
 			break;
@@ -410,30 +410,34 @@
 int ast_channel_register(const struct ast_channel_tech *tech)
 {
 	struct chanlist *chan;
+	int res = 0;
 
 	AST_LIST_LOCK(&backends);
 	AST_LIST_TRAVERSE(&backends, chan, list) {
 		if (!strcasecmp(tech->type, chan->tech->type)) {
 			ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
-			AST_LIST_UNLOCK(&backends);
-			return -1;
+			res = -1;
+			goto return_unlock;
 		}
 	}
 	
 	if (!(chan = ast_calloc(1, sizeof(*chan)))) {
-		AST_RWLIST_UNLOCK(&channels);
-		return -1;
-	}
+		res = -1;
+		goto return_unlock;
+	}
+
 	chan->tech = tech;
+
 	AST_LIST_INSERT_HEAD(&backends, chan, list);
 
 	ast_debug(1, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description);
 
 	ast_verb(2, "Registered channel type '%s' (%s)\n", chan->tech->type, chan->tech->description);
 
+return_unlock:
 	AST_LIST_UNLOCK(&backends);
 
-	return 0;
+	return res;
 }
 
 /*! \brief Unregister channel driver */
@@ -632,7 +636,7 @@
 	return ast_channel_unref(chan);
 }
 
-static int ast_channel_destructor(void *data)
+static void ast_channel_destructor(void *data)
 {
 	struct ast_channel *chan = data;
 	int fd;
@@ -645,17 +649,8 @@
 	struct ast_datastore *datastore = NULL;
 	char name[AST_CHANNEL_NAME];
 	
-	headp=&chan->varshead;
-	
-	AST_RWLIST_WRLOCK(&channels);
-	if (!AST_RWLIST_REMOVE(&channels, chan, chan_list)) {
-		AST_RWLIST_UNLOCK(&channels);
-		ast_log(LOG_ERROR, "Unable to find channel in list to free. Assuming it has already been done.\n");
-	}
-	/* Lock and unlock the channel just to be sure nobody
-	   has it locked still */
-	ast_channel_lock(chan);
-	ast_channel_unlock(chan);
+	headp = &chan->varshead;
+	
 	if (chan->tech_pvt) {
 		ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n", chan->name);
 		ast_free(chan->tech_pvt);
@@ -714,12 +709,11 @@
 
 	ast_app_group_discard(chan);
 
-	/* Destroy the jitterbuffer */
 	ast_jb_destroy(chan);
 
 	ast_string_field_free_pools(chan);
+
 	ast_free(chan);
-	AST_RWLIST_UNLOCK(&channels);
 
 	ast_device_state_changed_literal(name);
 }
@@ -3278,10 +3272,10 @@
 		if (res < 0) {
 			ast_log(LOG_WARNING, "No translator path exists for channel type %s (native 0x%x) to 0x%x\n", type, chan->tech->capabilities, format);
 			*cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
-			AST_RWLIST_UNLOCK(&channels);
+			AST_LIST_UNLOCK(&backends);
 			return NULL;
 		}
-		AST_RWLIST_UNLOCK(&channels);
+		AST_LIST_UNLOCK(&backends);
 		if (!chan->tech->requester)
 			return NULL;
 		
@@ -4788,43 +4782,6 @@
 	}
 };
 
-#ifdef DEBUG_CHANNEL_LOCKS
-
-/*! \brief Unlock AST channel (and print debugging output) 
-\note You need to enable DEBUG_CHANNEL_LOCKS for this function
-*/
-int ast_channel_unlock(struct ast_channel *chan)
-{
-	int res = 0;
-	ast_debug(3, "::::==== Unlocking AST channel %s\n", chan->name);
-	
-	if (!chan) {
-		ast_debug(1, "::::==== Unlocking non-existing channel \n");
-		return 0;
-	}
-
-	res = ast_mutex_unlock(&chan->lock);
-
-	if (option_debug > 2) {
-#ifdef DEBUG_THREADS
-		int count = 0;
-		if ((count = chan->lock.reentrancy))
-			ast_debug(3, ":::=== Still have %d locks (recursive)\n", count);
-#endif
-		if (!res)
-			ast_debug(3, "::::==== Channel %s was unlocked\n", chan->name);
-			if (res == EINVAL) {
-				ast_debug(3, "::::==== Channel %s had no lock by this thread. Failed unlocking\n", chan->name);
-			}
-		}
-		if (res == EPERM) {
-			/* We had no lock, so okay any way*/
-			ast_debug(4, "::::==== Channel %s was not locked at all \n", chan->name);
-		res = 0;
-	}
-	return res;
-}
-
 /*
  * Wrappers for various ast_say_*() functions that call the full version
  * of the same functions.




More information about the asterisk-commits mailing list