Moved work for isue 532 into it's own branch.

This commit is contained in:
martin-osborne 2014-10-19 10:59:08 +01:00
parent 558091d53f
commit 2a90e7de92
28 changed files with 383 additions and 2 deletions

View File

@ -43,7 +43,20 @@ private:
};
typedef MutexImpl FastMutexImpl;
class Foundation_API FastMutexImpl
{
protected:
FastMutexImpl();
~FastMutexImpl();
void lockImpl();
bool tryLockImpl();
bool tryLockImpl(long milliseconds);
void unlockImpl();
private:
CRITICAL_SECTION _cs;
int _lockCount;
};
//
@ -81,6 +94,13 @@ inline void MutexImpl::unlockImpl()
}
inline void FastMutexImpl::unlockImpl()
{
--_lockCount;
LeaveCriticalSection(&_cs);
}
} // namespace Poco

View File

@ -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

View File

@ -24,7 +24,7 @@ objects = ActiveMethodTest ActivityTest ActiveDispatcherTest \
NotificationsTestSuite NullStreamTest NumberFormatterTest \
NumberParserTest PathTest PatternFormatterTest PBKDF2EngineTest RWLockTest \
RandomStreamTest RandomTest RegularExpressionTest SHA1EngineTest \
SemaphoreTest ConditionTest SharedLibraryTest SharedLibraryTestSuite \
SemaphoreTest MutexTest ConditionTest SharedLibraryTest SharedLibraryTestSuite \
SimpleFileChannelTest StopwatchTest \
StreamConverterTest StreamCopierTest StreamTokenizerTest \
StreamsTestSuite StringTest StringTokenizerTest TaskTestSuite TaskTest \

View File

@ -66,6 +66,7 @@ SOURCES="
ManifestTest.cpp
MemoryPoolTest.cpp
MemoryStreamTest.cpp
MutexTest.cpp
NDCTest.cpp
NotificationCenterTest.cpp
NotificationQueueTest.cpp

View File

@ -1074,6 +1074,10 @@
RelativePath=".\src\ConditionTest.cpp"
>
</File>
<File
RelativePath=".\src\MutexTest.cpp"
>
</File>
<File
RelativePath=".\src\RWLockTest.cpp"
>
@ -1122,6 +1126,10 @@
RelativePath=".\src\ConditionTest.h"
>
</File>
<File
RelativePath=".\src\MutexTest.h"
>
</File>
<File
RelativePath=".\src\RWLockTest.h"
>

View File

@ -340,6 +340,7 @@
<ClCompile Include="src\ActiveMethodTest.cpp" />
<ClCompile Include="src\ActivityTest.cpp" />
<ClCompile Include="src\ConditionTest.cpp" />
<ClCompile Include="src\MutexTest.cpp" />
<ClCompile Include="src\RWLockTest.cpp" />
<ClCompile Include="src\SemaphoreTest.cpp" />
<ClCompile Include="src\ThreadingTestSuite.cpp" />
@ -477,6 +478,7 @@
<ClInclude Include="src\ActiveMethodTest.h" />
<ClInclude Include="src\ActivityTest.h" />
<ClInclude Include="src\ConditionTest.h" />
<ClInclude Include="src\MutexTest.h" />
<ClInclude Include="src\RWLockTest.h" />
<ClInclude Include="src\SemaphoreTest.h" />
<ClInclude Include="src\ThreadingTestSuite.h" />

View File

@ -336,6 +336,9 @@
<ClCompile Include="src\ConditionTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
<ClCompile Include="src\MutexTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
<ClCompile Include="src\RWLockTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
@ -746,6 +749,9 @@
<ClInclude Include="src\ConditionTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>
<ClInclude Include="src\MutexTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>
<ClInclude Include="src\RWLockTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>

View File

@ -343,6 +343,7 @@
<ClCompile Include="src\ActiveMethodTest.cpp" />
<ClCompile Include="src\ActivityTest.cpp" />
<ClCompile Include="src\ConditionTest.cpp" />
<ClCompile Include="src\MutexTest.cpp" />
<ClCompile Include="src\RWLockTest.cpp" />
<ClCompile Include="src\SemaphoreTest.cpp" />
<ClCompile Include="src\ThreadingTestSuite.cpp" />
@ -480,6 +481,7 @@
<ClInclude Include="src\ActiveMethodTest.h" />
<ClInclude Include="src\ActivityTest.h" />
<ClInclude Include="src\ConditionTest.h" />
<ClInclude Include="src\MutexTest.h" />
<ClInclude Include="src\RWLockTest.h" />
<ClInclude Include="src\SemaphoreTest.h" />
<ClInclude Include="src\ThreadingTestSuite.h" />

