Add RecursiveMutex, UnlockGuard, TryLockGuard

This commit is contained in:
Vinnie Falco
2013-10-04 10:23:03 -07:00
parent 5831a53697
commit 01fd05cb4b
16 changed files with 432 additions and 10 deletions

View File

@@ -84,6 +84,7 @@
<ClInclude Include="..\..\beast\Arithmetic.h" />
<ClInclude Include="..\..\beast\Atomic.h" />
<ClInclude Include="..\..\beast\ByteOrder.h" />
<ClInclude Include="..\..\beast\chrono\CPUUsage.h" />
<ClInclude Include="..\..\beast\chrono\RelativeTime.h" />
<ClInclude Include="..\..\beast\Config.h" />
<ClInclude Include="..\..\beast\config\CompilerConfig.h" />
@@ -151,10 +152,13 @@
<ClInclude Include="..\..\beast\strings\StringFromNumber.h" />
<ClInclude Include="..\..\beast\Thread.h" />
<ClInclude Include="..\..\beast\thread\LockGuard.h" />
<ClInclude Include="..\..\beast\thread\RecursiveMutex.h" />
<ClInclude Include="..\..\beast\thread\SharedData.h" />
<ClInclude Include="..\..\beast\thread\SharedLockGuard.h" />
<ClInclude Include="..\..\beast\thread\SharedMutexAdapter.h" />
<ClInclude Include="..\..\beast\Chrono.h" />
<ClInclude Include="..\..\beast\thread\TryLockGuard.h" />
<ClInclude Include="..\..\beast\thread\UnlockGuard.h" />
<ClInclude Include="..\..\beast\TypeTraits.h" />
<ClInclude Include="..\..\beast\type_traits\IntegralConstant.h" />
<ClInclude Include="..\..\beast\type_traits\IsIntegral.h" />
@@ -394,6 +398,12 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\beast\chrono\Chrono.cpp" />
<ClCompile Include="..\..\beast\chrono\impl\CPUUsage.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\beast\chrono\impl\RelativeTime.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
@@ -494,6 +504,13 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\beast\strings\Strings.cpp" />
<ClCompile Include="..\..\beast\thread\impl\RecursiveMutex.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\beast\thread\Thread.cpp" />
<ClCompile Include="..\..\beast\utility\impl\Debug.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>

View File

@@ -291,6 +291,9 @@
<Filter Include="beast\smart_ptr">
<UniqueIdentifier>{4e9c54da-1581-41d7-ac75-48140e4a13d4}</UniqueIdentifier>
</Filter>
<Filter Include="beast\thread\impl">
<UniqueIdentifier>{96d46096-0e7d-4ca8-9d81-72f6834f88f8}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\modules\beast_core\beast_core.h">
@@ -1221,6 +1224,18 @@
<ClInclude Include="..\..\modules\beast_asio\http\HTTPResponseParser.h">
<Filter>beast_asio\http</Filter>
</ClInclude>
<ClInclude Include="..\..\beast\chrono\CPUUsage.h">
<Filter>beast\chrono</Filter>
</ClInclude>
<ClInclude Include="..\..\beast\thread\RecursiveMutex.h">
<Filter>beast\thread</Filter>
</ClInclude>
<ClInclude Include="..\..\beast\thread\UnlockGuard.h">
<Filter>beast\thread</Filter>
</ClInclude>
<ClInclude Include="..\..\beast\thread\TryLockGuard.h">
<Filter>beast\thread</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\modules\beast_core\containers\AbstractFifo.cpp">
@@ -1760,6 +1775,15 @@
<ClCompile Include="..\..\modules\beast_asio\http\HTTPResponseParser.cpp">
<Filter>beast_asio\http</Filter>
</ClCompile>
<ClCompile Include="..\..\beast\chrono\impl\CPUUsage.cpp">
<Filter>beast\chrono\impl</Filter>
</ClCompile>
<ClCompile Include="..\..\beast\thread\impl\RecursiveMutex.cpp">
<Filter>beast\thread\impl</Filter>
</ClCompile>
<ClCompile Include="..\..\beast\thread\Thread.cpp">
<Filter>beast\thread</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\TODO.txt">

