PJ_DEF(void) pjsip_dlg_dec_lock(pjsip_dialog *dlg)
{
    PJ_ASSERT_ON_FAIL(dlg!=NULL, return);

    PJ_LOG(6,(dlg->obj_name, "Entering pjsip_dlg_dec_lock(), sess_count=%d",
	      dlg->sess_count));

    pj_assert(dlg->sess_count > 0);
    --dlg->sess_count;

    if (dlg->sess_count==0 && dlg->tsx_count==0) {
	pj_grp_lock_release(dlg->grp_lock_);
	pj_grp_lock_acquire(dlg->grp_lock_);
	/* We are holding the dialog group lock here, so before we destroy
	 * the dialog, make sure that we unlock it first to avoid
	 * undefined behaviour on some platforms. See ticket #1886.
	 */
	unregister_and_destroy_dialog(dlg, PJ_TRUE);
    } else {
	pj_grp_lock_release(dlg->grp_lock_);
    }

    PJ_LOG(6,(THIS_FILE, "Leaving pjsip_dlg_dec_lock() (dlg=%p)", dlg));
}
PJ_DEF(void) pjsip_dlg_dec_lock(pjsip_dialog *dlg)
{
    PJ_ASSERT_ON_FAIL(dlg!=NULL, return);

    PJ_LOG(6,(dlg->obj_name, "Entering pjsip_dlg_dec_lock(), sess_count=%d",
	      dlg->sess_count));

    pj_assert(dlg->sess_count > 0);
    --dlg->sess_count;

    if (dlg->sess_count==0 && dlg->tsx_count==0) {
	pj_grp_lock_release(dlg->grp_lock_);
	pj_grp_lock_acquire(dlg->grp_lock_);
	 
	unregister_and_destroy_dialog(dlg, PJ_TRUE);
    } else {
	pj_grp_lock_release(dlg->grp_lock_);
    }

    PJ_LOG(6,(THIS_FILE, "Leaving pjsip_dlg_dec_lock() (dlg=%p)", dlg));
}