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

@@ -45,16 +45,13 @@ class Foundation_API Mutex: private MutexImpl
/// A Mutex (mutual exclusion) is a synchronization
/// mechanism used to control access to a shared resource
/// in a concurrent (multithreaded) scenario.
/// Mutexes are recursive, that is, the same mutex can be
/// locked multiple times by the same thread (but, of course,
/// not by other threads).
/// Using the ScopedLock class is the preferred way to automatically
/// lock and unlock a mutex.
{
public:
typedef Poco::ScopedLock<Mutex> ScopedLock;
Mutex();
explicit Mutex(bool recursive = true);
/// creates the Mutex.
~Mutex();

View File

@@ -32,8 +32,7 @@ namespace Poco {
class Foundation_API MutexImpl
{
protected:
MutexImpl();
MutexImpl(bool fast);
MutexImpl(bool recursive);
~MutexImpl();
void lockImpl();
bool tryLockImpl();

View File

@@ -32,8 +32,7 @@ namespace Poco {
class Foundation_API MutexImpl
{
protected:
MutexImpl();
MutexImpl(bool fast);
MutexImpl(bool recursive);
~MutexImpl();
void lockImpl();
bool tryLockImpl();

View File

@@ -31,25 +31,46 @@ namespace Poco {
class Foundation_API MutexImpl
{
protected:
MutexImpl();
MutexImpl(bool recursive);
~MutexImpl();
void lockImpl();
bool tryLockImpl();
bool tryLockImpl(long milliseconds);
void unlockImpl();
private:
CRITICAL_SECTION _cs;
int _lockCount;
const bool _recursive;
};
class Foundation_API FastMutexImpl
{
protected:
FastMutexImpl();
~FastMutexImpl();
void lockImpl();
bool tryLockImpl();
bool tryLockImpl(long milliseconds);
void unlockImpl();
private:
CRITICAL_SECTION _cs;
};
typedef MutexImpl FastMutexImpl;
//
// inlines
//
inline void MutexImpl::lockImpl()
inline void MutexImpl::unlockImpl()
{
--_lockCount;
LeaveCriticalSection(&_cs);
}
inline void FastMutexImpl::lockImpl()
{
try
{
@@ -62,7 +83,7 @@ inline void MutexImpl::lockImpl()
}
inline bool MutexImpl::tryLockImpl()
inline bool FastMutexImpl::tryLockImpl()
{
try
{
@@ -75,7 +96,7 @@ inline bool MutexImpl::tryLockImpl()
}
inline void MutexImpl::unlockImpl()
inline void FastMutexImpl::unlockImpl()
{
LeaveCriticalSection(&_cs);
}

View File

@@ -31,21 +31,35 @@ namespace Poco {
class Foundation_API MutexImpl
{
protected:
MutexImpl();
MutexImpl(bool recursive);
~MutexImpl();
void lockImpl();
bool tryLockImpl();
bool tryLockImpl(long milliseconds);
void unlockImpl();
private:
HANDLE _mutex;
int _lockCount;
const bool _recursive;
};
class Foundation_API FastMutexImpl
{
protected:
FastMutexImpl();
~FastMutexImpl();
void lockImpl();
bool tryLockImpl();
bool tryLockImpl(long milliseconds);
void unlockImpl();
private:
HANDLE _mutex;
};
typedef MutexImpl FastMutexImpl;
} // namespace Poco

View File

@@ -33,7 +33,8 @@
namespace Poco {
Mutex::Mutex()
Mutex::Mutex(bool recursive)
: MutexImpl(recursive)
{
}

View File

@@ -38,7 +38,7 @@
namespace Poco {
MutexImpl::MutexImpl()
MutexImpl::MutexImpl(bool recursive)
{
#if defined(POCO_VXWORKS)
// This workaround is for VxWorks 5.x where
@@ -50,34 +50,9 @@ MutexImpl::MutexImpl()
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
#if defined(PTHREAD_MUTEX_RECURSIVE_NP)
pthread_mutexattr_settype_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
pthread_mutexattr_settype_np(&attr, recursive ? PTHREAD_MUTEX_RECURSIVE_NP : PTHREAD_MUTEX_NORMAL_NP);
#elif !defined(POCO_VXWORKS)
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
#endif
if (pthread_mutex_init(&_mutex, &attr))
{
pthread_mutexattr_destroy(&attr);
throw SystemException("cannot create mutex");
}
pthread_mutexattr_destroy(&attr);
}
MutexImpl::MutexImpl(bool fast)
{
#if defined(POCO_VXWORKS)
// This workaround is for VxWorks 5.x where
// pthread_mutex_init() won't properly initialize the mutex
// resulting in a subsequent freeze in pthread_mutex_destroy()
// if the mutex has never been used.
std::memset(&_mutex, 0, sizeof(_mutex));
#endif
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
#if defined(PTHREAD_MUTEX_RECURSIVE_NP)
pthread_mutexattr_settype_np(&attr, fast ? PTHREAD_MUTEX_NORMAL_NP : PTHREAD_MUTEX_RECURSIVE_NP);
#elif !defined(POCO_VXWORKS)
pthread_mutexattr_settype(&attr, fast ? PTHREAD_MUTEX_NORMAL : PTHREAD_MUTEX_RECURSIVE);
pthread_mutexattr_settype(&attr, recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL);
#endif
if (pthread_mutex_init(&_mutex, &attr))
{
@@ -144,7 +119,7 @@ bool MutexImpl::tryLockImpl(long milliseconds)
}
FastMutexImpl::FastMutexImpl(): MutexImpl(true)
FastMutexImpl::FastMutexImpl(): MutexImpl(false)
{
}

View File

@@ -21,23 +21,15 @@
namespace Poco {
MutexImpl::MutexImpl()
MutexImpl::MutexImpl(bool recursive)
{
if (recursive)
{
_sem = semMCreate(SEM_INVERSION_SAFE | SEM_Q_PRIORITY);
if (_sem == 0)
throw Poco::SystemException("cannot create mutex");
}
MutexImpl::MutexImpl(bool fast)
{
if (fast)
{
_sem = semBCreate(SEM_Q_PRIORITY, SEM_FULL);
}
else
{
_sem = semMCreate(SEM_INVERSION_SAFE | SEM_Q_PRIORITY);
_sem = semBCreate(SEM_Q_PRIORITY, SEM_FULL);
}
if (_sem == 0)
throw Poco::SystemException("cannot create mutex");
@@ -57,7 +49,7 @@ bool MutexImpl::tryLockImpl(long milliseconds)
}
FastMutexImpl::FastMutexImpl(): MutexImpl(true)
FastMutexImpl::FastMutexImpl(): MutexImpl(false)
{
}

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;

View File

@@ -15,12 +15,16 @@
#include "Poco/Mutex_WINCE.h"
#include "Poco/Thread.h"
#include "Poco/Timestamp.h"
namespace Poco {
MutexImpl::MutexImpl()
MutexImpl::MutexImpl(bool recursive)
: _lockCount(0)
, _recursive(recursive)
{
_mutex = CreateMutexW(NULL, FALSE, NULL);
if (!_mutex) throw SystemException("cannot create mutex");
@@ -38,6 +42,12 @@ void MutexImpl::lockImpl()
switch (WaitForSingleObject(_mutex, INFINITE))
{
case WAIT_OBJECT_0:
++_lockCount;
if (!_recursive && _lockCount > 1)
{
// We're trying to go recursive so self-deadlock
Thread::current()->join();
}
return;
default:
throw SystemException("cannot lock mutex");
@@ -52,6 +62,12 @@ bool MutexImpl::tryLockImpl()
case WAIT_TIMEOUT:
return false;
case WAIT_OBJECT_0:
if (!_recursive && _lockCount > 0)
{
ReleaseMutex(_mutex);
return false;
}
++_lockCount;
return true;
default:
throw SystemException("cannot lock mutex");
@@ -60,6 +76,74 @@ bool MutexImpl::tryLockImpl()
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;
}
void MutexImpl::unlockImpl()
{
ReleaseMutex(_mutex);
}
FastMutexImpl::FastMutexImpl()
{
_mutex = CreateMutexW(NULL, FALSE, NULL);
if (!_mutex) throw SystemException("cannot create mutex");
}
FastMutexImpl::~FastMutexImpl()
{
CloseHandle(_mutex);
}
void FastMutexImpl::lockImpl()
{
switch (WaitForSingleObject(_mutex, INFINITE))
{
case WAIT_OBJECT_0:
return;
default:
throw SystemException("cannot lock mutex");
}
}
bool FastMutexImpl::tryLockImpl()
{
switch (WaitForSingleObject(_mutex, 0))
{
case WAIT_TIMEOUT:
return false;
case WAIT_OBJECT_0:
return true;
default:
throw SystemException("cannot lock mutex");
}
}
bool FastMutexImpl::tryLockImpl(long milliseconds)
{
switch (WaitForSingleObject(_mutex, milliseconds + 1))
{
@@ -73,7 +157,7 @@ bool MutexImpl::tryLockImpl(long milliseconds)
}
void MutexImpl::unlockImpl()
void FastMutexImpl::unlockImpl()
{
ReleaseMutex(_mutex);
}

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());