Hello,

GitHub CI run: https://github.com/maiddaisuki/mingw-w64/actions/runs/35682236764

(the rest is some testing I did while worked on these changes)

While working in these changes, I was considering adding the spin loop which 
used a compile-time constant for the spin count (value of zero would complete 
remove this path during compilation) before entering wait state.

Back in May, when `pthread_spin_*` functions were updated, I mentioned that 
duration of tests pthread_rwlock/rwlock{7,8} increased from (all measurements 
are approximate) 1,100ms to 138,142ms and from 200ms to 13,810ms respectively.

I tried using these tests for measurements:

- With the attached changes, these tests take about 1,500ms and 250ms 
respectively.

- With the "spin loop", pthread_rwlock/rwlock8 would take about 300ms, while 
duration of pthread_rwlock/rwlock7 would range from 1,800ms to 2,200ms 
depending on spin count.

So, using "spin loop" was making code noticeable slower so I ditched that idea 
completely.

There is another thing I tried to measure: time spent in `pthread_spin_lock` 
before obtaining the lock when fast lock path failed. I called 
`QueryPerformanceCounter ` at three points:

1. Before entering the "spin loop`
2. Before entering wait state (if failed to obtain lock while spinning)
3. Once the lock was obtained

If lock was obtained while spinning, it wound print time spent spinning and 
total spin count. Otherwise, it would print time spent both spinning and 
waiting.

While time spent waiting looked pretty consistent, spinning time had absolutely 
insane spikes; for example, for some spin count, total wait time would not 
exceed 2 nanoseconds, while spinning time would sometimes spike to astronomical 
25 nanoseconds (which contributes to total time spent in `pthread_spin_lock` if 
it has to enter wait state).

- Kirill Makurin
From c88056a395d0ebb74a95d41cde2b0b297665aadc Mon Sep 17 00:00:00 2001
From: Kirill Makurin <[email protected]>
Date: Tue, 22 Sep 2026 12:11:13 +0900
Subject: [PATCH 1/3] winpthreads: spinlock.c: call `CreateEventW` instead of
 `CreateEventA`

Unicode APIs such as `CreateEventW` are preffered over ANSI such as
`CreateEventA`; the former, however, are not supported on Win9x systems.

In the current state, winpthreads does not support Win9x systems, so use
Unicode APIs instead.

Signed-off-by: Kirill Makurin <[email protected]>
---
 mingw-w64-libraries/winpthreads/src/spinlock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mingw-w64-libraries/winpthreads/src/spinlock.c 
b/mingw-w64-libraries/winpthreads/src/spinlock.c
index 836f16c50..2f05164da 100644
--- a/mingw-w64-libraries/winpthreads/src/spinlock.c
+++ b/mingw-w64-libraries/winpthreads/src/spinlock.c
@@ -171,7 +171,7 @@ int pthread_spin_init (pthread_spinlock_t *lock, int 
pshared)
   }
 
   wSpinlock->ThreadId = THREAD_ID_NO_OWNER;
-  wSpinlock->Event    = CreateEventA (NULL, FALSE, TRUE, NULL);
+  wSpinlock->Event    = CreateEventW (NULL, FALSE, TRUE, NULL);
 
   /**
    * The pthread_spin_init() function shall fail if:
-- 
2.55.0.windows.3

From 5d2e18c34b2019da2d09067882c88d6bc65c0fb8 Mon Sep 17 00:00:00 2001
From: Kirill Makurin <[email protected]>
Date: Tue, 22 Sep 2026 12:11:13 +0900
Subject: [PATCH 2/3] winpthreads: spinlock.c: add fast lock paths

These fast paths are equivalent to those used for stalled mutexes in mutex.c.

Signed-off-by: Kirill Makurin <[email protected]>
---
 .../winpthreads/src/spinlock.c                | 156 ++++++++++--------
 1 file changed, 89 insertions(+), 67 deletions(-)

diff --git a/mingw-w64-libraries/winpthreads/src/spinlock.c 
b/mingw-w64-libraries/winpthreads/src/spinlock.c
index 2f05164da..9867091cb 100644
--- a/mingw-w64-libraries/winpthreads/src/spinlock.c
+++ b/mingw-w64-libraries/winpthreads/src/spinlock.c
@@ -47,10 +47,41 @@
  *  
<https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/pthread_spin_unlock.html>
  */
 
