

--- a/camel/camel-net-utils.c	2010-02-09 11:08:27.000000000 -0500
+++ b/camel/camel-net-utils.c	2010-02-09 11:23:16.000000000 -0500
@@ -26,7 +26,6 @@
 #endif
 
 #include <errno.h>
-#include <pthread.h>
 #include <stdio.h>
 
 #include <glib.h>
@@ -478,8 +477,9 @@
 cs_waitinfo(gpointer (worker)(gpointer), struct _addrinfo_msg *msg, const gchar *error, CamelException *ex)
 {
 	CamelMsgPort *reply_port;
-	pthread_t id;
-	gint err, cancel_fd, cancel = 0, fd;
+	GThread *thread;
+	GError *err = NULL;
+	gint cancel_fd, cancel = 0, fd;
 
 	cancel_fd = camel_operation_cancel_fd(NULL);
 	if (cancel_fd == -1) {
@@ -489,7 +489,7 @@
 
 	reply_port = msg->msg.reply_port = camel_msgport_new();
 	fd = camel_msgport_fd(msg->msg.reply_port);
-	if ((err = pthread_create(&id, NULL, worker, msg)) == 0) {
+	if ((thread = g_thread_create (worker, msg, TRUE, &err)) != NULL) {
 		gint status;
 #ifndef G_OS_WIN32
 		GPollFD polls[2];
@@ -537,8 +537,7 @@
 			   We check the reply port incase we had a reply in the mean time, which we free later */
 			d(printf("Canceling lookup thread and leaving it\n"));
 			msg->cancelled = 1;
-			pthread_cancel(id);
-			pthread_join (id, NULL);
+			g_thread_join (thread);
 			cancel = 1;
 		} else {
 			struct _addrinfo_msg *reply = (struct _addrinfo_msg *)camel_msgport_try_pop(reply_port);
@@ -556,11 +555,13 @@
 				}
 			}
 			d(printf("waiting for child to exit\n"));
-			pthread_join(id, NULL);
+			g_thread_join (thread);
 			d(printf("child done\n"));
 		}
 	} else {
-		camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM, "%s: %s: %s", error, _("cannot create thread"), g_strerror(err));
+		camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM, "%s: %s: %s", error, _("cannot create thread"), err ? err->message : _("Unknown error"));
+		if (err)
+			g_error_free (err);
 	}
 	camel_msgport_destroy(reply_port);
 
@@ -587,13 +586,12 @@
 	/* This is a pretty simplistic emulation of getaddrinfo */
 
 	while ((msg->result = camel_gethostbyname_r(msg->name, &h, msg->hostbufmem, msg->hostbuflen, &herr)) == ERANGE) {
-		test_cancelled (msg);
+		if (msg->cancelled)
+			break;
                 msg->hostbuflen *= 2;
                 msg->hostbufmem = g_realloc(msg->hostbufmem, msg->hostbuflen);
 	}
 
-	test_cancelled (msg);
-
 	/* If we got cancelled, dont reply, just free it */
 	if (msg->cancelled)
 		goto cancel;
@@ -648,9 +646,7 @@
 		}
 	}
 
-	test_cancelled (msg);
-
-	for (i=0;h.h_addr_list[i];i++) {
+	for (i = 0; h.h_addr_list[i] && !msg->cancelled; i++) {
 		res = g_malloc0(sizeof(*res));
 		if (msg->hints) {
 			res->ai_flags = msg->hints->ai_flags;
@@ -705,8 +701,6 @@
 			info->result = getaddrinfo(info->name, "443", info->hints, info->res);
 	}
 
-	test_cancelled (info);
-
 	if (!info->cancelled) {
 		pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &old_cancelstate);
 		camel_msgport_reply((CamelMsg *)info);
@@ -804,7 +798,8 @@
 
 	while ((msg->result = camel_gethostbyaddr_r((const gchar *)&sin->sin_addr, sizeof(sin->sin_addr), AF_INET, &h,
 						    msg->hostbufmem, msg->hostbuflen, &herr)) == ERANGE) {
-		test_cancelled (msg);
+		if (msg->cancelled)
+			break;
                 msg->hostbuflen *= 2;
                 msg->hostbufmem = g_realloc(msg->hostbufmem, msg->hostbuflen);
 	}
@@ -812,8 +807,6 @@
 	if (msg->cancelled)
 		goto cancel;
 
-	test_cancelled (msg);
-
 	if (msg->host) {
 		g_free(msg->host);
 		if (msg->result == 0 && h.h_name && h.h_name[0]) {
@@ -830,7 +823,8 @@
 	if (msg->serv)
 		sprintf(msg->serv, "%d", sin->sin_port);
 
-	camel_msgport_reply((CamelMsg *)msg);
+	if (!msg->cancelled)
+		camel_msgport_reply ((CamelMsg *)msg);
 cancel:
 	return NULL;
 }
@@ -846,8 +840,6 @@
 	msg->result = getnameinfo(msg->addr, msg->addrlen, msg->host, msg->hostlen, msg->serv, msg->servlen, msg->flags);
 	pthread_setcancelstate (old_cancelstate, &old_cancelstate);
 
-	test_cancelled (msg);
-
 	if (!msg->cancelled) {
 		pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &old_cancelstate);
 		camel_msgport_reply((CamelMsg *)msg);
