fixed SF# 1896482: tryReadLock intermittent error

This commit is contained in:
Guenter Obiltschnig
2008-09-17 19:13:50 +00:00
parent cf1d1fa952
commit c83f8e2434
3 changed files with 31 additions and 9 deletions

View File

@@ -1,7 +1,7 @@
// //
// RWLock_WIN32.h // RWLock_WIN32.h
// //
// $Id: //poco/svn/Foundation/include/Poco/RWLock_WIN32.h#2 $ // $Id: //poco/1.3/Foundation/include/Poco/RWLock_WIN32.h#3 $
// //
// Library: Foundation // Library: Foundation
// Package: Threading // Package: Threading
@@ -67,6 +67,7 @@ private:
HANDLE _readEvent; HANDLE _readEvent;
HANDLE _writeEvent; HANDLE _writeEvent;
unsigned _readers; unsigned _readers;
unsigned _writersWaiting;
unsigned _writers; unsigned _writers;
}; };

View File

@@ -1,7 +1,7 @@
// //
// RWLock_WIN32.cpp // RWLock_WIN32.cpp
// //
// $Id: //poco/svn/Foundation/src/RWLock_WIN32.cpp#2 $ // $Id: //poco/1.3/Foundation/src/RWLock_WIN32.cpp#3 $
// //
// Library: Foundation // Library: Foundation
// Package: Threading // Package: Threading
@@ -40,7 +40,7 @@
namespace Poco { namespace Poco {
RWLockImpl::RWLockImpl(): _readers(0), _writers(0) RWLockImpl::RWLockImpl(): _readers(0), _writersWaiting(0), _writers(0)
{ {
_mutex = CreateMutexW(NULL, FALSE, NULL); _mutex = CreateMutexW(NULL, FALSE, NULL);
if (_mutex == NULL) if (_mutex == NULL)
@@ -69,7 +69,7 @@ inline void RWLockImpl::addWriter()
switch (WaitForSingleObject(_mutex, INFINITE)) switch (WaitForSingleObject(_mutex, INFINITE))
{ {
case WAIT_OBJECT_0: case WAIT_OBJECT_0:
if (++_writers == 1) ResetEvent(_readEvent); if (++_writersWaiting == 1) ResetEvent(_readEvent);
ReleaseMutex(_mutex); ReleaseMutex(_mutex);
break; break;
default: default:
@@ -83,7 +83,7 @@ inline void RWLockImpl::removeWriter()
switch (WaitForSingleObject(_mutex, INFINITE)) switch (WaitForSingleObject(_mutex, INFINITE))
{ {
case WAIT_OBJECT_0: case WAIT_OBJECT_0:
if (--_writers == 0) SetEvent(_readEvent); if (--_writersWaiting == 0 && _writers == 0) SetEvent(_readEvent);
ReleaseMutex(_mutex); ReleaseMutex(_mutex);
break; break;
default: default:
@@ -104,6 +104,7 @@ void RWLockImpl::readLockImpl()
++_readers; ++_readers;
ResetEvent(_writeEvent); ResetEvent(_writeEvent);
ReleaseMutex(_mutex); ReleaseMutex(_mutex);
poco_assert_dbg(_writers == 0);
break; break;
default: default:
throw SystemException("cannot lock reader/writer lock"); throw SystemException("cannot lock reader/writer lock");
@@ -123,6 +124,7 @@ bool RWLockImpl::tryReadLockImpl()
++_readers; ++_readers;
ResetEvent(_writeEvent); ResetEvent(_writeEvent);
ReleaseMutex(_mutex); ReleaseMutex(_mutex);
poco_assert_dbg(_writers == 0);
return true; return true;
case WAIT_TIMEOUT: case WAIT_TIMEOUT:
return false; return false;
@@ -142,11 +144,13 @@ void RWLockImpl::writeLockImpl()
{ {
case WAIT_OBJECT_0: case WAIT_OBJECT_0:
case WAIT_OBJECT_0 + 1: case WAIT_OBJECT_0 + 1:
--_writers; --_writersWaiting;
++_readers; ++_readers;
++_writers;
ResetEvent(_readEvent); ResetEvent(_readEvent);
ResetEvent(_writeEvent); ResetEvent(_writeEvent);
ReleaseMutex(_mutex); ReleaseMutex(_mutex);
poco_assert_dbg(_writers == 1);
break; break;
default: default:
removeWriter(); removeWriter();
@@ -165,11 +169,13 @@ bool RWLockImpl::tryWriteLockImpl()
{ {
case WAIT_OBJECT_0: case WAIT_OBJECT_0:
case WAIT_OBJECT_0 + 1: case WAIT_OBJECT_0 + 1:
--_writers; --_writersWaiting;
++_readers; ++_readers;
++_writers;
ResetEvent(_readEvent); ResetEvent(_readEvent);
ResetEvent(_writeEvent); ResetEvent(_writeEvent);
ReleaseMutex(_mutex); ReleaseMutex(_mutex);
poco_assert_dbg(_writers == 1);
return true; return true;
case WAIT_TIMEOUT: case WAIT_TIMEOUT:
removeWriter(); removeWriter();
@@ -186,7 +192,8 @@ void RWLockImpl::unlockImpl()
switch (WaitForSingleObject(_mutex, INFINITE)) switch (WaitForSingleObject(_mutex, INFINITE))
{ {
case WAIT_OBJECT_0: case WAIT_OBJECT_0:
if (_writers == 0) SetEvent(_readEvent); _writers = 0;
if (_writersWaiting == 0) SetEvent(_readEvent);
if (--_readers == 0) SetEvent(_writeEvent); if (--_readers == 0) SetEvent(_writeEvent);
ReleaseMutex(_mutex); ReleaseMutex(_mutex);
break; break;

View File

@@ -1,7 +1,7 @@
// //
// RWLockTest.cpp // RWLockTest.cpp
// //
// $Id: //poco/svn/Foundation/testsuite/src/RWLockTest.cpp#2 $ // $Id: //poco/1.3/Foundation/testsuite/src/RWLockTest.cpp#2 $
// //
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors. // and Contributors.
@@ -60,13 +60,20 @@ public:
for (int k = 0; k < 100; ++k) for (int k = 0; k < 100; ++k)
{ {
if (_counter != lastCount) _ok = false; if (_counter != lastCount) _ok = false;
Thread::yield();
} }
_lock.unlock(); _lock.unlock();
_lock.writeLock(); _lock.writeLock();
for (int k = 0; k < 100; ++k) for (int k = 0; k < 100; ++k)
{
--_counter; --_counter;
Thread::yield();
}
for (int k = 0; k < 100; ++k) for (int k = 0; k < 100; ++k)
{
++_counter; ++_counter;
Thread::yield();
}
++_counter; ++_counter;
if (_counter <= lastCount) _ok = false; if (_counter <= lastCount) _ok = false;
_lock.unlock(); _lock.unlock();
@@ -102,13 +109,20 @@ public:
for (int k = 0; k < 100; ++k) for (int k = 0; k < 100; ++k)
{ {
if (_counter != lastCount) _ok = false; if (_counter != lastCount) _ok = false;
Thread::yield();
} }
_lock.unlock(); _lock.unlock();
while (!_lock.tryWriteLock()) Thread::yield(); while (!_lock.tryWriteLock()) Thread::yield();
for (int k = 0; k < 100; ++k) for (int k = 0; k < 100; ++k)
{
--_counter; --_counter;
Thread::yield();
}
for (int k = 0; k < 100; ++k) for (int k = 0; k < 100; ++k)
{
++_counter; ++_counter;
Thread::yield();
}
++_counter; ++_counter;
if (_counter <= lastCount) _ok = false; if (_counter <= lastCount) _ok = false;
_lock.unlock(); _lock.unlock();