diff --git a/asterisk.c b/asterisk.c
index a267df996b9bff049d92b651e8d7e8676e4b9d50..7adb0279043fba999b7cf7a939066e1d6fb2effd 100755
--- a/asterisk.c
+++ b/asterisk.c
@@ -34,7 +34,7 @@
 #include <signal.h>
 #include <sched.h>
 #include <asterisk/io.h>
-#include <pthread.h>
+#include <asterisk/lock.h>
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <sys/select.h>
@@ -109,7 +109,7 @@ char ast_config_AST_RUN_DIR[AST_CONFIG_MAX_PATH];
 static char *_argv[256];
 static int shuttingdown = 0;
 static int restartnow = 0;
-static pthread_t consolethread = (pthread_t) -1;
+static pthread_t consolethread = AST_PTHREADT_NULL;
 
 int ast_register_atexit(void (*func)(void))
 {
@@ -544,7 +544,7 @@ static void quit_handler(int num, int nice, int safeshutdown, int restart)
 		restartnow = 1;
 		/* If there is a consolethread running send it a SIGHUP 
 		   so it can execvp, otherwise we can do it ourselves */
-		if (consolethread != (pthread_t) -1) {
+		if (consolethread != AST_PTHREADT_NULL) {
 			pthread_kill(consolethread, SIGHUP);
 			/* Give the signal handler some time to complete */
 			sleep(2);
@@ -591,7 +591,7 @@ static void console_verboser(const char *s, int pos, int replace, int complete)
 	fflush(stdout);
 	if (complete)
 	/* Wake up a select()ing console */
-		if (option_console && consolethread != (pthread_t) -1)
+		if (option_console && consolethread != AST_PTHREADT_NULL)
 			pthread_kill(consolethread, SIGURG);
 }
 
diff --git a/autoservice.c b/autoservice.c
index 7e5699b97fc3445059e1675f1b284a1568254266..3f24db8c19f9c78d52025b1cfe0ec13f9a20798f 100755
--- a/autoservice.c
+++ b/autoservice.c
@@ -44,7 +44,7 @@ struct asent {
 };
 
 static struct asent *aslist = NULL;
-static pthread_t asthread = (pthread_t) -1;
+static pthread_t asthread = AST_PTHREADT_NULL;
 
 static void *autoservice_run(void *ign)
 {
@@ -80,7 +80,7 @@ static void *autoservice_run(void *ign)
 				ast_frfree(f);
 		}
 	}
-	asthread = (pthread_t) -1;
+	asthread = AST_PTHREADT_NULL;
 	return NULL;
 }
 
@@ -90,7 +90,7 @@ int ast_autoservice_start(struct ast_channel *chan)
 	struct asent *as;
 	int needstart;
 	ast_mutex_lock(&autolock);
-	needstart = (asthread == (pthread_t) -1) ? 1 : 0 /* aslist ? 0 : 1 */;
+	needstart = (asthread == AST_PTHREADT_NULL) ? 1 : 0 /* aslist ? 0 : 1 */;
 	as = aslist;
 	while(as) {
 		if (as->chan == chan)
@@ -142,7 +142,7 @@ int ast_autoservice_stop(struct ast_channel *chan)
 		if (!chan->_softhangup)
 			res = 0;
 	}
-	if (asthread != (pthread_t) -1) 
+	if (asthread != AST_PTHREADT_NULL) 
 		pthread_kill(asthread, SIGURG);
 	ast_mutex_unlock(&autolock);
 	/* Wait for it to un-block */
diff --git a/channels/chan_h323.c b/channels/chan_h323.c
index 5b99ed80eb720afcfcb860c9125bb7426a812444..70761d883618c5fd7868ce623bc3b4d9ac3db24f 100755
--- a/channels/chan_h323.c
+++ b/channels/chan_h323.c
@@ -147,7 +147,7 @@ static ast_mutex_t monlock = AST_MUTEX_INITIALIZER;
 
 /* This is the thread for the monitor which checks for input on the channels
    which are not currently in use.  */
-static pthread_t monitor_thread = 0;
+static pthread_t monitor_thread = AST_PTHREADT_NULL;
 
 static int restart_monitor(void);
 