+/**
+ * Spinlock lock state.
+ */
+typedef enum {
+  /**
+   * Spinlock is unlocked.
+   */
+  Unlocked,
+  /**
+   * Spinlock is locked.
+   *
+   * This state inicates that there are no blocked threads waiting for
+   * the spinlock to be released.
+   *
+   * While in this state, if any thread blocks waiting for the spinlock to be
+   * released, the lock state will change to `LockedWithBlocking`.
+   */
+  Locked,
+  /**
+   * Spinlock is locked.
+   *
+   * This state indicates that there can be one or more blocked threads
+   * waiting for the spinlock to be released.
+   */
+  LockedWithBlocking,
+} WinpthreadsSpinlockLockState;
+
 /**
  * Internal structure pointed to by `pthread_spinlock_t` objects.
  */
 typedef struct {
+  /**
+   * One of `WinpthreadsSpinlockLockState` values.
+   */
+  LONG State;
   /**
    * This value is used to indicate that spin lock has no owner.
    */
@@ -62,15 +93,8 @@ typedef struct {
   /**
    * Auto-reset event.
    *
-   * This event is created in signaled state, which means any thread can
-   * `WaitForSingleObject` on it; only one thread will be released at a time,
-   * after which this event will be automatically put into non-signaled state.
-   *
-   * The released thread owns the lock; it unlocks it by calling `SetEvent` on
-   * this event, which puts this event into signaled state, allowing system
-   * release another thread which waits on it.
-   *
-   * The cycle repeats until this event is destroyed.
+   * When a thread releases the spinlock and `State` is `LockedWithBlocking`,
+   * this event will be signaled in order to release one blocked thread.
    */
   HANDLE Event;
 } WinpthreadsSpinlock;
@@ -170,8 +194,9 @@ int pthread_spin_init (pthread_spinlock_t *lock, int 
pshared)
     return ENOMEM;
   }
 
+  wSpinlock->State    = Unlocked;
   wSpinlock->ThreadId = THREAD_ID_NO_OWNER;
