[asterisk-commits] mmichelson: branch mmichelson/lock_backtraces r115311 - in /team/mmichelson/l...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon May 5 15:02:11 CDT 2008


Author: mmichelson
Date: Mon May  5 15:02:11 2008
New Revision: 115311

URL: http://svn.digium.com/view/asterisk?view=rev&rev=115311
Log:
Big change to lock.h

1. There are now two clear divisions to the file. One section that defines
a set of macros/functions if DEBUG_THREADS is defined, and one that defines the same
set if DEBUG_THREADS is not defined. This change paved the way for...

2. Handling the storage of backtraces for read-write locks was tricky since the
ast_rwlock_t was just a typedef of pthread_rwlock_t. Now, if DEBUG_THREADS is
defined, ast_rwlock_t is a structure that contains a lock and a backtrace. This
allows for easy removal of the backtrace associated with the lock when it is unlocked
(although I admit now that I have not done that yet. Next commit!!)

3. Because of the two above changes, there is no dynamic allocation of ast_bt's 
any more, so I have removed the ast_bt_destroy calls from utils.c.


Modified:
    team/mmichelson/lock_backtraces/include/asterisk/lock.h
    team/mmichelson/lock_backtraces/main/utils.c

Modified: team/mmichelson/lock_backtraces/include/asterisk/lock.h
URL: http://svn.digium.com/view/asterisk/team/mmichelson/lock_backtraces/include/asterisk/lock.h?view=diff&rev=115311&r1=115310&r2=115311
==============================================================================
--- team/mmichelson/lock_backtraces/include/asterisk/lock.h (original)
+++ team/mmichelson/lock_backtraces/include/asterisk/lock.h Mon May  5 15:02:11 2008
@@ -760,151 +760,12 @@
 #define ast_cond_wait(cond, mutex)		__ast_cond_wait(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #mutex, cond, mutex)
 #define ast_cond_timedwait(cond, mutex, time)	__ast_cond_timedwait(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #mutex, cond, mutex, time)
 