@@ -1260,7 +1260,7 @@ restartsearch:
 static int restart_monitor(void)
 {
 	/* If we're supposed to be stopped -- stay stopped */
-	if (monitor_thread == -2)
+	if (monitor_thread == AST_PTHREADT_STOP)
 		return 0;
 	if (ast_mutex_lock(&monlock)) {
 		ast_log(LOG_WARNING, "Unable to lock monitor\n");
@@ -1271,7 +1271,7 @@ static int restart_monitor(void)
 		ast_log(LOG_WARNING, "Cannot kill myself\n");
 		return -1;
 	}
-	if (monitor_thread && (monitor_thread != -2)) {
+	if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
 		/* Wake up the thread */
 		pthread_kill(monitor_thread, SIGURG);
 	} else {
@@ -1840,12 +1840,12 @@ int unload_module()
 	}
 
 	if (!ast_mutex_lock(&monlock)) {
-                if (monitor_thread && (monitor_thread != -2)) {
+                if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
                         pthread_cancel(monitor_thread);
                         pthread_kill(monitor_thread, SIGURG);
                         pthread_join(monitor_thread, NULL);
                  }
-                monitor_thread = (pthread_t) -2;
+                monitor_thread = AST_PTHREADT_STOP;
                 ast_mutex_unlock(&monlock);
         } else {
                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
diff --git a/channels/chan_iax.c b/channels/chan_iax.c
index ce0897066ca256b289532cc0494634fa96caa93c..a88e855f600dbba8e8accde0bd992be56f067ebc 100755
--- a/channels/chan_iax.c
+++ b/channels/chan_iax.c
@@ -149,7 +149,7 @@ static int iaxdebug = 0;
 static char accountcode[20];
 static int amaflags = 0;
 
-static pthread_t netthreadid;
+static pthread_t netthreadid = AST_PTHREADT_NULL;
 
 #define IAX_STATE_STARTED		(1 << 0)
 #define IAX_STATE_AUTHENTICATED (1 << 1)
@@ -5345,8 +5345,10 @@ static int __unload_module(void)
 {
 	int x;
 	/* Cancel the network thread, close the net socket */
-	pthread_cancel(netthreadid);
-	pthread_join(netthreadid, NULL);
+	if (netthreadid != AST_PTHREADT_NULL) {
+		pthread_cancel(netthreadid);
+		pthread_join(netthreadid, NULL);
+	}
 	close(netsocket);
 	for (x=0;x<AST_IAX_MAX_CALLS;x++)
 		if (iaxs[x])
diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c
index 04f80738f49d412a9e08eed1a87fac8bce3b2ede..c9da32ea92463851c44a3dada5221b1c88789b1c 100755
--- a/channels/chan_iax2.c
+++ b/channels/chan_iax2.c
@@ -172,7 +172,7 @@ static char accountcode[20];
 static int amaflags = 0;
 static int notransfer = 0;
 
-static pthread_t netthreadid;
+static pthread_t netthreadid = AST_PTHREADT_NULL;
 
 #define IAX_STATE_STARTED		(1 << 0)
 #define IAX_STATE_AUTHENTICATED (1 << 1)
@@ -6521,8 +6521,10 @@ static int __unload_module(void)
 {
 	int x;
 	/* Cancel the network thread, close the net socket */
-	pthread_cancel(netthreadid);
-	pthread_join(netthreadid, NULL);
+	if (netthreadid != AST_PTHREADT_NULL) {
+		pthread_cancel(netthreadid);
+		pthread_join(netthreadid, NULL);
+	}
 	close(netsocket);
 	for (x=0;x<IAX_MAX_CALLS;x++)
 		if (iaxs[x])
diff --git a/channels/chan_mgcp.c b/channels/chan_mgcp.c
index 6774ac743db239d30de841c01032085d9435b9b1..1554b33655ed715e6b73e12f780399681443478d 100755
--- a/channels/chan_mgcp.c
+++ b/channels/chan_mgcp.c
@@ -167,7 +167,7 @@ static ast_mutex_t monlock = AST_MUTEX_INITIALIZER;
 
 /* This is the thread for the monitor which checks for input on the channels
    which are not currently in use.  */
-static pthread_t monitor_thread = 0;
+static pthread_t monitor_thread = AST_PTHREADT_NULL;
 
 static int restart_monitor(void);
 
@@ -2558,7 +2558,7 @@ static int restart_monitor(void)
         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);	
 
 	/* If we're supposed to be stopped -- stay stopped */
-	if (monitor_thread == (pthread_t) -2)
+	if (monitor_thread == AST_PTHREADT_STOP)
 		return 0;
 	if (ast_mutex_lock(&monlock)) {
 		ast_log(LOG_WARNING, "Unable to lock monitor\n");
@@ -3052,12 +3052,12 @@ int unload_module()
 		return -1;
 	}
 	if (!ast_mutex_lock(&monlock)) {
-		if (monitor_thread && (monitor_thread != -2)) {
+		if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
 			pthread_cancel(monitor_thread);
 			pthread_kill(monitor_thread, SIGURG);
 			pthread_join(monitor_thread, NULL);
 		}
-		monitor_thread = -2;
+		monitor_thread = AST_PTHREADT_STOP;
 		ast_mutex_unlock(&monlock);
 	} else {
 		ast_log(LOG_WARNING, "Unable to lock the monitor\n");
diff --git a/channels/chan_modem.c b/channels/chan_modem.c
index 09451decd2209fcca84f5872689f04df3b6d7e49..728aa361ada85cb4b3d931add44ff7d0dc36febd 100755
--- a/channels/chan_modem.c
+++ b/channels/chan_modem.c
@@ -83,7 +83,7 @@ static ast_mutex_t monlock = AST_MUTEX_INITIALIZER;
 
 /* This is the thread for the monitor which checks for input on the channels
    which are not currently in use.  */
-static pthread_t monitor_thread = (pthread_t) -1;
+static pthread_t monitor_thread = AST_PTHREADT_NULL;
 
 static int restart_monitor(void);
 
@@ -646,7 +646,7 @@ static void *do_monitor(void *data)
 static int restart_monitor()
 {
 	/* If we're supposed to be stopped -- stay stopped */
-	if (monitor_thread == (pthread_t) -2)
+	if (monitor_thread == AST_PTHREADT_STOP)
 		return 0;
 	if (ast_mutex_lock(&monlock)) {
 		ast_log(LOG_WARNING, "Unable to lock monitor\n");
@@ -657,7 +657,7 @@ static int restart_monitor()
 		ast_log(LOG_WARNING, "Cannot kill myself\n");
 		return -1;
 	}
-	if (monitor_thread != (pthread_t) -1) {
+	if (monitor_thread != AST_PTHREADT_NULL) {
 		pthread_cancel(monitor_thread);
 		/* Nudge it a little, as it's probably stuck in select */
 		pthread_kill(monitor_thread, SIGURG);
@@ -861,11 +861,11 @@ static int __unload_module(void)
 		return -1;
 	}
 	if (!ast_mutex_lock(&monlock)) {
-		if (monitor_thread != (pthread_t) -1 && monitor_thread != (pthread_t) -2) {
+		if (monitor_thread != AST_PTHREADT_NULL && monitor_thread != AST_PTHREADT_STOP) {
 			pthread_cancel(monitor_thread);
 			pthread_join(monitor_thread, NULL);
 		}
-		monitor_thread = (pthread_t) -2;
+		monitor_thread = AST_PTHREADT_STOP;
 		ast_mutex_unlock(&monlock);
 	} else {
 		ast_log(LOG_WARNING, "Unable to lock the monitor\n");
diff --git a/channels/chan_phone.c b/channels/chan_phone.c
index 775734842991085e97f618d0a025079acd876cd5..5ebe0219277ac0dd36246fd8db25cfb93fdcd54a 100755
--- a/channels/chan_phone.c
+++ b/channels/chan_phone.c
@@ -67,7 +67,7 @@ static ast_mutex_t monlock = AST_MUTEX_INITIALIZER;
 
 /* This is the thread for the monitor which checks for input on the channels
    which are not currently in use.  */
-static pthread_t monitor_thread = -1;
+static pthread_t monitor_thread = AST_PTHREADT_NULL;
 
 static int restart_monitor(void);
 
@@ -910,7 +910,7 @@ static void *do_monitor(void *data)
 static int restart_monitor()
 {
 	/* If we're supposed to be stopped -- stay stopped */
-	if (monitor_thread == -2)
+	if (monitor_thread == AST_PTHREADT_STOP)
 		return 0;
 	if (ast_mutex_lock(&monlock)) {
 		ast_log(LOG_WARNING, "Unable to lock monitor\n");
@@ -921,7 +921,7 @@ static int restart_monitor()
 		ast_log(LOG_WARNING, "Cannot kill myself\n");
 		return -1;
 	}
-	if (monitor_thread != -1) {
+	if (monitor_thread != AST_PTHREADT_NULL) {
 		pthread_cancel(monitor_thread);
 #if 0
 		pthread_join(monitor_thread, NULL);
@@ -1071,11 +1071,11 @@ static int __unload_module(void)
 		return -1;
 	}
 	if (!ast_mutex_lock(&monlock)) {
-		if (monitor_thread > -1) {
+		if (monitor_thread > AST_PTHREADT_NULL) {
 			pthread_cancel(monitor_thread);
 			pthread_join(monitor_thread, NULL);
 		}
-		monitor_thread = -2;
+		monitor_thread = AST_PTHREADT_STOP;
 		ast_mutex_unlock(&monlock);
 	} else {
 		ast_log(LOG_WARNING, "Unable to lock the monitor\n");
diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index 2784277e1b6d044aafa722d7b2cbb365bae26b34..bcf9ff9a26ace13f67fd34d6600b2739fde2af1f 100755
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -134,7 +134,7 @@ static ast_mutex_t monlock = AST_MUTEX_INITIALIZER;
 
 /* This is the thread for the monitor which checks for input on the channels
    which are not currently in use.  */
-static pthread_t monitor_thread = 0;
+static pthread_t monitor_thread = AST_PTHREADT_NULL;
 
 static int restart_monitor(void);
 
@@ -5818,7 +5818,7 @@ restartsearch:
 static int restart_monitor(void)
 {
 	/* If we're supposed to be stopped -- stay stopped */
-	if (monitor_thread == (pthread_t) -2)
+	if (monitor_thread == AST_PTHREADT_STOP)
 		return 0;
 	if (ast_mutex_lock(&monlock)) {
 		ast_log(LOG_WARNING, "Unable to lock monitor\n");
@@ -6865,12 +6865,12 @@ int unload_module()
 		return -1;
 	}
 	if (!ast_mutex_lock(&monlock)) {
-		if (monitor_thread && ((int)monitor_thread != -2)) {
+		if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
 			pthread_cancel(monitor_thread);
 			pthread_kill(monitor_thread, SIGURG);
 			pthread_join(monitor_thread, NULL);
 		}
-		monitor_thread = (pthread_t) -2;
+		monitor_thread = AST_PTHREADT_STOP;
 		ast_mutex_unlock(&monlock);
 	} else {
 		ast_log(LOG_WARNING, "Unable to lock the monitor\n");
diff --git a/channels/chan_skinny.c b/channels/chan_skinny.c
index b44c494237498628c4cd384421f6a876aa914e1a..0a01bdc577673818e783ed03e1271665689071a5 100755
--- a/channels/chan_skinny.c
+++ b/channels/chan_skinny.c
@@ -599,7 +599,7 @@ static ast_mutex_t devicelock = AST_MUTEX_INITIALIZER;
 
 /* This is the thread for the monitor which checks for input on the channels
    which are not currently in use.  */
-static pthread_t monitor_thread = 0;
+static pthread_t monitor_thread = AST_PTHREADT_NULL;
 
 /* Wait up to 16 seconds for first digit */
 static int firstdigittimeout = 16000;
@@ -2415,7 +2415,7 @@ static int restart_monitor(void)
 {
 
 	/* If we're supposed to be stopped -- stay stopped */
-	if (monitor_thread == (pthread_t)-2)
+	if (monitor_thread == AST_PTHREADT_STOP)
 		return 0;
 	if (ast_mutex_lock(&monlock)) {
 		ast_log(LOG_WARNING, "Unable to lock monitor\n");
@@ -2718,12 +2718,12 @@ int unload_module()
 		return -1;
 	}
 	if (!ast_mutex_lock(&monlock)) {
-		if (monitor_thread && (monitor_thread != -2)) {
+		if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
 			pthread_cancel(monitor_thread);
 			pthread_kill(monitor_thread, SIGURG);
 			pthread_join(monitor_thread, NULL);
 		}
-		monitor_thread = -2;
+		monitor_thread = AST_PTHREADT_STOP;
 		ast_mutex_unlock(&monlock);
 	} else {
 		ast_log(LOG_WARNING, "Unable to lock the monitor\n");
diff --git a/channels/chan_vofr.c b/channels/chan_vofr.c
index 289e101a95c1c33761bd37832f10edd0f8784057..3d677bd3d4e0cea679706562799f6a083c7a052d 100755
--- a/channels/chan_vofr.c
+++ b/channels/chan_vofr.c
@@ -64,7 +64,7 @@ static ast_mutex_t monlock = AST_MUTEX_INITIALIZER;
 
 /* This is the thread for the monitor which checks for input on the channels
    which are not currently in use.  */
-static pthread_t monitor_thread = 0;
+static pthread_t monitor_thread = AST_PTHREADT_NULL;
 
 static int restart_monitor(void);
 
@@ -997,7 +997,7 @@ static void *do_monitor(void *data)
 static int restart_monitor(void)
 {
 	/* If we're supposed to be stopped -- stay stopped */
-	if (monitor_thread == -2)
+	if (monitor_thread == AST_PTHREADT_STOP)
 		return 0;
 	if (ast_mutex_lock(&monlock)) {
 		ast_log(LOG_WARNING, "Unable to lock monitor\n");
@@ -1161,7 +1161,7 @@ static int __unload_module(void)
 			pthread_kill(monitor_thread, SIGURG);
 			pthread_join(monitor_thread, NULL);
 		}
-		monitor_thread = -2;
+		monitor_thread = AST_PTHREADT_STOP;
 		ast_mutex_unlock(&monlock);
 	} else {
 		ast_log(LOG_WARNING, "Unable to lock the monitor\n");
diff --git a/channels/chan_zap.c b/channels/chan_zap.c
index dd089f5fbda16b75d15f57253c6147e1cccc6ae6..930a870fcab5a79d8aec2ccc3d7d3a7698693ef4 100755
--- a/channels/chan_zap.c
+++ b/channels/chan_zap.c
@@ -228,7 +228,7 @@ static ast_mutex_t monlock = AST_MUTEX_INITIALIZER;
 
 /* This is the thread for the monitor which checks for input on the channels
    which are not currently in use.  */
-static pthread_t monitor_thread = 0;
+static pthread_t monitor_thread = AST_PTHREADT_NULL;
 
 static int restart_monitor(void);
 
@@ -5171,7 +5171,7 @@ static int restart_monitor(void)
 	pthread_attr_init(&attr);
 	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 	/* If we're supposed to be stopped -- stay stopped */
-	if (monitor_thread == -2)
+	if (monitor_thread == AST_PTHREADT_STOP)
 		return 0;
 	if (ast_mutex_lock(&monlock)) {
 		ast_log(LOG_WARNING, "Unable to lock monitor\n");
@@ -7313,12 +7313,12 @@ static int __unload_module(void)
 		return -1;
 	}
 	if (!ast_mutex_lock(&monlock)) {
-		if (monitor_thread && (monitor_thread != -2)) {
+		if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
 			pthread_cancel(monitor_thread);
 			pthread_kill(monitor_thread, SIGURG);
 			pthread_join(monitor_thread, NULL);
 		}
-		monitor_thread = -2;
+		monitor_thread = AST_PTHREADT_STOP;
 		ast_mutex_unlock(&monlock);
 	} else {
 		ast_log(LOG_WARNING, "Unable to lock the monitor\n");