> This looks like no difference from reference counting. The reference count will only have to be incremented in `pthread_mutex_unlock()` if it wants to use the event, and in `pthread_mutex_destroy()`.

The advantage of hazard pointers is that you don't need Interlocked operations at all. Each thread writes atomically (with relaxed memory order) in its thread-private slot. destroy() scans all the threads slots for the presence of `mutex`. Ideally slots are in contiguous memory. destroy() only needs atomic reads (relaxed memory order) and we can use SIMD instructions. Well, either us or the compiler! ;)

That said, I'd go with reference counting as a start. We can always revisit later and introduce hazard pointers.

Thanks!
Luca
On 10/31/2026 11:39 AM, LIU Hao wrote:
在 2026-10-31 13:35, Kirill Makurin via Mingw-w64-public 写道:
On Friday, October 30th, 2026 at 5:48 PM, Luca Bacci <[email protected]> wrote:

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)

Can we just have an extra field which would be 0 when there are no threads
in the "critical" part of any operation and 1 when there are.

Before setting state to `Unlocked`, the thread would `InterlockedIncrement` it; just before returning it would `InterlockedDecrement` it. With this, destroy()
could spin until it becomes zero, and only then close the event handle.

I think the spin duration should be very brief and should only occur when
destroy() is in race with unlock().

This looks like no difference from reference counting. The reference count will only have to be incremented in `pthread_mutex_unlock()` if it wants to use the event, and in `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

Reply via email to