[asterisk-commits] jpeeler: trunk r298137 - in /trunk: ./ include/asterisk/ main/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sat Dec 11 21:58:37 CST 2010


Author: jpeeler
Date: Sat Dec 11 21:58:33 2010
New Revision: 298137

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=298137
Log:
Add support for several platforms to obtain the real thread ID.

Already had the pthread ID which is not the same.  The most obvious enhancement
is in the "core show threads" output. As stated in the utils header, if the
platform isn't supported -1 is reported (instead of the process ID previously).

Modified:
    trunk/configure
    trunk/configure.ac
    trunk/include/asterisk/autoconfig.h.in
    trunk/include/asterisk/utils.h
    trunk/main/asterisk.c
    trunk/main/logger.c
    trunk/main/utils.c

Modified: trunk/configure.ac
URL: http://svnview.digium.com/svn/asterisk/trunk/configure.ac?view=diff&rev=298137&r1=298136&r2=298137
==============================================================================
--- trunk/configure.ac (original)
+++ trunk/configure.ac Sat Dec 11 21:58:33 2010
@@ -774,6 +774,9 @@
 #)
 #fi
 
+# for FreeBSD thr_self
+AC_CHECK_HEADERS([sys/thr.h])
+
 AC_MSG_CHECKING(for compiler atomic operations)
 AC_LINK_IFELSE(
 AC_LANG_PROGRAM([], [int foo1; int foo2 = __sync_fetch_and_add(&foo1, 1);]),

Modified: trunk/include/asterisk/autoconfig.h.in
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/autoconfig.h.in?view=diff&rev=298137&r1=298136&r2=298137
==============================================================================
--- trunk/include/asterisk/autoconfig.h.in (original)
+++ trunk/include/asterisk/autoconfig.h.in Sat Dec 11 21:58:33 2010
@@ -914,6 +914,9 @@
 
 /* Define to 1 if you have the <sys/stat.h> header file. */
 #undef HAVE_SYS_STAT_H
+
+/* Define to 1 if you have the <sys/thr.h> header file. */
+#undef HAVE_SYS_THR_H
 
 /* Define to 1 if you have the <sys/time.h> header file. */
 #undef HAVE_SYS_TIME_H

Modified: trunk/include/asterisk/utils.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/utils.h?view=diff&rev=298137&r1=298136&r2=298137
==============================================================================
--- trunk/include/asterisk/utils.h (original)
+++ trunk/include/asterisk/utils.h Sat Dec 11 21:58:33 2010
@@ -758,4 +758,11 @@
  */
 int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2);
 
+/*!
+ * \brief Get current thread ID
+ * \param None
+ * \return the ID if platform is supported, else -1
+ */
+int ast_get_tid(void);
+
 #endif /* _ASTERISK_UTILS_H */

Modified: trunk/main/asterisk.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/asterisk.c?view=diff&rev=298137&r1=298136&r2=298137
==============================================================================
--- trunk/main/asterisk.c (original)
+++ trunk/main/asterisk.c Sat Dec 11 21:58:33 2010
@@ -379,6 +379,7 @@
 	AST_RWLIST_ENTRY(thread_list_t) list;
 	char *name;
 	pthread_t id;
+	int lwp;
 };
 
 static AST_RWLIST_HEAD_STATIC(thread_list, thread_list_t);
@@ -390,6 +391,7 @@
 	if (!new)
 		return;
 	new->id = pthread_self();
+	new->lwp = ast_get_tid();
 	new->name = name; /* steal the allocated memory for the thread name */
 	AST_RWLIST_WRLOCK(&thread_list);
 	AST_RWLIST_INSERT_HEAD(&thread_list, new, list);
@@ -516,7 +518,7 @@
 
 	AST_RWLIST_RDLOCK(&thread_list);
 	AST_RWLIST_TRAVERSE(&thread_list, cur, list) {
-		ast_cli(a->fd, "%p %s\n", (void *)cur->id, cur->name);
+		ast_cli(a->fd, "%p %d %s\n", (void *)cur->id, cur->lwp, cur->name);
 		count++;
 	}
         AST_RWLIST_UNLOCK(&thread_list);

Modified: trunk/main/logger.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/logger.c?view=diff&rev=298137&r1=298136&r2=298137
==============================================================================
--- trunk/main/logger.c (original)
+++ trunk/main/logger.c Sat Dec 11 21:58:33 2010
@@ -58,15 +58,6 @@
 #define MAX_BACKTRACE_FRAMES 20
 #endif
 
