[asterisk-addons-commits] mnicholson: branch mnicholson/chan-mobile-refactor r758 - /team/mnicholson/ch...

SVN commits to the Asterisk addons project asterisk-addons-commits at lists.digium.com
Wed Jan 28 11:54:41 CST 2009


Author: mnicholson
Date: Wed Jan 28 11:54:41 2009
New Revision: 758

URL: http://svn.digium.com/svn-view/asterisk-addons?view=rev&rev=758
Log:
Attempt to prevent a deadlock when unloading chan_mobile by not using
pthread_cancel.  Also avoid other race conditions by unregistering applications
and cli commands before tearing down internal structures.

Modified:
    team/mnicholson/chan-mobile-refactor/channels/chan_mobile.c

Modified: team/mnicholson/chan-mobile-refactor/channels/chan_mobile.c
URL: http://svn.digium.com/svn-view/asterisk-addons/team/mnicholson/chan-mobile-refactor/channels/chan_mobile.c?view=diff&rev=758&r1=757&r2=758
==============================================================================
--- team/mnicholson/chan-mobile-refactor/channels/chan_mobile.c (original)
+++ team/mnicholson/chan-mobile-refactor/channels/chan_mobile.c Wed Jan 28 11:54:41 2009
@@ -81,6 +81,11 @@
 static int discovery_interval = 60;			/* The device discovery interval, default 60 seconds. */
 static pthread_t discovery_thread = AST_PTHREADT_NULL;	/* The discovery thread */
 static sdp_session_t *sdp_session;
+
+AST_MUTEX_DEFINE_STATIC(unload_mutex);
+static int unloading_flag = 0;
+static inline int check_unloading();
+static inline void set_unloading();
 
 enum mbl_type {
 	MBL_TYPE_PHONE,
@@ -1846,7 +1851,7 @@
 	struct adapter_pvt *adapter;
 	struct mbl_pvt *pvt;
 
-	for (;;) {
+	while (!check_unloading()) {
 		AST_RWLIST_RDLOCK(&adapters);
 		AST_RWLIST_TRAVERSE(&adapters, adapter, entry) {
 			if (!adapter->inuse) {
@@ -1868,8 +1873,11 @@
 			}
 		}
 		AST_RWLIST_UNLOCK(&adapters);
-		/* Go to sleep */
-		sleep(discovery_interval);
+
+
+		/* Go to sleep (only if we are not unloading) */
+		if (!check_unloading())
+			sleep(discovery_interval);
 	}
 
 	return NULL;
@@ -2137,18 +2145,50 @@
 	return 0;
 }
 
+/*
+ * \brief Check if the module is unloading.
+ * \retval 0 not unloading
+ * \retval 1 unloading
+ */
+static inline int check_unloading()
+{
+	int res;
+	ast_mutex_lock(&unload_mutex);
+	res = unloading_flag;
+	ast_mutex_unlock(&unload_mutex);
+
+	return res;
+}
+
+/*
+ * \brief Set the unloading flag.
+ */
+static inline void set_unloading()
+{
+	ast_mutex_lock(&unload_mutex);
+	unloading_flag = 1;
+	ast_mutex_unlock(&unload_mutex);
+}
+
 static int unload_module(void)
 {
-
 	struct mbl_pvt *pvt;
 	struct adapter_pvt *adapter;
 
 	/* First, take us out of the channel loop */
 	ast_channel_unregister(&mbl_tech);
+	
+	/* Unregister the CLI & APP */
+	ast_cli_unregister_multiple(mbl_cli, sizeof(mbl_cli) / sizeof(mbl_cli[0]));
+	ast_unregister_application(app_mblstatus);
+	ast_unregister_application(app_mblsendsms);
+
+	/* signal everyone we are unloading */
+	set_unloading();
 
 	/* Kill the discovery thread */
 	if (discovery_thread != AST_PTHREADT_NULL) {
-		pthread_cancel(discovery_thread);
+		pthread_kill(discovery_thread, SIGURG);
 		pthread_join(discovery_thread, NULL);
 	}
 
@@ -2188,13 +2228,7 @@
 	if (sdp_session)
 		sdp_close(sdp_session);
 
-	/* Unregister the CLI & APP */
-	ast_cli_unregister_multiple(mbl_cli, sizeof(mbl_cli) / sizeof(mbl_cli[0]));
-	ast_unregister_application(app_mblstatus);
-	ast_unregister_application(app_mblsendsms);
-
 	return 0;
-
 }
 
 static int load_module(void)




More information about the asterisk-addons-commits mailing list