-#else /* !DEBUG_THREADS */
-
-
-typedef pthread_mutex_t ast_mutex_t;
-
-#define AST_MUTEX_INIT_VALUE			((ast_mutex_t) PTHREAD_MUTEX_INIT_VALUE)
-#define AST_MUTEX_INIT_VALUE_NOTRACKING		((ast_mutex_t) PTHREAD_MUTEX_INIT_VALUE)
-
-#define ast_mutex_init_notracking(m)		ast_mutex_init(m)
-
-static inline int ast_mutex_init(ast_mutex_t *pmutex)
-{
-	int res;
-	pthread_mutexattr_t attr;
-
-	pthread_mutexattr_init(&attr);
-	pthread_mutexattr_settype(&attr, AST_MUTEX_KIND);
-
-	res = pthread_mutex_init(pmutex, &attr);
-	pthread_mutexattr_destroy(&attr);
-	return res;
-}
-
-#define ast_pthread_mutex_init(pmutex,a) pthread_mutex_init(pmutex,a)
-
-static inline int ast_mutex_unlock(ast_mutex_t *pmutex)
-{
-	return pthread_mutex_unlock(pmutex);
-}
-
-static inline int ast_mutex_destroy(ast_mutex_t *pmutex)
-{
-	return pthread_mutex_destroy(pmutex);
-}
-
-static inline int ast_mutex_lock(ast_mutex_t *pmutex)
-{
-	__MTX_PROF(pmutex);
-}
-
-static inline int ast_mutex_trylock(ast_mutex_t *pmutex)
-{
-	return pthread_mutex_trylock(pmutex);
-}
-
-typedef pthread_cond_t ast_cond_t;
-
-static inline int ast_cond_init(ast_cond_t *cond, pthread_condattr_t *cond_attr)
-{
-	return pthread_cond_init(cond, cond_attr);
-}
-
-static inline int ast_cond_signal(ast_cond_t *cond)
-{
-	return pthread_cond_signal(cond);
-}
-
-static inline int ast_cond_broadcast(ast_cond_t *cond)
-{
-	return pthread_cond_broadcast(cond);
-}
-
-static inline int ast_cond_destroy(ast_cond_t *cond)
-{
-	return pthread_cond_destroy(cond);
-}
-
-static inline int ast_cond_wait(ast_cond_t *cond, ast_mutex_t *t)
-{
-	return pthread_cond_wait(cond, t);
-}
-
-static inline int ast_cond_timedwait(ast_cond_t *cond, ast_mutex_t *t, const struct timespec *abstime)
-{
-	return pthread_cond_timedwait(cond, t, abstime);
-}
-
-#endif /* !DEBUG_THREADS */
-
-#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
-/*
- * If AST_MUTEX_INIT_W_CONSTRUCTORS is defined, use file scope constructors
- * and destructors to create/destroy global mutexes.
- */
-#define __AST_MUTEX_DEFINE(scope, mutex, init_val, track)	\
-	scope ast_mutex_t mutex = init_val;			\
-static void  __attribute__ ((constructor)) init_##mutex(void)	\
-{								\
-	if (track)						\
-		ast_mutex_init(&mutex);				\
-	else							\
-		ast_mutex_init_notracking(&mutex);		\
-}								\
-								\
-static void  __attribute__ ((destructor)) fini_##mutex(void)	\
-{								\
-	ast_mutex_destroy(&mutex);				\
-}
-#else /* !AST_MUTEX_INIT_W_CONSTRUCTORS */
-/* By default, use static initialization of mutexes. */ 
-#define __AST_MUTEX_DEFINE(scope, mutex, init_val, track)	scope ast_mutex_t mutex = init_val
-#endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
-
-#ifndef __CYGWIN__	/* temporary disabled for cygwin */
-#define pthread_mutex_t		use_ast_mutex_t_instead_of_pthread_mutex_t
-#define pthread_cond_t		use_ast_cond_t_instead_of_pthread_cond_t
-#endif
-#define pthread_mutex_lock	use_ast_mutex_lock_instead_of_pthread_mutex_lock
-#define pthread_mutex_unlock	use_ast_mutex_unlock_instead_of_pthread_mutex_unlock
-#define pthread_mutex_trylock	use_ast_mutex_trylock_instead_of_pthread_mutex_trylock
-#define pthread_mutex_init	use_ast_mutex_init_instead_of_pthread_mutex_init
-#define pthread_mutex_destroy	use_ast_mutex_destroy_instead_of_pthread_mutex_destroy
-#define pthread_cond_init	use_ast_cond_init_instead_of_pthread_cond_init
-#define pthread_cond_destroy	use_ast_cond_destroy_instead_of_pthread_cond_destroy
-#define pthread_cond_signal	use_ast_cond_signal_instead_of_pthread_cond_signal
-#define pthread_cond_broadcast	use_ast_cond_broadcast_instead_of_pthread_cond_broadcast
-#define pthread_cond_wait	use_ast_cond_wait_instead_of_pthread_cond_wait
-#define pthread_cond_timedwait	use_ast_cond_timedwait_instead_of_pthread_cond_timedwait
-
-#define AST_MUTEX_DEFINE_STATIC(mutex)			__AST_MUTEX_DEFINE(static, mutex, AST_MUTEX_INIT_VALUE, 1)
-#define AST_MUTEX_DEFINE_STATIC_NOTRACKING(mutex)	__AST_MUTEX_DEFINE(static, mutex, AST_MUTEX_INIT_VALUE_NOTRACKING, 0)
-
-#define AST_MUTEX_INITIALIZER __use_AST_MUTEX_DEFINE_STATIC_rather_than_AST_MUTEX_INITIALIZER__
-
-#define gethostbyname __gethostbyname__is__not__reentrant__use__ast_gethostbyname__instead__
-
-#ifndef __linux__
-#define pthread_create __use_ast_pthread_create_instead__
-#endif
-
-/*
- * Same as above, definitions of ast_rwlock_t for the various cases:
- * simple wrappers for the pthread equivalent in the non-debug case,
- * more sophisticated tracking in the debug case.
- */
-
-typedef pthread_rwlock_t ast_rwlock_t;
-
-#ifdef HAVE_PTHREAD_RWLOCK_INITIALIZER
-#define AST_RWLOCK_INIT_VALUE PTHREAD_RWLOCK_INITIALIZER
-#else
-#define AST_RWLOCK_INIT_VALUE { 0 }
-#endif
-
-#ifdef DEBUG_THREADS
+struct ast_rwlock_info {
+	pthread_rwlock_t lock;
+	struct ast_bt backtrace;
+};
+
+typedef struct ast_rwlock_info ast_rwlock_t;
 
 #define ast_rwlock_init(rwlock)		__ast_rwlock_init(__FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
 #define ast_rwlock_destroy(rwlock)	__ast_rwlock_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
