trunk/branch integration: ScoppedRWLock

This commit is contained in:
Marian Krivos 2011-08-22 17:45:27 +00:00
parent 190dfbfde4
commit 8b5b500c0a
2 changed files with 44 additions and 15 deletions

View File

@ -38,7 +38,13 @@
#if defined(POCO_OS_FAMILY_WINDOWS)
#if defined(_WIN32_WCE)
#include "RWLock_WINCE.cpp"
#else
#include "RWLock_WIN32.cpp"
#endif
#elif defined(POCO_VXWORKS)
#include "RWLock_VX.cpp"
#else
#include "RWLock_POSIX.cpp"
#endif

View File

@ -114,22 +114,22 @@ void RWLockImpl::readLockImpl()
bool RWLockImpl::tryReadLockImpl()
{
HANDLE h[2];
h[0] = _mutex;
h[1] = _readEvent;
switch (WaitForMultipleObjects(2, h, TRUE, 1))
for (;;)
{
case WAIT_OBJECT_0:
case WAIT_OBJECT_0 + 1:
++_readers;
ResetEvent(_writeEvent);
ReleaseMutex(_mutex);
poco_assert_dbg(_writers == 0);
return true;
case WAIT_TIMEOUT:
return false;
default:
throw SystemException("cannot lock reader/writer lock");
if (_writers != 0 || _writersWaiting != 0)
return false;
DWORD result = tryReadLockOnce();
switch (result)
{
case WAIT_OBJECT_0:
case WAIT_OBJECT_0 + 1:
return true;
case WAIT_TIMEOUT:
continue; // try again
default:
throw SystemException("cannot lock reader/writer lock");
}
}
}
@ -203,4 +203,27 @@ void RWLockImpl::unlockImpl()
}
DWORD RWLockImpl::tryReadLockOnce()
{
HANDLE h[2];
h[0] = _mutex;
h[1] = _readEvent;
DWORD result = WaitForMultipleObjects(2, h, TRUE, 1);
switch (result)
{
case WAIT_OBJECT_0:
case WAIT_OBJECT_0 + 1:
++_readers;
ResetEvent(_writeEvent);
ReleaseMutex(_mutex);
poco_assert_dbg(_writers == 0);
return result;
case WAIT_TIMEOUT:
return result;
default:
throw SystemException("cannot lock reader/writer lock");
}
}
} // namespace Poco