[asterisk-commits] dvossel: branch dvossel/sip_nonblocking_tcp_client r219944 - /team/dvossel/si...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Sep 23 15:52:06 CDT 2009


Author: dvossel
Date: Wed Sep 23 15:52:02 2009
New Revision: 219944

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=219944
Log:
Converted the tcptls thread list, threadl, to an ao2 container.
This will allow the threadinfo object to be found quicker when
the alert pipe needs to be used.


Modified:
    team/dvossel/sip_nonblocking_tcp_client/channels/chan_sip.c

Modified: team/dvossel/sip_nonblocking_tcp_client/channels/chan_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/sip_nonblocking_tcp_client/channels/chan_sip.c?view=diff&rev=219944&r1=219943&r2=219944
==============================================================================
--- team/dvossel/sip_nonblocking_tcp_client/channels/chan_sip.c (original)
+++ team/dvossel/sip_nonblocking_tcp_client/channels/chan_sip.c Wed Sep 23 15:52:02 2009
@@ -2101,10 +2101,10 @@
 /*! \brief Definition of a thread that handles a socket */
 struct sip_threadinfo {
 	int stop;
+	int alert_pipe[2];
 	pthread_t threadid;
 	struct ast_tcptls_session_instance *tcptls_session;
 	enum sip_transport type;	/*!< We keep a copy of the type here so we can display it in the connection list */
-	AST_LIST_ENTRY(sip_threadinfo) list;
 };
 
 /*! \brief Definition of an MWI subscription to another server */
@@ -2138,8 +2138,8 @@
 static int hash_user_size = 563;
 #endif
 
-/*! \brief  The thread list of TCP threads */
-static AST_LIST_HEAD_STATIC(threadl, sip_threadinfo);
+/*! \brief  The table of TCP threads */
+static struct ao2_container *threadl;
 
 /*! \brief  The peer list: Users, Peers and Friends */
 static struct ao2_container *peers;
@@ -2229,6 +2229,21 @@
 
 	/* Now only return a match if the port matches, as well. */
 	return peer->addr.sin_port == peer2->addr.sin_port ? (CMP_MATCH | CMP_STOP) : 0;
