Hello,

There are multiple mutex-related changes:

0001.txt-0003.txt: minor changes in *-trylock* and *-timedlock mutex tests
0004.txt-0006.txt: changes and tests for `pthread_mutexattr_*` functions
0007.txt-0010.txt: various cleanups

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

The changes in 0004.txt-0006.txt are also preparation steps for implementing 
robust mutexes. I have the implementation ready; I just figured it's better to 
go with these minor changes first.

One more thing. Functions `pthread_mutexattr_{set,get}protocol` [1] caught my 
attention; from my understanding, those are POSIX realtime extensions. I think 
this something that can be implemented in a long run, but I didn't give much 
thought about how to keep track of this information.

- Kirill Makurin

[1] 
https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/pthread_mutexattr_getprotocol.html
From 3ba201588f812e1cd59c709a24a2402b503971c8 Mon Sep 17 00:00:00 2001
From: Kirill Makurin <[email protected]>
Date: Fri, 18 Sep 2026 20:22:32 +0900
Subject: [PATCH 01/10] winpthreads: tests: do not use `pthread_mutex_lock` in
 *-trylock mutex tests

These tests used `pthread_mutex_lock` to initially lock the mutex.
Use `pthread_mutex_trylock` instead.

Signed-off-by: Kirill Makurin <[email protected]>
---
 .../winpthreads/tests/pthread_mutex/default-trylock.c           | 2 +-
 .../winpthreads/tests/pthread_mutex/errorcheck-trylock.c        | 2 +-
 .../winpthreads/tests/pthread_mutex/normal-trylock.c            | 2 +-
 .../winpthreads/tests/pthread_mutex/recursive-trylock.c         | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git 
a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/default-trylock.c 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/default-trylock.c
index d7efbe9e3..a2a1e14af 100644
--- a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/default-trylock.c
+++ b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/default-trylock.c
@@ -54,7 +54,7 @@ int main(void)
   pthread_t thread;
   void *result;
 
-  assert(pthread_mutex_lock(&mutex) == 0);
+  assert(pthread_mutex_trylock(&mutex) == 0);
   assert(pthread_create(&thread, NULL, ThreadA, &mutex) == 0);
   assert(pthread_join(thread, &result) == 0);
   assert(result == &mutex);
diff --git 
a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-trylock.c 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-trylock.c
index 595aadb74..cdcc37673 100644
--- a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-trylock.c
+++ b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-trylock.c
@@ -55,7 +55,7 @@ int main(void)
   pthread_t thread;
   void *result;
 
-  assert(pthread_mutex_lock(&mutex) == 0);
+  assert(pthread_mutex_trylock(&mutex) == 0);
   assert(pthread_create(&thread, NULL, ThreadA, &mutex) == 0);
   assert(pthread_join(thread, &result) == 0);
   assert(result == &mutex);
diff --git 
a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/normal-trylock.c 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/normal-trylock.c
index 49af2f6f1..ef8aefe13 100644
--- a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/normal-trylock.c
+++ b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/normal-trylock.c
@@ -46,7 +46,7 @@ int main(void)
   pthread_t thread;
   void *result;
 
-  assert(pthread_mutex_lock(&mutex) == 0);
+  assert(pthread_mutex_trylock(&mutex) == 0);
   assert(pthread_create(&thread, NULL, ThreadA, &mutex) == 0);
   assert(pthread_join(thread, &result) == 0);
   assert(result == &mutex);
diff --git 
a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/recursive-trylock.c 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/recursive-trylock.c
index 5ad4cf978..dbf9fe9a3 100644
--- a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/recursive-trylock.c
+++ b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/recursive-trylock.c
@@ -55,7 +55,7 @@ int main(void)
   pthread_t thread;
   void *result;
 
-  assert(pthread_mutex_lock(&mutex) == 0);
+  assert(pthread_mutex_trylock(&mutex) == 0);
   assert(pthread_create(&thread, NULL, ThreadA, &mutex) == 0);
   assert(pthread_join(thread, &result) == 0);
   assert(result == &mutex);
-- 
2.55.0.windows.3

From a60042b7c1e2964ff72c91053fc059d2bdcf953c Mon Sep 17 00:00:00 2001
From: Kirill Makurin <[email protected]>
Date: Fri, 18 Sep 2026 20:22:32 +0900
Subject: [PATCH 02/10] winpthreads: tests: do not use `pthread_mutex_lock` in
 *-trylock-recursive mutex tests

These tests used `pthread_mutex_lock` to initially lock the mutex.
Use `pthread_mutex_trylock` instead.

Signed-off-by: Kirill Makurin <[email protected]>
---
 .../winpthreads/tests/pthread_mutex/default-trylock-recursive.c | 2 +-
 .../tests/pthread_mutex/errorcheck-trylock-recursive.c          | 2 +-
 .../winpthreads/tests/pthread_mutex/normal-trylock-recursive.c  | 2 +-
 .../tests/pthread_mutex/recursive-trylock-recursive.c           | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git 
a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/default-trylock-recursive.c
 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/default-trylock-recursive.c