@@ -914,6 +775,11 @@
 #define ast_rwlock_tryrdlock(a)		_ast_rwlock_tryrdlock(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
 #define ast_rwlock_trywrlock(a) _ast_rwlock_trywrlock(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
 
+#ifdef HAVE_PTHREAD_RWLOCK_INITIALIZER
+#define AST_RWLOCK_INIT_VALUE { PTHREAD_RWLOCK_INITIALIZER, {{0,},} }
+#else
+#define AST_RWLOCK_INIT_VALUE { 0 , {0,},}}
+#endif
 
 static inline int __ast_rwlock_init(const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *prwlock)
 {
@@ -934,7 +800,7 @@
 	pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NP);
 #endif
 
-	res = pthread_rwlock_init(prwlock, &attr);
+	res = pthread_rwlock_init(&prwlock->lock, &attr);
 	pthread_rwlockattr_destroy(&attr);
 	return res;
 }
@@ -953,7 +819,7 @@
 	}
 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
 	
-	if ((res = pthread_rwlock_destroy(prwlock)))
+	if ((res = pthread_rwlock_destroy(&prwlock->lock)))
 		__ast_mutex_logger("%s line %d (%s): Error destroying rwlock %s: %s\n",
 				filename, lineno, func, rwlock_name, strerror(res));
 
@@ -980,7 +846,7 @@
 	}
 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
 	
-	res = pthread_rwlock_unlock(lock);
+	res = pthread_rwlock_unlock(&lock->lock);
 	ast_remove_lock_info(lock, NULL);
 	return res;
 }
@@ -990,7 +856,6 @@
 	const char *file, int line, const char *func)
 {
 	int res;
-	struct ast_bt *bt = ast_bt_create();
 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
 	int canlog = strcmp(file, "logger.c");
 	
@@ -1008,8 +873,8 @@
 	}
 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
 	
-	ast_store_lock_info(AST_RDLOCK, file, line, func, name, lock, bt);
-	res = pthread_rwlock_rdlock(lock);
+	ast_store_lock_info(AST_RDLOCK, file, line, func, name, lock, &lock->backtrace);
+	res = pthread_rwlock_rdlock(&lock->lock);
 	if (!res)
 		ast_mark_lock_acquired(lock);
 	else
@@ -1022,7 +887,6 @@
 	const char *file, int line, const char *func)
 {
 	int res;
-	struct ast_bt *bt = ast_bt_create();
 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
 	int canlog = strcmp(file, "logger.c");
 	
@@ -1040,8 +904,8 @@
 	}
 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
 
-	ast_store_lock_info(AST_WRLOCK, file, line, func, name, lock, bt);
-	res = pthread_rwlock_wrlock(lock);
+	ast_store_lock_info(AST_WRLOCK, file, line, func, name, lock, &lock->backtrace);
+	res = pthread_rwlock_wrlock(&lock->lock);
 	if (!res)
 		ast_mark_lock_acquired(lock);
 	else
@@ -1054,7 +918,6 @@
 	const char *file, int line, const char *func)
 {
 	int res;
-	struct ast_bt *bt = ast_bt_create();
 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
 	int canlog = strcmp(file, "logger.c");
 	
@@ -1072,8 +935,8 @@
 	}
 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
 
-	ast_store_lock_info(AST_RDLOCK, file, line, func, name, lock, bt);
-	res = pthread_rwlock_tryrdlock(lock);
+	ast_store_lock_info(AST_RDLOCK, file, line, func, name, lock, &lock->backtrace);
+	res = pthread_rwlock_tryrdlock(&lock->lock);
 	if (!res)
 		ast_mark_lock_acquired(lock);
 	else