-#if defined(__linux__) && !defined(__NR_gettid)
-#include <asm/unistd.h>
-#endif
-
-#if defined(__linux__) && defined(__NR_gettid)
-#define GETTID() syscall(__NR_gettid)
-#else
-#define GETTID() getpid()
-#endif
 static char dateformat[256] = "%b %e %T";		/* Original Asterisk Format */
 
 static char queue_log_name[256] = QUEUELOG;
@@ -128,7 +119,7 @@
 	enum logmsgtypes type;
 	int level;
 	int line;
-	long process_id;
+	int lwp;
 	AST_DECLARE_STRING_FIELDS(
 		AST_STRING_FIELD(date);
 		AST_STRING_FIELD(file);
@@ -882,8 +873,8 @@
 		return;
 	}
 
-	snprintf(buf, sizeof(buf), "%s[%ld]: %s:%d in %s: %s",
-		 levels[msg->level], msg->process_id, msg->file, msg->line, msg->function, msg->message);
+	snprintf(buf, sizeof(buf), "%s[%d]: %s:%d in %s: %s",
+		 levels[msg->level], msg->lwp, msg->file, msg->line, msg->function, msg->message);
 
 	term_strip(buf, buf, strlen(buf) + 1);
 	syslog(syslog_level, "%s", buf);
@@ -928,10 +919,10 @@
 				/* Turn the numerical line number into a string */
 				snprintf(linestr, sizeof(linestr), "%d", logmsg->line);
 				/* Build string to print out */
-				snprintf(buf, sizeof(buf), "[%s] %s[%ld]: %s:%s %s: %s",
+				snprintf(buf, sizeof(buf), "[%s] %s[%d]: %s:%s %s: %s",
 					 logmsg->date,
 					 term_color(tmp1, logmsg->level_name, colors[logmsg->level], 0, sizeof(tmp1)),
-					 logmsg->process_id,
+					 logmsg->lwp,
 					 term_color(tmp2, logmsg->file, COLOR_BRWHITE, 0, sizeof(tmp2)),
 					 term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
 					 term_color(tmp4, logmsg->function, COLOR_BRWHITE, 0, sizeof(tmp4)),
@@ -948,8 +939,8 @@
 				}
 
 				/* Print out to the file */
-				res = fprintf(chan->fileptr, "[%s] %s[%ld] %s: %s",
-					      logmsg->date, logmsg->level_name, logmsg->process_id, logmsg->file, term_strip(buf, logmsg->message, BUFSIZ));
+				res = fprintf(chan->fileptr, "[%s] %s[%d] %s: %s",
+					      logmsg->date, logmsg->level_name, logmsg->lwp, logmsg->file, term_strip(buf, logmsg->message, BUFSIZ));
 				if (res <= 0 && !ast_strlen_zero(logmsg->message)) {
 					fprintf(stderr, "**** Asterisk Logging Error: ***********\n");
 					if (errno == ENOMEM || errno == ENOSPC)
@@ -1173,7 +1164,7 @@
 	ast_string_field_set(logmsg, level_name, levels[level]);
 	ast_string_field_set(logmsg, file, file);
 	ast_string_field_set(logmsg, function, function);
-	logmsg->process_id = (long) GETTID();
+	logmsg->lwp = ast_get_tid();
 
 	/* If the logger thread is active, append it to the tail end of the list - otherwise skip that step */
 	if (logthread != AST_PTHREADT_NULL) {

Modified: trunk/main/utils.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/utils.c?view=diff&rev=298137&r1=298136&r2=298137
==============================================================================
--- trunk/main/utils.c (original)
+++ trunk/main/utils.c Sat Dec 11 21:58:33 2010
@@ -32,6 +32,13 @@
 
 #ifdef HAVE_DEV_URANDOM
 #include <fcntl.h>
+#endif
+
+#include <sys/syscall.h>
+#if defined(__APPLE__)
+#include <mach/mach.h>
+#elif defined(HAVE_SYS_THR_H)
+#include <sys/thr.h>
 #endif
 
 #include "asterisk/network.h"
@@ -2081,3 +2088,21 @@
 	return res;
 }
 #endif
+
+int ast_get_tid(void)
+{
+	int ret = -1;
+#if defined (__linux) && defined(SYS_gettid)
+	ret = syscall(SYS_gettid); /* available since Linux 1.4.11 */
+#elif defined(__sun)
+	ret = pthread_self();
+#elif defined(__APPLE__)
+	ret = mach_thread_self();
+	mach_port_deallocate(mach_task_self(), ret);
+#elif defined(__FreeBSD__) && defined(HAVE_SYS_THR_H)
+	long lwpid;
+	thr_self(&lwpid); /* available since sys/thr.h creation 2003 */
+	ret = lwpid;
+#endif
+	return ret;
+}




More information about the asterisk-commits mailing list