Fix potential race condition when creating detached threads

This commit is contained in:
Marisa Heit 2022-07-25 21:15:29 -05:00
parent b58d142dd9
commit 2867c8d8db
1 changed files with 18 additions and 7 deletions

View File

@ -213,26 +213,37 @@ GThread *fluid_g_thread_create(GThreadFunc func, void *data, BOOL joinable, GErr
info->func = func;
info->data = data;
info->handle = (HANDLE)_beginthreadex(NULL, 0, g_thread_wrapper, info, 0, NULL);
HANDLE thread = (HANDLE)_beginthreadex(NULL, 0, g_thread_wrapper, info, CREATE_SUSPENDED, NULL);
if (error != NULL)
{
error_container.code = errno;
error_container.code = thread ? 0 : errno;
if (errno != 0)
{
error_container.message = strerror(errno);
}
*error = &error_container;
}
if (info->handle == 0)
if (thread == NULL)
{
free(info);
info = NULL;
}
else if (!joinable)
else
{
/* Release thread reference, if caller doesn't want to join */
CloseHandle(info->handle);
info->handle = NULL;
if (!joinable)
{
/* Release thread reference, if caller doesn't want to join */
CloseHandle(thread);
info->handle = NULL;
}
else
{
info->handle = thread;
}
ResumeThread(thread);
}
return info;
}