[asterisk-commits] branch oej/test-this-branch r12135 - in /team/oej/test-this-branch: ./ includ...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Mon Mar 6 13:46:46 MST 2006


Author: oej
Date: Mon Mar  6 14:46:42 2006
New Revision: 12135

URL: http://svn.digium.com/view/asterisk?rev=12135&view=rev
Log:
Issue 6053 - New CLI command "show threads" (Luigi Rizzo)

Modified:
    team/oej/test-this-branch/README.test-this-branch
    team/oej/test-this-branch/asterisk.c
    team/oej/test-this-branch/include/asterisk/utils.h
    team/oej/test-this-branch/utils.c

Modified: team/oej/test-this-branch/README.test-this-branch
URL: http://svn.digium.com/view/asterisk/team/oej/test-this-branch/README.test-this-branch?rev=12135&r1=12134&r2=12135&view=diff
==============================================================================
--- team/oej/test-this-branch/README.test-this-branch (original)
+++ team/oej/test-this-branch/README.test-this-branch Mon Mar  6 14:46:42 2006
@@ -36,6 +36,7 @@
 - HDLC mode support for ZAP channels (crich, #6251)
 - Show manager CLI commands (junky, #5240)
 - Support SIP_CODEC for early media (oej, #6576)
+- Show threads CLI command (rizzo, #6053)
 
 Coming here soon:
 - siptransfer: Improved SIP transfer support

Modified: team/oej/test-this-branch/asterisk.c
URL: http://svn.digium.com/view/asterisk/team/oej/test-this-branch/asterisk.c?rev=12135&r1=12134&r2=12135&view=diff
==============================================================================
--- team/oej/test-this-branch/asterisk.c (original)
+++ team/oej/test-this-branch/asterisk.c Mon Mar  6 14:46:42 2006
@@ -269,6 +269,65 @@
 	AST_LIST_UNLOCK(&file_versions);
 	if (find)
 		free(find);
+}
+
+struct thread_list_t {
+	AST_LIST_ENTRY(thread_list_t) list;
+	char *name;
+	const void *id;
+};
+
+static AST_LIST_HEAD_STATIC(thread_list, thread_list_t);
+
+static char show_threads_help[] =
+"Usage: show threads\n"
+"       List threads currently active in the system.\n";
+
+void ast_register_thread(char *name)
+{ 
+	struct thread_list_t *new = ast_calloc(1, sizeof(*new));
+
+	if (!new)
+		return;
+	new->id = pthread_self();
+	new->name = name; /* this was a copy already */
+	AST_LIST_LOCK(&thread_list);
+	AST_LIST_INSERT_HEAD(&thread_list, new, list);
+	AST_LIST_UNLOCK(&thread_list);
+}
+
+void ast_unregister_thread(void *id)
+{
+	struct thread_list_t *x;
+
+	AST_LIST_LOCK(&thread_list);
+	AST_LIST_TRAVERSE_SAFE_BEGIN(&thread_list, x, list) {
+		if (x->id == id) {
+			AST_LIST_REMOVE_CURRENT(&thread_list, list);
+			break;
+		}
+	}
+	AST_LIST_TRAVERSE_SAFE_END;
+	AST_LIST_UNLOCK(&thread_list);
+	if (x) {
+		free(x->name);
+		free(x);
+	}
+}
+
+static int handle_show_threads(int fd, int argc, char *argv[])
+{
+	int count = 0;
+	struct thread_list_t *cur;
+
+	AST_LIST_LOCK(&thread_list);
+	AST_LIST_TRAVERSE(&thread_list, cur, list) {
+		ast_cli(fd, "%p %s\n", cur->id, cur->name);
+		count++;
+	}
+        AST_LIST_UNLOCK(&thread_list);
+	ast_cli(fd, "%d threads listed.\n", count);
+	return 0;
 }
 
 static char show_version_files_help[] = 
@@ -1228,6 +1287,8 @@
 #if !defined(LOW_MEMORY)
 	{ { "show", "version", "files", NULL }, handle_show_version_files,
 	  "Show versions of files used to build Asterisk", show_version_files_help, complete_show_version_files },
+	{ { "show", "threads", NULL }, handle_show_threads,
+	  "Show running threads", show_threads_help, NULL },
 #endif /* ! LOW_MEMORY */
 };
 

Modified: team/oej/test-this-branch/include/asterisk/utils.h
URL: http://svn.digium.com/view/asterisk/team/oej/test-this-branch/include/asterisk/utils.h?rev=12135&r1=12134&r2=12135&view=diff
==============================================================================
--- team/oej/test-this-branch/include/asterisk/utils.h (original)
+++ team/oej/test-this-branch/include/asterisk/utils.h Mon Mar  6 14:46:42 2006
@@ -225,8 +225,14 @@
 }
 
 #define AST_STACKSIZE 256 * 1024
-#define ast_pthread_create(a,b,c,d) ast_pthread_create_stack(a,b,c,d,0)
-int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data, size_t stacksize);
+
+void ast_register_thread(char *name);
+void ast_unregister_thread(void *id);
+
+#define ast_pthread_create(a,b,c,d) ast_pthread_create_stack(a,b,c,d,0, \
+	 __FILE__, __FUNCTION__, __LINE__, #c)
+int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data, size_t stacksize,
+	const char *file, const char *caller, int line, const char *start_fn);
 
 /*!
 	\brief Process a string to find and replace characters

Modified: team/oej/test-this-branch/utils.c
URL: http://svn.digium.com/view/asterisk/team/oej/test-this-branch/utils.c?rev=12135&r1=12134&r2=12135&view=diff
==============================================================================
--- team/oej/test-this-branch/utils.c (original)
+++ team/oej/test-this-branch/utils.c Mon Mar  6 14:46:42 2006
@@ -506,8 +506,35 @@
 #undef pthread_create /* For ast_pthread_create function only */
 #endif /* !__linux__ */
 
