From 38b0018a1d208793b79ec1004a4f73ddc922ef5c Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sun, 12 Oct 2008 15:13:06 +0200
Subject: [PATCH] Use msvcrt aware primitives for creation/termination of Win32
 threads.

---
 ChangeLog             |  8 ++++++++
 lib/glthread/thread.c | 13 ++++++++-----
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index dd1b73d92a..972387525c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2008-10-12  Bruno Haible  <bruno@clisp.org>
+
+	Use msvcrt aware primitives for creation/termination of Win32 threads.
+	* lib/glthread/thread.c: Include <process.h>.
+	(glthread_create_func): Use _beginthreadex instead of CreateThread.
+	(wrapper_func): Update signature.
+	(gl_thread_exit_func): Use _endthreadex instead of EndThread.
+
 2008-10-11  Yoann Vandoorselaere  <yoann@prelude-ids.org>
             Bruno Haible  <bruno@clisp.org>
 
diff --git a/lib/glthread/thread.c b/lib/glthread/thread.c
index 521b68ccc4..4ebfc5446f 100644
--- a/lib/glthread/thread.c
+++ b/lib/glthread/thread.c
@@ -31,6 +31,8 @@
 
 #if USE_WIN32_THREADS
 
+#include <process.h>
+
 /* -------------------------- gl_thread_t datatype -------------------------- */
 
 /* The Thread-Local Storage (TLS) key that allows to access each thread's
@@ -118,7 +120,7 @@ gl_thread_self_func (void)
 
 /* The main function of a freshly creating thread.  It's a wrapper around
    the FUNC and ARG arguments passed to glthread_create_func.  */
-static DWORD WINAPI
+static unsigned int WINAPI
 wrapper_func (void *varg)
 {
   struct gl_thread_struct *thread = (struct gl_thread_struct *)varg;
@@ -154,11 +156,12 @@ glthread_create_func (gl_thread_t *threadp, void * (*func) (void *), void *arg)
   thread->arg = arg;
 
   {
-    DWORD thread_id;
+    unsigned int thread_id;
     HANDLE thread_handle;
 
-    thread_handle =
-      CreateThread (NULL, 100000, wrapper_func, thread, 0, &thread_id);
+    thread_handle = (HANDLE)
+      _beginthreadex (NULL, 100000, wrapper_func, thread, 0, &thread_id);
+      /* calls CreateThread with the same arguments */
     if (thread_handle == NULL)
       {
 	DeleteCriticalSection (&thread->handle_lock);
@@ -206,7 +209,7 @@ gl_thread_exit_func (void *retval)
 {
   gl_thread_t thread = gl_thread_self ();
   thread->result = retval;
-  ExitThread (0);
+  _endthreadex (0); /* calls ExitThread (0) */
 }
 
 #endif
-- 
2.39.5