mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-30 05:29:41 +01:00
Moved work for isue 532 into it's own branch.
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
|
||||
|
||||
#include "Poco/Mutex_WIN32.h"
|
||||
#include "Poco/Thread.h"
|
||||
#include "Poco/Timestamp.h"
|
||||
|
||||
|
||||
@@ -58,4 +59,97 @@ bool MutexImpl::tryLockImpl(long milliseconds)
|
||||
}
|
||||
|
||||
|
||||
FastMutexImpl::FastMutexImpl()
|
||||
: _lockCount(0)
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
|
||||
|
||||
void FastMutexImpl::lockImpl()
|
||||
{
|
||||
try
|
||||
{
|
||||
EnterCriticalSection(&_cs);
|
||||
++_lockCount;
|
||||
|
||||
if (_lockCount > 1)
|
||||
{
|
||||
// We're trying to go recursive so self-deadlock
|
||||
Thread::current()->join();
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
throw SystemException("cannot lock mutex");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool FastMutexImpl::tryLockImpl()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (TryEnterCriticalSection(&_cs) == 0)
|
||||
return false;
|
||||
|
||||
if (_lockCount > 0)
|
||||
{
|
||||
// We're trying to go recursive
|
||||
LeaveCriticalSection(&_cs);
|
||||
return false;
|
||||
}
|
||||
|
||||
++_lockCount;
|
||||
return true;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
throw SystemException("cannot lock mutex");
|
||||
}
|
||||
|
||||
|
||||
bool FastMutexImpl::tryLockImpl(long milliseconds)
|
||||
{
|
||||
const int sleepMillis = 5;
|
||||
Timestamp now;
|
||||
Timestamp::TimeDiff diff(Timestamp::TimeDiff(milliseconds)*1000);
|
||||
|
||||
do
|
||||
{
|
||||
try
|
||||
{
|
||||
if (TryEnterCriticalSection(&_cs) == TRUE)
|
||||
{
|
||||
if (_lockCount == 0)
|
||||
{
|
||||
++_lockCount;
|
||||
return true;
|
||||
}
|
||||
|
||||
// We're trying to go recursive
|
||||
LeaveCriticalSection(&_cs);
|
||||
Sleep(milliseconds);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
throw SystemException("cannot lock mutex");
|
||||
}
|
||||
Sleep(sleepMillis);
|
||||
} while (!now.isElapsed(diff));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
||||
Reference in New Issue
Block a user