在 2026-9-23 11:35, Kirill Makurin 写道:
Hm, you're right. Stored thread ID is used for the following:1. Ownership check in `pthread_mutex_unlock`; using `ReleaseMutex` alone would be sufficient as it fails if calling thread does not own the mutex.2. Avoid recursive locking for error-checking mutexes (fails with `EDEADLK`). 3. Handle lock count in user mode for recursive mutexes.We really only need it for case 2. Is there a way to check whether calling thread owns the mutex without recursively locking it?
Likely no. There's a syscall named `NtQueryMutant` which returns that information, but there's no public Windows API for it.
An alternative approach is that, for a non-recursive robust mutex, there should also be a thread ID field, such that `pthread_mutex_lock()` always calls `WaitForSingleObject()` first to detect a potential dead owner; after the call returns `WAIT_OBJECT_0`, it can use the thread ID field to check whether the call is recursive, and if so, it will release the mutex and return an error.
> Because a Windows Mutex object is recursive itself, it's probably not necessary record this information > in user mode?Yes, we can do this. My idea was that handling lock count in user mode may be a little faster: we avoid extra calls to `WaitForSingleObject` and `ReleaseMutex` on recursive lock/unlock. I'll go with your judgment on this.
For recursive robust mutex, there's no need to keep a user-mode recursion counter, I think. -- 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
