From ea544cf5157d16540f6e4356d3865f7edea79f08 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 31 May 2024 18:45:20 +0200 Subject: [PATCH] windows-once: Simplify. * lib/windows-once.c (glwthread_once): Use InterlockedCompareExchange instead of InterlockedIncrement. --- ChangeLog | 6 ++++++ lib/windows-once.c | 5 ++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2ebc8a792b..eff0fcfc4d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2024-05-31 Bruno Haible + + windows-once: Simplify. + * lib/windows-once.c (glwthread_once): Use InterlockedCompareExchange + instead of InterlockedIncrement. + 2024-05-31 Bruno Haible pthread-once: Fix race in Cygwin workaround implementation. diff --git a/lib/windows-once.c b/lib/windows-once.c index b6e1c77566..75525ab197 100644 --- a/lib/windows-once.c +++ b/lib/windows-once.c @@ -30,7 +30,8 @@ glwthread_once (glwthread_once_t *once_control, void (*initfunction) (void)) if (once_control->inited <= 0) { InterlockedIncrement (&once_control->num_threads); - if (InterlockedIncrement (&once_control->started) == 0) + /* If once_control->started is == -1, set it to 0. */ + if (InterlockedCompareExchange (&once_control->started, 0, -1) < 0) { /* This thread is the first one to come to this once_control. */ InitializeCriticalSection (&once_control->lock); @@ -42,8 +43,6 @@ glwthread_once (glwthread_once_t *once_control, void (*initfunction) (void)) } else { - /* Don't let once_control->started grow and wrap around. */ - InterlockedDecrement (&once_control->started); /* Some other thread has already started the initialization. Yield the CPU while waiting for the other thread to finish initializing and taking the lock. */ -- 2.39.5