-  wSpinlock->Event    = CreateEventW (NULL, FALSE, TRUE, NULL);
+  wSpinlock->Event    = CreateEventW (NULL, FALSE, FALSE, NULL);
 
   /**
    * The pthread_spin_init() function shall fail if:
@@ -221,29 +246,17 @@ int pthread_spin_destroy (pthread_spinlock_t *lock)
     return 0;
   }
 
-  switch (_pthread_wait_for_single_object (wSpinlock->Event, 0)) {
-    /**
-     * `wSpinlock->Event` was in signaled state, which means it was unlocked;
-     * we are holding the lock now which prevents other threads from locking 
it.
-     */
-    case WAIT_OBJECT_0:
-      break;
-    /**
-     * `wSpinlock->Event` was in not-signaled state, which means some thread
-     * holds the lock.
-     *
-     * POSIX:
-     *
-     * If an implementation detects that the value specified by the lock 
argument
-     * to pthread_spin_destroy() or pthread_spin_init() refers to a locked spin
-     * lock object, or detects that the value specified by the lock argument to
-     * pthread_spin_init() refers to an already initialized spin lock object,
-     * it is recommended that the function should fail and report an [EBUSY] 
error.
-     */
-    case WAIT_TIMEOUT:
-      return EBUSY;
-    default:
-      return EINVAL;
+  /**
+   * POSIX:
+   *
+   * If an implementation detects that the value specified by the lock argument
+   * to pthread_spin_destroy() or pthread_spin_init() refers to a locked spin
+   * lock object, or detects that the value specified by the lock argument to
+   * pthread_spin_init() refers to an already initialized spin lock object,
+   * it is recommended that the function should fail and report an [EBUSY] 
error.
+   */
+  if (unlikely (InterlockedCompareExchange (&wSpinlock->State, Locked, 
Unlocked) != Unlocked)) {
+    return EBUSY;
   }
 
   /**
@@ -279,17 +292,37 @@ int pthread_spin_lock (pthread_spinlock_t *lock)
     return EDEADLK;
   }
 
-  switch (_pthread_wait_for_single_object (wSpinlock->Event, INFINITE)) {
-    /**
-     * We are holding the lock now and `wSpinlock->Event` was reset to
-     * non-signaled state.
-     */
-    case WAIT_OBJECT_0:
-      break;
-    default:
-      return EINVAL;
+  /**
+   * Try fast path.
+   */
+  if (InterlockedCompareExchange (&wSpinlock->State, Locked, Unlocked) == 
Unlocked) {
+    goto locked;
+  }
+
+  /**
+   * Setting `wSpinlock->State` to `LockedWithBlocking` causes
+   * `pthread_spin_unlock` to signal `wSpinlock->Event`.
+   */
+  LONG oldLockState = InterlockedExchange (&wSpinlock->State, 
LockedWithBlocking);
+
+  if (likely (oldLockState != Unlocked)) {
+    do {
+      switch (_pthread_wait_for_single_object (wSpinlock->Event, INFINITE)) {
+        /**
+         * `wSpinlock->Event` was signaled.
+         *
+         * There is a small chance that another thread grabs the lock faster
+         * than we do; lock state is updated before wait handle is signaled.
+         */
+        case WAIT_OBJECT_0:
+          break;
+        default:
+          return EINVAL;
+      }
+    } while (unlikely (InterlockedExchange (&wSpinlock->State, 
LockedWithBlocking) != Unlocked));
   }
 
+locked:
   wSpinlock->ThreadId = threadId;
 
   return 0;
@@ -306,30 +339,13 @@ int pthread_spin_trylock (pthread_spinlock_t *lock)
   }
 
   /**
-   * Unlike `pthread_spin_lock`, no deadlock can occur even if calling thread
-   * owns the lock.
+   * The pthread_spin_trylock() function shall fail if:
+   *
+   * [EBUSY]
+   *  A thread currently holds the lock.
    */
