在 2026-9-26 08:32, Kirill Makurin 写道:
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).
Yes, there's an edge case that the identifier of the dead owner gets reused for the calling thread.This lock-unlock-unlock pattern looks a bit tricky. You can also prevent the race condition by protecting the internal state with a critical section:
pthread_mutex_lock(pthread_mutex_t* m)
{
// ...
EnterCriticalSection(&m->state_cs);
WaitForSingleObject(m->mutex, timeout);
LeaveCriticalSection(&m->state_cs);
}
pthread_mutex_unlock(pthread_mutex_t* m)
{
// ...
EnterCriticalSection(&m->state_cs);
ReleaseMutex(m->mutex);
LeaveCriticalSection(&m->state_cs);
}
--
Best regards,
LIU Hao
OpenPGP_signature.asc
Description: OpenPGP digital signature
_______________________________________________ Mingw-w64-public mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