index a11ee5b45..3f7aa5884 100644
--- 
a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/default-trylock-recursive.c
+++ 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/default-trylock-recursive.c
@@ -52,7 +52,7 @@ static void *ThreadA(void *arg)
 {
   pthread_mutex_t *mutex = arg;
 
-  assert(pthread_mutex_lock(mutex) == 0);
+  assert(pthread_mutex_trylock(mutex) == 0);
   assert(pthread_mutex_trylock(mutex) == EBUSY);
   assert(pthread_mutex_unlock(mutex) == 0);
   /**
diff --git 
a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-trylock-recursive.c
 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-trylock-recursive.c
index a12df32be..1a2613d53 100644
--- 
a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-trylock-recursive.c
+++ 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-trylock-recursive.c
@@ -52,7 +52,7 @@ static void *ThreadA(void *arg)
 {
   pthread_mutex_t *mutex = arg;
 
-  assert(pthread_mutex_lock(mutex) == 0);
+  assert(pthread_mutex_trylock(mutex) == 0);
   assert(pthread_mutex_trylock(mutex) == EBUSY);
   assert(pthread_mutex_unlock(mutex) == 0);
   assert(pthread_mutex_unlock(mutex) == EPERM);
diff --git 
a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/normal-trylock-recursive.c
 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/normal-trylock-recursive.c
index 1abcc8c37..68ed87b74 100644
--- 
a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/normal-trylock-recursive.c
+++ 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/normal-trylock-recursive.c
@@ -52,7 +52,7 @@ static void *ThreadA(void *arg)
 {
   pthread_mutex_t *mutex = arg;
 
-  assert(pthread_mutex_lock(mutex) == 0);
+  assert(pthread_mutex_trylock(mutex) == 0);
   assert(pthread_mutex_trylock(mutex) == EBUSY);
   assert(pthread_mutex_unlock(mutex) == 0);
   /**
diff --git 
a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/recursive-trylock-recursive.c
 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/recursive-trylock-recursive.c
index 2c0c02be1..b6c891acd 100644
--- 
a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/recursive-trylock-recursive.c
+++ 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/recursive-trylock-recursive.c
@@ -52,7 +52,7 @@ static void *ThreadA(void *arg)
 {
   pthread_mutex_t *mutex = arg;
 
-  assert(pthread_mutex_lock(mutex) == 0);
+  assert(pthread_mutex_trylock(mutex) == 0);
   assert(pthread_mutex_trylock(mutex) == 0);
   assert(pthread_mutex_unlock(mutex) == 0);
   assert(pthread_mutex_unlock(mutex) == 0);
-- 
2.55.0.windows.3

From 16de1fbb0ff5b9f5f253354e027695bc6df06bd7 Mon Sep 17 00:00:00 2001
From: Kirill Makurin <[email protected]>
Date: Fri, 18 Sep 2026 20:22:32 +0900
Subject: [PATCH 03/10] winpthreads: tests: do not use `pthread_mutex_lock` in
 *-timedlock mutex tests

These tests used `pthread_mutex_lock` to initially lock the mutex.
Use `pthread_mutex_timedlock` instead.

Signed-off-by: Kirill Makurin <[email protected]>
---
 .../winpthreads/tests/pthread_mutex/default-timedlock.c      | 5 ++++-
 .../winpthreads/tests/pthread_mutex/errorcheck-timedlock.c   | 5 ++++-
 .../winpthreads/tests/pthread_mutex/normal-timedlock.c       | 5 ++++-
 .../winpthreads/tests/pthread_mutex/recursive-timedlock.c    | 5 ++++-
 4 files changed, 16 insertions(+), 4 deletions(-)

diff --git 
a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/default-timedlock.c 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/default-timedlock.c
index 4840558e3..49e625cf2 100644
--- a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/default-timedlock.c
+++ b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/default-timedlock.c
@@ -48,12 +48,15 @@ static void *ThreadA(void *arg)
 
 int main(void)
 {
+  struct timespec abstime;
   pthread_mutex_t mutex;
   pthread_t thread;
   void *result;
 
   assert(pthread_mutex_init(&mutex, NULL) == 0);
-  assert(pthread_mutex_lock(&mutex) == 0);
+  assert(clock_gettime(CLOCK_REALTIME, &abstime) == 0);
+  abstime.tv_sec += 1;
+  assert(pthread_mutex_timedlock(&mutex, &abstime) == 0);
   assert(pthread_create(&thread, NULL, ThreadA, &mutex) == 0);
   assert(pthread_join(thread, &result) == 0);
   assert(result == &mutex);
diff --git 
a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-timedlock.c 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-timedlock.c
index 2e0c43b94..127661ac1 100644
--- a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-timedlock.c
+++ b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-timedlock.c
@@ -49,6 +49,7 @@ static void *ThreadA(void *arg)
 
 int main(void)
 {
+  struct timespec abstime;
   pthread_mutexattr_t mutexAttr;
   pthread_mutex_t mutex;
   pthread_t thread;
@@ -60,7 +61,9 @@ int main(void)
   assert(pthread_mutexattr_gettype(&mutexAttr, &mutexType) == 0);
   assert(mutexType == PTHREAD_MUTEX_ERRORCHECK);
   assert(pthread_mutex_init(&mutex, &mutexAttr) == 0);
-  assert(pthread_mutex_lock(&mutex) == 0);
+  assert(clock_gettime(CLOCK_REALTIME, &abstime) == 0);
+  abstime.tv_sec += 1;
+  assert(pthread_mutex_timedlock(&mutex, &abstime) == 0);
   assert(pthread_create(&thread, NULL, ThreadA, &mutex) == 0);
   assert(pthread_join(thread, &result) == 0);
   assert(result == &mutex);
diff --git 
a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/normal-timedlock.c 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/normal-timedlock.c
index a2d5eb44f..25ac29cc4 100644
--- a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/normal-timedlock.c
+++ b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/normal-timedlock.c
@@ -48,6 +48,7 @@ static void *ThreadA(void *arg)
 
 int main(void)
 {
+  struct timespec abstime;
   pthread_mutexattr_t mutexAttr;
   pthread_mutex_t mutex;
   pthread_t thread;
@@ -59,7 +60,9 @@ int main(void)
   assert(pthread_mutexattr_gettype(&mutexAttr, &mutexType) == 0);
   assert(mutexType == PTHREAD_MUTEX_NORMAL);
   assert(pthread_mutex_init(&mutex, &mutexAttr) == 0);
-  assert(pthread_mutex_lock(&mutex) == 0);
+  assert(clock_gettime(CLOCK_REALTIME, &abstime) == 0);
+  abstime.tv_sec += 1;
+  assert(pthread_mutex_timedlock(&mutex, &abstime) == 0);
   assert(pthread_create(&thread, NULL, ThreadA, &mutex) == 0);
   assert(pthread_join(thread, &result) == 0);
   assert(result == &mutex);
diff --git 
a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/recursive-timedlock.c 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/recursive-timedlock.c
index 9ec6e727b..0639b88d1 100644
--- a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/recursive-timedlock.c
+++ b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/recursive-timedlock.c
@@ -49,6 +49,7 @@ static void *ThreadA(void *arg)
 
 int main(void)
 {
+  struct timespec abstime;
   pthread_mutexattr_t mutexAttr;
   pthread_mutex_t mutex;
   pthread_t thread;
@@ -60,7 +61,9 @@ int main(void)
   assert(pthread_mutexattr_gettype(&mutexAttr, &mutexType) == 0);
   assert(mutexType == PTHREAD_MUTEX_RECURSIVE);
   assert(pthread_mutex_init(&mutex, &mutexAttr) == 0);
-  assert(pthread_mutex_lock(&mutex) == 0);
+  assert(clock_gettime(CLOCK_REALTIME, &abstime) == 0);
+  abstime.tv_sec += 1;
+  assert(pthread_mutex_timedlock(&mutex, &abstime) == 0);
   assert(pthread_create(&thread, NULL, ThreadA, &mutex) == 0);
   assert(pthread_join(thread, &result) == 0);
   assert(result == &mutex);
-- 
2.55.0.windows.3

From 2e7f43392d8b6d7764ad8b09c213a7f96298c8f9 Mon Sep 17 00:00:00 2001
From: Kirill Makurin <[email protected]>
Date: Fri, 18 Sep 2026 20:22:32 +0900
Subject: [PATCH 04/10] winpthreads: mutex.c: update `pthread_mutexattr_*`
 functions

Interpret `pthread_mutexattr_t` as a union consisting of bit fields.

This change makes it easier to understand how `pthread_mutexattr_*` functions
operate on `pthread_mutexattr_t` objects and provide support for new attributes.

Remove macro `PTHREAD_PRIO_MULT` from pthread.h; this constant is not defined
by POSIX and is only used by `pthread_mutexattr_{set,get}prioceiling` functions.

Signed-off-by: Kirill Makurin <[email protected]>
---
 .../winpthreads/include/pthread.h             |   1 -
 mingw-w64-libraries/winpthreads/src/mutex.c   | 253 +++++++++++++++---
 2 files changed, 209 insertions(+), 45 deletions(-)

diff --git a/mingw-w64-libraries/winpthreads/include/pthread.h 
b/mingw-w64-libraries/winpthreads/include/pthread.h
index 3ab5db741..f9eba441c 100644
--- a/mingw-w64-libraries/winpthreads/include/pthread.h
+++ b/mingw-w64-libraries/winpthreads/include/pthread.h
@@ -128,7 +128,6 @@ extern "C" {
 #define PTHREAD_PRIO_NONE 0
 #define PTHREAD_PRIO_INHERIT 8
 #define PTHREAD_PRIO_PROTECT 16
-#define PTHREAD_PRIO_MULT 32
 #define PTHREAD_PROCESS_SHARED 1
 #define PTHREAD_PROCESS_PRIVATE 0
 
diff --git a/mingw-w64-libraries/winpthreads/src/mutex.c 
b/mingw-w64-libraries/winpthreads/src/mutex.c
index 3b3edf261..93b854f98 100644
--- a/mingw-w64-libraries/winpthreads/src/mutex.c
+++ b/mingw-w64-libraries/winpthreads/src/mutex.c
@@ -89,6 +89,11 @@
  */
 typedef union WinpthreadsMutex WinpthreadsMutex;
 
+/**
+ * Forward declaration; see definition below.
+ */
+typedef union WinpthreadsMutexAttributes WinpthreadsMutexAttributes;
+
 /**
  * Mutex type-specific "init" routine.
  */
@@ -130,6 +135,71 @@ typedef struct {
   FuncMutexUnlock Unlock;
 } WinpthreadsMutexVtable;
 
+/**
+ * Union stored in `pthread_mutexattr_t` objects.
+ */
+union WinpthreadsMutexAttributes {
+  pthread_mutexattr_t Value;
+  struct {
+    /**
+     * Can be one of the following values:
+     *
+     * - PTHREAD_MUTEX_NORMAL
+     * - PTHREAD_MUTEX_ERRORCHECK
+     * - PTHREAD_MUTEX_RECURSIVE
+     */
+    int Type            : 4;
+    /**
+     * Zero for `PTHREAD_PROCESS_PRIVATE` and non-zero for
+     * `PTHREAD_PROCESS_SHARED`.
+     *
+     * Currently, winpthreads only supports `PTHREAD_PROCESS_PRIVATE`.
+     */
+    int Shared          : 1;
+    int Reserved        : 3;
+    /**
+     * Can be one of the following values:
+     *
+     * - PTHREAD_PRIO_NONE
+     * - PTHREAD_PRIO_INHERIT
+     * - PTHREAD_PRIO_PROTECT
+     *
+     * Currently, winpthreads does not implement POSIX realtime extensions.
+     * As such, the only supported value is `PTHREAD_PRIO_NONE`.
+     */
+    int Protocol        : 8;
+    /**
+     * Can be any valid `THREAD_PRIORITY_*` value.
+     *
+     * Only has effect when `Protocol` is `PTHREAD_PRIO_INHERIT` or
+     * `PTHREAD_PRIO_PROTECT`, which are not currently supported.
+     */
+    int PriorityCeiling : 8;
+#define WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC 0x67
+    /**
+     * Must be `WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC`.
+     *
+     * If needed, these bits may be used for future extensions.
+     */
+    int Magic           : 8;
+  } Attributes;
+};
+
+WINPTHREADS_STATIC_ASSERT (sizeof ((WinpthreadsMutexAttributes) {}.Attributes) 
== sizeof (pthread_mutexattr_t), "");
+
+/**
+ * Expands into an expression of type `WinpthreadsMutexAttributes`
+ * corresponding to the default mutex attributes.
+ */
+#define WINPTHREADS_MUTEX_ATTRIBUTES_DEFAULT ((WinpthreadsMutexAttributes) { \
+    .Attributes.Type            = PTHREAD_MUTEX_NORMAL,                      \
+    .Attributes.Shared          = PTHREAD_PROCESS_PRIVATE,                   \
+    .Attributes.Reserved        = 0,                                         \
+    .Attributes.Protocol        = PTHREAD_PRIO_NONE,                         \
+    .Attributes.PriorityCeiling = THREAD_PRIORITY_NORMAL,                    \
+    .Attributes.Magic           = WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC         \
+  })
+
 #define THREAD_ID_NO_OWNER ((DWORD) -1)
 
 /**
@@ -1038,102 +1108,197 @@ int pthread_mutex_destroy(pthread_mutex_t *m)
 
 int pthread_mutexattr_init(pthread_mutexattr_t *a)
 {
-  *a = PTHREAD_MUTEX_NORMAL | (PTHREAD_PROCESS_PRIVATE << 3);
+  *a = WINPTHREADS_MUTEX_ATTRIBUTES_DEFAULT.Value;
   return 0;
 }
 
 int pthread_mutexattr_destroy(pthread_mutexattr_t *a)
 {
-  if (!a)
+  if (a == NULL) {
     return EINVAL;
+  }
 
+  memset (a, 0, sizeof (pthread_mutexattr_t));
   return 0;
 }
 
-int pthread_mutexattr_gettype(const pthread_mutexattr_t *a, int *type)
+int pthread_mutexattr_gettype(const pthread_mutexattr_t *a, int *value)
 {
-  if (!a || !type)
+  if (a == NULL || value == NULL) {
     return EINVAL;
+  }
 
-  *type = *a & 3;
+  const WinpthreadsMutexAttributes *wMutexAttr = (const 
WinpthreadsMutexAttributes *) a;
 
-  return 0;
+  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
+    return EINVAL;
+  }
+
+  switch (wMutexAttr->Attributes.Type) {
+    case PTHREAD_MUTEX_NORMAL:
+    case PTHREAD_MUTEX_ERRORCHECK:
+    case PTHREAD_MUTEX_RECURSIVE:
+      *value = wMutexAttr->Attributes.Type;
+      return 0;
+  }
+
+  return EINVAL;
 }
 
-int pthread_mutexattr_settype(pthread_mutexattr_t *a, int type)
+int pthread_mutexattr_settype(pthread_mutexattr_t *a, int value)
 {
-  if (!a)
+  if (a == NULL) {
     return EINVAL;
+  }
+
+  WinpthreadsMutexAttributes *wMutexAttr = (WinpthreadsMutexAttributes *) a;
 
-  if (type != PTHREAD_MUTEX_NORMAL && type != PTHREAD_MUTEX_RECURSIVE && type 
!= PTHREAD_MUTEX_ERRORCHECK)
+  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
     return EINVAL;
+  }
 
-  *a &= ~3;
-  *a |= type;
+  switch (value) {
+    case PTHREAD_MUTEX_NORMAL:
+    case PTHREAD_MUTEX_ERRORCHECK:
+    case PTHREAD_MUTEX_RECURSIVE:
+      wMutexAttr->Attributes.Type = value;
+      return 0;
+  }
 
-  return 0;
+  return EINVAL;
 }
 
-int pthread_mutexattr_getpshared(const pthread_mutexattr_t *a, int *type)
+int pthread_mutexattr_getpshared(const pthread_mutexattr_t *a, int *value)
 {
-  if (!a || !type)
+  if (a == NULL || value == NULL) {
     return EINVAL;
+  }
 
-  *type = (*a & 4 ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE);
+  const WinpthreadsMutexAttributes *wMutexAttr = (const 
WinpthreadsMutexAttributes *) a;
+
+  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
+    return EINVAL;
+  }
 
+  *value = !!wMutexAttr->Attributes.Shared;
   return 0;
 }
 
-int pthread_mutexattr_setpshared(pthread_mutexattr_t *a, int type)
+int pthread_mutexattr_setpshared(pthread_mutexattr_t *a, int value)
 {
-  int r = 0;
-
-  if (!a)
+  if (a == NULL) {
     return EINVAL;
+  }
 
-  if (type != PTHREAD_PROCESS_SHARED && type != PTHREAD_PROCESS_PRIVATE)
-    return EINVAL;
+  WinpthreadsMutexAttributes *wMutexAttr = (WinpthreadsMutexAttributes *) a;
 
-  if (type == PTHREAD_PROCESS_SHARED) {
-    type = PTHREAD_PROCESS_PRIVATE;
-    r = ENOSYS;
+  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
+    return EINVAL;
   }
 
-  type = (type == PTHREAD_PROCESS_SHARED ? 4 : 0);
-
-  *a &= ~4;
-  *a |= type;
+  switch (value) {
+    case PTHREAD_PROCESS_PRIVATE:
+    case PTHREAD_PROCESS_SHARED:
+      wMutexAttr->Attributes.Shared = value;
+      return 0;
+  }
 
-  return r;
+  return EINVAL;
 }
 
-int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *a, int *type)
+int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *a, int *value)
 {
-  *type = *a & (8 + 16);
-  return 0;
+  if (a == NULL || value == NULL) {
+    return EINVAL;
+  }
+
+  const WinpthreadsMutexAttributes *wMutexAttr = (const 
WinpthreadsMutexAttributes *) a;
+
+  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
+    return EINVAL;
+  }
+
+  switch (wMutexAttr->Attributes.Protocol) {
+    case PTHREAD_PRIO_NONE:
+    case PTHREAD_PRIO_INHERIT:
+    case PTHREAD_PRIO_PROTECT:
+      *value = wMutexAttr->Attributes.Protocol;
+      return 0;
+  }
+
+  return EINVAL;
 }
 
-int pthread_mutexattr_setprotocol(pthread_mutexattr_t *a, int type)
+int pthread_mutexattr_setprotocol(pthread_mutexattr_t *a, int value)
 {
-  if ((type & (8 + 16)) != 8 + 16)
+  if (a == NULL) {
     return EINVAL;
+  }
 
-  *a &= ~(8 + 16);
-  *a |= type;
+  WinpthreadsMutexAttributes *wMutexAttr = (WinpthreadsMutexAttributes *) a;
 
-  return 0;
+  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
+    return EINVAL;
+  }
+
+  switch (value) {
+    case PTHREAD_PRIO_NONE:
+      wMutexAttr->Attributes.Protocol = value;
+      return 0;
+    /**
+     * POSIX realtime extensions are not implemented.
+     *
+     * The pthread_mutexattr_setprotocol() function shall fail if:
+     *
+     * [ENOTSUP]
+     *   The value specified by protocol is an unsupported value.
+     */
+    case PTHREAD_PRIO_INHERIT:
+    case PTHREAD_PRIO_PROTECT:
+      return ENOTSUP;
+  }
+
+  return EINVAL;
 }
 
