store the list of 'atexit' functions using linked list macros (issue #6329)
authorRussell Bryant <russell@russellbryant.com>
Tue, 24 Jan 2006 20:27:09 +0000 (20:27 +0000)
committerRussell Bryant <russell@russellbryant.com>
Tue, 24 Jan 2006 20:27:09 +0000 (20:27 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@8572 65c4cc65-6c06-0410-ace0-fbb531ad65f3

asterisk.c

index 16903da..acdaca6 100644 (file)
@@ -167,12 +167,12 @@ struct console {
        pthread_t t;                    /*!< Thread of handler */
 };
 
-static struct ast_atexit {
+struct ast_atexit {
        void (*func)(void);
-       struct ast_atexit *next;
-} *atexits = NULL;
+       AST_LIST_ENTRY(ast_atexit) list;
+};
 
-AST_MUTEX_DEFINE_STATIC(atexitslock);
+static AST_LIST_HEAD_STATIC(atexits, ast_atexit);
 
 time_t ast_startuptime;
 time_t ast_lastreloadtime;
@@ -355,35 +355,29 @@ int ast_register_atexit(void (*func)(void))
        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 @@ int ast_set_priority(int pri)
 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)