From 129951975f8ca943d1a564c438986877a7bba47f Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 23 Aug 2014 00:11:03 -0700 Subject: [PATCH] Fix removal of the last thread from the thread list --- limelight-common/PlatformThreads.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/limelight-common/PlatformThreads.c b/limelight-common/PlatformThreads.c index 71ca562..7f79285 100644 --- a/limelight-common/PlatformThreads.c +++ b/limelight-common/PlatformThreads.c @@ -97,19 +97,30 @@ void PltCloseThread(PLT_THREAD *thread) { PLT_THREAD *current_thread; PltLockMutex(&thread_list_lock); - current_thread = thread_head; - while (current_thread != NULL) { - if (current_thread->next == thread) { - break; + + if (thread_head == thread) + { + // Remove the thread from the head + thread_head = thread_head->next; + } + else + { + // Find the thread in the list + current_thread = thread_head; + while (current_thread != NULL) { + if (current_thread->next == thread) { + break; + } + + current_thread = current_thread->next; } - current_thread = current_thread->next; + LC_ASSERT(current_thread != NULL); + + // Unlink this thread + current_thread->next = thread->next; } - LC_ASSERT(current_thread != NULL); - - // Unlink this thread - current_thread->next = thread->next; PltUnlockMutex(&thread_list_lock); CloseHandle(thread->termevent);