View File

@@ -20,6 +20,7 @@
#ifndef BEAST_CHRONO_H_INCLUDED
#define BEAST_CHRONO_H_INCLUDED
#include "chrono/CPUUsage.h"
#include "chrono/RelativeTime.h"
#endif

View File

@@ -21,6 +21,8 @@
#define BEAST_THREAD_H_INCLUDED
#include "thread/LockGuard.h"
#include "thread/UnlockGuard.h"
#include "thread/TryLockGuard.h"
#include "thread/SharedLockGuard.h"
#include "thread/SharedMutexAdapter.h"
#include "thread/SharedData.h"

44
beast/chrono/CPUUsage.h Normal file
View File

@@ -0,0 +1,44 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef BEAST_CHRONO_CPUUSAGE_H_INCLUDED
#define BEAST_CHRONO_CPUUSAGE_H_INCLUDED
#include "RelativeTime.h"
namespace beast {
/** Measurements of CPU utilization. */
#if 0
/** Scoped lifetime measurement. */
class ScopedTimeInterval
{
public:
ScopedTimeInterval ();
~ScopedTimeInterval ();
private:
RelativeTime m_start;
};
#endif
}
#endif

View File

@@ -19,4 +19,5 @@
#include "BeastConfig.h"
#include "impl/CPUUsage.cpp"
#include "impl/RelativeTime.cpp"

View File

@@ -0,0 +1,21 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include "../CPUUsage.h"

View File

