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

View file

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