Skip to content

Alternative implementation of TMonitor for Windows Vista+.

Rika requested to merge runewalsh/source:wvv into main

Implement TMonitor with condition variables and slim R/W locks.

Bad point is that I’m not particularly sure if my solution is even correct.

Good point is that my monitors are 2× smaller by themselves, and also uhm...

CRITICAL_SECTION is implemented by creating an event (LockSemaphore field) on first thread contention. So, once contention happened, the event lingers for the rest of the CRITICAL_SECTION life.

SRWLOCK is implemented with a lockfree (and maybe even cross-process if I got it right) hash table on behalf of the OS that maps addresses to a tiny amount of kernel objects.

Knowing that, the following artificial example can be built: Monitor.pas. It shows some immediate savings in Pascal heap (80→50 Mb on x86-64, 50→25 on i386), but then attempts to cause thread contention which makes CRITICAL_SECTIONs create their events and has no effect on SRWLOCKs, so actual savings become more like 130→50 Mb (x86-64), 70→25 (i386).

Merge request reports