Skip to content

Commit 9f49293

Browse files
committed
rt: Initialize Windows CRITICAL_SECTION with non-zero spin count
If a CRITICAL_SECTION is not initialized with a spin count, it will default to 0, even on multi-processor systems. MSDN suggests using 4000. On single-processor systems, the spin count parameter is ignored and the critical section's spin count defaults to 0. For Windows >= Vista, extra debug info is allocated for CRITICAL_SECTIONs but not released in a timely manner. Consider using InitializeCriticalSectionEx(CRITICAL_SECTION_NO_DEBUG_INFO).
1 parent 159dfd7 commit 9f49293

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/rt/sync/lock_and_signal.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,18 @@ lock_and_signal::lock_and_signal()
1919
: _holding_thread(INVALID_THREAD)
2020
{
2121
_event = CreateEvent(NULL, FALSE, FALSE, NULL);
22-
InitializeCriticalSection(&_cs);
22+
23+
// If a CRITICAL_SECTION is not initialized with a spin count, it will
24+
// default to 0, even on multi-processor systems. MSDN suggests using
25+
// 4000. On single-processor systems, the spin count parameter is ignored
26+
// and the critical section's spin count defaults to 0.
27+
const DWORD SPIN_COUNT = 4000;
28+
CHECKED(!InitializeCriticalSectionAndSpinCount(&_cs, SPIN_COUNT));
29+
30+
// TODO? Consider checking GetProcAddress("InitializeCriticalSectionEx")
31+
// so Windows >= Vista we can use CRITICAL_SECTION_NO_DEBUG_INFO to avoid
32+
// allocating CRITICAL_SECTION debug info that is never released. See:
33+
// http://stackoverflow.com/questions/804848/critical-sections-leaking-memory-on-vista-win2008#889853
2334
}
2435

2536
#else

0 commit comments

Comments
 (0)