+}
+
+
+static int threadl_hash_cb(const void *obj, const int flags)
+{
+	const struct sip_threadinfo *th = obj;
+
+	return abs((int) th->tcptls_session->remote_address.sin_addr.s_addr);
+}
+
+static int threadl_cmp_cb(void *obj, void *arg, int flags)
+{
+	struct sip_threadinfo *th = obj, *th2 = arg;
+
+	return (th->tcptls_session == th2->tcptls_session) ? CMP_MATCH | CMP_STOP : 0;
 }
 
 /*!
@@ -2892,7 +2907,7 @@
 	struct sip_threadinfo *me;
 	char buf[1024] = "";
 
-	me = ast_calloc(1, sizeof(*me));
+	me = ao2_alloc(sizeof(*me), NULL);
 
 	if (!me)
 		goto cleanup2;
@@ -2906,9 +2921,7 @@
 
 	ast_debug(2, "Starting thread for %s server\n", tcptls_session->ssl ? "SSL" : "TCP");
 
-	AST_LIST_LOCK(&threadl);
-	AST_LIST_INSERT_TAIL(&threadl, me, list);
-	AST_LIST_UNLOCK(&threadl);
+	ao2_t_link(threadl, me, "Adding new tcptls helper thread");
 
 	if (!(req.data = ast_str_create(SIP_MIN_PACKET)))
 		goto cleanup;
@@ -2985,10 +2998,8 @@
 	}
 
 cleanup:
-	AST_LIST_LOCK(&threadl);
-	AST_LIST_REMOVE(&threadl, me, list);
-	AST_LIST_UNLOCK(&threadl);
-	ast_free(me);
+	ao2_t_unlink(threadl, me, "Removing tcptls helper thread, thread is closing");
+	ao2_t_ref(me, -1, "Closing tcptls thread, decrementing ref of sip_threadinfo");
 cleanup2:
 	fclose(tcptls_session->f);
 	tcptls_session->f = NULL;
@@ -3003,11 +3014,9 @@
 	}
 
 	ast_debug(2, "Shutting down thread for %s server\n", tcptls_session->ssl ? "SSL" : "TCP");
-	
 
 	ao2_ref(tcptls_session, -1);
 	tcptls_session = NULL;
-
 	return NULL;
 }
 
@@ -14684,6 +14693,7 @@
 static char *sip_show_tcp(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
 	struct sip_threadinfo *th;
+	struct ao2_iterator i;
 
 #define FORMAT2 "%-30.30s %3.6s %9.9s %6.6s\n"
 #define FORMAT  "%-30.30s %-6d %-9.9s %-6.6s\n"
@@ -14703,15 +14713,18 @@
 		return CLI_SHOWUSAGE;
 
 	ast_cli(a->fd, FORMAT2, "Host", "Port", "Transport", "Type");
-	AST_LIST_LOCK(&threadl);
-	AST_LIST_TRAVERSE(&threadl, th, list) {
+
+	ao2_lock(threadl);
+	i = ao2_iterator_init(threadl, 0);
+	while ((th = ao2_t_iterator_next(&i, "iterate through tcp threads for 'sip show tcp'"))) {
 		ast_cli(a->fd, FORMAT, ast_inet_ntoa(th->tcptls_session->remote_address.sin_addr),
 			ntohs(th->tcptls_session->remote_address.sin_port),
 			get_transport(th->type),
 			(th->tcptls_session->client ? "Client" : "Server"));
-
-	}
-	AST_LIST_UNLOCK(&threadl);
+		ao2_t_ref(th, -1, "decrement ref from iterator");
+	}
+	ao2_unlock(threadl);
+
 	return CLI_SUCCESS;
 #undef FORMAT
 #undef FORMAT2
@@ -22564,6 +22577,20 @@
 		return port == STANDARD_SIP_PORT;
 }
 
+static int threadinfo_locate_cb(void *obj, void *arg, int flags)
+{
+	struct sip_threadinfo *th = obj;
+	struct sockaddr_in *s = arg;
+
+	if ((s->sin_family == th->tcptls_session->remote_address.sin_family) &&
+		(s->sin_addr.s_addr == th->tcptls_session->remote_address.sin_addr.s_addr) &&
+		(s->sin_port == th->tcptls_session->remote_address.sin_port)) {
+
+		return CMP_MATCH | CMP_STOP;
+	}
+	return 0;
+}
+
 /*!
  * \brief Find thread for TCP/TLS session (based on IP/Port
  *
@@ -22574,16 +22601,10 @@
 	struct sip_threadinfo *th;
 	struct ast_tcptls_session_instance *tcptls_instance = NULL;
 
-	AST_LIST_LOCK(&threadl);
-	AST_LIST_TRAVERSE(&threadl, th, list) {
-		if ((s->sin_family == th->tcptls_session->remote_address.sin_family) &&
-			(s->sin_addr.s_addr == th->tcptls_session->remote_address.sin_addr.s_addr) &&
-			(s->sin_port == th->tcptls_session->remote_address.sin_port))  {
-				tcptls_instance = (ao2_ref(th->tcptls_session, +1), th->tcptls_session);
-				break;
-			}
-	}
-	AST_LIST_UNLOCK(&threadl);
+	if ((th = ao2_callback(threadl, 0, threadinfo_locate_cb, s))) {
+		tcptls_instance = (ao2_ref(th->tcptls_session, +1), th->tcptls_session);
+		ao2_t_ref(th, -1, "decrement ref from callback");
+	}
 
 	return tcptls_instance;
 }
@@ -26144,6 +26165,7 @@
 	peers = ao2_t_container_alloc(hash_peer_size, peer_hash_cb, peer_cmp_cb, "allocate peers");
 	peers_by_ip = ao2_t_container_alloc(hash_peer_size, peer_iphash_cb, peer_ipcmp_cb, "allocate peers_by_ip");
 	dialogs = ao2_t_container_alloc(hash_dialog_size, dialog_hash_cb, dialog_cmp_cb, "allocate dialogs");
+	threadl = ao2_t_container_alloc(hash_dialog_size, threadl_hash_cb, threadl_cmp_cb, "allocate threadl table");
 	
 	ASTOBJ_CONTAINER_INIT(&regl); /* Registry object list -- not searched for anything */
 	ASTOBJ_CONTAINER_INIT(&submwil); /* MWI subscription object list */
@@ -26274,17 +26296,14 @@
 		ast_tcptls_server_stop(&sip_tls_desc);
 
 	/* Kill all existing TCP/TLS threads */
-	AST_LIST_LOCK(&threadl);
-	AST_LIST_TRAVERSE_SAFE_BEGIN(&threadl, th, list) {
+	i = ao2_iterator_init(threadl, 0);
+	while ((th = ao2_t_iterator_next(&i, "iterate through tcp threads for 'sip show tcp'"))) {
 		pthread_t thread = th->threadid;
 		th->stop = 1;
-		AST_LIST_UNLOCK(&threadl);
 		pthread_kill(thread, SIGURG);
 		pthread_join(thread, NULL);
-		AST_LIST_LOCK(&threadl);
-	}
-	AST_LIST_TRAVERSE_SAFE_END;
-	AST_LIST_UNLOCK(&threadl);
+		ao2_t_ref(th, -1, "decrement ref from iterator");
+	}
 
 	/* Hangup all dialogs if they have an owner */
 	i = ao2_iterator_init(dialogs, 0);
@@ -26335,6 +26354,7 @@
 	ao2_t_ref(peers, -1, "unref the peers table");
 	ao2_t_ref(peers_by_ip, -1, "unref the peers_by_ip table");
 	ao2_t_ref(dialogs, -1, "unref the dialogs table");
+	ao2_t_ref(threadl, -1, "unref the thread table");
 
 	clear_sip_domains();
 	close(sipsock);




More information about the asterisk-commits mailing list