[asterisk-commits] mjordan: branch mjordan/trunk-deadlock r376228 - /team/mjordan/trunk-deadlock...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Nov 13 14:54:09 CST 2012


Author: mjordan
Date: Tue Nov 13 14:54:06 2012
New Revision: 376228

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=376228
Log:
Kill reentrant mutex with fire

Modified:
    team/mjordan/trunk-deadlock/main/lock.c

Modified: team/mjordan/trunk-deadlock/main/lock.c
URL: http://svnview.digium.com/svn/asterisk/team/mjordan/trunk-deadlock/main/lock.c?view=diff&rev=376228&r1=376227&r2=376228
==============================================================================
--- team/mjordan/trunk-deadlock/main/lock.c (original)
+++ team/mjordan/trunk-deadlock/main/lock.c Tue Nov 13 14:54:06 2012
@@ -720,11 +720,41 @@
 	return res;
 }
 
+static void rwlock_track_init(struct ast_lock_track **plt)
+{
+#ifdef DEBUG_THREADS
+	struct ast_lock_track *lt = *plt;
+
+	pthread_mutex_lock(&reentrancy_lock);
+	if (!plt) {
+		lt = *plt = (struct ast_lock_track *) calloc(1, sizeof(*lt));
+	} else {
+		pthread_mutex_unlock(&reentrancy_lock);
+		goto bail;
+	}
+	pthread_mutex_unlock(&reentrancy_lock);
+
+	lt->file[0] = NULL;
+	lt->lineno[0] = 0;
+	lt->func[0] = NULL;
+	lt->thread[0] = 0;
+#ifdef HAVE_BKTR
+	memset(&lt->backtrace[0], 0, sizeof(lt->backtrace[0]));
+#endif
+	lt->reentrancy = 0;
+
+	pthread_mutex_init(&lt->reentr_mutex, NULL);
+bail:
+	return;
+#endif
+}
+
 int __ast_rwlock_destroy(const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t)
 {
 	int res;
 
 #ifdef DEBUG_THREADS
+	int pres;
 	struct ast_lock_track *lt = t->track;
 	int canlog = strcmp(filename, "logger.c") & t->tracking;
 
@@ -746,7 +776,10 @@
 				filename, lineno, func, rwlock_name, strerror(res));
 	}
 	if (t->tracking) {
-		ast_reentrancy_lock(lt);
+		if ((pres = pthread_mutex_lock(&lt->reentr_mutex))) {
+			fprintf(stderr, "Error locking mutex: %s [%d]", strerror(pres), pres);
+			abort();
+		}
 		lt->file[0] = filename;
 		lt->lineno[0] = lineno;
 		lt->func[0] = func;
@@ -755,7 +788,10 @@
 #ifdef HAVE_BKTR
 		memset(&lt->backtrace[0], 0, sizeof(lt->backtrace[0]));
 #endif
-		ast_reentrancy_unlock(lt);
+		if ((pres = pthread_mutex_unlock(&lt->reentr_mutex))) {
+			fprintf(stderr, "Error locking mutex: %s [%d]", strerror(pres), pres);
+			abort();
+		}
 		delete_reentrancy_cs(&t->track);
 	}
 #endif /* DEBUG_THREADS */
@@ -768,6 +804,7 @@
 	int res;
 
 #ifdef DEBUG_THREADS
+	int pres;
 	struct ast_lock_track *lt;
 	int canlog = strcmp(filename, "logger.c") & t->tracking;
 #ifdef HAVE_BKTR
@@ -790,12 +827,15 @@
 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
 
 	if (t->tracking && !t->track) {
-		ast_reentrancy_init(&t->track);
+		rwlock_track_init(&t->track);
 	}
 	lt = t->track;
 
 	if (t->tracking) {
-		ast_reentrancy_lock(lt);
+		if ((pres = pthread_mutex_lock(&lt->reentr_mutex))) {
+			fprintf(stderr, "Error locking mutex: %s [%d]", strerror(pres), pres);
+			abort();
+		}
 		if (lt->reentrancy) {
 			int i;
 			pthread_t self = pthread_self();
@@ -826,7 +866,10 @@
 			lt->reentrancy = 0;
 		}
 
-		ast_reentrancy_unlock(lt);
+		if ((pres = pthread_mutex_unlock(&lt->reentr_mutex))) {
+			fprintf(stderr, "Error locking mutex: %s [%d]", strerror(pres), pres);
+			abort();
+		}
 
 #ifdef HAVE_BKTR
 		ast_remove_lock_info(t, bt);
@@ -854,6 +897,7 @@
 	int res;
 
 #ifdef DEBUG_THREADS
+	int pres;
 	struct ast_lock_track *lt;
 	int canlog = strcmp(filename, "logger.c") & t->tracking;
 #ifdef HAVE_BKTR
@@ -876,18 +920,24 @@
 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
 
 	if (t->tracking && !t->track) {
-		ast_reentrancy_init(&t->track);
+		rwlock_track_init(&t->track);
 	}
 	lt = t->track;
 
 	if (t->tracking) {
 #ifdef HAVE_BKTR
-		ast_reentrancy_lock(lt);
+		if ((pres = pthread_mutex_lock(&lt->reentr_mutex))) {
+			fprintf(stderr, "Error locking mutex: %s [%d]", strerror(pres), pres);
+			abort();
+		}
 		if (lt->reentrancy != AST_MAX_REENTRANCY) {
 			ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
 			bt = &lt->backtrace[lt->reentrancy];
 		}
-		ast_reentrancy_unlock(lt);
+		if ((pres = pthread_mutex_unlock(&lt->reentr_mutex))) {
+			fprintf(stderr, "Error locking mutex: %s [%d]", strerror(pres), pres);
+			abort();
+		}
 		ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t, bt);
 #else
 		ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t);
