--- include/asterisk/lock.h	2007-10-02 01:07:06.000000000 +0400
+++ include/asterisk/lock.h	2007-10-09 04:17:42.000000000 +0400
@@ -113,11 +113,13 @@
 	pthread_mutex_t mutex;
 	/*! Track which thread holds this lock */
 	unsigned int track:1;
+	/* Debug reenrancy*/
 	const char *file[AST_MAX_REENTRANCY];
 	int lineno[AST_MAX_REENTRANCY];
 	int reentrancy;
 	const char *func[AST_MAX_REENTRANCY];
 	pthread_t thread[AST_MAX_REENTRANCY];
+	pthread_mutex_t reentr_mutex;
 };
 
 typedef struct ast_mutex_info ast_mutex_t;
@@ -161,6 +163,38 @@
 	memset(&empty_mutex, 0, sizeof(empty_mutex));
 }
 
+static inline void reentrancy_lock_cs(ast_mutex_t *p_ast_mutex)
+{
+	pthread_mutex_lock(&p_ast_mutex->reentr_mutex);
+}
+
+static inline void reentrancy_unlock_cs(ast_mutex_t *p_ast_mutex)
+{
+	pthread_mutex_unlock(&p_ast_mutex->reentr_mutex);
+}
+
+static inline void init_reentrancy_cs(ast_mutex_t *p_ast_mutex)
+{
+	int i;
+	static pthread_mutexattr_t  reentr_attr;
+	for (i=0; i<AST_MAX_REENTRANCY; i++)
+	{
+		p_ast_mutex->file[i] = NULL;
+		p_ast_mutex->lineno[i] = 0;
+		p_ast_mutex->func[i] = NULL;
+		p_ast_mutex->thread[i] = 0;
+	};
+	p_ast_mutex->reentrancy = 0;
+	pthread_mutexattr_init(&reentr_attr);
+	pthread_mutexattr_settype(&reentr_attr, AST_MUTEX_KIND);
+	pthread_mutex_init(&p_ast_mutex->reentr_mutex, &reentr_attr);
+}
+
+static inline void delete_reentrancy_cs(ast_mutex_t * p_ast_mutex)
+{
+	pthread_mutex_destroy(&p_ast_mutex->reentr_mutex);
+}
+
 static inline int __ast_pthread_mutex_init_attr(int track, const char *filename, int lineno, const char *func,
 						const char *mutex_name, ast_mutex_t *t,
 						pthread_mutexattr_t *attr) 
@@ -180,11 +214,11 @@
 	}
 #endif
 
+	init_reentrancy_cs(t);
 	t->file[0] = filename;
 	t->lineno[0] = lineno;
 	t->func[0] = func;
 	t->thread[0]  = 0;
-	t->reentrancy = 0;
 	t->track = track;
 
 	return pthread_mutex_init(&t->mutex, attr);
@@ -229,8 +263,10 @@
 	case EBUSY:
 		__ast_mutex_logger("%s line %d (%s): Error: attempt to destroy locked mutex '%s'.\n",
 				   filename, lineno, func, mutex_name);
+		reentrancy_lock_cs(t);
 		__ast_mutex_logger("%s line %d (%s): Error: '%s' was locked here.\n",
-				   t->file[t->reentrancy-1], t->lineno[t->reentrancy-1], t->func[t->reentrancy-1], mutex_name);
+			    t->file[t->reentrancy-1], t->lineno[t->reentrancy-1], t->func[t->reentrancy-1], mutex_name);
+		reentrancy_unlock_cs(t);
 		break;
 	}
 
@@ -241,9 +277,14 @@
 	else
 		t->mutex = PTHREAD_MUTEX_INIT_VALUE;
 #endif
+	reentrancy_lock_cs(t);
 	t->file[0] = filename;
 	t->lineno[0] = lineno;
 	t->func[0] = func;
+	t->reentrancy=0;
+	t->thread[0]  = 0;
+	reentrancy_unlock_cs(t);
+	delete_reentrancy_cs(t);
 
 	return res;
 }
@@ -282,9 +323,11 @@
 				if ((current - seconds) && (!((current - seconds) % 5))) {
 					__ast_mutex_logger("%s line %d (%s): Deadlock? waited %d sec for mutex '%s'?\n",
 							   filename, lineno, func, (int)(current - seconds), mutex_name);
+					reentrancy_lock_cs(t);
 					__ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
 							   t->file[t->reentrancy-1], t->lineno[t->reentrancy-1],
 							   t->func[t->reentrancy-1], mutex_name);
+					reentrancy_unlock_cs(t);
 				}
 				usleep(200);
 			}
