LIU Hao wrote:
> 在 2026-9-25 11:19, Kirill Makurin 写道:
>>> 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`.
>>
>> Would you prefer me to update this or you're ok with keeping it as is?
>
> I'd suggest you use `GetLastError () == ERROR_MUTANT_LIMIT_EXCEEDED` or move
> `DWORD errorCode` out of the
> switch statement. At my first glance this really looked like a syntax error,
> but later I realized that C
> might be more permissive.
Ok, I'll change that.
> Your comments say
>
> /**
> * State protected by the mutex is inconsistent.
> *
> * This state is set by `pthread_mutex_unlock` when it detects that mutex
> * it tries to unlock was abandoned; the mutex becomes unlocked.
> *
> * The next thread to gain ownership of the mutex will upgrade this state
> * to `Inconsistent`.
> */
> OwnerDied,
State `OwnerDied` is just an internal implementation detail.
> I think this state doesn't have to exist. The scenario where a thread calls
> `pthread_mutex_unlock()` and
> the owner no longer exists is exactly that the thread which calls
> `pthread_mutex_unlock()` isn't the
> owner, and under such circumstance `pthread_mutex_unlock()` should fail with
> `EPERM`.
My bad, the "break;" here in `*Unlock* functions is not intentional:
```
case EOWNERDEAD:
mutex->Owner = THREAD_ID_NO_OWNER;
InterlockedCompareExchange (&mutex->State, OwnerDied, Consistent);
ReleaseMutex (mutex->Mutex);
break;
/**
* Some other thread owns the mutex.
*/
case EBUSY:
case ENOTRECOVERABLE:
return EPERM;
default:
return error_code;
```
It is supposed to fall through and return `EPERM` just like you describe above.
I guess this is where some misunderstanding came from?
In the first version of changes unlock logic was:
```
if (InterlockedCompareExchange ((LONG *) &mutex->Owner, (LONG)
THREAD_ID_NO_OWNER, threadId) != threadId) {
return EPERM;
}
switch (InterlockedCompareExchange (&mutex->State, Unrecoverable,
Inconsistent)) {
case Consistent:
/**
* `mutex->State` was set to `Unrecoverable`, which will signal released
* threads that state protected by the mutex is unrecoverable.
*/
case Inconsistent:
if (!ReleaseMutex (mutex->Mutex)) {
return EINVAL;
}
}
```
We were updating `mutex->State` before calling `ReleaseMutex`, so the next
thread which gains ownership could see updated state.
Since thread IDs can be recycled, we cannot solely rely on them, so now we
attempt recursive locking before doing actual unlocking. We cannot first call
`ReleaseMutex` and only then update `mutex->State` and `mutex->Owner` as this
introduces races.
Using recursive locking introduced case when we may obtain ownership for an
abandoned mutex: in such case, we will fail with `EPERM` (calling thread did
not own the mutex), set `mutex->State` to `OwnerDied` and then call
`ReleaseMutex` so another thread can lock it (Windows mutex is no longer
abandoned).
The next thread which locks the mutex will not get WAIT_ABANDONED/EOWNERDEAD
notification, but it will check `mutex->State`, and if it is set to
`OwnerDied`, it will properly report `EOWNERDEAD` condition as well as set
`mutex->State` to `Inconsistent` so that `pthread_mutex_consistent` can be
called on this mutex.
I considered moving checks for `OwnerDied` to `WinpthreadsRobustMutexTimedLock
`, when `WaitForSingleObject` succeeds with `WAIT_OBJECT_0`. However, I think
this makes general logic more complicated and I would like to avoid it.
You may argue that `Inconsistent` alone is sufficient, but I disagree with that:
1. For recursive mutex, state check would occur on recursive lock, causing
recursive lock attempts to reset user-mode (but not kernel mode) lock count to
1 and fail with `EOWERDEAD` (although, calling thread owns it)
2. A thread may call `pthread_mutex_consistent` without getting prior
`EOWNERDEAD` notification which means no threads will be notified about
inconsistent state
I may somewhat agree with 1 (since POSIX is silent about such case), but not 2.
- Kirill Makurin
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public