在 2026-9-11 18:41, Kirill Makurin 写道:
Hello,

This another version of mutex changes.
GitHub CI run: https://github.com/maiddaisuki/mingw-w64/actions/runs/34578970906 <https://github.com/ maiddaisuki/mingw-w64/actions/runs/34578970906>

Hopefully, this is the final revision. As I have already mentioned, after these changes I want to implement POSIX robust mutexes. The rest of the message is a few details on how I plan to implement them.

I'd like to suggest these changes:

diff --git a/mingw-w64-libraries/winpthreads/src/mutex.c 
b/mingw-w64-libraries/winpthreads/src/mutex.c
index a2169efa0..f8cf652f5 100644
--- a/mingw-w64-libraries/winpthreads/src/mutex.c
+++ b/mingw-w64-libraries/winpthreads/src/mutex.c
@@ -239,7 +239,7 @@ typedef struct {
    * once lock count reaches zero, the owning thread releases the ownership of
    * the mutex.
    */
-  unsigned __int64 LockCount;
+  unsigned int LockCount;
 } WinpthreadsRecursiveMutex;

WINPTHREADS_STATIC_ASSERT (offsetof (WinpthreadsMutexBase, Vtable) == offsetof (WinpthreadsRecursiveMutex, Base.Vtable), "");
@@ -367,7 +367,7 @@ static int WinpthreadsNormalMutexInit (WinpthreadsMutex 
**wMutex, const pthread_
     return ENOMEM;
   }

-  mutex->Event = CreateEventW (NULL, FALSE, FALSE, NULL);
+  mutex->Event = CreateEventA (NULL, FALSE, FALSE, NULL);

   /**
    * The pthread_mutex_init() function shall fail if:
@@ -472,7 +472,7 @@ static int WinpthreadsErrorCheckMutexInit (WinpthreadsMutex 
**wMutex, const pthr
     return ENOMEM;
   }

-  mutex->Event = CreateEventW (NULL, FALSE, FALSE, NULL);
+  mutex->Event = CreateEventA (NULL, FALSE, FALSE, NULL);

   /**
    * The pthread_mutex_init() function shall fail if:
@@ -602,7 +602,7 @@ static int WinpthreadsRecursiveMutexInit (WinpthreadsMutex 
**wMutex, const pthre
     return ENOMEM;
   }

-  mutex->Event = CreateEventW (NULL, FALSE, FALSE, NULL);
+  mutex->Event = CreateEventA (NULL, FALSE, FALSE, NULL);

   /**
    * The pthread_mutex_init() function shall fail if:
@@ -646,7 +646,7 @@ static int WinpthreadsRecursiveMutexLock (WinpthreadsMutex 
*wMutex, const struct
    * If calling thread already owns the mutex, simply increment the lock count.
    */
   if (mutex->Owner == threadId) {
-    if (unlikely (mutex->LockCount == _UI64_MAX)) {
+    if (unlikely (mutex->LockCount == UINT_MAX)) {
       return EAGAIN;
     }

@@ -668,8 +668,8 @@ static int WinpthreadsRecursiveMutexLock (WinpthreadsMutex 
*wMutex, const struct
   }

 done:
-  mutex->Owner      = threadId;
-  mutex->LockCount += 1;
+  mutex->Owner = threadId;
+  mutex->LockCount = 1;

   return 0;
 }
@@ -687,7 +687,7 @@ static int WinpthreadsRecursiveMutexTryLock 
(WinpthreadsMutex *wMutex, BOOL excl
       return EBUSY;
     }

-    if (unlikely (mutex->LockCount == _UI64_MAX)) {
+    if (unlikely (mutex->LockCount == UINT_MAX)) {
       return EAGAIN;
     }

@@ -699,8 +699,8 @@ static int WinpthreadsRecursiveMutexTryLock 
(WinpthreadsMutex *wMutex, BOOL excl
     return EBUSY;
   }

-  mutex->Owner      = threadId;
-  mutex->LockCount += 1;
+  mutex->Owner = threadId;
+  mutex->LockCount = 1;

   return 0;
 }
diff --git a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-lock-recursive.c b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-lock-recursive.c
index 3ead86aea..4d97a50f6 100644
--- 
a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-lock-recursive.c
+++ 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-lock-recursive.c
@@ -38,7 +38,7 @@
  * Thread A locks L.
  *
  * Thread A attempts to lock L again; the call to `pthread_mutex_lock` must
- * fail with EBUSY.
+ * fail with EDEADLK.
  *
  * Thread A unlocks L.
  *
diff --git a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-static-lock-recursive.c b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-static-lock-recursive.c
index ebfbb8be5..b0c70aff5 100644
--- 
a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-static-lock-recursive.c
+++ 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-static-lock-recursive.c
@@ -39,7 +39,7 @@
  * Thread A locks L.
  *
  * Thread A attempts to lock L again; the call to `pthread_mutex_lock` must
- * fail with EBUSY.
+ * fail with EDEADLK.
  *
  * Thread A unlocks L.
  *

First, `LockCount` doesn't have to be 64-bit which would introduce some complexity for 32-bit targets; and when using a 64-bit value, it's unnecessary to check for overflows which could take hundreds of years.

And, `CreateEventW()` fails on Windows 9x. Since you are not passing a name, it can be equivalently replaced with `CreateEventA()`.



--
Best regards,
LIU Hao

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

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

Reply via email to