-int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *a, int *prio)
+int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *a, int *value)
 {
-  *prio = *a / PTHREAD_PRIO_MULT;
-  return 0;
+  if (a == NULL || value == NULL) {
+    return EINVAL;
+  }
+
+  const WinpthreadsMutexAttributes *wMutexAttr = (const 
WinpthreadsMutexAttributes *) a;
+
+  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
+    return EINVAL;
+  }
+
+  int priorityCeiling = wMutexAttr->Attributes.PriorityCeiling;
+
+  if (priorityCeiling >= THREAD_PRIORITY_IDLE && priorityCeiling <= 
THREAD_PRIORITY_TIME_CRITICAL) {
+    *value = priorityCeiling;
+    return 0;
+  }
+
+  return EINVAL;
 }
 
-int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *a, int prio)
+int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *a, int value)
 {
-  *a &= (PTHREAD_PRIO_MULT - 1);
-  *a += prio * PTHREAD_PRIO_MULT;
+  if (a == NULL) {
+    return EINVAL;
+  }
 
-  return 0;
+  WinpthreadsMutexAttributes *wMutexAttr = (WinpthreadsMutexAttributes *) a;
+
+  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
+    return EINVAL;
+  }
+
+  if (value >= THREAD_PRIORITY_IDLE && value <= THREAD_PRIORITY_TIME_CRITICAL) 
{
+    wMutexAttr->Attributes.PriorityCeiling = value;
+    return 0;
+  }
+
+  return EINVAL;
 }
-- 
2.55.0.windows.3

From bc230453d160a4f7fc2af15819f09a2043b2bcb1 Mon Sep 17 00:00:00 2001
From: Kirill Makurin <[email protected]>
Date: Fri, 18 Sep 2026 20:22:32 +0900
Subject: [PATCH 05/10] winpthreads: tests: add tests for `pthread_mutexattr_*`
 functions

Signed-off-by: Kirill Makurin <[email protected]>
---
 .../winpthreads/tests/Makefile.am             |  4 ++
 .../tests/pthread_mutex/attr-prioceiling.c    | 63 ++++++++++++++++++
 .../tests/pthread_mutex/attr-protocol.c       | 65 +++++++++++++++++++
 .../tests/pthread_mutex/attr-pshared.c        | 60 +++++++++++++++++
 .../tests/pthread_mutex/attr-type.c           | 65 +++++++++++++++++++
 5 files changed, 257 insertions(+)
 create mode 100644 
mingw-w64-libraries/winpthreads/tests/pthread_mutex/attr-prioceiling.c
 create mode 100644 
mingw-w64-libraries/winpthreads/tests/pthread_mutex/attr-protocol.c
 create mode 100644 
mingw-w64-libraries/winpthreads/tests/pthread_mutex/attr-pshared.c
 create mode 100644 
mingw-w64-libraries/winpthreads/tests/pthread_mutex/attr-type.c

diff --git a/mingw-w64-libraries/winpthreads/tests/Makefile.am 
b/mingw-w64-libraries/winpthreads/tests/Makefile.am
index 785da971a..80878994c 100644
--- a/mingw-w64-libraries/winpthreads/tests/Makefile.am
+++ b/mingw-w64-libraries/winpthreads/tests/Makefile.am
@@ -110,6 +110,10 @@ check_PROGRAMS =               \
        pthread_misc/stress1       \
        pthread_misc/valid1        \
        pthread_misc/valid2        \
+       pthread_mutex/attr-prioceiling                 \
+       pthread_mutex/attr-protocol                    \
+       pthread_mutex/attr-pshared                     \
+       pthread_mutex/attr-type                        \
        pthread_mutex/default-basic                    \
        pthread_mutex/default-lock-recursive           \
        pthread_mutex/default-static                   \
