[fpc-devel] Exact details for RTLeventWaitFor and related ?

Martin fpc at mfriebe.de
Sat Aug 22 02:44:15 CEST 2020


I am tracing a race condition in lazCollections TLazThreadedQueue
on win-64

What is supposed to happen in the following case:

2 or more threads are waiting on the some event
   RTLeventWaitFor(UniqueEvent)

1 thread sets it
   RTLeventSetEvent(UniqueEvent)

I expect that wakes up exactly ONE thread (never mind wich)?
Or ALL?

If no one resets the event (NO call to RTLeventResetEvent), then that 
does not change anything? It is still just the one thread that got woken up?

Thanks for any feedback
Martin


p.s.
Background

TLazThreadedQueue.PopItem works like this

EnterCritical
   If item avail then return item
ExitCritical
    // race condition between those lines.
RTLeventWaitFor(itemAdded)
EnterCritical
   If item avail then return
ExitCritical

If 2 or more reading (popping) threads are both at the indicated location,
and if during this time 2 or more items are added
then there is a problem (I believe)

adding (pushing) an item does
   RTLeventSet(itemAdded)
adding 2 items (with no pop in between) calls Set twice
    => 
https://lazarus-ccr.sourceforge.io/docs/rtl/system/rtleventsetevent.html
     It is unclear if the 2nd RTLeventSet has any effect ?


According to my tests:
- I "sleep()" 2 listeners at the critical line, so they both will 
WaitFor after the sleep
- while they sleep I add 2 items (2 calls to RTLeventSet, no one 
listening yet)
- Both start RTLeventWaitFor
   => only one listener wakes up.
Is that the expected behaviour?



More information about the fpc-devel mailing list