Rework pthread_once to use wait-on-address functions,
either the implementation provided by the OS or its
emulation based on Windows events.
---
 mingw-w64-libraries/winpthreads/Makefile.am  |   1 +
 mingw-w64-libraries/winpthreads/src/once.c   | 415 +++++++++++++++++++
 mingw-w64-libraries/winpthreads/src/once.h   |  31 ++
 mingw-w64-libraries/winpthreads/src/thread.c | 107 +----
 4 files changed, 450 insertions(+), 104 deletions(-)
 create mode 100644 mingw-w64-libraries/winpthreads/src/once.c
 create mode 100644 mingw-w64-libraries/winpthreads/src/once.h

diff --git a/mingw-w64-libraries/winpthreads/Makefile.am 
b/mingw-w64-libraries/winpthreads/Makefile.am
index d602e6c6..4c3c3d81 100644
--- a/mingw-w64-libraries/winpthreads/Makefile.am
+++ b/mingw-w64-libraries/winpthreads/Makefile.am
@@ -22,6 +22,7 @@ libwinpthread_la_SOURCES =      \
   src/misc.c      src/misc.h    \
   src/mutex.c                   \
   src/nanosleep.c               \
+  src/once.c      src/once.h    \
   src/rwlock.c    src/rwlock.h  \
   src/sem.c       src/sem.h     \
   src/spinlock.c                \
