在 2026-5-14 22:19, Kirill Makurin 写道:
I attached updated version of 0004.txt (applies on top of 0003.txt in original 
message) which stores thread ID directly in `pthread_spinlock_t`. Other patches 
that follow need to be adjusted, so this is by no means final version. I just 
want to know what you think about this version.

The idea looks correct.


The problem with new (attached) implementation is that we have to use InterlockedCompareExchange*. 
We cannot simply use InterlockedExchange* since it would store current thread's ID in 
`pthread_spinlock_t` when it should not. It didn't matter for previous implementation since it 
simply had two states "locked" and "unlocked", without keeping track of lock 
ownership.

The difference is trivial on x86, since `cmpxchg` always gains a write cycle (in case of failure it writes the old value).


Leaving aside "spinning" part of `pthread_spin_lock`, I think my original 
implementation has one important advantage: it uses `pthread_spinlock_t` (which is 
`intptr_t`) as a handle. Currently, winpthreads does not support `PTHREAD_PROCESS_SHARED` 
for interprocess synchronization, but moving towards using handles for `pthread_*_t` 
objects is a direction to make it possible in the long run.

I think that in your new implementation, `PTHREAD_PROCESS_SHARED` works 
implicitly.

If one process (or multiple processes) creates a file mapping and initialize a spinlock on it, then creates multiple views of it, then every view references the spinlock at different virtual addresses - but that still works, because no code depends on the address of the spinlock itself.


Also note that current spinlock implementation was added in 249898d9ae (11 
years ago), and this is probably what broke winpthreads' spinlocks (that they 
no longer keep track of lock ownership). Before that winpthreads had fairly 
complex spinlock implementation.

It looks like that the old implementation was just `CRITICAL_SECTION`, 
allocated on demand.

This way, a spinlock that was initialized with `PTHREAD_PROCESS_SHARED` was only shareable through different views within the same process.

--
Best regards,
LIU Hao

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to