@@ -22,8 +22,7 @@
#include "../Uncopyable.h"
namespace beast
{
namespace beast {
template <typename Mutex>
class LockGuard : public Uncopyable

View File

@@ -0,0 +1,84 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Portions of this file are from JUCE.
Copyright (c) 2013 - Raw Material Software Ltd.
Please visit http://www.juce.com
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef BEAST_THREAD_RECURSIVEMUTEX_H_INCLUDED
#define BEAST_THREAD_RECURSIVEMUTEX_H_INCLUDED
#include "../Config.h"
#include "LockGuard.h"
#include "UnlockGuard.h"
#include "TryLockGuard.h"
#if ! BEAST_WINDOWS
#include <pthread.h>
#endif
namespace beast {
class RecursiveMutex
{
public:
typedef LockGuard <RecursiveMutex> ScopedLockType;
typedef UnlockGuard <RecursiveMutex> ScopedUnlockType;
typedef TryLockGuard <RecursiveMutex> ScopedTryLockType;
/** Create the mutex.
The mutux is initially unowned.
*/
RecursiveMutex ();
/** Destroy the mutex.
If the lock is owned, the result is undefined.
*/
~RecursiveMutex ();
// Boost concept compatibility:
// http://www.boost.org/doc/libs/1_54_0/doc/html/thread/synchronization.html#thread.synchronization.mutex_concepts
/** BasicLockable */
/** @{ */
void lock () const;
void unlock () const;
/** @} */
/** Lockable */
bool try_lock () const;
private:
// To avoid including windows.h in the public Beast headers, we'll just
// reserve storage here that's big enough to be used internally as a windows
// CRITICAL_SECTION structure.
#if BEAST_WINDOWS
# if BEAST_64BIT
char section[44];
# else
char section[24];
# endif
#else
mutable pthread_mutex_t mutex;
#endif
};
}
#endif

View File

@@ -20,12 +20,10 @@
#ifndef BEAST_THREAD_SHAREDDATA_H_INCLUDED
#define BEAST_THREAD_SHAREDDATA_H_INCLUDED
#include "RecursiveMutex.h"
#include "SharedMutexAdapter.h"
namespace beast
{
class CriticalSection;
namespace beast {
/** Structured, multi-threaded access to a shared state.
@@ -111,7 +109,7 @@ class CriticalSection;
@tparam SharedMutexType The type of shared mutex to use.
*/
template <typename Value, class SharedMutexType =
SharedMutexAdapter <CriticalSection> >
SharedMutexAdapter <RecursiveMutex> >
class SharedData : public Uncopyable
{
private:

View File

@@ -23,8 +23,7 @@
#include "LockGuard.h"
#include "SharedLockGuard.h"
namespace beast
{
namespace beast {
/** Adapts a regular Lockable to conform to the SharedMutex concept.
Shared locks become unique locks with this interface. Two threads may not

22
beast/thread/Thread.cpp Normal file
View File

@@ -0,0 +1,22 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include "BeastConfig.h"
#include "impl/RecursiveMutex.cpp"

View File

@@ -0,0 +1,55 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef BEAST_THREAD_TRYLOCKGUARD_H_INCLUDED
#define BEAST_THREAD_TRYLOCKGUARD_H_INCLUDED
#include "../Uncopyable.h"
namespace beast {
template <typename Mutex>
class TryLockGuard : public Uncopyable
{
public:
typedef Mutex MutexType;
explicit TryLockGuard (Mutex const& mutex)
: m_mutex (mutex)
, m_owns_lock (m_mutex.try_lock())
{
}
~TryLockGuard ()
{
if (m_owns_lock)
m_mutex.unlock();
}
bool owns_lock() const
{ return m_owns_lock; }
private:
Mutex const& m_mutex;
bool m_owns_lock;
};
}
#endif

View File

@@ -0,0 +1,50 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef BEAST_THREAD_UNLOCKGUARD_H_INCLUDED
#define BEAST_THREAD_UNLOCKGUARD_H_INCLUDED
#include "../Uncopyable.h"
namespace beast {
template <typename Mutex>
class UnlockGuard : public Uncopyable
{
public:
typedef Mutex MutexType;
explicit UnlockGuard (Mutex const& mutex)
: m_mutex (mutex)
{
m_mutex.unlock();
}
~UnlockGuard ()
{
m_mutex.lock();
}
private:
Mutex const& m_mutex;
};
}
#endif

View File

@@ -0,0 +1,106 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Portions of this file are from JUCE.
Copyright (c) 2013 - Raw Material Software Ltd.
Please visit http://www.juce.com
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include "../RecursiveMutex.h"
#if BEAST_WINDOWS
#include "../StaticAssert.h"
#include <Windows.h>
namespace beast {
RecursiveMutex::RecursiveMutex()
{
// (just to check the MS haven't changed this structure and broken things...)
#if BEAST_VC7_OR_EARLIER
static_bassert (sizeof (CRITICAL_SECTION) <= 24);
#else
static_bassert (sizeof (CRITICAL_SECTION) <= sizeof (section));
#endif
InitializeCriticalSection ((CRITICAL_SECTION*) section);
}
RecursiveMutex::~RecursiveMutex()
{
DeleteCriticalSection ((CRITICAL_SECTION*) section);
}
void RecursiveMutex::lock() const
{
EnterCriticalSection ((CRITICAL_SECTION*) section);
}
void RecursiveMutex::unlock() const
{
LeaveCriticalSection ((CRITICAL_SECTION*) section);
}
bool RecursiveMutex::try_lock() const
{
return TryEnterCriticalSection ((CRITICAL_SECTION*) section) != FALSE;
}
}
#else
namespace beast {
RecursiveMutex::RecursiveMutex()
{
pthread_mutexattr_t atts;
pthread_mutexattr_init (&atts);
pthread_mutexattr_settype (&atts, PTHREAD_MUTEX_RECURSIVE);
#if ! BEAST_ANDROID
pthread_mutexattr_setprotocol (&atts, PTHREAD_PRIO_INHERIT);
#endif
pthread_mutex_init (&mutex, &atts);
pthread_mutexattr_destroy (&atts);
}
RecursiveMutex::~RecursiveMutex()
{
pthread_mutex_destroy (&mutex);
}
void RecursiveMutex::lock() const
{
pthread_mutex_lock (&mutex);
}
void RecursiveMutex::unlock() const
{
pthread_mutex_unlock (&mutex);
}
bool RecursiveMutex::try_lock() const
{
return pthread_mutex_trylock (&mutex) == 0;
}
}
#endif

View File

@@ -82,7 +82,6 @@ public:
*/
void exit() const noexcept;
//==============================================================================
/** Provides the type of scoped lock to use with a CriticalSection. */
typedef GenericScopedLock <CriticalSection> ScopedLockType;