diff options
Diffstat (limited to 'ptw32_threadDestroy.c')
-rw-r--r-- | ptw32_threadDestroy.c | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/ptw32_threadDestroy.c b/ptw32_threadDestroy.c index 1c503a6..18dc6a9 100644 --- a/ptw32_threadDestroy.c +++ b/ptw32_threadDestroy.c @@ -42,6 +42,8 @@ void ptw32_threadDestroy (pthread_t thread) { + struct pthread_t_ threadCopy; + if (thread != NULL) { (void) pthread_mutex_lock(&thread->cancelLock); @@ -50,22 +52,33 @@ ptw32_threadDestroy (pthread_t thread) ptw32_callUserDestroyRoutines (thread); - if (thread->cancelEvent != NULL) - { - CloseHandle (thread->cancelEvent); - } + /* + * Copy thread state so that the thread can be atomically NULLed. + */ + memcpy(&threadCopy, thread, sizeof(threadCopy)); + + /* + * Thread ID structs are never freed. They're NULLed and reused. + * This also sets the thread to PThreadStateInitial (invalid). + */ + ptw32_threadReusePush(thread); + + /* Now work on the copy. */ + if (threadCopy.cancelEvent != NULL) + { + CloseHandle (threadCopy.cancelEvent); + } - (void) pthread_mutex_destroy(&thread->cancelLock); + (void) pthread_mutex_destroy(&threadCopy.cancelLock); #if ! defined (__MINGW32__) || defined (__MSVCRT__) /* See documentation for endthread vs endthreadex. */ - if( thread->threadH != 0 ) - { - CloseHandle( thread->threadH ); - } + if( threadCopy.threadH != 0 ) + { + CloseHandle( threadCopy.threadH ); + } #endif - free (thread); } } /* ptw32_threadDestroy */ |