diff --git a/rtl/objpas/sysutils/sysuthrd.inc b/rtl/objpas/sysutils/sysuthrd.inc index 2e69693080..2ee3ea2169 100644 --- a/rtl/objpas/sysutils/sysuthrd.inc +++ b/rtl/objpas/sysutils/sysuthrd.inc @@ -61,6 +61,7 @@ destructor TMultiReadExclusiveWriteSynchronizer.Destroy; begin InterlockedExchange(fwritelocked,0); RtlEventDestroy(fwritelock); + RtlEventDestroy(fwaitingwriterlock); BasicEventDestroy(freaderqueue); end; @@ -125,7 +126,7 @@ begin { wait until there is no more writer } while InterLockedExchangeAdd(fwritelocked,0)<>0 do begin - { there's a writer busy or ir wanting to start -> wait until it's + { there's a writer busy or wanting to start -> wait until it's finished; a writer may already be blocked in the mean time, so wake it up if we're the last to go to sleep } if InterlockedDecrement(freadercount)=0 then diff --git a/tests/test/units/sysutils/trwsync.pp b/tests/test/units/sysutils/trwsync.pp index 5c233b0a76..50fff33897 100644 --- a/tests/test/units/sysutils/trwsync.pp +++ b/tests/test/units/sysutils/trwsync.pp @@ -111,6 +111,11 @@ begin if (gcount<>w1.localcount+w2.localcount+w3.localcount+w4.localcount) then halt(1); + w1.free; + w2.free; + w3.free; + w4.free; + { now try some mixed readers/writers } gcount:=0; r1:=treadcounter.create; @@ -143,4 +148,15 @@ begin { updating via the readcount must have caused data races } if (gcount>=r1.localcount+r2.localcount+r3.localcount+r4.localcount+r5.localcount+r6.localcount+w1.localcount+w2.localcount) then halt(2); + + r1.free; + r2.free; + r3.free; + r4.free; + r5.free; + r6.free; + w1.free; + w2.free; + + lock.free; end.