LIU Hao write:

> 在 2026-9-24 16:21, Kirill Makurin 写道:
>> 0003.txt
>> +
>> +  /**
>> +   * 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`.

Yes, it should be. Good catch.

>> 0004.txt
>> +    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:`.

It was intentional:

```
  CC       src/libwinpthread_la-mutex.lo
F:/mingw-w64/mingw-w64-libraries/winpthreads/src/mutex.c:631:7: warning: label 
followed by a declaration is a C23 extension [-Wc23-extensions]
  631 |       DWORD errorCode = GetLastError ();
      |       ^
1 warning generated.
```

Only clang gives a warning here, gcc seems to be totally ok with it.

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

I would normally use `switch`, but in this case it would be a nested `switch` 
which is pretty nasty.

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

`WinpthreadsRobustMutexTimedLock` returns `EOWNERDEAD` when 
`WaitForSingleObject` returns `WAIT_ABANDONED`, in which case it also sets 
`mutex->State` to `Inconsistent`.

OTOH, `*Unlock` sets `mutex->State` to `OwnerDied` when `WaitForSingleObject` 
returns `WAIT_ABANDONED`, just before it releases mutex with `ReleaseMutex`. 
So, after `*Unlock` returns mutex becomes unlocked (it's no longer in abandoned 
state), and `mutex->State` set to `OwnerDied` tells the next thread which gains 
the ownership (`WaitForSingleObject` will return `WAIT_OBJECT_0`, not 
`WAIT_ABANDONED`) that a previous owner of the mutex abandoned it.

- Kirill Makurin

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

Reply via email to