@@ -907,7 +957,10 @@
 					__ast_mutex_logger("%s line %d (%s): Deadlock? waited %d sec for readlock '%s'?\n",
 						filename, line, func, (int)wait_time, name);
 					if (t->tracking) {
-						ast_reentrancy_lock(lt);
+						if ((pres = pthread_mutex_lock(&lt->reentr_mutex))) {
+							fprintf(stderr, "Error locking mutex: %s [%d]", strerror(pres), pres);
+							abort();
+						}
 #ifdef HAVE_BKTR
 						__dump_backtrace(&lt->backtrace[lt->reentrancy], canlog);
 #endif
@@ -917,7 +970,10 @@
 #ifdef HAVE_BKTR
 						__dump_backtrace(&lt->backtrace[lt->reentrancy-1], canlog);
 #endif
-						ast_reentrancy_unlock(lt);
+						if ((pres = pthread_mutex_unlock(&lt->reentr_mutex))) {
+							fprintf(stderr, "Error locking mutex: %s [%d]", strerror(pres), pres);
+							abort();
+						}
 					}
 					reported_wait = wait_time;
 				}
@@ -931,7 +987,10 @@
 
 #ifdef DEBUG_THREADS
 	if (!res && t->tracking) {
-		ast_reentrancy_lock(lt);
+		if ((pres = pthread_mutex_lock(&lt->reentr_mutex))) {
+			fprintf(stderr, "Error locking mutex: %s [%d]", strerror(pres), pres);
+			abort();
+		}
 		if (lt->reentrancy < AST_MAX_REENTRANCY) {
 			lt->file[lt->reentrancy] = filename;
 			lt->lineno[lt->reentrancy] = line;
@@ -939,16 +998,25 @@
 			lt->thread[lt->reentrancy] = pthread_self();
 			lt->reentrancy++;
 		}
-		ast_reentrancy_unlock(lt);
+		if ((pres = pthread_mutex_unlock(&lt->reentr_mutex))) {
+			fprintf(stderr, "Error locking mutex: %s [%d]", strerror(pres), pres);
+			abort();
+		}
 		if (t->tracking) {
 			ast_mark_lock_acquired(t);
 		}
 	} else if (t->tracking) {
 #ifdef HAVE_BKTR
 		if (lt->reentrancy) {
-			ast_reentrancy_lock(lt);
+			if ((pres = pthread_mutex_lock(&lt->reentr_mutex))) {
+				fprintf(stderr, "Error locking mutex: %s [%d]", strerror(pres), pres);
+				abort();
+			}
 			bt = &lt->backtrace[lt->reentrancy-1];
-			ast_reentrancy_unlock(lt);
+			if ((pres = pthread_mutex_unlock(&lt->reentr_mutex))) {
+				fprintf(stderr, "Error locking mutex: %s [%d]", strerror(pres), pres);
+				abort();
+			}
 		} else {
 			bt = NULL;
 		}
@@ -973,6 +1041,7 @@
 	int res;
 
 #ifdef DEBUG_THREADS
+	int pres;
 	struct ast_lock_track *lt;
 	int canlog = strcmp(filename, "logger.c") & t->tracking;
 #ifdef HAVE_BKTR
@@ -995,14 +1064,14 @@
 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
 
 	if (t->tracking && !t->track) {
-		ast_reentrancy_init(&t->track);
+		rwlock_track_init(&t->track);
 	}
 	lt = t->track;
 
 	if (t->tracking) {
 #ifdef HAVE_BKTR
-		if (pthread_mutex_lock(&lt->reentr_mutex)) {
-			__dump_backtrace(&lt->backtrace[lt->reentrancy], 0);
+		if ((pres = pthread_mutex_lock(&lt->reentr_mutex))) {
+			fprintf(stderr, "Error locking mutex: %s [%d]", strerror(pres), pres);
 			abort();
 		}
 		lt->thread[0] = pthread_self();
@@ -1011,11 +1080,8 @@
 		lt->func[0] = func;
 		ast_bt_get_addresses(&lt->backtrace[0]);
 		bt = &lt->backtrace[0];
-		if (pthread_mutex_unlock(&lt->reentr_mutex)) {
-			fprintf(stderr, "Original thread ID %lx\n", lt->thread[0]);
-			fprintf(stderr, "Current thread ID %lx\n", pthread_self());
-			fprintf(stderr, "Asterisk (crashing) pid: %ld\n", (long)getpid());
-			__dump_backtrace(&lt->backtrace[lt->reentrancy], 0);
+		if ((pres = pthread_mutex_unlock(&lt->reentr_mutex))) {
+			fprintf(stderr, "Error locking mutex: %s [%d]", strerror(pres), pres);
 			abort();
 		}
 		ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t, bt);
@@ -1037,7 +1103,10 @@
 					__ast_mutex_logger("%s line %d (%s): Deadlock? waited %d sec for writelock '%s'?\n",
 						filename, line, func, (int)wait_time, name);
 					if (t->tracking) {
-						ast_reentrancy_lock(lt);
+						if ((pres = pthread_mutex_lock(&lt->reentr_mutex))) {
+							fprintf(stderr, "Error locking mutex: %s [%d]", strerror(pres), pres);
+							abort();
+						}
 #ifdef HAVE_BKTR
 						__dump_backtrace(&lt->backtrace[lt->reentrancy], canlog);
 #endif
@@ -1047,7 +1116,10 @@
 #ifdef HAVE_BKTR
 						__dump_backtrace(&lt->backtrace[lt->reentrancy-1], canlog);
 #endif
-						ast_reentrancy_unlock(lt);
+						if ((pres = pthread_mutex_unlock(&lt->reentr_mutex))) {
+							fprintf(stderr, "Error locking mutex: %s [%d]", strerror(pres), pres);
+							abort();
+						}
 					}
 					reported_wait = wait_time;
 				}
@@ -1061,7 +1133,10 @@
 
 #ifdef DEBUG_THREADS
 	if (!res && t->tracking) {
-		ast_reentrancy_lock(lt);
+		if ((pres = pthread_mutex_lock(&lt->reentr_mutex))) {
+			fprintf(stderr, "Error locking mutex: %s [%d]", strerror(pres), pres);
+			abort();
+		}
 		if (lt->reentrancy < AST_MAX_REENTRANCY) {
 			lt->file[lt->reentrancy] = filename;
 			lt->lineno[lt->reentrancy] = line;
@@ -1069,16 +1144,25 @@
 			lt->thread[lt->reentrancy] = pthread_self();
 			lt->reentrancy++;
 		}
-		ast_reentrancy_unlock(lt);
+		if ((pres = pthread_mutex_unlock(&lt->reentr_mutex))) {
+			fprintf(stderr, "Error locking mutex: %s [%d]", strerror(pres), pres);
+			abort();
+		}
 		if (t->tracking) {
 			ast_mark_lock_acquired(t);
 		}
 	} else if (t->tracking) {
 #ifdef HAVE_BKTR
 		if (lt->reentrancy) {
-			ast_reentrancy_lock(lt);
+			if ((pres = pthread_mutex_lock(&lt->reentr_mutex))) {
+				fprintf(stderr, "Error locking mutex: %s [%d]", strerror(pres), pres);
+				abort();
+			}
 			bt = &lt->backtrace[lt->reentrancy-1];
-			ast_reentrancy_unlock(lt);
+			if ((pres = pthread_mutex_unlock(&lt->reentr_mutex))) {
+				fprintf(stderr, "Error locking mutex: %s [%d]", strerror(pres), pres);
+				abort();
+			}
 		} else {
 			bt = NULL;
 		}
@@ -1129,7 +1213,7 @@
 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
 
 	if (t->tracking && !t->track) {
-		ast_reentrancy_init(&t->track);
+		rwlock_track_init(&t->track);
 	}
 	lt = t->track;
 
@@ -1232,7 +1316,7 @@
 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
 
 	if (t->tracking && !t->track) {
-		ast_reentrancy_init(&t->track);
+		rwlock_track_init(&t->track);
 	}
 	lt = t->track;
 
@@ -1338,7 +1422,7 @@
 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
 
 	if (t->tracking && !t->track) {
-		ast_reentrancy_init(&t->track);
+		rwlock_track_init(&t->track);
 	}
 	lt = t->track;
 
@@ -1408,7 +1492,7 @@
 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
 
 	if (t->tracking && !t->track) {
-		ast_reentrancy_init(&t->track);
+		rwlock_track_init(&t->track);
 	}
 	lt = t->track;
 




More information about the asterisk-commits mailing list