LIU Hao wrote:
> 在 2026-5-21 13:53, Kirill Makurin 写道:
>> I wrote:
>>
>>> I guess winpthreads' `pthread_spinlock_t` could be a mutex in disguise or
>>> something like that, in which case first implementation could be more
>>> flexible in the sense that we can change underlying structure and logic
>>> anytime we want. I can as well update first implementation to not use loops
>>> at all and instead use some Windows object for synchronization.
>>
>> So, here's another implementation for spinlocks. This time, it uses
>> auto-reset events; it is mostly original 0004.txt updated to use auto-reset
>> events instead of loops. Unlike second version, no other changes to original
>> patch series needed. I pushed this version to run through CI[1] and it
>> passes.
>
> Does it make sense to implement a spinlock just as a mutex? like this:
>
> int
> pthread_spin_init (pthread_spinlock_t *lock, int pshared)
> {
> if (pshared == PTHREAD_PROCESS_SHARED) {
> return ENOSYS;
> }
> return pthread_mutex_init((pthread_mutex_t*)lock, NULL);
> }
>
> int
> pthread_spin_destroy (pthread_spinlock_t *lock)
> {
> return pthread_mutex_destroy((pthread_mutex_t*)lock);
> }
>
> int
> pthread_spin_lock (pthread_spinlock_t *lock)
> {
> return pthread_mutex_lock((pthread_mutex_t*)lock);
> }
>
> int
> pthread_spin_trylock (pthread_spinlock_t *lock)
> {
> return pthread_mutex_trylock((pthread_mutex_t*)lock);
> }
>
> int
> pthread_spin_unlock (pthread_spinlock_t *lock)
> {
> return pthread_mutex_unlock((pthread_mutex_t*)lock);
> }
This surely an option; we probably would need to use `PTHREAD_MUTEX_ERRORCHECK`
mutex to avoid deadlocks and cases when thread A may unlock lock held by thread
B. Our implementation of normal (PTHREAD_MUTEX_NORMAL) mutexes does not check
ownership at all, so we would end up with the same problem as we have now.
One major issue is that static initializer for `pthread_spinlock_t` would
correspond to `PTHREAD_NORMAL_MUTEX_INITIALIZER`, but we want error-checking
mutex... I'm not sure how we can properly work around it; changing value of
`PTHREAD_SPINLOCK_INITIALIZER` is not an option as it would be an ABI break...
- Kirill Makurin
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public