diff --git 
a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/attr-prioceiling.c 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/attr-prioceiling.c
new file mode 100644
index 000000000..789b87e27
--- /dev/null
+++ b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/attr-prioceiling.c
@@ -0,0 +1,63 @@
+/*
+   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.
+*/
+
+#include "test.h"
+
+/**
+ * Test Summary:
+ *
+ * Test functions `pthread_mutexattr_setprioceiling` and
+ * `pthread_mutexattr_getprioceiling`.
+ */
+
+static void DoTest (int value, int expectedValue, int expectedReturn1, int 
expectedReturn2) {
+  pthread_mutexattr_t mutexAttr;
+  pthread_mutex_t mutex;
+
+  assert (pthread_mutexattr_init (&mutexAttr) == 0);
+  assert (pthread_mutexattr_setprioceiling (&mutexAttr, value) == 
expectedReturn1);
+  assert (pthread_mutexattr_getprioceiling (&mutexAttr, &value) == 0);
+  assert (value == expectedValue);
+  assert (pthread_mutex_init (&mutex, &mutexAttr) == expectedReturn2);
+  if (expectedReturn2 == 0) {
+    assert (pthread_mutex_destroy (&mutex) == 0);
+  }
+  assert (pthread_mutexattr_destroy (&mutexAttr) == 0);
+}
+
+int main (void) {
+  /**
+   * Any valid `THRAED_PRIORITY_*` value can be set for prioceiling attribute.
+   *
+   * Currently, it has no effect; POSIX realtime extensions are not 
implemented.
+   */
+  for (int value = THREAD_PRIORITY_IDLE; value <= 
THREAD_PRIORITY_TIME_CRITICAL; ++value) {
+    DoTest (value, value, 0, 0);
+  }
+
+  /**
+   * Try setting an invalid value; default attribute value must be used.
+   */
+  DoTest (0xFF, THREAD_PRIORITY_NORMAL, EINVAL, 0);
+
+  return 0;
+}
diff --git 
a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/attr-protocol.c 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/attr-protocol.c
new file mode 100644
index 000000000..d1c0b0015
--- /dev/null
+++ b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/attr-protocol.c
@@ -0,0 +1,65 @@
+/*
+   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.
+*/
+
+#include "test.h"
+
+/**
+ * Test Summary:
+ *
+ * Test functions `pthread_mutexattr_setprotocol` and
+ * `pthread_mutexattr_getprotocol`.
+ */
+
+static void DoTest (int value, int expectedValue, int expectedReturn1, int 
expectedReturn2) {
+  pthread_mutexattr_t mutexAttr;
+  pthread_mutex_t mutex;
+
+  assert (pthread_mutexattr_init (&mutexAttr) == 0);
+  assert (pthread_mutexattr_setprotocol (&mutexAttr, value) == 
expectedReturn1);
+  assert (pthread_mutexattr_getprotocol (&mutexAttr, &value) == 0);
+  assert (value == expectedValue);
+  assert (pthread_mutex_init (&mutex, &mutexAttr) == expectedReturn2);
+  if (expectedReturn2 == 0) {
+    assert (pthread_mutex_destroy (&mutex) == 0);
+  }
+  assert (pthread_mutexattr_destroy (&mutexAttr) == 0);
+}
+
+int main (void) {
+  /**
+   * Currently winpthreads only supports `PTHREAD_PRIO_NONE`.
+   */
+  DoTest (PTHREAD_PRIO_NONE, PTHREAD_PRIO_NONE, 0, 0);
+
+  /**
+   * POSIX realtime extensions are not implemented.
+   */
+  DoTest (PTHREAD_PRIO_INHERIT, PTHREAD_PRIO_NONE, ENOTSUP, 0);
+  DoTest (PTHREAD_PRIO_PROTECT, PTHREAD_PRIO_NONE, ENOTSUP, 0);
+
+  /**
+   * Try setting an invalid value; default attribute value must be used.
+   */
+  DoTest (0xFF, PTHREAD_PRIO_NONE, EINVAL, 0);
+
+  return 0;
+}
diff --git a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/attr-pshared.c 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/attr-pshared.c
new file mode 100644
index 000000000..f7937f03e
--- /dev/null
+++ b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/attr-pshared.c
@@ -0,0 +1,60 @@
+/*
+   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.
+*/
+
+#include "test.h"
+
+/**
+ * Test Summary:
+ *
+ * Test functions `pthread_mutexattr_setpshared` and
+ * `pthread_mutexattr_getpshared`.
+ */
+
+static void DoTest (int value, int expectedValue, int expectedReturn1, int 
expectedReturn2) {
+  pthread_mutexattr_t mutexAttr;
+  pthread_mutex_t mutex;
+
+  assert (pthread_mutexattr_init (&mutexAttr) == 0);
+  assert (pthread_mutexattr_setpshared (&mutexAttr, value) == expectedReturn1);
+  assert (pthread_mutexattr_getpshared (&mutexAttr, &value) == 0);
+  assert (value == expectedValue);
+  assert (pthread_mutex_init (&mutex, &mutexAttr) == expectedReturn2);
+  if (expectedReturn2 == 0) {
+    assert (pthread_mutex_destroy (&mutex) == 0);
+  }
+  assert (pthread_mutexattr_destroy (&mutexAttr) == 0);
+}
+
+int main (void) {
+  /**
+   * Currently winpthreads only supports `PTHREAD_PROCESS_PRIVATE`.
+   */
+  DoTest (PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_PRIVATE, 0, 0);
+  DoTest (PTHREAD_PROCESS_SHARED, PTHREAD_PROCESS_SHARED, 0, ENOSYS);
+
+  /**
+   * Try setting an invalid value; default attribute value must be used.
+   */
+  DoTest (0xFF, PTHREAD_PROCESS_PRIVATE, EINVAL, 0);
+
+  return 0;
+}
diff --git a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/attr-type.c 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/attr-type.c
new file mode 100644
index 000000000..16fa713c2
--- /dev/null
+++ b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/attr-type.c
@@ -0,0 +1,65 @@
+/*
+   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.
+*/
+
+#include "test.h"
+
+/**
+ * Test Summary:
+ *
+ * Test functions `pthread_mutexattr_settype` and `pthread_mutexattr_gettype`.
+ */
+
+static void DoTest (int value, int expectedValue, int expectedReturn1, int 
expectedReturn2) {
+  pthread_mutexattr_t mutexAttr;
+  pthread_mutex_t mutex;
+
+  assert (pthread_mutexattr_init (&mutexAttr) == 0);
+  assert (pthread_mutexattr_settype (&mutexAttr, value) == expectedReturn1);
+  assert (pthread_mutexattr_gettype (&mutexAttr, &value) == 0);
+  assert (value == expectedValue);
+  assert (pthread_mutex_init (&mutex, &mutexAttr) == expectedReturn2);
+  if (expectedReturn2 == 0) {
+    assert (pthread_mutex_destroy (&mutex) == 0);
+  }
+  assert (pthread_mutexattr_destroy (&mutexAttr) == 0);
+}
+
+int main (void) {
+  /**
+   * Default mutex type is `PTHREAD_MUTEX_NORMAL`.
+   */
+  DoTest (PTHREAD_MUTEX_DEFAULT, PTHREAD_MUTEX_NORMAL, 0, 0);
+
+  /**
+   * Mutex types defined by POSIX.
+   */
+  DoTest (PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_NORMAL, 0, 0);
+  DoTest (PTHREAD_MUTEX_ERRORCHECK, PTHREAD_MUTEX_ERRORCHECK, 0, 0);
+  DoTest (PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_RECURSIVE, 0, 0);
+
+  /**
+   * Try setting an invalid value; default attribute value must be used.
+   */
+  DoTest (0xFF, PTHREAD_MUTEX_NORMAL, EINVAL, 0);
+
+  return 0;
+}
-- 
2.55.0.windows.3

From 843695b02d0c0315429adf07e2104cd239d8708f Mon Sep 17 00:00:00 2001
From: Kirill Makurin <[email protected]>
Date: Fri, 18 Sep 2026 20:22:32 +0900
Subject: [PATCH 06/10] winpthreads: mutex.c: update `pthread_mutex_init`
 function

Use `pthread_mutexattr_t` value passed to `pthread_mutex_init` directly,
interpreted as `WinpthreadsMutexAttributes` union, instead of retrieving
attributes by calling `pthread_mutexattr_get*` functions; these functions
are still called to validate passed `pthread_mutexattr_t` object.

Simplify `WinpthreadsMutexGet` function by using `WinpthreadsMutexAttributes`
union directly instead of manipulating `pthread_mutexattr_t` object.

Signed-off-by: Kirill Makurin <[email protected]>
---
 mingw-w64-libraries/winpthreads/src/mutex.c | 59 +++++++++++----------
 1 file changed, 30 insertions(+), 29 deletions(-)

diff --git a/mingw-w64-libraries/winpthreads/src/mutex.c 
b/mingw-w64-libraries/winpthreads/src/mutex.c
index 93b854f98..9e8b06471 100644
--- a/mingw-w64-libraries/winpthreads/src/mutex.c
+++ b/mingw-w64-libraries/winpthreads/src/mutex.c
@@ -97,7 +97,7 @@ typedef union WinpthreadsMutexAttributes 
WinpthreadsMutexAttributes;
 /**
  * Mutex type-specific "init" routine.
  */
-typedef int (* FuncMutexInit) (WinpthreadsMutex **, const pthread_mutexattr_t 
*);
+typedef int (* FuncMutexInit) (WinpthreadsMutex **, const 
WinpthreadsMutexAttributes *);
 
 /**
  * Mutex type-specific "destroy" routine.
@@ -424,7 +424,7 @@ static WINPTHREADS_INLINE int WinpthreadsTimedWait (HANDLE 
handle, LONG *lockSta
  * - A thread can unlock mutex owned by another thread.
  */
 
