[asterisk-commits] trunk - r8572 /trunk/asterisk.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Tue Jan 24 13:27:11 MST 2006


Author: russell
Date: Tue Jan 24 14:27:09 2006
New Revision: 8572

URL: http://svn.digium.com/view/asterisk?rev=8572&view=rev
Log:
store the list of 'atexit' functions using linked list macros (issue #6329)

Modified:
    trunk/asterisk.c

Modified: trunk/asterisk.c
URL: http://svn.digium.com/view/asterisk/trunk/asterisk.c?rev=8572&r1=8571&r2=8572&view=diff
==============================================================================
--- trunk/asterisk.c (original)
+++ trunk/asterisk.c Tue Jan 24 14:27:09 2006
@@ -167,12 +167,12 @@
 	pthread_t t;			/*!< Thread of handler */
 };
 
-static struct ast_atexit {
+struct ast_atexit {
 	void (*func)(void);
-	struct ast_atexit *next;
-} *atexits = NULL;
-
-AST_MUTEX_DEFINE_STATIC(atexitslock);
+	AST_LIST_ENTRY(ast_atexit) list;
+};
+
+static AST_LIST_HEAD_STATIC(atexits, ast_atexit);
 
 time_t ast_startuptime;
 time_t ast_lastreloadtime;
@@ -355,35 +355,29 @@
 	struct ast_atexit *ae;
 	ast_unregister_atexit(func);
 	ae = malloc(sizeof(struct ast_atexit));
-	ast_mutex_lock(&atexitslock);
+	AST_LIST_LOCK(&atexits);
 	if (ae) {
 		memset(ae, 0, sizeof(struct ast_atexit));
-		ae->next = atexits;
+		AST_LIST_INSERT_HEAD(&atexits, ae, list);
 		ae->func = func;
-		atexits = ae;
 		res = 0;
 	}
-	ast_mutex_unlock(&atexitslock);
+	AST_LIST_UNLOCK(&atexits);
 	return res;
 }
 
 void ast_unregister_atexit(void (*func)(void))
 {
-	struct ast_atexit *ae, *prev = NULL;
-	ast_mutex_lock(&atexitslock);
-	ae = atexits;
-	while(ae) {
+	struct ast_atexit *ae;
+	AST_LIST_LOCK(&atexits);
+	AST_LIST_TRAVERSE_SAFE_BEGIN(&atexits, ae, list) {
 		if (ae->func == func) {
-			if (prev)
-				prev->next = ae->next;
-			else
-				atexits = ae->next;
+			AST_LIST_REMOVE_CURRENT(&atexits, list);
 			break;
 		}
-		prev = ae;
-		ae = ae->next;
-	}
-	ast_mutex_unlock(&atexitslock);
+	}
+	AST_LIST_TRAVERSE_SAFE_END
+	AST_LIST_UNLOCK(&atexits);
 }
 
 static int fdprint(int fd, const char *s)
@@ -806,14 +800,12 @@
 static void ast_run_atexits(void)
 {
 	struct ast_atexit *ae;
-	ast_mutex_lock(&atexitslock);
-	ae = atexits;
-	while(ae) {
+	AST_LIST_LOCK(&atexits);
+	AST_LIST_TRAVERSE(&atexits, ae, list) {
 		if (ae->func) 
 			ae->func();
-		ae = ae->next;
-	}
-	ast_mutex_unlock(&atexitslock);
+	}
+	AST_LIST_UNLOCK(&atexits);
 }
 
 static void quit_handler(int num, int nice, int safeshutdown, int restart)



More information about the asterisk-commits mailing list