@@ -301,8 +344,7 @@
 #endif /* DETECT_DEADLOCKS */
 
 	if (!res) {
-		if (t->track)
-			ast_mark_lock_acquired();
+		reentrancy_lock_cs(t);
 		if (t->reentrancy < AST_MAX_REENTRANCY) {
 			t->file[t->reentrancy] = filename;
 			t->lineno[t->reentrancy] = lineno;
@@ -313,6 +355,9 @@
 			__ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
 							   filename, lineno, func, mutex_name);
 		}
+		reentrancy_unlock_cs(t);
+		if (t->track)
+			ast_mark_lock_acquired();
 	} else {
 		if (t->track)
 			ast_remove_lock_info(&t->mutex);
@@ -342,8 +387,7 @@
 		ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, &t->mutex);
 
 	if (!(res = pthread_mutex_trylock(&t->mutex))) {
-		if (t->track)
-			ast_mark_lock_acquired();
+		reentrancy_lock_cs(t);
 		if (t->reentrancy < AST_MAX_REENTRANCY) {
 			t->file[t->reentrancy] = filename;
 			t->lineno[t->reentrancy] = lineno;
@@ -354,6 +398,9 @@
 			__ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
 					   filename, lineno, func, mutex_name);
 		}
+		reentrancy_unlock_cs(t);
+		if (t->track)
+			ast_mark_lock_acquired();
 	} else if (t->track) {
 			ast_remove_lock_info(&t->mutex);
 	}
@@ -374,11 +421,13 @@
 	}
 #endif
 
+	reentrancy_lock_cs(t);
 	if (t->reentrancy && (t->thread[t->reentrancy-1] != pthread_self())) {
 		__ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
 				   filename, lineno, func, mutex_name);
 		__ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
 				   t->file[t->reentrancy-1], t->lineno[t->reentrancy-1], t->func[t->reentrancy-1], mutex_name);
+		reentrancy_unlock_cs(t);
 		DO_THREAD_CRASH;
 	}
 
@@ -394,6 +443,7 @@
 		t->func[t->reentrancy] = NULL;
 		t->thread[t->reentrancy] = 0;
 	}
+	reentrancy_unlock_cs(t);
 
 	if (t->track)
 		ast_remove_lock_info(&t->mutex);
@@ -445,11 +495,13 @@
 	}
 #endif
 
+	reentrancy_lock_cs(t);
 	if (t->reentrancy && (t->thread[t->reentrancy-1] != pthread_self())) {
 		__ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
 				   filename, lineno, func, mutex_name);
 		__ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
 				   t->file[t->reentrancy-1], t->lineno[t->reentrancy-1], t->func[t->reentrancy-1], mutex_name);
+		reentrancy_lock_cs(t);
 		DO_THREAD_CRASH;
 	}
 
@@ -465,6 +517,7 @@
 		t->func[t->reentrancy] = NULL;
 		t->thread[t->reentrancy] = 0;
 	}
+	reentrancy_unlock_cs(t);
 
 	if (t->track)
 		ast_remove_lock_info(&t->mutex);
@@ -474,9 +527,7 @@
 				   filename, lineno, func, strerror(res));
 		DO_THREAD_CRASH;
 	} else {
-		if (t->track)
-			ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, &t->mutex);
-
+		reentrancy_lock_cs(t);
 		if (t->reentrancy < AST_MAX_REENTRANCY) {
 			t->file[t->reentrancy] = filename;
 			t->lineno[t->reentrancy] = lineno;
@@ -487,6 +538,10 @@
 			__ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
 							   filename, lineno, func, mutex_name);
 		}
+		reentrancy_unlock_cs(t);
+
+		if (t->track)
+			ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, &t->mutex);
 	}
 
 	return res;
@@ -506,6 +561,7 @@
 	}
 #endif
 
+	reentrancy_lock_cs(t);
 	if (t->reentrancy && (t->thread[t->reentrancy-1] != pthread_self())) {
 		__ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
 				   filename, lineno, func, mutex_name);
@@ -526,6 +582,7 @@
 		t->func[t->reentrancy] = NULL;
 		t->thread[t->reentrancy] = 0;
 	}
+	reentrancy_unlock_cs(t);
 
 	if (t->track)
 		ast_remove_lock_info(&t->mutex);
@@ -535,9 +592,7 @@
 				   filename, lineno, func, strerror(res));
 		DO_THREAD_CRASH;
 	} else {
-		if (t->track)
-			ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, &t->mutex);
-
+		reentrancy_lock_cs(t);
 		if (t->reentrancy < AST_MAX_REENTRANCY) {
 			t->file[t->reentrancy] = filename;
 			t->lineno[t->reentrancy] = lineno;
@@ -548,6 +603,10 @@
 			__ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
 							   filename, lineno, func, mutex_name);
 		}
+		reentrancy_unlock_cs(t);
+
+		if (t->track)
+			ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, &t->mutex);
 	}
 
 	return res;