diff --git a/mingw-w64-libraries/winpthreads/src/once.c 
b/mingw-w64-libraries/winpthreads/src/once.c
new file mode 100644
index 00000000..5576e4fb
--- /dev/null
+++ b/mingw-w64-libraries/winpthreads/src/once.c
@@ -0,0 +1,415 @@
+/*
+ * Copyright (c) 2026 mingw-w64 project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Author: Luca Bacci <[email protected]>
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <windows.h>
+
+#include <assert.h>
+
+#include "pthread.h"
+#include "misc.h"
+
+#if defined (__MINGW64_VERSION_MAJOR) && \
+    __MINGW64_VERSION_MAJOR < 15
+
+static LONG
+ReadAcquire (const volatile LONG *ptr)
+{
+  return __atomic_load_n (ptr, __ATOMIC_ACQUIRE);
+}
+
+static VOID
+WriteRelease (volatile LONG *ptr, LONG value)
+{
+  __atomic_store_n (ptr, value, __ATOMIC_RELEASE);
+}
+
+static LONG
+ReadNoFence (const volatile LONG *ptr)
+{
+  return __atomic_load_n (ptr, __ATOMIC_RELAXED);
+}
+
+static VOID
+WriteNoFence (volatile LONG *ptr, LONG value)
+{
+  __atomic_store_n (ptr, value, __ATOMIC_RELAXED);
+}
+
+#ifndef InterlockedCompareExchangeNoFence
+#define InterlockedCompareExchangeNoFence InterlockedCompareExchange
+#endif
+
+#ifndef InterlockedDecrementNoFence
+#define InterlockedDecrementNoFence InterlockedDecrement
+#endif
+
+#endif
+
+/* ReadAcquire / WriteRelease from Windows SDK use a freestanding
+ * memory barrier. Here we define variants which use ldar / ldapr
+ * / stlr for better performance on AArch64.
+ */
+
+static WINPTHREADS_ALWAYS_INLINE LONG
+ReadAcquireWrapper (const LONG *ptr)
+{
+#if defined (_MSC_VER) && defined (_M_ARM64)
+  LONG result =
+# if _MSC_FULL_VER >= 193632407
+    /* Newer MSVC has __load_acquire32 intrinsic */
+    __load_acquire32 (ptr);
+# elif defined (__ARM_ARCH) && __ARM_ARCH >= (8 * 100 + 3)
+    /* Older MSVC, compiling for ARMv8.3 or later */
+    __ldapr32 (ptr);
+# else
+    __ldar32 (ptr);
+# endif
+  _ReadWriteBarrier ();
+  return result;
+#else
+  return ReadAcquire (ptr);
+#endif
+}
+
+static WINPTHREADS_ALWAYS_INLINE void
+WriteReleaseWrapper (LONG *ptr,
+                     LONG  value)
+{
+#if defined (_MSC_VER) && defined (_M_ARM64)
+  _ReadWriteBarrier ();
+  __stlr32 (ptr, value);
+#else
+  WriteRelease (ptr, value);
+#endif
+}
+
+/* Downlevel implementation (up to Windows 7)
+ *
+ * We associate external data to a pthread_once_t. External data
+ * is created on-the-fly by secondary threads and contains a
+ * Windows event to wait on. This is pretty much an emulation of
+ * WaitOnAddress / WakeByAddressAll.
+ */
+
+typedef struct {
+  LONG waiters_count;
+  HANDLE completion_event;
+} external_data_t;
+
+static external_data_t *
+external_data_new (void)
+{
+  external_data_t *data;
+  HANDLE completion_event;
+
+  completion_event = CreateEvent (NULL, TRUE, FALSE, NULL);
+  if (!completion_event)
+    return NULL;
+
+  data = malloc (sizeof (external_data_t));
+  if (!data) {
+    CloseHandle (completion_event);
+    return NULL;
+  }
+
+  data->waiters_count = 0;
+  data->completion_event = completion_event;
+
+  return data;
+}
+
+static void
+external_data_free (external_data_t *data)
+{
+  CloseHandle (data->completion_event);
+
+  free (data);
+}
+
+static struct {
+  winpthreads_table_t table;
+  CRITICAL_SECTION lock;
+} downlevel;
+
+static external_data_t *
+downlevel_secondary_thread_register (pthread_once_t *once)
+{
+  winpthreads_table_entry_t *table_entry;
+  external_data_t *data = NULL;
+
+  EnterCriticalSection (&downlevel.lock);
+
+  if (ReadNoFence (once) == 2) {
+    /* Initialization already completed, nothing to do */
+    goto leave;
+  }
+
+  table_entry = winpthreads_table_find_entry (&downlevel.table, once);
+  if (!table_entry) {
+    data = external_data_new ();
+    if (!data)
+      goto leave;
+
+    table_entry = winpthreads_table_add_entry (&downlevel.table, once);
+    if (!table_entry) {
+      external_data_free (data);
+      data = NULL;
+      goto leave;
+    }
+
+    table_entry->value = data;
+  }
+
+  data = table_entry->value;
+  if (data->waiters_count == LONG_MAX)
+    goto leave;
+
+  data->waiters_count++;
+
+leave:
+  LeaveCriticalSection (&downlevel.lock);
+
+  return data;
+}
+
+static void
+downlevel_secondary_thread_unregister (external_data_t *data)
+{
+  if (data) {
+    if (InterlockedDecrementNoFence (&data->waiters_count) == 0)
+      external_data_free (data);
+  }
+}
+
+static void
+downlevel_primary_thread_done (pthread_once_t *once)
+{
+  winpthreads_table_entry_t *table_entry;
+  HANDLE completion_event = NULL;
+
+  EnterCriticalSection (&downlevel.lock);
+
+  table_entry = winpthreads_table_find_entry (&downlevel.table, once);
+  if (table_entry) {
+    external_data_t *data = table_entry->value;
+
+    completion_event = data->completion_event;
+    assert (completion_event != NULL);
+
+    winpthreads_table_remove_entry (&downlevel.table, table_entry);
+  }
+
+  LeaveCriticalSection (&downlevel.lock);
+
+  /* It's best to set the event out of the lock. This way
+   * the awaken thread has a higher chance of entering the
+   * lock.
+   */
+  if (completion_event)
+  {
+    DWORD ret = SetEvent (completion_event);
+    assert (ret != 0);
+  }
+}
+
+static void
+downlevel_primary_thread_completed (pthread_once_t *once)
+{
+  WriteRelease (once, 2);
+
+  downlevel_primary_thread_done (once);
+}
+
+static void
+downlevel_primary_thread_cancelled (pthread_once_t *once)
+{
+  WriteNoFence (once, 0);
+
+  downlevel_primary_thread_done (once);
+}
+
+static void
+downlevel_once_cancelled (void *user_data)
+{
+  downlevel_primary_thread_cancelled ((pthread_once_t *) user_data);
+}
+
+static void
+modern_once_cancelled (void *user_data)
+{
+  pthread_once_t *once = (pthread_once_t *) user_data;
+
+  WriteNoFence (once, 0);
+
+  pWakeByAddressSingle (once);
+}
+
+int
+pthread_once (pthread_once_t *once,
+              void (* func) (void))
+{
+  LONG state;
+
+  if (once == NULL)
+    return EINVAL;
+
+  if (func == NULL)
+    return EINVAL;
+
+  while (1)
+  {
+    state = ReadAcquireWrapper (once);
+    if (state == 2) {
+      /* Already initialized. */
+      return 0;
+    }
+
+    if (pWaitOnAddress)
+    {
+      /* Implementation for modern Windows (Windows 8 and above) */
+
+      switch (state) {
+        case 0:
+          /* Try to become the initializer thread. */
+          if (InterlockedCompareExchangeNoFence (once, 1, 0) == 0) {
+            /* Perform initialization */
+            pthread_cleanup_push (modern_once_cancelled, once);
+            func ();
+            pthread_cleanup_pop (0);
+
+            /* Publish the final "initialization done" value. */
+            WriteReleaseWrapper (once, 2);
+
+            /* Wake all waiters. */
+            pWakeByAddressAll (once);
+
+            return 0;
+          }
+
+          /* If we are here, another thread arrived first and
+           * was granted with the initialization task.
+           */
+
+          /* fallthrough */
+#ifdef __GNUC__
+         __attribute__ ((fallthrough));
+#endif
+        case 1:
+          while ((state = ReadNoFence (once)) == 1)
+            pWaitOnAddress (once, &state, sizeof (state), INFINITE);
+          break;
+        default:
+          abort ();
+          break;
+      }
+    }
+    else
+    {
+      /* Downlevel support (up to Windows 7) */
+
+      switch (state) {
+        case 0:
+          /* Attempt to become the primary thread. */
+          if (InterlockedCompareExchangeNoFence (once, 1, 0) == 0) {
+            /* Perform initialization. */
+            pthread_cleanup_push (downlevel_once_cancelled, once);
+            func ();
+            pthread_cleanup_pop (0);
+
+            /* Publish the final "initialization done" value
+             * and wake all waiters.
+             */
+            downlevel_primary_thread_completed (once);
+
+            return 0;
+          }
+
+          /* Another thread arrived first and was granted with the
+           * initialization task. Now go waiting.
+           */
+
+          /* fallthrough */
+#ifdef __GNUC__
+         __attribute__ ((fallthrough));
+#endif
+        case 1:
+        {
+          external_data_t *data;
+
+          /* Attempt to register as secondary thread in the external
+           * data. This way we get an event that signals when once
+           * is completed.
+           */
+          data = downlevel_secondary_thread_register (once);
+
+          if (data) {
+            DWORD ret = WaitForSingleObject (data->completion_event, INFINITE);
+            assert (ret == WAIT_OBJECT_0);
+
+            /* Unregister so that the last thread can clean up.
+             */
+            downlevel_secondary_thread_unregister (data);
+          }
+          else
+          {
+            /* Either initialization is already completed or resources
+             * couldn't be allocated. Fall back to checks at regular
+             * intervals. In case initialization is already completed
+             * we won't sleep at all.
+             */
+            while (ReadNoFence (once) == 1)
+              Sleep (10);
+          }
+        }
+      }
+    }
+  }
+
+  return 0;
+}
+
+/* Called from DLL_PROCESS_ATTACH
+ */
+void
+once_static_initialize (void)
+{
+  if (!pWaitOnAddress) {
+    InitializeCriticalSection (&downlevel.lock);
+  }
+}
+
+/* Called from DLL_PROCESS_DETACH (unless the process is terminating)
+ */
+void
+once_static_finalize (void)
+{
+  if (!pWaitOnAddress) {
+    DeleteCriticalSection (&downlevel.lock);
+    winpthreads_table_finalize (&downlevel.table);
+  }
+}
diff --git a/mingw-w64-libraries/winpthreads/src/once.h 
b/mingw-w64-libraries/winpthreads/src/once.h
new file mode 100644
index 00000000..b20c30e1
--- /dev/null
+++ b/mingw-w64-libraries/winpthreads/src/once.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2026 mingw-w64 project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Author: Luca Bacci <[email protected]>
+ */
+
+#ifndef WIN_PTHREADS_ONCE_H
+#define WIN_PTHREADS_ONCE_H
+
+void once_static_initialize (void);
+void once_static_finalize (void);
+
+#endif /* WIN_PTHREADS_ONCE_H */
diff --git a/mingw-w64-libraries/winpthreads/src/thread.c 
b/mingw-w64-libraries/winpthreads/src/thread.c
index 671da2c9..ebad853c 100644
--- a/mingw-w64-libraries/winpthreads/src/thread.c
+++ b/mingw-w64-libraries/winpthreads/src/thread.c
@@ -49,6 +49,7 @@
 #include "pthread.h"
 /* internal header files */
 #include "misc.h"