View File

@ -336,6 +336,9 @@
<ClCompile Include="src\ConditionTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
<ClCompile Include="src\MutexTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
<ClCompile Include="src\RWLockTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
@ -746,6 +749,9 @@
<ClInclude Include="src\ConditionTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>
<ClInclude Include="src\MutexTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>
<ClInclude Include="src\RWLockTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>

View File

@ -360,6 +360,7 @@
<ClCompile Include="src\ActiveMethodTest.cpp" />
<ClCompile Include="src\ActivityTest.cpp" />
<ClCompile Include="src\ConditionTest.cpp" />
<ClCompile Include="src\MutexTest.cpp" />
<ClCompile Include="src\RWLockTest.cpp" />
<ClCompile Include="src\SemaphoreTest.cpp" />
<ClCompile Include="src\ThreadingTestSuite.cpp" />
@ -498,6 +499,7 @@
<ClInclude Include="src\ActiveMethodTest.h" />
<ClInclude Include="src\ActivityTest.h" />
<ClInclude Include="src\ConditionTest.h" />
<ClInclude Include="src\MutexTest.h" />
<ClInclude Include="src\RWLockTest.h" />
<ClInclude Include="src\SemaphoreTest.h" />
<ClInclude Include="src\ThreadingTestSuite.h" />

View File

@ -330,6 +330,9 @@
<ClCompile Include="src\ConditionTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
<ClCompile Include="src\MutexTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
<ClCompile Include="src\RWLockTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
@ -740,6 +743,9 @@
<ClInclude Include="src\ConditionTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>
<ClInclude Include="src\MutexTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>
<ClInclude Include="src\RWLockTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>

View File

@ -365,6 +365,7 @@
<ClCompile Include="src\ActiveMethodTest.cpp" />
<ClCompile Include="src\ActivityTest.cpp" />
<ClCompile Include="src\ConditionTest.cpp" />
<ClCompile Include="src\MutexTest.cpp" />
<ClCompile Include="src\RWLockTest.cpp" />
<ClCompile Include="src\SemaphoreTest.cpp" />
<ClCompile Include="src\ThreadingTestSuite.cpp" />
@ -503,6 +504,7 @@
<ClInclude Include="src\ActiveMethodTest.h" />
<ClInclude Include="src\ActivityTest.h" />
<ClInclude Include="src\ConditionTest.h" />
<ClInclude Include="src\MutexTest.h" />
<ClInclude Include="src\RWLockTest.h" />
<ClInclude Include="src\SemaphoreTest.h" />
<ClInclude Include="src\ThreadingTestSuite.h" />

View File

@ -336,6 +336,9 @@
<ClCompile Include="src\ConditionTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
<ClCompile Include="src\MutexTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
<ClCompile Include="src\RWLockTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
@ -746,6 +749,9 @@
<ClInclude Include="src\ConditionTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>
<ClInclude Include="src\MutexTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>
<ClInclude Include="src\RWLockTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>

View File

@ -388,6 +388,7 @@
<ClCompile Include="src\ActiveMethodTest.cpp" />
<ClCompile Include="src\ActivityTest.cpp" />
<ClCompile Include="src\ConditionTest.cpp" />
<ClCompile Include="src\MutexTest.cpp" />
<ClCompile Include="src\RWLockTest.cpp" />
<ClCompile Include="src\SemaphoreTest.cpp" />
<ClCompile Include="src\ThreadingTestSuite.cpp" />
@ -525,6 +526,7 @@
<ClInclude Include="src\ActiveMethodTest.h" />
<ClInclude Include="src\ActivityTest.h" />
<ClInclude Include="src\ConditionTest.h" />
<ClInclude Include="src\MutexTest.h" />
<ClInclude Include="src\RWLockTest.h" />
<ClInclude Include="src\SemaphoreTest.h" />
<ClInclude Include="src\ThreadingTestSuite.h" />

View File

@ -336,6 +336,9 @@
<ClCompile Include="src\ConditionTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
<ClCompile Include="src\MutexTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
<ClCompile Include="src\RWLockTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
@ -743,6 +746,9 @@
<ClInclude Include="src\ConditionTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>
<ClInclude Include="src\MutexTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>
<ClInclude Include="src\RWLockTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>

View File

