Fix directed pickup to a call that is up (bug #5425 with mods)
[asterisk/asterisk.git] / autoservice.c
index 5f15c24..7dcec2a 100755 (executable)
@@ -1,42 +1,58 @@
 /*
- * Asterisk -- A telephony toolkit for Linux.
+ * Asterisk -- An open source telephony toolkit.
  *
- * Automatic channel service routines
- * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999 - 2005, Digium, Inc.
+ *
+ * Mark Spencer <markster@digium.com>
  *
- * Mark Spencer <markster@linux-support.net>
+ * 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
- * the GNU General Public License
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*
+ *
+ * Automatic channel service routines
+ * 
  */
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <pthread.h>
 #include <string.h>
 #include <sys/time.h>
 #include <signal.h>
 #include <errno.h>
 #include <unistd.h>
 #include <math.h>                      /* For PI */
-#include <asterisk/pbx.h>
-#include <asterisk/frame.h>
-#include <asterisk/sched.h>
-#include <asterisk/options.h>
-#include <asterisk/channel.h>
-#include <asterisk/channel_pvt.h>
-#include <asterisk/logger.h>
-#include <asterisk/file.h>
-#include <asterisk/translate.h>
-#include <asterisk/manager.h>
-#include <asterisk/chanvars.h>
-#include <asterisk/linkedlists.h>
-#include <asterisk/indications.h>
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/pbx.h"
+#include "asterisk/frame.h"
+#include "asterisk/sched.h"
+#include "asterisk/options.h"
+#include "asterisk/channel.h"
+#include "asterisk/logger.h"
+#include "asterisk/file.h"
+#include "asterisk/translate.h"
+#include "asterisk/manager.h"
+#include "asterisk/chanvars.h"
+#include "asterisk/linkedlists.h"
+#include "asterisk/indications.h"
+#include "asterisk/lock.h"
+#include "asterisk/utils.h"
 
 #define MAX_AUTOMONS 256
 
-static pthread_mutex_t autolock = AST_MUTEX_INITIALIZER;
+AST_MUTEX_DEFINE_STATIC(autolock);
 
 struct asent {
        struct ast_channel *chan;
@@ -44,7 +60,7 @@ struct asent {
 };
 
 static struct asent *aslist = NULL;
-static pthread_t asthread = -1;
+static pthread_t asthread = AST_PTHREADT_NULL;
 
 static void *autoservice_run(void *ign)
 {
@@ -56,7 +72,7 @@ static void *autoservice_run(void *ign)
        struct ast_frame *f;
        for(;;) {
                x = 0;
-               ast_pthread_mutex_lock(&autolock);
+               ast_mutex_lock(&autolock);
                as = aslist;
                while(as) {
                        if (!as->chan->_softhangup) {
@@ -67,7 +83,7 @@ static void *autoservice_run(void *ign)
                        }
                        as = as->next;
                }
-               ast_pthread_mutex_unlock(&autolock);
+               ast_mutex_unlock(&autolock);
 
 /*             if (!aslist)
                        break; */
@@ -80,7 +96,7 @@ static void *autoservice_run(void *ign)
                                ast_frfree(f);
                }
        }
-       asthread = -1;
+       asthread = AST_PTHREADT_NULL;
        return NULL;
 }
 
@@ -89,8 +105,8 @@ int ast_autoservice_start(struct ast_channel *chan)
        int res = -1;
        struct asent *as;
        int needstart;
-       ast_pthread_mutex_lock(&autolock);
-       needstart = (asthread == -1) ? 1 : 0 /* aslist ? 0 : 1 */;
+       ast_mutex_lock(&autolock);
+       needstart = (asthread == AST_PTHREADT_NULL) ? 1 : 0 /* aslist ? 0 : 1 */;
        as = aslist;
        while(as) {
                if (as->chan == chan)
@@ -106,7 +122,7 @@ int ast_autoservice_start(struct ast_channel *chan)
                        aslist = as;
                        res = 0;
                        if (needstart) {
-                               if (pthread_create(&asthread, NULL, autoservice_run, NULL)) {
+                               if (ast_pthread_create(&asthread, NULL, autoservice_run, NULL)) {
                                        ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
                                        free(aslist);
                                        aslist = NULL;
@@ -116,7 +132,7 @@ int ast_autoservice_start(struct ast_channel *chan)
                        }
                }
        }
-       ast_pthread_mutex_unlock(&autolock);
+       ast_mutex_unlock(&autolock);
        return res;
 }
 
@@ -124,7 +140,7 @@ int ast_autoservice_stop(struct ast_channel *chan)
 {
        int res = -1;
        struct asent *as, *prev;
-       ast_pthread_mutex_lock(&autolock);
+       ast_mutex_lock(&autolock);
        as = aslist;
        prev = NULL;
        while(as) {
@@ -142,11 +158,11 @@ int ast_autoservice_stop(struct ast_channel *chan)
                if (!chan->_softhangup)
                        res = 0;
        }
-       if (asthread != -1) 
+       if (asthread != AST_PTHREADT_NULL) 
                pthread_kill(asthread, SIGURG);
-       ast_pthread_mutex_unlock(&autolock);
+       ast_mutex_unlock(&autolock);
        /* Wait for it to un-block */
-       while(chan->blocking)
+       while(ast_test_flag(chan, AST_FLAG_BLOCKING))
                usleep(1000);
        return res;
 }