-static int WinpthreadsNormalMutexInit (WinpthreadsMutex **wMutex, const 
pthread_mutexattr_t *attr) {
+static int WinpthreadsNormalMutexInit (WinpthreadsMutex **wMutex, const 
WinpthreadsMutexAttributes *wMutexAttr) {
   WinpthreadsNormalMutex *mutex = malloc (sizeof (WinpthreadsNormalMutex));
 
   /**
@@ -455,7 +455,7 @@ static int WinpthreadsNormalMutexInit (WinpthreadsMutex 
**wMutex, const pthread_
   *wMutex = (WinpthreadsMutex *) mutex;
 
   return 0;
-  UNREFERENCED_PARAMETER (attr);
+  UNREFERENCED_PARAMETER (wMutexAttr);
 }
 
 static void WinpthreadsNormalMutexDestroy (WinpthreadsMutex *wMutex) {
@@ -529,7 +529,7 @@ static const WinpthreadsMutexVtable 
WinpthreadsNormalMutexVtable = {
  * - Attempt to unlock an unlocked mutex fails with EPERM.
  */
 
-static int WinpthreadsErrorCheckMutexInit (WinpthreadsMutex **wMutex, const 
pthread_mutexattr_t *attr) {
+static int WinpthreadsErrorCheckMutexInit (WinpthreadsMutex **wMutex, const 
WinpthreadsMutexAttributes *wMutexAttr) {
   WinpthreadsErrorCheckMutex *mutex = malloc (sizeof 
(WinpthreadsErrorCheckMutex));
 
   /**
@@ -562,7 +562,7 @@ static int WinpthreadsErrorCheckMutexInit (WinpthreadsMutex 
**wMutex, const pthr
   *wMutex = (WinpthreadsMutex *) mutex;
 
   return 0;
-  UNREFERENCED_PARAMETER (attr);
+  UNREFERENCED_PARAMETER (wMutexAttr);
 }
 
 static void WinpthreadsErrorCheckMutexDestroy (WinpthreadsMutex *wMutex) {
@@ -659,7 +659,7 @@ static const WinpthreadsMutexVtable 
WinpthreadsErrorCheckMutexVtable = {
  * - Attempt to unlock an unlocked mutex fails with EPERM.
  */
 
-static int WinpthreadsRecursiveMutexInit (WinpthreadsMutex **wMutex, const 
pthread_mutexattr_t *attr) {
+static int WinpthreadsRecursiveMutexInit (WinpthreadsMutex **wMutex, const 
WinpthreadsMutexAttributes *wMutexAttr) {
   WinpthreadsRecursiveMutex *mutex = malloc (sizeof 
(WinpthreadsRecursiveMutex));
 
   /**
@@ -693,7 +693,7 @@ static int WinpthreadsRecursiveMutexInit (WinpthreadsMutex 
**wMutex, const pthre
   *wMutex = (WinpthreadsMutex *) mutex;
 
   return 0;
-  UNREFERENCED_PARAMETER (attr);
+  UNREFERENCED_PARAMETER (wMutexAttr);
 }
 
 static void WinpthreadsRecursiveMutexDestroy (WinpthreadsMutex *wMutex) {
@@ -852,34 +852,24 @@ static WINPTHREADS_INLINE int 
WinpthreadsMutexGet(pthread_mutex_t *m, Winpthread
    * and use mutex pointed to by `m`.
    */
   if (unlikely (STATIC_MUTEX_INITIALIZER (*wMutex))) {
-    pthread_mutexattr_t mutexAttr;
-    int mutexType;
-
+    WinpthreadsMutexAttributes wMutexAttr = 
WINPTHREADS_MUTEX_ATTRIBUTES_DEFAULT;
     WinpthreadsMutex *volatile initializer = *wMutex;
 
     switch ((pthread_mutex_t)initializer) {
       case PTHREAD_NORMAL_MUTEX_INITIALIZER:
-        mutexType = PTHREAD_MUTEX_NORMAL;
+        wMutexAttr.Attributes.Type = PTHREAD_MUTEX_NORMAL;
         break;
       case PTHREAD_ERRORCHECK_MUTEX_INITIALIZER:
-        mutexType = PTHREAD_MUTEX_ERRORCHECK;
+        wMutexAttr.Attributes.Type = PTHREAD_MUTEX_ERRORCHECK;
         break;
       case PTHREAD_RECURSIVE_MUTEX_INITIALIZER:
-        mutexType = PTHREAD_MUTEX_RECURSIVE;
+        wMutexAttr.Attributes.Type = PTHREAD_MUTEX_RECURSIVE;
         break;
       default:
         UNREACHABLE ();
     }
 
-    int error_code = pthread_mutexattr_init (&mutexAttr);
-
-    if (error_code) {
-      return error_code;
-    }
-
-    pthread_mutexattr_settype (&mutexAttr, mutexType);
-    error_code = pthread_mutex_init ((pthread_mutex_t *)wMutex, &mutexAttr);
-    pthread_mutexattr_destroy (&mutexAttr);
+    int error_code = pthread_mutex_init ((pthread_mutex_t *)wMutex, 
&wMutexAttr.Value);
 
     if (error_code) {
       return error_code;
@@ -1005,10 +995,13 @@ int pthread_mutex_trylock(pthread_mutex_t *m)
 
 int pthread_mutex_init(pthread_mutex_t *m, const pthread_mutexattr_t *a)
 {
-  int pshared = PTHREAD_PROCESS_PRIVATE;
-  int type    = PTHREAD_MUTEX_DEFAULT;
+  WinpthreadsMutexAttributes wMutexAttr = WINPTHREADS_MUTEX_ATTRIBUTES_DEFAULT;
 
   if (a != NULL) {
+    int value;
+
+    wMutexAttr.Value = *a;
+
     /**
      * POSIX:
      *
@@ -1017,22 +1010,30 @@ int pthread_mutex_init(pthread_mutex_t *m, const 
pthread_mutexattr_t *a)
      * attributes object, it is recommended that the function should fail and
      * report an [EINVAL] error.
      */
-    if (pthread_mutexattr_getpshared (a, &pshared) != 0) {
+    if (pthread_mutexattr_gettype (&wMutexAttr.Value, &value) != 0) {
+      return EINVAL;
+    }
+
+    if (pthread_mutexattr_getpshared (&wMutexAttr.Value, &value) != 0) {
+      return EINVAL;
+    }
+
+    if (pthread_mutexattr_getprotocol (&wMutexAttr.Value, &value) != 0) {
       return EINVAL;
     }
 
-    if (pthread_mutexattr_gettype (a, &type) != 0) {
+    if (pthread_mutexattr_getprioceiling (&wMutexAttr.Value, &value) != 0) {
       return EINVAL;
     }
 
-    if (pshared == PTHREAD_PROCESS_SHARED) {
+    if (wMutexAttr.Attributes.Shared) {
       return ENOSYS;
     }
   }
 
   const WinpthreadsMutexVtable *wMutexVtable = NULL;
 
-  switch (type) {
+  switch (wMutexAttr.Attributes.Type) {
     case PTHREAD_MUTEX_NORMAL:
       wMutexVtable = &WinpthreadsNormalMutexVtable;
       break;
@@ -1048,7 +1049,7 @@ int pthread_mutex_init(pthread_mutex_t *m, const 
pthread_mutexattr_t *a)
 
   WinpthreadsMutex *wMutex = NULL;
 
-  int error_code = wMutexVtable->Init (&wMutex, a);
+  int error_code = wMutexVtable->Init (&wMutex, &wMutexAttr);
 
   if (error_code) {
     return error_code;
-- 
2.55.0.windows.3

From 41f12bb7d907461d4d7efbccc9633a6dbbe1f46c Mon Sep 17 00:00:00 2001
From: Kirill Makurin <[email protected]>
Date: Fri, 18 Sep 2026 20:22:33 +0900
Subject: [PATCH 07/10] winpthreads: mutex.c: move `pthread_mutexattr_*`
 functions in their own section within the file

Signed-off-by: Kirill Makurin <[email protected]>
---
 mingw-w64-libraries/winpthreads/src/mutex.c | 398 ++++++++++----------
 1 file changed, 201 insertions(+), 197 deletions(-)

diff --git a/mingw-w64-libraries/winpthreads/src/mutex.c 
b/mingw-w64-libraries/winpthreads/src/mutex.c
index 9e8b06471..4b23cd3e0 100644
--- a/mingw-w64-libraries/winpthreads/src/mutex.c
+++ b/mingw-w64-libraries/winpthreads/src/mutex.c
@@ -813,6 +813,207 @@ static const WinpthreadsMutexVtable 
WinpthreadsRecursiveMutexVtable = {
   .Unlock  = WinpthreadsRecursiveMutexUnlock,
 };
 
+/*******************************************************************************
+ * Implementation for public `pthread_mutexattr_*` functions.
+ */
+
+int pthread_mutexattr_init(pthread_mutexattr_t *a)
+{
+  *a = WINPTHREADS_MUTEX_ATTRIBUTES_DEFAULT.Value;
+  return 0;
+}
+
+int pthread_mutexattr_destroy(pthread_mutexattr_t *a)
+{
+  if (a == NULL) {
+    return EINVAL;
+  }
+
+  memset (a, 0, sizeof (pthread_mutexattr_t));
+  return 0;
+}
+
+int pthread_mutexattr_settype(pthread_mutexattr_t *a, int value)
+{
+  if (a == NULL) {
+    return EINVAL;
+  }
+
+  WinpthreadsMutexAttributes *wMutexAttr = (WinpthreadsMutexAttributes *) a;
+
+  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
+    return EINVAL;
+  }
+
+  switch (value) {
+    case PTHREAD_MUTEX_NORMAL:
+    case PTHREAD_MUTEX_ERRORCHECK:
+    case PTHREAD_MUTEX_RECURSIVE:
+      wMutexAttr->Attributes.Type = value;
+      return 0;
+  }
+
+  return EINVAL;
+}
+
+int pthread_mutexattr_gettype(const pthread_mutexattr_t *a, int *value)
+{
+  if (a == NULL || value == NULL) {
+    return EINVAL;
+  }
+
+  const WinpthreadsMutexAttributes *wMutexAttr = (const 
WinpthreadsMutexAttributes *) a;
+
+  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
+    return EINVAL;
+  }
+
+  switch (wMutexAttr->Attributes.Type) {
+    case PTHREAD_MUTEX_NORMAL:
+    case PTHREAD_MUTEX_ERRORCHECK:
+    case PTHREAD_MUTEX_RECURSIVE:
+      *value = wMutexAttr->Attributes.Type;
+      return 0;
+  }
+
+  return EINVAL;
+}
+
+int pthread_mutexattr_setpshared(pthread_mutexattr_t *a, int value)
+{
+  if (a == NULL) {
+    return EINVAL;
+  }
+
+  WinpthreadsMutexAttributes *wMutexAttr = (WinpthreadsMutexAttributes *) a;
+
+  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
+    return EINVAL;
+  }
+
+  switch (value) {
+    case PTHREAD_PROCESS_PRIVATE:
+    case PTHREAD_PROCESS_SHARED:
+      wMutexAttr->Attributes.Shared = value;
+      return 0;
+  }
+
+  return EINVAL;
+}
+
+int pthread_mutexattr_getpshared(const pthread_mutexattr_t *a, int *value)
+{
+  if (a == NULL || value == NULL) {
+    return EINVAL;
+  }
+
+  const WinpthreadsMutexAttributes *wMutexAttr = (const 
WinpthreadsMutexAttributes *) a;
+
+  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
+    return EINVAL;
+  }
+
+  *value = !!wMutexAttr->Attributes.Shared;
+  return 0;
+}
+
+int pthread_mutexattr_setprotocol(pthread_mutexattr_t *a, int value)
+{
+  if (a == NULL) {
+    return EINVAL;
+  }
+
+  WinpthreadsMutexAttributes *wMutexAttr = (WinpthreadsMutexAttributes *) a;
+
+  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
+    return EINVAL;
+  }
+
+  switch (value) {
+    case PTHREAD_PRIO_NONE:
+      wMutexAttr->Attributes.Protocol = value;
+      return 0;
+    /**
+     * POSIX realtime extensions are not implemented.
+     *
+     * The pthread_mutexattr_setprotocol() function shall fail if:
+     *
+     * [ENOTSUP]
+     *   The value specified by protocol is an unsupported value.
+     */
+    case PTHREAD_PRIO_INHERIT:
+    case PTHREAD_PRIO_PROTECT:
+      return ENOTSUP;
+  }
+
+  return EINVAL;
+}
+
+int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *a, int *value)
+{
+  if (a == NULL || value == NULL) {
+    return EINVAL;
+  }
+
+  const WinpthreadsMutexAttributes *wMutexAttr = (const 
WinpthreadsMutexAttributes *) a;
+
+  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
+    return EINVAL;
+  }
+
+  switch (wMutexAttr->Attributes.Protocol) {
+    case PTHREAD_PRIO_NONE:
+    case PTHREAD_PRIO_INHERIT:
+    case PTHREAD_PRIO_PROTECT:
+      *value = wMutexAttr->Attributes.Protocol;
+      return 0;
+  }
+
+  return EINVAL;
+}
+
+int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *a, int value)
+{
+  if (a == NULL) {
+    return EINVAL;
+  }
+
+  WinpthreadsMutexAttributes *wMutexAttr = (WinpthreadsMutexAttributes *) a;
+
+  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
+    return EINVAL;
+  }
+
+  if (value >= THREAD_PRIORITY_IDLE && value <= THREAD_PRIORITY_TIME_CRITICAL) 
{
+    wMutexAttr->Attributes.PriorityCeiling = value;
+    return 0;
+  }
+
+  return EINVAL;
+}
+
+int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *a, int *value)
+{
+  if (a == NULL || value == NULL) {
+    return EINVAL;
+  }
+
+  const WinpthreadsMutexAttributes *wMutexAttr = (const 
WinpthreadsMutexAttributes *) a;
+
+  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
+    return EINVAL;
+  }
+
+  int priorityCeiling = wMutexAttr->Attributes.PriorityCeiling;
+
+  if (priorityCeiling >= THREAD_PRIORITY_IDLE && priorityCeiling <= 
THREAD_PRIORITY_TIME_CRITICAL) {
+    *value = priorityCeiling;
+    return 0;
+  }
+
+  return EINVAL;
+}
+
 
/*******************************************************************************
  * Implementation for public `pthread_mutex_*` functions.
  */
@@ -1106,200 +1307,3 @@ int pthread_mutex_destroy(pthread_mutex_t *m)
 
   return 0;
 }
-
-int pthread_mutexattr_init(pthread_mutexattr_t *a)
-{
-  *a = WINPTHREADS_MUTEX_ATTRIBUTES_DEFAULT.Value;
-  return 0;
-}
-
-int pthread_mutexattr_destroy(pthread_mutexattr_t *a)
-{
-  if (a == NULL) {
-    return EINVAL;
-  }
-
-  memset (a, 0, sizeof (pthread_mutexattr_t));
-  return 0;
-}
-
-int pthread_mutexattr_gettype(const pthread_mutexattr_t *a, int *value)
-{
-  if (a == NULL || value == NULL) {
-    return EINVAL;
-  }
-
-  const WinpthreadsMutexAttributes *wMutexAttr = (const 
WinpthreadsMutexAttributes *) a;
-
-  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
-    return EINVAL;
-  }
-
-  switch (wMutexAttr->Attributes.Type) {
-    case PTHREAD_MUTEX_NORMAL:
-    case PTHREAD_MUTEX_ERRORCHECK:
-    case PTHREAD_MUTEX_RECURSIVE:
-      *value = wMutexAttr->Attributes.Type;
-      return 0;
-  }
-
-  return EINVAL;
-}
-
-int pthread_mutexattr_settype(pthread_mutexattr_t *a, int value)
-{
-  if (a == NULL) {
-    return EINVAL;
-  }
-
-  WinpthreadsMutexAttributes *wMutexAttr = (WinpthreadsMutexAttributes *) a;
-
-  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
-    return EINVAL;
-  }
-
-  switch (value) {
-    case PTHREAD_MUTEX_NORMAL:
-    case PTHREAD_MUTEX_ERRORCHECK:
-    case PTHREAD_MUTEX_RECURSIVE:
-      wMutexAttr->Attributes.Type = value;
-      return 0;
-  }
-
-  return EINVAL;
-}
-
-int pthread_mutexattr_getpshared(const pthread_mutexattr_t *a, int *value)
-{
-  if (a == NULL || value == NULL) {
-    return EINVAL;
-  }
-
-  const WinpthreadsMutexAttributes *wMutexAttr = (const 
WinpthreadsMutexAttributes *) a;
-
-  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
-    return EINVAL;
-  }
-
-  *value = !!wMutexAttr->Attributes.Shared;
-  return 0;
-}
-
-int pthread_mutexattr_setpshared(pthread_mutexattr_t *a, int value)
-{
-  if (a == NULL) {
-    return EINVAL;
-  }
-
-  WinpthreadsMutexAttributes *wMutexAttr = (WinpthreadsMutexAttributes *) a;
-
-  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
-    return EINVAL;
-  }
-
-  switch (value) {
-    case PTHREAD_PROCESS_PRIVATE:
-    case PTHREAD_PROCESS_SHARED:
-      wMutexAttr->Attributes.Shared = value;
-      return 0;
-  }
-
-  return EINVAL;
-}
-
-int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *a, int *value)
-{
-  if (a == NULL || value == NULL) {
-    return EINVAL;
-  }
-
-  const WinpthreadsMutexAttributes *wMutexAttr = (const 
WinpthreadsMutexAttributes *) a;
-
-  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
-    return EINVAL;
-  }
-
-  switch (wMutexAttr->Attributes.Protocol) {
-    case PTHREAD_PRIO_NONE:
-    case PTHREAD_PRIO_INHERIT:
-    case PTHREAD_PRIO_PROTECT:
-      *value = wMutexAttr->Attributes.Protocol;
-      return 0;
-  }
-
-  return EINVAL;
-}
-
-int pthread_mutexattr_setprotocol(pthread_mutexattr_t *a, int value)
-{
-  if (a == NULL) {
-    return EINVAL;
-  }
-
-  WinpthreadsMutexAttributes *wMutexAttr = (WinpthreadsMutexAttributes *) a;
-
-  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
-    return EINVAL;
-  }
-
-  switch (value) {
-    case PTHREAD_PRIO_NONE:
-      wMutexAttr->Attributes.Protocol = value;
-      return 0;
-    /**
-     * POSIX realtime extensions are not implemented.
-     *
-     * The pthread_mutexattr_setprotocol() function shall fail if:
-     *
-     * [ENOTSUP]
-     *   The value specified by protocol is an unsupported value.
-     */
-    case PTHREAD_PRIO_INHERIT:
-    case PTHREAD_PRIO_PROTECT:
-      return ENOTSUP;
-  }
-
-  return EINVAL;
-}
-
-int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *a, int *value)
-{
-  if (a == NULL || value == NULL) {
-    return EINVAL;
-  }
-
-  const WinpthreadsMutexAttributes *wMutexAttr = (const 
WinpthreadsMutexAttributes *) a;
-
-  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
-    return EINVAL;
-  }
-
-  int priorityCeiling = wMutexAttr->Attributes.PriorityCeiling;
-
-  if (priorityCeiling >= THREAD_PRIORITY_IDLE && priorityCeiling <= 
THREAD_PRIORITY_TIME_CRITICAL) {
-    *value = priorityCeiling;
-    return 0;
-  }
-
-  return EINVAL;
-}
-
-int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *a, int value)
-{
-  if (a == NULL) {
-    return EINVAL;
-  }
-
-  WinpthreadsMutexAttributes *wMutexAttr = (WinpthreadsMutexAttributes *) a;
-
-  if (wMutexAttr->Attributes.Magic != WINPTHREADS_MUTEX_ATTRIBUTES_MAGIC) {
-    return EINVAL;
-  }
-
-  if (value >= THREAD_PRIORITY_IDLE && value <= THREAD_PRIORITY_TIME_CRITICAL) 
{
-    wMutexAttr->Attributes.PriorityCeiling = value;
-    return 0;
-  }
-
-  return EINVAL;
-}
-- 
2.55.0.windows.3

