various cleanups (issue #6389)
[asterisk/asterisk.git] / autoservice.c
old mode 100755 (executable)
new mode 100644 (file)
index 21bc44d..08cbb47
@@ -1,14 +1,26 @@
 /*
 /*
- * Asterisk -- A telephony toolkit for Linux.
+ * Asterisk -- An open source telephony toolkit.
  *
  *
- * Automatic channel service routines
- * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999 - 2006, Digium, Inc.
  *
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
  *
  * This program is free software, distributed under the terms of
  *
  * This program is free software, distributed under the terms of
- * the GNU General Public License
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Automatic channel service routines
+ *
+ * \author Mark Spencer <markster@digium.com> 
  */
 
 #include <stdio.h>
  */
 
 #include <stdio.h>
@@ -18,7 +30,6 @@
 #include <signal.h>
 #include <errno.h>
 #include <unistd.h>
 #include <signal.h>
 #include <errno.h>
 #include <unistd.h>
-#include <math.h>                      /* For PI */
 
 #include "asterisk.h"
 
 
 #include "asterisk.h"
 
@@ -41,46 +52,39 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #define MAX_AUTOMONS 256
 
 
 #define MAX_AUTOMONS 256
 
-AST_MUTEX_DEFINE_STATIC(autolock);
-
 struct asent {
        struct ast_channel *chan;
 struct asent {
        struct ast_channel *chan;
-       struct asent *next;
+       AST_LIST_ENTRY(asent) list;
 };
 
 };
 
-static struct asent *aslist = NULL;
+static AST_LIST_HEAD_STATIC(aslist, asent);
+
 static pthread_t asthread = AST_PTHREADT_NULL;
 
 static void *autoservice_run(void *ign)
 {
 static pthread_t asthread = AST_PTHREADT_NULL;
 
 static void *autoservice_run(void *ign)
 {
-       struct ast_channel *mons[MAX_AUTOMONS];
-       int x;
-       int ms;
-       struct ast_channel *chan;
-       struct asent *as;
-       struct ast_frame *f;
+
        for(;;) {
        for(;;) {
-               x = 0;
-               ast_mutex_lock(&autolock);
-               as = aslist;
-               while(as) {
+               struct ast_channel *mons[MAX_AUTOMONS];
+               struct ast_channel *chan;
+               struct asent *as;
+               int x = 0, ms = 500;
+
+               AST_LIST_LOCK(&aslist);
+               AST_LIST_TRAVERSE(&aslist, as, list) {
                        if (!as->chan->_softhangup) {
                                if (x < MAX_AUTOMONS)
                                        mons[x++] = as->chan;
                                else
                                        ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events.  Fix autoservice.c\n");
                        }
                        if (!as->chan->_softhangup) {
                                if (x < MAX_AUTOMONS)
                                        mons[x++] = as->chan;
                                else
                                        ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events.  Fix autoservice.c\n");
                        }
-                       as = as->next;
                }
                }
-               ast_mutex_unlock(&autolock);
+               AST_LIST_UNLOCK(&aslist);
 
 
-/*             if (!aslist)
-                       break; */
-               ms = 500;
                chan = ast_waitfor_n(mons, x, &ms);
                if (chan) {
                        /* Read and ignore anything that occurs */
                chan = ast_waitfor_n(mons, x, &ms);
                if (chan) {
                        /* Read and ignore anything that occurs */
-                       f = ast_read(chan);
+                       struct ast_frame *f = ast_read(chan);
                        if (f)
                                ast_frfree(f);
                }
                        if (f)
                                ast_frfree(f);
                }
@@ -93,63 +97,60 @@ int ast_autoservice_start(struct ast_channel *chan)
 {
        int res = -1;
        struct asent *as;
 {
        int res = -1;
        struct asent *as;
-       int needstart;
-       ast_mutex_lock(&autolock);
-       needstart = (asthread == AST_PTHREADT_NULL) ? 1 : 0 /* aslist ? 0 : 1 */;
-       as = aslist;
-       while(as) {
+       AST_LIST_LOCK(&aslist);
+
+       /* Check if the channel already has autoservice */
+       AST_LIST_TRAVERSE(&aslist, as, list) {
                if (as->chan == chan)
                        break;
                if (as->chan == chan)
                        break;
-               as = as->next;
        }
        }
+       /* XXX if found, we return -1, why ??? */
+
+       /* If not, start autoservice on channel */
        if (!as) {
        if (!as) {
-               as = malloc(sizeof(struct asent));
+               as = calloc(1, sizeof(struct asent));
                if (as) {
                if (as) {
-                       memset(as, 0, sizeof(struct asent));
                        as->chan = chan;
                        as->chan = chan;
-                       as->next = aslist;
-                       aslist = as;
+                       AST_LIST_INSERT_HEAD(&aslist, as, list);
                        res = 0;
                        res = 0;
-                       if (needstart) {
+                       if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
                                if (ast_pthread_create(&asthread, NULL, autoservice_run, NULL)) {
                                        ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
                                if (ast_pthread_create(&asthread, NULL, autoservice_run, NULL)) {
                                        ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
-                                       free(aslist);
-                                       aslist = NULL;
+                                       /* There will only be a single member in the list at this point,
+                                          the one we just added. */
+                                       AST_LIST_REMOVE(&aslist, as, list);
+                                       free(as);
                                        res = -1;
                                } else
                                        pthread_kill(asthread, SIGURG);
                        }
                }
        }
                                        res = -1;
                                } else
                                        pthread_kill(asthread, SIGURG);
                        }
                }
        }
-       ast_mutex_unlock(&autolock);
+       AST_LIST_UNLOCK(&aslist);
        return res;
 }
 
 int ast_autoservice_stop(struct ast_channel *chan)
 {
        int res = -1;
        return res;
 }
 
 int ast_autoservice_stop(struct ast_channel *chan)
 {
        int res = -1;
-       struct asent *as, *prev;
-       ast_mutex_lock(&autolock);
-       as = aslist;
-       prev = NULL;
-       while(as) {
-               if (as->chan == chan)
+       struct asent *as;
+
+       AST_LIST_LOCK(&aslist);
+       AST_LIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {       
+               if (as->chan == chan) {
+                       AST_LIST_REMOVE_CURRENT(&aslist, list);
+                       free(as);
+                       if (!chan->_softhangup)
+                               res = 0;
                        break;
                        break;
-               prev = as;
-               as = as->next;
-       }
-       if (as) {
-               if (prev)
-                       prev->next = as->next;
-               else
-                       aslist = as->next;
-               free(as);
-               if (!chan->_softhangup)
-                       res = 0;
+               }
        }
        }
+       AST_LIST_TRAVERSE_SAFE_END
+
        if (asthread != AST_PTHREADT_NULL) 
                pthread_kill(asthread, SIGURG);
        if (asthread != AST_PTHREADT_NULL) 
                pthread_kill(asthread, SIGURG);
-       ast_mutex_unlock(&autolock);
+       AST_LIST_UNLOCK(&aslist);
+
        /* Wait for it to un-block */
        while(ast_test_flag(chan, AST_FLAG_BLOCKING))
                usleep(1000);
        /* Wait for it to un-block */
        while(ast_test_flag(chan, AST_FLAG_BLOCKING))
                usleep(1000);