Thanks a lot! Yeah, the fix was incorrect. Actually we can have an
unbounded count of threads preempted in unlock() and about to set the
event while destroy runs.
There are two possible fixes here: either set the event before changing
state to UNLOCKED, or introduce a reference count between unlock() and
destroy(). The former is a bit sub-optimal; SetEvent transitions into
the kernel and back to user-space, which means that it can take quite
some CPU cycles between the signalling and the reset of state to
UNLOCKED. If the waiting thread is fast enough, it can wake and find the
mutex still locked. This is also made worse by the dynamic priority
boost of Windows, which assigns a temporary boost to the awaken thread.
That alone can preempt the thread calling SetEvent (though the
probability lowers as the CPU count increases). For the latter, we can
still use a single Interlocked operation in unlock() when the mutex is
not contended; On contention, however, we need two Interlocked
operations. I guess that's acceptable...
I'll send a new patchset very soon!
Best Regards,
Luca
On 10/22/2026 5:19 AM, LIU Hao wrote:
在 2026-10-22 10:46, LIU Hao 写道:
在 2026-10-22 02:56, Luca Bacci 写道:
See previous commit about pthread_mutex_t.
---
.../winpthreads/src/spinlock.c | 24
++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)
Claude Opus 5.5 still says there's potential deadlock. I think the
reason is that `SetEvent()` is called after the state has been
changed to `Unlocked` so it happens outside the critical section.
Perhaps we need another state for it.
An alternative approach is to make `WinpthreadsMutex`
reference-counted, which will only be needed by
`pthread_mutex_unlock()` and `pthread_mutex_destroy()`.
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public