[asterisk-commits] coreyfarrell: branch 1.8 r415463 - in /branches/1.8: include/ main/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sun Jun 8 22:43:30 CDT 2014


Author: coreyfarrell
Date: Sun Jun  8 22:43:21 2014
New Revision: 415463

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=415463
Log:
autoservice: stop thread on graceful shutdown

This change adds thread shutdown to autoservice for graceful shutdowns only.
ast_register_cleanup is backported to 1.8 to allow this.  The logger callid
is also released on shutdown in 11+.

ASTERISK-23827 #close
Reported by: Corey Farrell
Review: https://reviewboard.asterisk.org/r/3594/

Modified:
    branches/1.8/include/asterisk.h
    branches/1.8/main/asterisk.c
    branches/1.8/main/autoservice.c

Modified: branches/1.8/include/asterisk.h
URL: http://svnview.digium.com/svn/asterisk/branches/1.8/include/asterisk.h?view=diff&rev=415463&r1=415462&r2=415463
==============================================================================
--- branches/1.8/include/asterisk.h (original)
+++ branches/1.8/include/asterisk.h Sun Jun  8 22:43:21 2014
@@ -90,6 +90,22 @@
 int ast_register_atexit(void (*func)(void));
 
 /*!
+ * \since 1.8.29
+ * \brief Register a function to be executed before Asterisk gracefully exits.
+ *
+ * If Asterisk is immediately shutdown (core stop now, or sending the TERM
+ * signal), the callback is not run. When the callbacks are run, they are run in
+ * sequence with ast_register_atexit() callbacks, in the reverse order of
+ * registration.
+ *
+ * \param func The callback function to use.
+ *
+ * \retval 0 on success.
+ * \retval -1 on error.
+ */
+int ast_register_cleanup(void (*func)(void));
+
+/*!
  * \brief Unregister a function registered with ast_register_atexit().
  * \param func The callback function to unregister.
  */

Modified: branches/1.8/main/asterisk.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.8/main/asterisk.c?view=diff&rev=415463&r1=415462&r2=415463
==============================================================================
--- branches/1.8/main/asterisk.c (original)
+++ branches/1.8/main/asterisk.c Sun Jun  8 22:43:21 2014
@@ -209,6 +209,7 @@
 
 struct ast_atexit {
 	void (*func)(void);
+	int is_cleanup;
 	AST_LIST_ENTRY(ast_atexit) list;
 };
 
@@ -944,13 +945,13 @@
 
 #endif /* ! LOW_MEMORY */
 
-static void ast_run_atexits(void)
+static void ast_run_atexits(int run_cleanups)
 {
 	struct ast_atexit *ae;
 
 	AST_LIST_LOCK(&atexits);
 	while ((ae = AST_LIST_REMOVE_HEAD(&atexits, list))) {
-		if (ae->func) {
+		if (ae->func && (!ae->is_cleanup || run_cleanups)) {
 			ae->func();
 		}
 		ast_free(ae);
@@ -972,7 +973,7 @@
 	AST_LIST_TRAVERSE_SAFE_END;
 }
 
-int ast_register_atexit(void (*func)(void))
+static int register_atexit(void (*func)(void), int is_cleanup)
 {
 	struct ast_atexit *ae;
 
@@ -981,6 +982,7 @@
 		return -1;
 	}
 	ae->func = func;
+	ae->is_cleanup = is_cleanup;
 
 	AST_LIST_LOCK(&atexits);
 	__ast_unregister_atexit(func);
@@ -988,6 +990,16 @@
 	AST_LIST_UNLOCK(&atexits);
 
 	return 0;
+}
+
+int ast_register_atexit(void (*func)(void))
+{
+	return register_atexit(func, 0);
+}
+
+int ast_register_cleanup(void (*func)(void))
+{
+	return register_atexit(func, 1);
 }
 
 void ast_unregister_atexit(void (*func)(void))
@@ -1748,8 +1760,9 @@
 static void really_quit(int num, shutdown_nice_t niceness, int restart)
 {
 	int active_channels;
-
-	if (niceness >= SHUTDOWN_NICE) {
+	int run_cleanups = niceness >= SHUTDOWN_NICE;
+
+	if (run_cleanups) {
 		ast_module_shutdown();
 	}
 
@@ -1791,7 +1804,7 @@
 
 	if (option_verbose)
 		ast_verbose("Executing last minute cleanups\n");
-	ast_run_atexits();
+	ast_run_atexits(run_cleanups);
 
 	ast_debug(1, "Asterisk ending (%d).\n", num);
 	if (ast_socket > -1) {

Modified: branches/1.8/main/autoservice.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.8/main/autoservice.c?view=diff&rev=415463&r1=415462&r2=415463
==============================================================================
--- branches/1.8/main/autoservice.c (original)
+++ branches/1.8/main/autoservice.c Sun Jun  8 22:43:21 2014
@@ -72,6 +72,7 @@
 static ast_cond_t as_cond;
 
 static pthread_t asthread = AST_PTHREADT_NULL;
+static volatile int asexit = 0;
 
 static int as_chan_list_state;
 
@@ -82,7 +83,7 @@
 		.subclass.integer = AST_CONTROL_HANGUP,
 	};
 
-	for (;;) {
+	while (!asexit) {
 		struct ast_channel *mons[MAX_AUTOMONS];
 		struct asent *ents[MAX_AUTOMONS];
 		struct ast_channel *chan;
@@ -320,7 +321,19 @@
 	return res;
 }
 
+static void autoservice_shutdown(void)
+{
+	pthread_t th = asthread;
+	asexit = 1;
+	if (th != AST_PTHREADT_NULL) {
+		ast_cond_signal(&as_cond);
+		pthread_kill(th, SIGURG);
+		pthread_join(th, NULL);
+	}
+}
+
 void ast_autoservice_init(void)
 {
+	ast_register_cleanup(autoservice_shutdown);
 	ast_cond_init(&as_cond, NULL);
 }




More information about the asterisk-commits mailing list