Fix the Win32 implementation of the 'thread' module.
[gnulib.git] / lib / glthread / thread.h
index 1a3b695..a04e4a4 100644 (file)
@@ -277,13 +277,16 @@ extern "C" {
 
 /* -------------------------- gl_thread_t datatype -------------------------- */
 
-/* The gl_thread_t is the thread id, not the thread handle.  If it were the
-   thread handle, it would be hard to implement gl_thread_self()
-   (since GetCurrentThread () returns a pseudo-handle,
-   DuplicateHandle (GetCurrentThread ()) returns a handle that must be closed
-   afterwards, and there is no function for quickly retrieving a thread handle
-   from its id).  */
-typedef DWORD gl_thread_t;
+/* The gl_thread_t is a pointer to a structure in memory.
+   Why not the thread handle?  If it were the thread handle, it would be hard
+   to implement gl_thread_self() (since GetCurrentThread () returns a pseudo-
+   handle, DuplicateHandle (GetCurrentThread ()) returns a handle that must be
+   closed afterwards, and there is no function for quickly retrieving a thread
+   handle from its id).
+   Why not the thread id?  I tried it.  It did not work: Sometimes ids appeared
+   that did not belong to running threads, and glthread_join failed with ESRCH.
+ */
+typedef struct gl_thread_struct *gl_thread_t;
 # define glthread_create(THREADP, FUNC, ARG) \
     glthread_create_func (THREADP, FUNC, ARG)
 # define glthread_sigmask(HOW, SET, OSET) \
@@ -291,12 +294,13 @@ typedef DWORD gl_thread_t;
 # define glthread_join(THREAD, RETVALP) \
     glthread_join_func (THREAD, RETVALP)
 # define gl_thread_self() \
-    GetCurrentThreadId ()
+    gl_thread_self_func ()
 # define gl_thread_exit(RETVAL) \
     gl_thread_exit_func (RETVAL)
 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
 extern int glthread_create_func (gl_thread_t *threadp, void * (*func) (void *), void *arg);
 extern int glthread_join_func (gl_thread_t thread, void **retvalp);
+extern gl_thread_t gl_thread_self_func (void);
 extern int gl_thread_exit_func (void *retval);
 
 # ifdef __cplusplus