Revert Jim's earlier "fix" :)
[asterisk/asterisk.git] / autoservice.c
index fc81c48..df4973a 100755 (executable)
@@ -13,7 +13,6 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <pthread.h>
 #include <string.h>
 #include <sys/time.h>
 #include <signal.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 ast_mutex_t autolock = AST_MUTEX_INITIALIZER;
+AST_MUTEX_DEFINE_STATIC(autolock);
 
 struct asent {
        struct ast_channel *chan;
@@ -44,7 +45,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)
 {
@@ -80,7 +81,7 @@ static void *autoservice_run(void *ign)
                                ast_frfree(f);
                }
        }
-       asthread = -1;
+       asthread = AST_PTHREADT_NULL;
        return NULL;
 }
 
@@ -90,7 +91,7 @@ int ast_autoservice_start(struct ast_channel *chan)
        struct asent *as;
        int needstart;
        ast_mutex_lock(&autolock);
-       needstart = (asthread == -1) ? 1 : 0 /* aslist ? 0 : 1 */;
+       needstart = (asthread == AST_PTHREADT_NULL) ? 1 : 0 /* aslist ? 0 : 1 */;
        as = aslist;
        while(as) {
                if (as->chan == chan)
@@ -106,7 +107,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;
@@ -142,11 +143,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_mutex_unlock(&autolock);
        /* Wait for it to un-block */
-       while(chan->blocking)
+       while(ast_test_flag(chan, AST_FLAG_BLOCKING))
                usleep(1000);
        return res;
 }