forked from boostorg/beast
Add RecursiveMutex, UnlockGuard, TryLockGuard
This commit is contained in:
1 parent
5831a53697
commit
01fd05cb4b
16 files changed
+432
-10
No files matched your search
@@ -22,8 +22,7 @@
|
||||
|
||||
#include "../Uncopyable.h"
|
||||
|
||||
namespace beast
|
||||
{
|
||||
namespace beast {
|
||||
|
||||
template <typename Mutex>
|
||||
class LockGuard : public Uncopyable
|
||||
|
||||
@@ -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
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in new issue
Block a user