在 2026-9-30 11:39, LIU Hao 写道:
在 2026-9-30 11:36, Kirill Makurin 写道:
LIU Hao wrote:

 > These patches look mostly fine, with only a trivial issue:
 >
 > Does `mutex->State` have to be updated with an interlocked function? Looks like that it can only changed
 > while the kernel mutex object has been locked, so it can be updated with 
plain assignment.

The only function that may update it without owning the mutex is `pthread_mutex_consistent` (POSIX does not require calling thread to own the mutex), when it is set to `Inconsistent`. If you think it's ok to use plain assignment elsewhere, I have no objections to that.
Yes, but there's also

       if (likely (*state != Unrecoverable)) {
         *state = Inconsistent;
       }

I'll try changing those to plain assignments.

I have pushed this series of patches with these changes.

The diff is generated by DeepSeek. I have read all the changes and I'm sure these are trivial; and the revised `WinpthreadsRecursiveRobustMutexSetConsistentState()` is more readable.


diff --git a/mingw-w64-libraries/winpthreads/src/mutex.c 
b/mingw-w64-libraries/winpthreads/src/mutex.c
index fa09901c2..eff3ad80d 100644
--- a/mingw-w64-libraries/winpthreads/src/mutex.c
+++ b/mingw-w64-libraries/winpthreads/src/mutex.c
@@ -1206,7 +1206,8 @@ static int WinpthreadsErrorCheckRobustMutexLock 
(WinpthreadsMutex *wMutex, const
         return EDEADLK;
       }

-      if (unlikely (InterlockedCompareExchange (&mutex->State, Inconsistent, 
OwnerDied) == OwnerDied)) {
+      if (unlikely (mutex->State == OwnerDied)) {
+        mutex->State = Inconsistent;
         error_code = EOWNERDEAD;
       }
     }
@@ -1252,7 +1253,8 @@ static int WinpthreadsErrorCheckRobustMutexTryLock 
(WinpthreadsMutex *wMutex, BO
         return EBUSY;
       }

-      if (unlikely (InterlockedCompareExchange (&mutex->State, Inconsistent, 
OwnerDied) == OwnerDied)) {
+      if (unlikely (mutex->State == OwnerDied)) {
+        mutex->State = Inconsistent;
         error_code = EOWNERDEAD;
       }
     }
@@ -1318,7 +1320,7 @@ static int WinpthreadsErrorCheckRobustMutexUnlock 
(WinpthreadsMutex *wMutex) {
      */
     case EOWNERDEAD:
       mutex->Owner = THREAD_ID_NO_OWNER;
-      InterlockedExchange (&mutex->State, OwnerDied);
+      mutex->State = OwnerDied;
       ReleaseMutex (mutex->Mutex);
       return EPERM;
     /**
@@ -1345,7 +1347,9 @@ static int WinpthreadsErrorCheckRobustMutexUnlock 
(WinpthreadsMutex *wMutex) {
    * Set `mutex->State` to `Unrecoverable`, which will signal unblocked threads
    * that state protected by the mutex is unrecoverable.
    */
-  InterlockedCompareExchange (&mutex->State, Unrecoverable, Inconsistent);
+  if (mutex->State == Inconsistent) {
+    mutex->State = Unrecoverable;
+  }

   if (!ReleaseMutex (mutex->Mutex)) {
     return EINVAL;
@@ -1364,10 +1368,11 @@ static int 
WinpthreadsErrorCheckRobustMutexSetConsistentState (WinpthreadsMutex
    *   The mutex object referenced by mutex is not robust or does not protect
    *   an inconsistent state.
    */
-  if (InterlockedCompareExchange (&mutex->State, Consistent, Inconsistent) != 
Inconsistent) {
+  if (mutex->State != Inconsistent) {
     return EINVAL;
   }

+  mutex->State = Consistent;
   return 0;
 }

@@ -1465,7 +1470,8 @@ static int WinpthreadsRecursiveRobustMutexLock 
(WinpthreadsMutex *wMutex, const
       return EOWNERDEAD;
     }

-    if (unlikely (InterlockedCompareExchange (&mutex->State, Inconsistent, 
OwnerDied) == OwnerDied)) {
+    if (unlikely (mutex->State == OwnerDied)) {
+      mutex->State     = Inconsistent;
       mutex->Owner     = threadId;
       mutex->LockCount = 1;
       return EOWNERDEAD;
@@ -1510,7 +1516,8 @@ static int WinpthreadsRecursiveRobustMutexTryLock 
(WinpthreadsMutex *wMutex, BOO
       return EOWNERDEAD;
     }

-    if (unlikely (InterlockedCompareExchange (&mutex->State, Inconsistent, 
OwnerDied) == OwnerDied)) {
+    if (unlikely (mutex->State == OwnerDied)) {
+      mutex->State     = Inconsistent;
       mutex->Owner     = threadId;
       mutex->LockCount = 1;
       return EOWNERDEAD;
@@ -1590,7 +1597,7 @@ static int WinpthreadsRecursiveRobustMutexUnlock 
(WinpthreadsMutex *wMutex) {
      */
     case EOWNERDEAD:
       mutex->Owner = THREAD_ID_NO_OWNER;
-      InterlockedExchange (&mutex->State, OwnerDied);
+      mutex->State = OwnerDied;
       ReleaseMutex (mutex->Mutex);
       return EPERM;
     /**
@@ -1636,7 +1643,9 @@ static int WinpthreadsRecursiveRobustMutexUnlock 
(WinpthreadsMutex *wMutex) {
    * Set `mutex->State` to `Unrecoverable`, which will signal released threads
    * that state protected by the mutex is unrecoverable.
    */
-  InterlockedCompareExchange (&mutex->State, Unrecoverable, Inconsistent);
+  if (mutex->State == Inconsistent) {
+    mutex->State = Unrecoverable;
+  }

   if (!ReleaseMutex (mutex->Mutex)) {
     return EPERM;
@@ -1655,10 +1664,11 @@ static int 
WinpthreadsRecursiveRobustMutexSetConsistentState (WinpthreadsMutex *
    *   The mutex object referenced by mutex is not robust or does not protect
    *   an inconsistent state.
    */
-  if (InterlockedCompareExchange (&mutex->State, Consistent, Inconsistent) != 
Inconsistent) {
+  if (mutex->State != Inconsistent) {
     return EINVAL;
   }

+  mutex->State = Consistent;
   return 0;
 }






--
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