From f64185a6d8bf8b5733774fe5c57ba2e3a1c158c5 Mon Sep 17 00:00:00 2001
From: Kirill Makurin <[email protected]>
Date: Fri, 18 Sep 2026 20:22:33 +0900
Subject: [PATCH 08/10] winpthreads: mutex.c: reorder function definitions

Signed-off-by: Kirill Makurin <[email protected]>
---
 mingw-w64-libraries/winpthreads/src/mutex.c | 200 ++++++++++----------
 1 file changed, 100 insertions(+), 100 deletions(-)

diff --git a/mingw-w64-libraries/winpthreads/src/mutex.c 
b/mingw-w64-libraries/winpthreads/src/mutex.c
index 4b23cd3e0..34a0176bb 100644
--- a/mingw-w64-libraries/winpthreads/src/mutex.c
+++ b/mingw-w64-libraries/winpthreads/src/mutex.c
@@ -1094,106 +1094,6 @@ static WINPTHREADS_INLINE int 
WinpthreadsMutexGet(pthread_mutex_t *m, Winpthread
   return 0;
 }
 
-int pthread_mutex_lock(pthread_mutex_t *m)
-{
-  WinpthreadsMutex *wMutex = NULL;
-
-  int error_code = WinpthreadsMutexGet (m, &wMutex);
-
-  if (error_code) {
-    return error_code;
-  }
-
-  return wMutex->Base.Vtable->Lock (wMutex, NULL);
-}
-
-int pthread_mutex_timedlock64(pthread_mutex_t *m, const struct _timespec64 *ts)
-{
-  WinpthreadsMutex *wMutex = NULL;
-
-  int error_code = WinpthreadsMutexGet (m, &wMutex);
-
-  if (error_code) {
-    return error_code;
-  }
-
-  /**
-   * POSIX:
-   *
-   * Under no circumstance shall the function fail with a timeout if the mutex
-   * can be locked immediately. The validity of the abstime parameter need
-   * not be checked if the mutex can be locked immediately.
-   */
-  error_code = wMutex->Base.Vtable->TryLock (wMutex, FALSE);
-
-  switch (error_code) {
-    /**
-     * Some thread owns the mutex.
-     */
-    case EBUSY:
-      break;
-    /**
-     * Recursive lock count limit has been reached.
-     */
-    case EAGAIN:
-    /**
-     * The calling thread owns the mutex now.
-     */
-    case 0:
-    /**
-     * An unexpected error has occurred.
-     */
-    default:
-      return error_code;
-  }
-
-  /**
-   * The pthread_mutex_timedlock() function shall fail if:
-   *
-   * [EINVAL]
-   *  The process or thread would have blocked, and the abstime parameter
-   *  specified a nanoseconds field value less than zero or greater than
-   *  or equal to 1000 million.
-   */
-  if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) {
-    return EINVAL;
-  }
-
-  return wMutex->Base.Vtable->Lock (wMutex, ts);
-}
-
-int pthread_mutex_timedlock32(pthread_mutex_t *m, const struct _timespec32 *ts)
-{
-  struct _timespec64 ts64 = {.tv_sec = ts->tv_sec, .tv_nsec = ts->tv_nsec};
-  return pthread_mutex_timedlock64 (m, &ts64);
-}
-
-int pthread_mutex_unlock(pthread_mutex_t *m)
-{
-  WinpthreadsMutex *wMutex = NULL;
-
-  int error_code = WinpthreadsMutexGet (m, &wMutex);
-
-  if (error_code) {
-    return error_code;
-  }
-
-  return wMutex->Base.Vtable->Unlock (wMutex);
-}
-
-int pthread_mutex_trylock(pthread_mutex_t *m)
-{
-  WinpthreadsMutex *wMutex = NULL;
-
-  int error_code = WinpthreadsMutexGet (m, &wMutex);
-
-  if (error_code) {
-    return error_code;
-  }
-
-  return wMutex->Base.Vtable->TryLock (wMutex, FALSE);
-}
-
 int pthread_mutex_init(pthread_mutex_t *m, const pthread_mutexattr_t *a)
 {
   WinpthreadsMutexAttributes wMutexAttr = WINPTHREADS_MUTEX_ATTRIBUTES_DEFAULT;
@@ -1307,3 +1207,103 @@ int pthread_mutex_destroy(pthread_mutex_t *m)
 
   return 0;
 }
