Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pthreads.h: avoid undefined behavior #967

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions include/compat/pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,23 @@ struct pthread_once {
};
typedef struct pthread_once pthread_once_t;

struct _pthread_win32_cb_arg {
void (*cb)(void);
};

static inline BOOL CALLBACK
_pthread_once_win32_cb(PINIT_ONCE once, PVOID param, PVOID *context)
{
void (*cb) (void) = param;
cb();
struct _pthread_win32_cb_arg *arg = param;
arg->cb();
return TRUE;
}

static inline int
pthread_once(pthread_once_t *once, void (*cb) (void))
{
BOOL rc = InitOnceExecuteOnce(&once->once, _pthread_once_win32_cb, cb, NULL);
struct _pthread_win32_cb_arg arg = { .cb = cb };
BOOL rc = InitOnceExecuteOnce(&once->once, _pthread_once_win32_cb, &arg, NULL);
if (rc == 0)
return -1;
else
Expand Down