I wrote:
> LIU Hao write:
>
>>> + 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.
Oh, I see where I overlooked it; in `*Unlock` functions:
```
case EOWNERDEAD:
mutex->Owner = THREAD_ID_NO_OWNER;
InterlockedCompareExchange (&mutex->State, OwnerDied, Consistent);
ReleaseMutex (mutex->Mutex);
break;
```
There should be `Inconsistent` instead of `Consistent`.
- Kirill Makurin
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public