-  switch (_pthread_wait_for_single_object (wSpinlock->Event, 0)) {
-    /**
-     * `wSpinlock->Event` was in signaled state, which means it was unlocked;
-     * we are holding the lock now and `wSpinlock->Event` was reset to
-     * non-signaled state.
-     */
-    case WAIT_OBJECT_0:
-      break;
-    /**
-     * `wSpinlock->Event` was in non-signaled state, which means some thread
-     * holds the lock.
-     *
-     * The pthread_spin_trylock() function shall fail if:
-     *
-     * [EBUSY]
-     *  A thread currently holds the lock.
-     */
-    case WAIT_TIMEOUT:
-      return EBUSY;
-    default:
-      return EINVAL;
+  if (InterlockedCompareExchange (&wSpinlock->State, Locked, Unlocked) != 
Unlocked) {
+    return EBUSY;
   }
 
   wSpinlock->ThreadId = GetCurrentThreadId ();
@@ -363,8 +379,14 @@ int pthread_spin_unlock (pthread_spinlock_t *lock)
 
   wSpinlock->ThreadId = THREAD_ID_NO_OWNER;
 
-  if (!SetEvent (wSpinlock->Event)) {
-    return EINVAL;
+  /**
+   * If `wSpinlock->State` is `LockedWithBlocking`, then some other thread is
+   * waiting for `wSpinlock->Event` to become signaled.
+   */
+  if (InterlockedExchange (&wSpinlock->State, Unlocked) == LockedWithBlocking) 
{
+    if (!SetEvent (wSpinlock->Event)) {
+      return EINVAL;
+    }
   }
 
   return 0;
-- 
2.55.0.windows.3

From 1137780e51de7dd114083e53005ca68c0a74ab60 Mon Sep 17 00:00:00 2001
From: Kirill Makurin <[email protected]>
Date: Tue, 22 Sep 2026 12:11:13 +0900
Subject: [PATCH 3/3] winpthreads: pthread.h: group spinlock-related
 declarations together

Signed-off-by: Kirill Makurin <[email protected]>
---
 .../winpthreads/include/pthread.h             | 30 ++++++++++++-------
 1 file changed, 19 insertions(+), 11 deletions(-)

diff --git a/mingw-w64-libraries/winpthreads/include/pthread.h 
b/mingw-w64-libraries/winpthreads/include/pthread.h
index 9c0ef5062..a61753c16 100644
--- a/mingw-w64-libraries/winpthreads/include/pthread.h
+++ b/mingw-w64-libraries/winpthreads/include/pthread.h
@@ -280,15 +280,30 @@ WINPTHREAD_MUTEX_DECL int 
pthread_mutex_timedlock(pthread_mutex_t *_M, const str
 #define pthread_mutex_setprioceiling(M, P) ENOTSUP
 #define pthread_mutex_getprioceiling(M, P) ENOTSUP
 
-/* synchronization objects */
+/**
+ * Spinlock (pthread_spinlock_t) declarations, definitions and functions.
+ *
+ * NOTE: they are not really spinlocks; they are nearly equivalent to stalled
+ *   error-checking mutexes.
+ */
+
 typedef intptr_t pthread_spinlock_t;
+
+#define PTHREAD_SPINLOCK_INITIALIZER ((pthread_spinlock_t)-1)
+
+WINPTHREAD_API int pthread_spin_init(pthread_spinlock_t *, int);
+WINPTHREAD_API int pthread_spin_destroy(pthread_spinlock_t *);
+WINPTHREAD_API int pthread_spin_lock(pthread_spinlock_t *);
+WINPTHREAD_API int pthread_spin_trylock(pthread_spinlock_t *);
+WINPTHREAD_API int pthread_spin_unlock(pthread_spinlock_t *);
+
+/* synchronization objects */
 typedef intptr_t pthread_cond_t;
 typedef intptr_t pthread_rwlock_t;
 typedef void   *pthread_barrier_t;
 
-#define PTHREAD_COND_INITIALIZER             ((pthread_cond_t)-1)
-#define PTHREAD_RWLOCK_INITIALIZER           ((pthread_rwlock_t)-1)
-#define PTHREAD_SPINLOCK_INITIALIZER         ((pthread_spinlock_t)-1)
+#define PTHREAD_COND_INITIALIZER   ((pthread_cond_t)-1)
+#define PTHREAD_RWLOCK_INITIALIZER ((pthread_rwlock_t)-1)
 
 WINPTHREAD_API extern void (**_pthread_key_dest)(void *);
 WINPTHREAD_API int         pthread_key_create(pthread_key_t *key, void (* 
dest)(void *));
@@ -377,13 +392,6 @@ WINPTHREAD_API int 
pthread_barrier_destroy(pthread_barrier_t *b);
 WINPTHREAD_API int pthread_barrier_init(pthread_barrier_t *b, const void 
*attr, unsigned int count);
 WINPTHREAD_API int pthread_barrier_wait(pthread_barrier_t *b);
 
-WINPTHREAD_API int pthread_spin_init(pthread_spinlock_t *l, int pshared);
-WINPTHREAD_API int pthread_spin_destroy(pthread_spinlock_t *l);
-/* No-fair spinlock due to lack of knowledge of thread number.  */
-WINPTHREAD_API int pthread_spin_lock(pthread_spinlock_t *l);
-WINPTHREAD_API int pthread_spin_trylock(pthread_spinlock_t *l);
-WINPTHREAD_API int pthread_spin_unlock(pthread_spinlock_t *l);
-
 WINPTHREAD_API int pthread_attr_init(pthread_attr_t *attr);
 WINPTHREAD_API int pthread_attr_destroy(pthread_attr_t *attr);
 WINPTHREAD_API int pthread_attr_setdetachstate(pthread_attr_t *a, int flag);
-- 
2.55.0.windows.3

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

Reply via email to