]> Savannah Git Hosting - gnulib.git/commitdiff
windows-once: Simplify.
authorBruno Haible <bruno@clisp.org>
Fri, 31 May 2024 16:45:20 +0000 (18:45 +0200)
committerBruno Haible <bruno@clisp.org>
Fri, 31 May 2024 16:54:10 +0000 (18:54 +0200)
* lib/windows-once.c (glwthread_once): Use InterlockedCompareExchange
instead of InterlockedIncrement.

ChangeLog
lib/windows-once.c

index 2ebc8a792b5ba5de55af05e5d334e63860a7326b..eff0fcfc4d45c5e97a275ab0830b73da16433e7d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2024-05-31  Bruno Haible  <bruno@clisp.org>
+
+       windows-once: Simplify.
+       * lib/windows-once.c (glwthread_once): Use InterlockedCompareExchange
+       instead of InterlockedIncrement.
+
 2024-05-31  Bruno Haible  <bruno@clisp.org>
 
        pthread-once: Fix race in Cygwin workaround implementation.
index b6e1c775662021392fbbf2479119e55c444ed21f..75525ab197527fa496698be7f0f349dc09045cf0 100644 (file)
@@ -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.  */