Commit 3fbe73e7 by Nicolas Capens

Guard against threads joining twice.

After a pthread_join() call the handle becomes invalid or could be recycled by another thread, leading to undefined behavior when attempting to join it again. Also, on Windows the handle has to be closed to free it. Bug b/34883464 Change-Id: Ib20d0539b6b46e331c6378b3a9f0c2a334d34892 Reviewed-on: https://swiftshader-review.googlesource.com/8612Reviewed-by: 's avatarAlexis Hétu <sugoi@google.com> Reviewed-by: 's avatarNicolas Capens <capn@google.com> Tested-by: 's avatarNicolas Capens <capn@google.com>
parent 0ceb7f7d
...@@ -22,9 +22,9 @@ namespace sw ...@@ -22,9 +22,9 @@ namespace sw
Entry entry = {threadFunction, parameters, &init}; Entry entry = {threadFunction, parameters, &init};
#if defined(_WIN32) #if defined(_WIN32)
handle = CreateThread(0, 1024 * 1024, startFunction, &entry, 0, 0); handle = CreateThread(NULL, 1024 * 1024, startFunction, &entry, 0, NULL);
#else #else
pthread_create(&handle, 0, startFunction, &entry); pthread_create(&handle, NULL, startFunction, &entry);
#endif #endif
init.wait(); init.wait();
...@@ -37,11 +37,17 @@ namespace sw ...@@ -37,11 +37,17 @@ namespace sw
void Thread::join() void Thread::join()
{ {
#if defined(_WIN32) if(!hasJoined)
WaitForSingleObject(handle, INFINITE); {
#else #if defined(_WIN32)
pthread_join(handle, 0); WaitForSingleObject(handle, INFINITE);
#endif CloseHandle(handle);
#else
pthread_join(handle, NULL);
#endif
hasJoined = true;
}
} }
#if defined(_WIN32) #if defined(_WIN32)
...@@ -58,17 +64,17 @@ namespace sw ...@@ -58,17 +64,17 @@ namespace sw
Entry entry = *(Entry*)parameters; Entry entry = *(Entry*)parameters;
entry.init->signal(); entry.init->signal();
entry.threadFunction(entry.threadParameters); entry.threadFunction(entry.threadParameters);
return 0; return nullptr;
} }
#endif #endif
Event::Event() Event::Event()
{ {
#if defined(_WIN32) #if defined(_WIN32)
handle = CreateEvent(0, FALSE, FALSE, 0); handle = CreateEvent(NULL, FALSE, FALSE, NULL);
#else #else
pthread_cond_init(&handle, 0); pthread_cond_init(&handle, NULL);
pthread_mutex_init(&mutex, 0); pthread_mutex_init(&mutex, NULL);
signaled = false; signaled = false;
#endif #endif
} }
......
...@@ -70,6 +70,8 @@ namespace sw ...@@ -70,6 +70,8 @@ namespace sw
static void *startFunction(void *parameters); static void *startFunction(void *parameters);
pthread_t handle; pthread_t handle;
#endif #endif
bool hasJoined = false;
}; };
class Event class Event
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment