Index: pjproject/trunk/pjsip/src/pjsip/sip_dialog.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsip/sip_dialog.c	(revision 635)
+++ pjproject/trunk/pjsip/src/pjsip/sip_dialog.c	(revision 671)
@@ -81,5 +81,5 @@
     pj_list_init(&dlg->inv_hdr);
 
-    status = pj_mutex_create_recursive(pool, "dlg%p", &dlg->mutex);
+    status = pj_mutex_create_recursive(pool, "dlg%p", &dlg->mutex_);
     if (status != PJ_SUCCESS)
 	goto on_error;
@@ -90,6 +90,6 @@
 
 on_error:
-    if (dlg->mutex)
-	pj_mutex_destroy(dlg->mutex);
+    if (dlg->mutex_)
+	pj_mutex_destroy(dlg->mutex_);
     pjsip_endpt_release_pool(endpt, pool);
     return status;
@@ -98,6 +98,8 @@
 static void destroy_dialog( pjsip_dialog *dlg )
 {
-    if (dlg->mutex)
-	pj_mutex_destroy(dlg->mutex);
+    if (dlg->mutex_) {
+	pj_mutex_destroy(dlg->mutex_);
+	dlg->mutex_ = NULL;
+    }
     pjsip_endpt_release_pool(dlg->endpt, dlg->pool);
 }
@@ -607,6 +609,5 @@
 
     /* Destroy this dialog. */
-    pj_mutex_destroy(dlg->mutex);
-    pjsip_endpt_release_pool(dlg->endpt, dlg->pool);
+    destroy_dialog(dlg);
 
     return PJ_SUCCESS;
@@ -639,5 +640,5 @@
     PJ_ASSERT_RETURN(dlg, PJ_EINVAL);
 
-    pj_mutex_lock(dlg->mutex);
+    pjsip_dlg_inc_lock(dlg);
 
     /* Clear route set. */
@@ -645,5 +646,5 @@
 
     if (!route_set) {
-	pj_mutex_unlock(dlg->mutex);
+	pjsip_dlg_dec_lock(dlg);
 	return PJ_SUCCESS;
     }
@@ -659,6 +660,5 @@
     }
 
-    pj_mutex_unlock(dlg->mutex);
-
+    pjsip_dlg_dec_lock(dlg);
     return PJ_SUCCESS;
 }
@@ -673,7 +673,7 @@
     PJ_ASSERT_RETURN(dlg && mod, PJ_EINVAL);
 
-    pj_mutex_lock(dlg->mutex);
+    pjsip_dlg_inc_lock(dlg);
     ++dlg->sess_count;
-    pj_mutex_unlock(dlg->mutex);
+    pjsip_dlg_dec_lock(dlg);
 
     PJ_LOG(5,(dlg->obj_name, "Session count inc to %d by %.*s",
@@ -685,10 +685,15 @@
 /*
  * Lock dialog and increment session counter temporarily
- * to prevent it from being deleted.
+ * to prevent it from being deleted. In addition, it must lock
+ * the user agent's dialog table first, to prevent deadlock.
  */
 PJ_DEF(void) pjsip_dlg_inc_lock(pjsip_dialog *dlg)
 {
-    pj_mutex_lock(dlg->mutex);
+    pjsip_ua_lock_dlg_table();
+
+    pj_mutex_lock(dlg->mutex_);
     dlg->sess_count++;
+
+    pjsip_ua_unlock_dlg_table();
 }
 
@@ -700,14 +705,18 @@
 PJ_DEF(void) pjsip_dlg_dec_lock(pjsip_dialog *dlg)
 {
+    pjsip_ua_lock_dlg_table();
+
     pj_assert(dlg->sess_count > 0);
     --dlg->sess_count;
 
     if (dlg->sess_count==0 && dlg->tsx_count==0) {
-	pj_mutex_unlock(dlg->mutex);
-	pj_mutex_lock(dlg->mutex);
+	pj_mutex_unlock(dlg->mutex_);
+	pj_mutex_lock(dlg->mutex_);
 	unregister_and_destroy_dialog(dlg);
     } else {
-	pj_mutex_unlock(dlg->mutex);
-    }
+	pj_mutex_unlock(dlg->mutex_);
+    }
+
+    pjsip_ua_unlock_dlg_table();
 }
 
@@ -725,5 +734,6 @@
 	      dlg->sess_count-1, (int)mod->name.slen, mod->name.ptr));
 
-    pj_mutex_lock(dlg->mutex);
+    pjsip_dlg_inc_lock(dlg);
+    --dlg->sess_count;
     pjsip_dlg_dec_lock(dlg);
 
@@ -749,5 +759,5 @@
 	      (int)mod->name.slen, mod->name.ptr, mod_data));
 
-    pj_mutex_lock(dlg->mutex);
+    pjsip_dlg_inc_lock(dlg);
 
     /* Usages are sorted on priority, lowest number first.
@@ -758,5 +768,5 @@
 	if (dlg->usage[index] == mod) {
 	    pj_assert(!"This module is already registered");
-	    pj_mutex_unlock(dlg->mutex);
+	    pjsip_dlg_dec_lock(dlg);
 	    return PJSIP_ETYPEEXISTS;
 	}
@@ -778,5 +788,5 @@
     ++dlg->usage_cnt;
 
-    pj_mutex_unlock(dlg->mutex);
+    pjsip_dlg_dec_lock(dlg);
 
     return PJ_SUCCESS;
@@ -897,5 +907,5 @@
 
     /* Lock dialog. */
-    pj_mutex_lock(dlg->mutex);
+    pjsip_dlg_inc_lock(dlg);
 
     /* Use outgoing CSeq and increment it by one. */
@@ -922,5 +932,5 @@
 
     /* Unlock dialog. */
-    pj_mutex_unlock(dlg->mutex);
+    pjsip_dlg_dec_lock(dlg);
 
     *p_tdata = tdata;
@@ -1114,10 +1124,10 @@
 
     /* Lock the dialog. */
-    pj_mutex_lock(dlg->mutex);
+    pjsip_dlg_inc_lock(dlg);
 
     dlg_beautify_response(dlg, st_code, tdata);
 
     /* Unlock the dialog. */
-    pj_mutex_unlock(dlg->mutex);
+    pjsip_dlg_dec_lock(dlg);
 
     /* Done. */
