A few updates on this:
We can't really set the event from the the safe part of unlock() (I call
"safe" the part of unlock() before the state reset to UNLOCKED). Setting
the event early is clearly prone to deadlock! To make things safe we can:
1. Add a count for users in the critical part of unlock() (I call
"critical" the part of unlock() after the state reset to UNLOCKED)
2. Add something like hazard pointers: every thread has a slot
indicating if it's running the critical part for a specific mutex.
Then destroy() has to synchronize with threads in unlock(). Turn out we
can't use the main event of the mutex however. What we can do:
1. Spin wait within destroy(). Since we can't fall back to a proper
wait, we have to spin indefinitely. In this case we should
interleave calls to SwitchToThread, Sleep(0), Sleep(1).
2. Use a dedicated event. We can use another event to synchronize
unlock() and destroy(). Fortunately we don't have to allocate this
event upfront; instead the event can be allocated on-the-fly.
If we go with option 2) we can also use the WaitOnAddress emulation that
I developed for pthread_once.
Best Regards!
Luca
On 10/27/2026 6:36 PM, Luca Bacci wrote:
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
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public