在 2026-9-24 16:21, Kirill Makurin 写道:
0003.txt

diff --git a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/attr-robust.c 
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/attr-robust.c
new file mode 100644
index 000000000..6d0703260
--- /dev/null
+++ b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/attr-robust.c
@@ -0,0 +1,57 @@
+
+  /**
+   * Try setting an invalid value; default attribute value must be used.
+   */
+  DoTest (0xFF, PTHREAD_PROCESS_PRIVATE, EINVAL, 0);

Looks like this should be `PTHREAD_MUTEX_STALLED`.


0004.txt

+  switch (_pthread_wait_for_single_object (mutex, (DWORD) waitTimeout)) {
+    /**
+     * `mutex` was in signaled state (unlocked) or it became signaled before
+     * `waitTimeout` expired.
+     */
+    case WAIT_OBJECT_0:
+      error_code = 0;
+      break;
+    /**
+     * Previous owner of `mutex` terminated without releasing it;
+     * the calling thread owns the mutex now.
+     */
+    case WAIT_ABANDONED:
+      error_code = EOWNERDEAD;
+      break;
+    /**
+     * `mutex` was not signaled (unlocked) before `waitTimeout` expired.
+     */
+    case WAIT_TIMEOUT:
+      error_code = waitUntil == NULL ? EBUSY : ETIMEDOUT;
+      break;
+    case WAIT_FAILED:;
+      DWORD errorCode = GetLastError ();
+
+      /**
+       * Recursive lock count has been exceeded.
+       */
+      if (errorCode == ERROR_MUTANT_LIMIT_EXCEEDED) {
+        error_code = EAGAIN;
+        break;
+      }

There's a superfluous semicolon after `case WAIT_FAILED:`.

The `default` label below jumps pass the initialization of `errorCode`; if this was c++, it would be a compiler error. Perhaps it's not necessary to introduce a local variable here. You can just do `GetLastError () == ERROR_MUTANT_LIMIT_EXCEEDED`.

+
+      /* FALLTHROUGH */
+    default:
+      error_code = EINVAL;
+      break;
+  }
+


+static int WinpthreadsErrorCheckRobustMutexLock (WinpthreadsMutex *wMutex, 
const struct _timespec64 *waitUntil) {
+  WinpthreadsErrorCheckRobustMutex *mutex = &wMutex->ErrorCheckRobustMutex;
+
+  /**
+   * The pthread_mutex_lock() function shall fail if:
+   *
+   * [ENOTRECOVERABLE]
+   *   The state protected by the mutex is not recoverable.
+   */
+  if (unlikely (mutex->State == Unrecoverable)) {
+    return ENOTRECOVERABLE;
+  }
+
+  int error_code = WinpthreadsRobustMutexTimedLock (mutex->Mutex, 
&mutex->State, FALSE, waitUntil);
+
+  /**
+   * Recursive lock count has been exceeded.
+   * Normally, this must never happen.
+   */
+  if (unlikely (error_code == EAGAIN)) {
+    return EDEADLK;
+  }
+
+  if (error_code == 0 || error_code == EOWNERDEAD) {
+    DWORD threadId = GetCurrentThreadId ();
+
+    if (likely (error_code == 0)) {
+      /**
+       * Avoid recursive locking.
+       */
+      if (unlikely (mutex->Owner == threadId)) {
+        ReleaseMutex (mutex->Mutex);
+        return EDEADLK;
+      }
+
+      if (unlikely (InterlockedCompareExchange (&mutex->State, Inconsistent, 
OwnerDied) == OwnerDied)) {
+        error_code = EOWNERDEAD;
+      }

If `WinpthreadsRobustMutexTimedLock()` returns `OwnerDied`, the state should already be `Inconsistent`, so this CAS will never succeed?


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