Skip to content

rt: lock_and_signal fixes #1869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 20, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/rt/sync/lock_and_signal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,18 @@ lock_and_signal::lock_and_signal()
: _holding_thread(INVALID_THREAD)
{
_event = CreateEvent(NULL, FALSE, FALSE, NULL);
InitializeCriticalSection(&_cs);

// 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.
const DWORD SPIN_COUNT = 4000;
CHECKED(!InitializeCriticalSectionAndSpinCount(&_cs, SPIN_COUNT));

// TODO? Consider checking GetProcAddress("InitializeCriticalSectionEx")
// so Windows >= Vista we can use CRITICAL_SECTION_NO_DEBUG_INFO to avoid
// allocating CRITICAL_SECTION debug info that is never released. See:
// http://stackoverflow.com/questions/804848/critical-sections-leaking-memory-on-vista-win2008#889853
}

#else
Expand All @@ -32,15 +43,18 @@ lock_and_signal::lock_and_signal()
#endif

lock_and_signal::~lock_and_signal() {
assert(_holding_thread == INVALID_THREAD);
#if defined(__WIN32__)
CloseHandle(_event);
DeleteCriticalSection(&_cs);
#else
CHECKED(pthread_cond_destroy(&_cond));
CHECKED(pthread_mutex_destroy(&_mutex));
#endif
}

void lock_and_signal::lock() {
assert(!lock_held_by_current_thread());
#if defined(__WIN32__)
EnterCriticalSection(&_cs);
_holding_thread = GetCurrentThreadId();
Expand All @@ -51,6 +65,7 @@ void lock_and_signal::lock() {
}

void lock_and_signal::unlock() {
assert(lock_held_by_current_thread());
_holding_thread = INVALID_THREAD;
#if defined(__WIN32__)
LeaveCriticalSection(&_cs);
Expand All @@ -69,9 +84,11 @@ void lock_and_signal::wait() {
LeaveCriticalSection(&_cs);
WaitForSingleObject(_event, INFINITE);
EnterCriticalSection(&_cs);
assert(_holding_thread == INVALID_THREAD);
_holding_thread = GetCurrentThreadId();
#else
CHECKED(pthread_cond_wait(&_cond, &_mutex));
assert(_holding_thread == INVALID_THREAD);
_holding_thread = pthread_self();
#endif
}
Expand Down