@@ -1086,7 +949,6 @@
 	const char *file, int line, const char *func)
 {
 	int res;
-	struct ast_bt *bt = ast_bt_create();
 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
 	int canlog = strcmp(file, "logger.c");
 	
@@ -1104,8 +966,8 @@
 	}
 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
 
-	ast_store_lock_info(AST_WRLOCK, file, line, func, name, lock, bt);
-	res = pthread_rwlock_trywrlock(lock);
+	ast_store_lock_info(AST_WRLOCK, file, line, func, name, lock, &lock->backtrace);
+	res = pthread_rwlock_trywrlock(&lock->lock);
 	if (!res)
 		ast_mark_lock_acquired(lock);
 	else
@@ -1115,6 +977,89 @@
 
 #else /* !DEBUG_THREADS */
 
+typedef pthread_mutex_t ast_mutex_t;
+
+#define AST_MUTEX_INIT_VALUE			((ast_mutex_t) PTHREAD_MUTEX_INIT_VALUE)
+#define AST_MUTEX_INIT_VALUE_NOTRACKING		((ast_mutex_t) PTHREAD_MUTEX_INIT_VALUE)
+
+#define ast_mutex_init_notracking(m)		ast_mutex_init(m)
+
+static inline int ast_mutex_init(ast_mutex_t *pmutex)
+{
+	int res;
+	pthread_mutexattr_t attr;
+
+	pthread_mutexattr_init(&attr);
+	pthread_mutexattr_settype(&attr, AST_MUTEX_KIND);
+
+	res = pthread_mutex_init(pmutex, &attr);
+	pthread_mutexattr_destroy(&attr);
+	return res;
+}
+
+#define ast_pthread_mutex_init(pmutex,a) pthread_mutex_init(pmutex,a)
+
+static inline int ast_mutex_unlock(ast_mutex_t *pmutex)
+{
+	return pthread_mutex_unlock(pmutex);
+}
+
+static inline int ast_mutex_destroy(ast_mutex_t *pmutex)
+{
+	return pthread_mutex_destroy(pmutex);
+}
+
+static inline int ast_mutex_lock(ast_mutex_t *pmutex)
+{
+	__MTX_PROF(pmutex);
+}
+
+static inline int ast_mutex_trylock(ast_mutex_t *pmutex)
+{
+	return pthread_mutex_trylock(pmutex);
+}
+
+typedef pthread_cond_t ast_cond_t;
+
+static inline int ast_cond_init(ast_cond_t *cond, pthread_condattr_t *cond_attr)
+{
+	return pthread_cond_init(cond, cond_attr);
+}
+
+static inline int ast_cond_signal(ast_cond_t *cond)
+{
+	return pthread_cond_signal(cond);
+}
+
+static inline int ast_cond_broadcast(ast_cond_t *cond)
+{
+	return pthread_cond_broadcast(cond);
+}
+
+static inline int ast_cond_destroy(ast_cond_t *cond)
+{
+	return pthread_cond_destroy(cond);
+}
+
+static inline int ast_cond_wait(ast_cond_t *cond, ast_mutex_t *t)
+{
+	return pthread_cond_wait(cond, t);
+}
+
+static inline int ast_cond_timedwait(ast_cond_t *cond, ast_mutex_t *t, const struct timespec *abstime)
+{
+	return pthread_cond_timedwait(cond, t, abstime);
+}
+
+
+typedef pthread_rwlock_t ast_rwlock_t;
+
+#ifdef HAVE_PTHREAD_RWLOCK_INITIALIZER
+#define AST_RWLOCK_INIT_VALUE PTHREAD_RWLOCK_INITIALIZER
+#else
+#define AST_RWLOCK_INIT_VALUE { 0 }
+#endif
+
 static inline int ast_rwlock_init(ast_rwlock_t *prwlock)
 {
 	int res;
@@ -1160,7 +1105,65 @@
 {
 	return pthread_rwlock_trywrlock(prwlock);
 }
+
 #endif /* !DEBUG_THREADS */
+
+#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
+/*
+ * If AST_MUTEX_INIT_W_CONSTRUCTORS is defined, use file scope constructors
+ * and destructors to create/destroy global mutexes.
+ */
+#define __AST_MUTEX_DEFINE(scope, mutex, init_val, track)	\
+	scope ast_mutex_t mutex = init_val;			\
+static void  __attribute__ ((constructor)) init_##mutex(void)	\
+{								\
+	if (track)						\
+		ast_mutex_init(&mutex);				\
+	else							\
+		ast_mutex_init_notracking(&mutex);		\
+}								\
+								\
+static void  __attribute__ ((destructor)) fini_##mutex(void)	\
+{								\
+	ast_mutex_destroy(&mutex);				\
+}
+#else /* !AST_MUTEX_INIT_W_CONSTRUCTORS */
+/* By default, use static initialization of mutexes. */ 
+#define __AST_MUTEX_DEFINE(scope, mutex, init_val, track)	scope ast_mutex_t mutex = init_val
+#endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
+
+#ifndef __CYGWIN__	/* temporary disabled for cygwin */
+#define pthread_mutex_t		use_ast_mutex_t_instead_of_pthread_mutex_t
+#define pthread_cond_t		use_ast_cond_t_instead_of_pthread_cond_t
+#endif
+#define pthread_mutex_lock	use_ast_mutex_lock_instead_of_pthread_mutex_lock
+#define pthread_mutex_unlock	use_ast_mutex_unlock_instead_of_pthread_mutex_unlock
+#define pthread_mutex_trylock	use_ast_mutex_trylock_instead_of_pthread_mutex_trylock
+#define pthread_mutex_init	use_ast_mutex_init_instead_of_pthread_mutex_init
+#define pthread_mutex_destroy	use_ast_mutex_destroy_instead_of_pthread_mutex_destroy
+#define pthread_cond_init	use_ast_cond_init_instead_of_pthread_cond_init
+#define pthread_cond_destroy	use_ast_cond_destroy_instead_of_pthread_cond_destroy
+#define pthread_cond_signal	use_ast_cond_signal_instead_of_pthread_cond_signal
+#define pthread_cond_broadcast	use_ast_cond_broadcast_instead_of_pthread_cond_broadcast
+#define pthread_cond_wait	use_ast_cond_wait_instead_of_pthread_cond_wait
+#define pthread_cond_timedwait	use_ast_cond_timedwait_instead_of_pthread_cond_timedwait
+
+#define AST_MUTEX_DEFINE_STATIC(mutex)			__AST_MUTEX_DEFINE(static, mutex, AST_MUTEX_INIT_VALUE, 1)
+#define AST_MUTEX_DEFINE_STATIC_NOTRACKING(mutex)	__AST_MUTEX_DEFINE(static, mutex, AST_MUTEX_INIT_VALUE_NOTRACKING, 0)
+
+#define AST_MUTEX_INITIALIZER __use_AST_MUTEX_DEFINE_STATIC_rather_than_AST_MUTEX_INITIALIZER__
+
+#define gethostbyname __gethostbyname__is__not__reentrant__use__ast_gethostbyname__instead__
+
+#ifndef __linux__
+#define pthread_create __use_ast_pthread_create_instead__
+#endif
+
+/*
+ * Same as above, definitions of ast_rwlock_t for the various cases:
+ * simple wrappers for the pthread equivalent in the non-debug case,
+ * more sophisticated tracking in the debug case.
+ */
 
 /* Statically declared read/write locks */
 

Modified: team/mmichelson/lock_backtraces/main/utils.c
URL: http://svn.digium.com/view/asterisk/team/mmichelson/lock_backtraces/main/utils.c?view=diff&rev=115311&r1=115310&r2=115311
==============================================================================
--- team/mmichelson/lock_backtraces/main/utils.c (original)
+++ team/mmichelson/lock_backtraces/main/utils.c Mon May  5 15:02:11 2008
@@ -688,13 +688,10 @@
 
 	if (lock_info->locks[i].times_locked > 1) {
 		lock_info->locks[i].times_locked--;
-		ast_bt_destroy(lock_info->locks[i].backtrace);
 		lock_info->locks[i].backtrace = bt;
 		pthread_mutex_unlock(&lock_info->lock);
 		return;
 	}
-
-	ast_bt_destroy(lock_info->locks[i].backtrace);
 
 	if (i < lock_info->num_locks - 1) {
 		/* Not the last one ... *should* be rare! */




More information about the asterisk-commits mailing list