-int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data, size_t stacksize)
-{
+/*
+ * support for 'show threads'. The start routine is wrapped by
+ * dummy_start(), so that ast_register_thread() and
+ * ast_unregister_thread() know the thread identifier.
+ */
+struct thr_arg {
+	void *(*start_routine)(void *);
+	void *data;
+	char *name;
+};
+
+static void *dummy_start(void *data)
+{
+	void *ret;
+	struct thr_arg a = *((struct thr_arg *)data);	/* make a local copy */
+
+	free(data);
+	ast_register_thread(a.name);
+	pthread_cleanup_push(ast_unregister_thread, pthread_self());	/* on unregister */
+	ret = a.start_routine(a.data);
+	pthread_cleanup_pop(1);
+	return ret;
+}
+
+int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data, size_t stacksize,
+	const char *file, const char *caller, int line, const char *start_fn)
+{
+	struct thr_arg *a;
+
 	pthread_attr_t lattr;
 	if (!attr) {
 		pthread_attr_init(&lattr);
@@ -531,6 +558,17 @@
 	errno = pthread_attr_setstacksize(attr, stacksize);
 	if (errno)
 		ast_log(LOG_WARNING, "pthread_attr_setstacksize returned non-zero: %s\n", strerror(errno));
+	a = ast_malloc(sizeof(*a));
+	if (!a)
+		ast_log(LOG_WARNING, "no memory, thread %s will not be listed\n", start_fn);
+	else {	/* remap parameters */
+		a->start_routine = start_routine;
+		a->data = data;
+		start_routine = dummy_start;
+		asprintf(&a->name, "%-20s started at [%5d] %s %s()",
+			start_fn, line, file, caller);
+		data = a;
+	}
 	return pthread_create(thread, attr, start_routine, data); /* We're in ast_pthread_create, so it's okay */
 }
 



More information about the asterisk-commits mailing list