@ -775,6 +775,9 @@
<File
RelativePath=".\src\ConditionTest.cpp">
</File>
<File
RelativePath=".\src\MutexTest.cpp">
</File>
<File
RelativePath=".\src\RWLockTest.cpp">
</File>
@ -812,6 +815,9 @@
<File
RelativePath=".\src\ConditionTest.h">
</File>
<File
RelativePath=".\src\MutexTest.h">
</File>
<File
RelativePath=".\src\RWLockTest.h">
</File>

View File

@ -1043,6 +1043,10 @@
RelativePath=".\src\ConditionTest.cpp"
>
</File>
<File
RelativePath=".\src\MutexTest.cpp"
>
</File>
<File
RelativePath=".\src\RWLockTest.cpp"
>
@ -1091,6 +1095,10 @@
RelativePath=".\src\ConditionTest.h"
>
</File>
<File
RelativePath=".\src\MutexTest.h"
>
</File>
<File
RelativePath=".\src\RWLockTest.h"
>

View File

@ -1029,6 +1029,10 @@
RelativePath=".\src\ConditionTest.cpp"
>
</File>
<File
RelativePath=".\src\MutexTest.cpp"
>
</File>
<File
RelativePath=".\src\RWLockTest.cpp"
>
@ -1077,6 +1081,10 @@
RelativePath=".\src\ConditionTest.h"
>
</File>
<File
RelativePath=".\src\MutexTest.h"
>
</File>
<File
RelativePath=".\src\RWLockTest.h"
>

View File

@ -359,6 +359,7 @@
<ClCompile Include="src\ActiveMethodTest.cpp" />
<ClCompile Include="src\ActivityTest.cpp" />
<ClCompile Include="src\ConditionTest.cpp" />
<ClCompile Include="src\MutexTest.cpp" />
<ClCompile Include="src\RWLockTest.cpp" />
<ClCompile Include="src\SemaphoreTest.cpp" />
<ClCompile Include="src\ThreadingTestSuite.cpp" />
@ -498,6 +499,7 @@
<ClInclude Include="src\ActiveMethodTest.h" />
<ClInclude Include="src\ActivityTest.h" />
<ClInclude Include="src\ConditionTest.h" />
<ClInclude Include="src\MutexTest.h" />
<ClInclude Include="src\RWLockTest.h" />
<ClInclude Include="src\SemaphoreTest.h" />
<ClInclude Include="src\ThreadingTestSuite.h" />

View File

@ -339,6 +339,9 @@
<ClCompile Include="src\ConditionTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
<ClCompile Include="src\MutexTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
<ClCompile Include="src\RWLockTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
@ -752,6 +755,9 @@
<ClInclude Include="src\ConditionTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>
<ClInclude Include="src\MutexTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>
<ClInclude Include="src\RWLockTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>

View File

@ -365,6 +365,7 @@
<ClCompile Include="src\ActiveMethodTest.cpp" />
<ClCompile Include="src\ActivityTest.cpp" />
<ClCompile Include="src\ConditionTest.cpp" />
<ClCompile Include="src\MutexTest.cpp" />
<ClCompile Include="src\RWLockTest.cpp" />
<ClCompile Include="src\SemaphoreTest.cpp" />
<ClCompile Include="src\ThreadingTestSuite.cpp" />
@ -504,6 +505,7 @@
<ClInclude Include="src\ActiveMethodTest.h" />
<ClInclude Include="src\ActivityTest.h" />
<ClInclude Include="src\ConditionTest.h" />
<ClInclude Include="src\MutexTest.h" />
<ClInclude Include="src\RWLockTest.h" />
<ClInclude Include="src\SemaphoreTest.h" />
<ClInclude Include="src\ThreadingTestSuite.h" />

View File

@ -336,6 +336,9 @@
<ClCompile Include="src\ConditionTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
<ClCompile Include="src\MutexTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
<ClCompile Include="src\RWLockTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
@ -749,6 +752,9 @@
<ClInclude Include="src\ConditionTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>
<ClInclude Include="src\MutexTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>
<ClInclude Include="src\RWLockTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>

View File

@ -372,6 +372,7 @@
<ClCompile Include="src\ActiveMethodTest.cpp" />
<ClCompile Include="src\ActivityTest.cpp" />
<ClCompile Include="src\ConditionTest.cpp" />
<ClCompile Include="src\MutexTest.cpp" />
<ClCompile Include="src\RWLockTest.cpp" />
<ClCompile Include="src\SemaphoreTest.cpp" />
<ClCompile Include="src\ThreadingTestSuite.cpp" />
@ -510,6 +511,7 @@
<ClInclude Include="src\ActiveMethodTest.h" />
<ClInclude Include="src\ActivityTest.h" />
<ClInclude Include="src\ConditionTest.h" />
<ClInclude Include="src\MutexTest.h" />
<ClInclude Include="src\RWLockTest.h" />
<ClInclude Include="src\SemaphoreTest.h" />
<ClInclude Include="src\ThreadingTestSuite.h" />

