Skip to content

Commit

Permalink
Fix race condition when retrieving thread return value.
Browse files Browse the repository at this point in the history
Return value is not used anywhere currently.

(cherry picked from commit 2c0b899)
  • Loading branch information
skullernet authored and res2k committed Jan 2, 2025
1 parent 0b68137 commit 7820c5f
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions inc/system/pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,39 @@ typedef struct {
void *(*func)(void *);
void *arg, *ret;
HANDLE handle;
} pthread_t;
} *pthread_t;

static unsigned __stdcall thread_func(void *arg)
{
pthread_t *t = arg;
pthread_t t = arg;
t->ret = t->func(t->arg);
return 0;
}

static inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg)
{
thread->func = start_routine;
thread->arg = arg;
thread->handle = (HANDLE)_beginthreadex(NULL, 0, thread_func, thread, 0, NULL);
return thread->handle ? 0 : EAGAIN;
pthread_t t = calloc(1, sizeof(*t));
if (!t)
return EAGAIN;
t->func = start_routine;
t->arg = arg;
t->handle = (HANDLE)_beginthreadex(NULL, 0, thread_func, t, 0, NULL);
if (!t->handle) {
free(t);
return EAGAIN;
}
*thread = t;
return 0;
}

static inline int pthread_join(pthread_t thread, void **retval)
{
int ret = WaitForSingleObject(thread.handle, INFINITE);
CloseHandle(thread.handle);
int ret = WaitForSingleObject(thread->handle, INFINITE);
CloseHandle(thread->handle);
if (retval)
*retval = thread.ret;
*retval = thread->ret;
free(thread);
return ret ? EINVAL : 0;
}

Expand Down

0 comments on commit 7820c5f

Please sign in to comment.