+#include "once.h"
 #include "thread.h"
 
 static _pthread_v *__pthread_self_lite (void);
@@ -480,6 +481,7 @@ __dyn_tls_pthread (HANDLE hDllHandle, DWORD dwReason, 
LPVOID lpreserved)
   if (dwReason == DLL_PROCESS_DETACH)
     {
       if (!lpreserved) {
+        once_static_finalize ();
         free_pthread_mem ();
         pthread_tls_fini ();
       }
@@ -487,6 +489,7 @@ __dyn_tls_pthread (HANDLE hDllHandle, DWORD dwReason, 
LPVOID lpreserved)
   else if (dwReason == DLL_PROCESS_ATTACH)
     {
       pthread_tls_init ();
+      once_static_initialize ();
     }
   else if (dwReason == DLL_THREAD_DETACH)
     {
@@ -581,82 +584,6 @@ static __attribute__((used))
 WINPTHREADS_ATTRIBUTE((WINPTHREADS_SECTION(".CRT$XLF")))
 const PIMAGE_TLS_CALLBACK __xl_f = __dyn_tls_pthread;
 
-/* Internal collect-once structure.  */
-typedef struct collect_once_t {
-  pthread_once_t *o;
-  pthread_mutex_t m;
-  int count;
-  struct collect_once_t *next;
-} collect_once_t;
-
-static collect_once_t *once_obj = NULL;
-
-static pthread_spinlock_t once_global = PTHREAD_SPINLOCK_INITIALIZER;
-
-static collect_once_t *
-enterOnceObject (pthread_once_t *o)
-{
-  collect_once_t *c, *p = NULL;
-  pthread_spin_lock (&once_global);
-  c = once_obj;
-  while (c != NULL && c->o != o)
-    {
-      c = (p = c)->next;
-    }
-  if (!c)
-    {
-      c = (collect_once_t *) calloc(1,sizeof(collect_once_t));
-      c->o = o;
-      c->count = 1;
-      if (!p)
-        once_obj = c;
-      else
-        p->next = c;
-      pthread_mutex_init(&c->m, NULL);
-    }
-  else
-    c->count += 1;
-  pthread_spin_unlock (&once_global);
-  return c;
-}
-
-static void
-leaveOnceObject (collect_once_t *c)
-{
-  collect_once_t *h, *p = NULL;
-  if (!c)
-    return;
-  pthread_spin_lock (&once_global);
-  h = once_obj;
-  while (h != NULL && c != h)
-    h = (p = h)->next;
-
-  if (h)
-    {
-      c->count -= 1;
-      if (c->count == 0)
-       {
-         pthread_mutex_destroy(&c->m);
-         if (!p)
-           once_obj = c->next;
-         else
-           p->next = c->next;
-         free (c);
-       }
-    }
-  else
-    fprintf(stderr, "%p not found?!?!\n", (void *) c);
-  pthread_spin_unlock (&once_global);
-}
-
-static void
-_pthread_once_cleanup (void *o)
-{
-  collect_once_t *co = (collect_once_t *) o;
-  pthread_mutex_unlock (&co->m);
-  leaveOnceObject (co);
-}
-
 /* Unimplemented.  */
 void *
 pthread_timechange_handler_np(void *dummy)
@@ -764,34 +691,6 @@ pthread_set_num_processors_np(int n)
   return r;
 }
 
-int
-pthread_once (pthread_once_t *o, void (*func)(void))
-{
-  collect_once_t *co;
-  long state = *o;
-
-  CHECK_PTR(o);
-  CHECK_PTR(func);
-
-  if (state == 1)
-    return 0;
-  co = enterOnceObject(o);
-  pthread_mutex_lock(&co->m);
-  if (*o == 0)
-    {
-      pthread_cleanup_push(_pthread_once_cleanup, co);
-      func();
-      pthread_cleanup_pop(0);
-      *o = 1;
-    }
-  else if (*o != 1)
-    fprintf (stderr," once %p is %ld\n", (void *) o, (long) *o);
-  pthread_mutex_unlock(&co->m);
-  leaveOnceObject(co);
-
-  return 0;
-}
-
 int
 pthread_key_create (pthread_key_t *key, void (* dest)(void *))
 {
-- 
2.55.0.windows.5



_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to