在 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.


Below is a direct quote from Claude Opus 5.5:

   1. winpthreads: unlock/destroy race in mutex and spinlock (eddded910, 
85e9f2ffd, c5520f6cd)

   These three commits create the event in the signaled state and make destroy 
wait on the event
   before closing it. Compared with the version reviewed on 10-17, the two 
mutex commits change only
   wording, and destroy now calls _pthread_wait_for_single_object(..., 
INFINITE). With INFINITE that
   function just calls WaitForSingleObject(), so it is not a cancellation point.

   Issue: a race is still left. It affects all three stalled mutex types 
(normal, errorcheck,
   recursive) and the spinlock. This interleaving of a main thread M and a 
waiter thread W triggers
   it:

     1. M locks; State = Locked.
     2. W exchanges State to LockedWithBlocking (old value Locked) and is about 
to wait.
     3. M unlocks: it exchanges State to Unlocked; its SetEvent() has not run 
yet.
     4. W's wait returns at once, consuming the token the event was created 
with.
     5. W exchanges State again, sees the old value Unlocked, and so owns the 
lock (State is still
   LockedWithBlocking).
     6. M's SetEvent() runs only now. Nobody consumes this token, so it stays 
on the event.
     7. W sets done = 1 and unlocks: it exchanges State to Unlocked; its 
SetEvent() has not run yet.
     8. M's trylock succeeds and sees done. M unlocks and calls destroy. The 
wait in destroy gets the
   token left over from step 6 at once, then destroy calls CloseHandle() and 
free().
     9. W calls SetEvent(mutex->Event). It reads freed memory and uses a closed 
handle, so it returns
   EINVAL. If the handle value has been reused, it signals an unrelated object.

   At step 8 the lock is released and no thread is trying to lock it, so M's 
destroy is valid POSIX
   usage. The event is left signaled after every contention period, not only 
after init, so the race
   can happen during any contention period, not just the first.

   I wrote two probes (spinrace.c and mutexrace.c in 
C:\Windows\Temp\wp17-probe\) and ran 200,000
   iterations per configuration. Each number below is how many times unlock 
returned EINVAL:

     upstream/master:
       spinlock 13902; mutex normal 17626, errorcheck 3247, recursive 3417
     HEAD:
       spinlock 23 and 16 (two runs); mutex normal 8, errorcheck 10, recursive 
16
     HEAD, initial token consumed right after init (--drain, control):
       spinlock 0; mutex normal 0, errorcheck 0, recursive 0

   The control dropping to zero confirms that the cause is the interleaving 
above.

   Suggested fix (a spinlock prototype passed 3 x 200,000 iterations with 0 
failures):

     - Create the event non-signaled again.
     - Unlock fast path: InterlockedCompareExchange(&State, Unlocked, Locked). 
This is the last
       access to the lock object.
     - Unlock slow path: InterlockedIncrement(&Waking), then 
InterlockedExchange(&State, Unlocked),
       then SetEvent(), then InterlockedDecrement(&Waking).
     - Destroy no longer waits on the event; it spins with "while (Waking) 
SwitchToThread();" instead.

   Uncontended unlock still does only one atomic operation.

   Nits:

     - The comment at `spinlock.c` line 407 says mutex->Event; it should be 
wSpinlock->Event.
     - The mutex commit message says "CONTENDED", but the code calls this state 
LockedWithBlocking.

   Verification: the clang64 build passes make check 189/189. `mutex.c` and 
`spinlock.c` have no
   warnings with gcc or clang -Wall -Wextra. Running the tests with the ucrt64 
gcc build fails at
   link time (libmingwex is missing __bid_*). That is a problem with this 
machine's toolchain, not
   with these commits.



   I rewrote the winpthreads section above as plain text, with no Markdown. I 
turned the table into
   an indented list so the columns can't break when pasted.




--
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