Skip to content

Commit 6e0f966

Browse files
committed
Merge branch 'sk/win32-close-handle-upon-pthread-join'
Pthread emulation on Win32 leaked thread handle when a thread is joined. * sk/win32-close-handle-upon-pthread-join: win32: close handles of threads that have been joined win32: prepare pthread.c for change by formatting
2 parents 5427bb4 + 238a9df commit 6e0f966

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

compat/win32/pthread.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ static unsigned __stdcall win32_start_routine(void *arg)
2222
}
2323

2424
int pthread_create(pthread_t *thread, const void *unused,
25-
void *(*start_routine)(void*), void *arg)
25+
void *(*start_routine)(void *), void *arg)
2626
{
2727
thread->arg = arg;
2828
thread->start_routine = start_routine;
29-
thread->handle = (HANDLE)
30-
_beginthreadex(NULL, 0, win32_start_routine, thread, 0, NULL);
29+
thread->handle = (HANDLE)_beginthreadex(NULL, 0, win32_start_routine,
30+
thread, 0, NULL);
3131

3232
if (!thread->handle)
3333
return errno;
@@ -39,14 +39,17 @@ int win32_pthread_join(pthread_t *thread, void **value_ptr)
3939
{
4040
DWORD result = WaitForSingleObject(thread->handle, INFINITE);
4141
switch (result) {
42-
case WAIT_OBJECT_0:
43-
if (value_ptr)
44-
*value_ptr = thread->arg;
45-
return 0;
46-
case WAIT_ABANDONED:
47-
return EINVAL;
48-
default:
49-
return err_win_to_posix(GetLastError());
42+
case WAIT_OBJECT_0:
43+
if (value_ptr)
44+
*value_ptr = thread->arg;
45+
CloseHandle(thread->handle);
46+
return 0;
47+
case WAIT_ABANDONED:
48+
CloseHandle(thread->handle);
49+
return EINVAL;
50+
default:
51+
/* the wait failed, so do not detach */
52+
return err_win_to_posix(GetLastError());
5053
}
5154
}
5255

0 commit comments

Comments
 (0)