View File

@ -336,6 +336,9 @@
<ClCompile Include="src\ConditionTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
<ClCompile Include="src\MutexTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
<ClCompile Include="src\RWLockTest.cpp">
<Filter>Threading\Source Files</Filter>
</ClCompile>
@ -749,6 +752,9 @@
<ClInclude Include="src\ConditionTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>
<ClInclude Include="src\MutexTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>
<ClInclude Include="src\RWLockTest.h">
<Filter>Threading\Header Files</Filter>
</ClInclude>

View File

@ -1023,6 +1023,10 @@
RelativePath=".\src\ConditionTest.cpp"
>
</File>
<File
RelativePath=".\src\MutexTest.cpp"
>
</File>
<File
RelativePath=".\src\RWLockTest.cpp"
>
@ -1071,6 +1075,10 @@
RelativePath=".\src\ConditionTest.h"
>
</File>
<File
RelativePath=".\src\MutexTest.h"
>
</File>
<File
RelativePath=".\src\RWLockTest.h"
>

View File

@ -0,0 +1,122 @@
//
// MutexTest.cpp
//
// $Id: //poco/1.4/Foundation/testsuite/src/MutexTest.cpp#1 $
//
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "MutexTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Exception.h"
#include "Poco/Mutex.h"
#include "Poco/Runnable.h"
#include "Poco/Thread.h"
#include "Poco/Timestamp.h"
using Poco::FastMutex;
using Poco::Runnable;
using Poco::SystemException;
using Poco::Thread;
using Poco::Timestamp;
namespace
{
class SelfDeadlockRunnable: public Runnable
{
public:
SelfDeadlockRunnable():
_ran(false)
{
}
void run()
{
try
{
FastMutex mtx;
mtx.lock();
mtx.lock();
}
catch (...)
{
}
_ran = true;
}
bool ran() const
{
return _ran;
}
private:
bool _ran;
};
}
MutexTest::MutexTest(const std::string& name): CppUnit::TestCase(name)
{
}
MutexTest::~MutexTest()
{
}
void MutexTest::testFastMutexRecursion()
{
FastMutex mtx;
mtx.lock();
bool success = mtx.tryLock();
assert (!success);
Timestamp mark;
success = mtx.tryLock(2000); // Wait 2 seconds
assert (!success);
// Test that we waited approx. 2 sec
Timestamp::TimeDiff elapsed = mark.elapsed();
assert (elapsed > 1000000);
assert (elapsed < 3000000);
success = mtx.tryLock(0);
assert (!success);
SelfDeadlockRunnable r;
Thread t;
t.start(r);
success = t.tryJoin(2000);
assert (!success);
assert (!r.ran());
}
void MutexTest::setUp()
{
}
void MutexTest::tearDown()
{
}
CppUnit::Test* MutexTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MutexTest");
CppUnit_addTest(pSuite, MutexTest, testFastMutexRecursion);
return pSuite;
}

View File

@ -0,0 +1,40 @@
//
// MutexTest.h
//
// $Id: //poco/1.4/Foundation/testsuite/src/MutexTest.h#1 $
//
// Definition of the MutexTest class.
//
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef MutexTest_INCLUDED
#define MutexTest_INCLUDED
#include "Poco/Foundation.h"
#include "CppUnit/TestCase.h"
class MutexTest: public CppUnit::TestCase
{
public:
MutexTest(const std::string& name);
~MutexTest();
void testFastMutexRecursion();
void setUp();
void tearDown();
static CppUnit::Test* suite();
private:
};
#endif // MutexTest_INCLUDED

View File

@ -12,6 +12,7 @@
#include "ThreadingTestSuite.h"
#include "ThreadTest.h"
#include "MutexTest.h"
#include "SemaphoreTest.h"
#include "RWLockTest.h"
#include "ThreadPoolTest.h"
@ -28,6 +29,7 @@ CppUnit::Test* ThreadingTestSuite::suite()
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ThreadingTestSuite");
pSuite->addTest(ThreadTest::suite());
pSuite->addTest(MutexTest::suite());
pSuite->addTest(SemaphoreTest::suite());
pSuite->addTest(RWLockTest::suite());
pSuite->addTest(ThreadPoolTest::suite());