Merge branch 'issue_532' into issue_532-3 with edits

This commit is contained in:
martin-osborne
2014-10-30 11:10:39 +00:00
36 changed files with 499 additions and 67 deletions

View File

@@ -15,13 +15,16 @@
#include "Poco/Mutex_WIN32.h"
#include "Poco/Thread.h"
#include "Poco/Timestamp.h"
namespace Poco {
MutexImpl::MutexImpl()
MutexImpl::MutexImpl(bool recursive)
: _lockCount(0)
, _recursive(recursive)
{
// the fct has a boolean return value under WInnNt/2000/XP but not on Win98
// the return only checks if the input address of &_cs was valid, so it is safe to omit it
@@ -35,7 +38,87 @@ MutexImpl::~MutexImpl()
}
void MutexImpl::lockImpl()
{
try
{
EnterCriticalSection(&_cs);
++_lockCount;
if (!_recursive && _lockCount > 1)
{
// We're trying to go recursive so self-deadlock
Thread::current()->join();
}
}
catch (...)
{
throw SystemException("cannot lock mutex");
}
}
bool MutexImpl::tryLockImpl()
{
try
{
if (TryEnterCriticalSection(&_cs) == 0)
return false;
if (!_recursive && _lockCount > 0)
{
LeaveCriticalSection(&_cs);
return false;
}
++_lockCount;
return true;
}
catch (...)
{
}
throw SystemException("cannot lock mutex");
}
bool MutexImpl::tryLockImpl(long milliseconds)
{
const int sleepMillis = 5;
Timestamp now;
Timestamp::TimeDiff diff(Timestamp::TimeDiff(milliseconds)*1000);
do
{
try
{
if (tryLockImpl())
return true;
}
catch (...)
{
throw SystemException("cannot lock mutex");
}
Sleep(sleepMillis);
} while (!now.isElapsed(diff));
return false;
}
FastMutexImpl::FastMutexImpl()
{
// the fct has a boolean return value under WInnNt/2000/XP but not on Win98
// the return only checks if the input address of &_cs was valid, so it is safe to omit it
InitializeCriticalSectionAndSpinCount(&_cs, 4000);
}
FastMutexImpl::~FastMutexImpl()
{
DeleteCriticalSection(&_cs);
}
bool FastMutexImpl::tryLockImpl(long milliseconds)
{
const int sleepMillis = 5;
Timestamp now;