+
+int pthread_mutex_lock(pthread_mutex_t *m)
+{
+  WinpthreadsMutex *wMutex = NULL;
+
+  int error_code = WinpthreadsMutexGet (m, &wMutex);
+
+  if (error_code) {
+    return error_code;
+  }
+
+  return wMutex->Base.Vtable->Lock (wMutex, NULL);
+}
+
+int pthread_mutex_trylock(pthread_mutex_t *m)
+{
+  WinpthreadsMutex *wMutex = NULL;
+
+  int error_code = WinpthreadsMutexGet (m, &wMutex);
+
+  if (error_code) {
+    return error_code;
+  }
+
+  return wMutex->Base.Vtable->TryLock (wMutex, FALSE);
+}
+
+int pthread_mutex_timedlock64(pthread_mutex_t *m, const struct _timespec64 *ts)
+{
+  WinpthreadsMutex *wMutex = NULL;
+
+  int error_code = WinpthreadsMutexGet (m, &wMutex);
+
+  if (error_code) {
+    return error_code;
+  }
+
+  /**
+   * POSIX:
+   *
+   * Under no circumstance shall the function fail with a timeout if the mutex
+   * can be locked immediately. The validity of the abstime parameter need
+   * not be checked if the mutex can be locked immediately.
+   */
+  error_code = wMutex->Base.Vtable->TryLock (wMutex, FALSE);
+
+  switch (error_code) {
+    /**
+     * Some thread owns the mutex.
+     */
+    case EBUSY:
+      break;
+    /**
+     * Recursive lock count limit has been reached.
+     */
+    case EAGAIN:
+    /**
+     * The calling thread owns the mutex now.
+     */
+    case 0:
+    /**
+     * An unexpected error has occurred.
+     */
+    default:
+      return error_code;
+  }
+
+  /**
+   * The pthread_mutex_timedlock() function shall fail if:
+   *
+   * [EINVAL]
+   *  The process or thread would have blocked, and the abstime parameter
+   *  specified a nanoseconds field value less than zero or greater than
+   *  or equal to 1000 million.
+   */
+  if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) {
+    return EINVAL;
+  }
+
+  return wMutex->Base.Vtable->Lock (wMutex, ts);
+}
+
+int pthread_mutex_timedlock32(pthread_mutex_t *m, const struct _timespec32 *ts)
+{
+  struct _timespec64 ts64 = {.tv_sec = ts->tv_sec, .tv_nsec = ts->tv_nsec};
+  return pthread_mutex_timedlock64 (m, &ts64);
+}
+
+int pthread_mutex_unlock(pthread_mutex_t *m)
+{
+  WinpthreadsMutex *wMutex = NULL;
+
+  int error_code = WinpthreadsMutexGet (m, &wMutex);
+
+  if (error_code) {
+    return error_code;
+  }
+
+  return wMutex->Base.Vtable->Unlock (wMutex);
+}
-- 
2.55.0.windows.3

From 94a60cc00e049fbdbff08902f10ee83f60380c9f Mon Sep 17 00:00:00 2001
From: Kirill Makurin <[email protected]>
Date: Fri, 18 Sep 2026 20:22:33 +0900
Subject: [PATCH 09/10] winpthreads: pthread.h: remove `GENERIC_*INITIALIZER`
 constants

These constants were used to define `PTHREAD_*_INITIALIZER` constants.

Remove `GENERIC_*INITIALIZER` constants and define `PTHREAD_*_INITIALIZER`
constants using appropriate values.

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

diff --git a/mingw-w64-libraries/winpthreads/include/pthread.h 
b/mingw-w64-libraries/winpthreads/include/pthread.h
index f9eba441c..b3b1c9692 100644
--- a/mingw-w64-libraries/winpthreads/include/pthread.h
+++ b/mingw-w64-libraries/winpthreads/include/pthread.h
@@ -235,18 +235,14 @@ typedef void      *pthread_barrier_t;
 #define PTHREAD_MUTEX_ERRORCHECK 1
 #define PTHREAD_MUTEX_RECURSIVE 2
 
-#define GENERIC_INITIALIZER                            -1
-#define GENERIC_ERRORCHECK_INITIALIZER                 -2
-#define GENERIC_RECURSIVE_INITIALIZER                  -3
-#define GENERIC_NORMAL_INITIALIZER                     -1
-#define PTHREAD_MUTEX_INITIALIZER                      
(pthread_mutex_t)GENERIC_INITIALIZER
-#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER            
(pthread_mutex_t)GENERIC_RECURSIVE_INITIALIZER
-#define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER           
(pthread_mutex_t)GENERIC_ERRORCHECK_INITIALIZER
-#define PTHREAD_NORMAL_MUTEX_INITIALIZER               
(pthread_mutex_t)GENERIC_NORMAL_INITIALIZER
-#define PTHREAD_DEFAULT_MUTEX_INITIALIZER              
PTHREAD_NORMAL_MUTEX_INITIALIZER
-#define PTHREAD_COND_INITIALIZER                       
(pthread_cond_t)GENERIC_INITIALIZER
-#define PTHREAD_RWLOCK_INITIALIZER                     
(pthread_rwlock_t)GENERIC_INITIALIZER
-#define PTHREAD_SPINLOCK_INITIALIZER                   
(pthread_spinlock_t)GENERIC_INITIALIZER
+#define PTHREAD_MUTEX_INITIALIZER            (pthread_mutex_t)-1
+#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER  (pthread_mutex_t)-3
+#define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER (pthread_mutex_t)-2
+#define PTHREAD_NORMAL_MUTEX_INITIALIZER     (pthread_mutex_t)-1
+#define PTHREAD_DEFAULT_MUTEX_INITIALIZER    PTHREAD_NORMAL_MUTEX_INITIALIZER
+#define PTHREAD_COND_INITIALIZER             (pthread_cond_t)-1
+#define PTHREAD_RWLOCK_INITIALIZER           (pthread_rwlock_t)-1
+#define PTHREAD_SPINLOCK_INITIALIZER         (pthread_spinlock_t)-1
 
 WINPTHREAD_API extern void (**_pthread_key_dest)(void *);
 WINPTHREAD_API int         pthread_key_create(pthread_key_t *key, void (* 
dest)(void *));
-- 
2.55.0.windows.3

From 88a140d508d5e3c22933e54af2c778d1bd4a7fe7 Mon Sep 17 00:00:00 2001
From: Kirill Makurin <[email protected]>
Date: Fri, 18 Sep 2026 20:22:33 +0900
Subject: [PATCH 10/10] winpthreads: pthread.h: group mutex-related
 declarations together

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

