Revert Jim's earlier "fix" :)
[asterisk/asterisk.git] / autoservice.c
index 5f15c24..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 pthread_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)
 {
@@ -56,7 +57,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 +68,7 @@ static void *autoservice_run(void *ign)
                        }
                        as = as->next;
                }
-               ast_pthread_mutex_unlock(&autolock);
+               ast_mutex_unlock(&autolock);
 
 /*             if (!aslist)
                        break; */
@@ -80,7 +81,7 @@ static void *autoservice_run(void *ign)
                                ast_frfree(f);
                }
        }
-       asthread = -1;
+       asthread = AST_PTHREADT_NULL;
        return NULL;
 }
 
@@ -89,8 +90,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 +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;
@@ -116,7 +117,7 @@ int ast_autoservice_start(struct ast_channel *chan)
                        }
                }
        }
-       ast_pthread_mutex_unlock(&autolock);
+       ast_mutex_unlock(&autolock);
        return res;
 }
 
@@ -124,7 +125,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 +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_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;
 }