diff --git a/mingw-w64-libraries/winpthreads/include/pthread.h 
b/mingw-w64-libraries/winpthreads/include/pthread.h
index b3b1c9692..1914e6dbb 100644
--- a/mingw-w64-libraries/winpthreads/include/pthread.h
+++ b/mingw-w64-libraries/winpthreads/include/pthread.h
@@ -117,26 +117,12 @@ extern "C" {
 #define PTHREAD_DESTRUCTOR_ITERATIONS 256
 #define PTHREAD_KEYS_MAX (1<<20)
 
-#define PTHREAD_MUTEX_NORMAL 0
-#define PTHREAD_MUTEX_ERRORCHECK 1
-#define PTHREAD_MUTEX_RECURSIVE 2
-#define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
-
-#define PTHREAD_MUTEX_SHARED 1
-#define PTHREAD_MUTEX_PRIVATE 0
-
 #define PTHREAD_PRIO_NONE 0
 #define PTHREAD_PRIO_INHERIT 8
 #define PTHREAD_PRIO_PROTECT 16
 #define PTHREAD_PROCESS_SHARED 1
 #define PTHREAD_PROCESS_PRIVATE 0
 
-#define PTHREAD_MUTEX_FAST_NP          PTHREAD_MUTEX_NORMAL
-#define PTHREAD_MUTEX_TIMED_NP         PTHREAD_MUTEX_FAST_NP
-#define PTHREAD_MUTEX_ADAPTIVE_NP      PTHREAD_MUTEX_FAST_NP
-#define PTHREAD_MUTEX_ERRORCHECK_NP    PTHREAD_MUTEX_ERRORCHECK
-#define PTHREAD_MUTEX_RECURSIVE_NP     PTHREAD_MUTEX_RECURSIVE
-
 WINPTHREAD_API void * pthread_timechange_handler_np(void * dummy);
 WINPTHREAD_API int    pthread_delay32_np (const struct _timespec32 *interval);
 WINPTHREAD_API int    pthread_delay64_np (const struct _timespec64 *interval);
@@ -160,14 +146,11 @@ WINPTHREAD_API int    pthread_set_num_processors_np(int 
n);
 #define pthread_atfork(F1,F2,F3) 0
 
 /* unsupported stuff: */
-#define pthread_mutex_getprioceiling(M, P) ENOTSUP
-#define pthread_mutex_setprioceiling(M, P) ENOTSUP
 #define pthread_getcpuclockid(T, C) ENOTSUP
 #define pthread_attr_getguardsize(A, S) ENOTSUP
 #define pthread_attr_setguardsize(A, S) ENOTSUP
 
 typedef long pthread_once_t;
-typedef unsigned pthread_mutexattr_t;
 typedef unsigned pthread_key_t;
 typedef void *pthread_barrierattr_t;
 typedef int pthread_condattr_t;
@@ -224,22 +207,79 @@ WINPTHREAD_API int pthread_setschedparam(pthread_t 
thread, int pol, const struct
 WINPTHREAD_API int pthread_attr_setschedpolicy (pthread_attr_t *attr, int pol);
 WINPTHREAD_API int pthread_attr_getschedpolicy (const pthread_attr_t *attr, 
int *pol);
 
+/**
+ * Mutex (pthread_mutex_t) declarations, definitons and functions.
+ */
+
+#define PTHREAD_MUTEX_DEFAULT    PTHREAD_MUTEX_NORMAL
+#define PTHREAD_MUTEX_NORMAL     0
+#define PTHREAD_MUTEX_ERRORCHECK 1
+#define PTHREAD_MUTEX_RECURSIVE  2
+
+/**
+ * Implementation-defined mutex types.
+ */
+#define PTHREAD_MUTEX_ADAPTIVE_NP   PTHREAD_MUTEX_NORMAL
+#define PTHREAD_MUTEX_FAST_NP       PTHREAD_MUTEX_NORMAL
+#define PTHREAD_MUTEX_TIMED_NP      PTHREAD_MUTEX_NORMAL
+#define PTHREAD_MUTEX_ERRORCHECK_NP PTHREAD_MUTEX_ERRORCHECK
+#define PTHREAD_MUTEX_RECURSIVE_NP  PTHREAD_MUTEX_RECURSIVE
+
+/**
+ * Not POSIX; pthread-win32 compatibility?
+ */
+#define PTHREAD_MUTEX_PRIVATE 0
+#define PTHREAD_MUTEX_SHARED  1
+
+typedef unsigned pthread_mutexattr_t;
+
+WINPTHREAD_API int pthread_mutexattr_init(pthread_mutexattr_t *);
+WINPTHREAD_API int pthread_mutexattr_destroy(pthread_mutexattr_t *);
+WINPTHREAD_API int pthread_mutexattr_settype(pthread_mutexattr_t *, int);
+WINPTHREAD_API int pthread_mutexattr_gettype(const pthread_mutexattr_t *, int 
*);
+WINPTHREAD_API int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int);
+WINPTHREAD_API int pthread_mutexattr_getpshared(const pthread_mutexattr_t *, 
int *);
+WINPTHREAD_API int pthread_mutexattr_setprotocol(pthread_mutexattr_t *, int);
+WINPTHREAD_API int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *, 
int *);
+WINPTHREAD_API int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *, 
int);
+WINPTHREAD_API int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t 
*, int *);
+
+typedef intptr_t pthread_mutex_t;
+
+#define PTHREAD_MUTEX_INITIALIZER            PTHREAD_DEFAULT_MUTEX_INITIALIZER
+#define PTHREAD_DEFAULT_MUTEX_INITIALIZER    PTHREAD_NORMAL_MUTEX_INITIALIZER
+#define PTHREAD_NORMAL_MUTEX_INITIALIZER     (pthread_mutex_t)-1
+#define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER (pthread_mutex_t)-2
+#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER  (pthread_mutex_t)-3
+
+WINPTHREAD_API int pthread_mutex_init(pthread_mutex_t *, const 
pthread_mutexattr_t *);
+WINPTHREAD_API int pthread_mutex_destroy(pthread_mutex_t *);
+WINPTHREAD_API int pthread_mutex_lock(pthread_mutex_t *);
+WINPTHREAD_API int pthread_mutex_trylock(pthread_mutex_t *);
+WINPTHREAD_API int pthread_mutex_unlock(pthread_mutex_t *);
+WINPTHREAD_API int pthread_mutex_timedlock32(pthread_mutex_t *, const struct 
_timespec32 *);
+WINPTHREAD_API int pthread_mutex_timedlock64(pthread_mutex_t *, const struct 
_timespec64 *);
+WINPTHREAD_MUTEX_DECL int pthread_mutex_timedlock(pthread_mutex_t *_M, const 
struct timespec *_T)
+{
+#if WINPTHREADS_TIME_BITS == 32
+  return pthread_mutex_timedlock32 (_M, (const struct _timespec32 *) _T);
+#else
+  return pthread_mutex_timedlock64 (_M, (const struct _timespec64 *) _T);
+#endif
+}
+
+/**
+ * Not implemented.
+ */
+#define pthread_mutex_setprioceiling(M, P) ENOTSUP
+#define pthread_mutex_getprioceiling(M, P) ENOTSUP
+
 /* synchronization objects */
 typedef intptr_t pthread_spinlock_t;
-typedef intptr_t pthread_mutex_t;
 typedef intptr_t pthread_cond_t;
 typedef intptr_t pthread_rwlock_t;
 typedef void   *pthread_barrier_t;
 
-#define PTHREAD_MUTEX_NORMAL 0
-#define PTHREAD_MUTEX_ERRORCHECK 1
-#define PTHREAD_MUTEX_RECURSIVE 2
-
-#define PTHREAD_MUTEX_INITIALIZER            (pthread_mutex_t)-1
-#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER  (pthread_mutex_t)-3
-#define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER (pthread_mutex_t)-2
-#define PTHREAD_NORMAL_MUTEX_INITIALIZER     (pthread_mutex_t)-1
-#define PTHREAD_DEFAULT_MUTEX_INITIALIZER    PTHREAD_NORMAL_MUTEX_INITIALIZER
 #define PTHREAD_COND_INITIALIZER             (pthread_cond_t)-1
 #define PTHREAD_RWLOCK_INITIALIZER           (pthread_rwlock_t)-1
 #define PTHREAD_SPINLOCK_INITIALIZER         (pthread_spinlock_t)-1
@@ -327,22 +367,6 @@ WINPTHREAD_COND_DECL int 
pthread_cond_timedwait_relative_np(pthread_cond_t *cv,
 #endif
 }
 
-WINPTHREAD_API int pthread_mutex_lock(pthread_mutex_t *m);
-WINPTHREAD_API int pthread_mutex_timedlock32(pthread_mutex_t *m, const struct 
_timespec32 *ts);
-WINPTHREAD_API int pthread_mutex_timedlock64(pthread_mutex_t *m, const struct 
_timespec64 *ts);
-WINPTHREAD_MUTEX_DECL int pthread_mutex_timedlock(pthread_mutex_t *m, const 
struct timespec *ts)
-{
-#if WINPTHREADS_TIME_BITS == 32
-  return pthread_mutex_timedlock32 (m, (const struct _timespec32 *) ts);
-#else
-  return pthread_mutex_timedlock64 (m, (const struct _timespec64 *) ts);
-#endif
-}
-WINPTHREAD_API int pthread_mutex_unlock(pthread_mutex_t *m);
-WINPTHREAD_API int pthread_mutex_trylock(pthread_mutex_t *m);
-WINPTHREAD_API int pthread_mutex_init(pthread_mutex_t *m, const 
pthread_mutexattr_t *a);
-WINPTHREAD_API int pthread_mutex_destroy(pthread_mutex_t *m);
-
 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);
@@ -369,16 +393,6 @@ WINPTHREAD_API int 
pthread_attr_setstackaddr(pthread_attr_t *attr, void *stack);
 WINPTHREAD_API int pthread_attr_getstacksize(const pthread_attr_t *attr, 
size_t *size);
 WINPTHREAD_API int pthread_attr_setstacksize(pthread_attr_t *attr, size_t 
size);
 
-WINPTHREAD_API int pthread_mutexattr_init(pthread_mutexattr_t *a);
-WINPTHREAD_API int pthread_mutexattr_destroy(pthread_mutexattr_t *a);
-WINPTHREAD_API int pthread_mutexattr_gettype(const pthread_mutexattr_t *a, int 
*type);
-WINPTHREAD_API int pthread_mutexattr_settype(pthread_mutexattr_t *a, int type);
-WINPTHREAD_API int pthread_mutexattr_getpshared(const pthread_mutexattr_t *a, 
int *type);
-WINPTHREAD_API int pthread_mutexattr_setpshared(pthread_mutexattr_t * a, int 
type);
-WINPTHREAD_API int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *a, 
int *type);
-WINPTHREAD_API int pthread_mutexattr_setprotocol(pthread_mutexattr_t *a, int 
type);
-WINPTHREAD_API int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t 
*a, int * prio);
-WINPTHREAD_API int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *a, 
int prio);
 WINPTHREAD_API int pthread_getconcurrency(void);
 WINPTHREAD_API int pthread_setconcurrency(int new_level);
 
-- 
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