diff --git a/Builds/VisualStudio2012/beast.vcxproj b/Builds/VisualStudio2012/beast.vcxproj index 64b48b8c7..e4b8bfebf 100644 --- a/Builds/VisualStudio2012/beast.vcxproj +++ b/Builds/VisualStudio2012/beast.vcxproj @@ -84,6 +84,7 @@ + @@ -151,10 +152,13 @@ + + + @@ -394,6 +398,12 @@ + + true + true + true + true + true true @@ -494,6 +504,13 @@ true + + true + true + true + true + + true true diff --git a/Builds/VisualStudio2012/beast.vcxproj.filters b/Builds/VisualStudio2012/beast.vcxproj.filters index cdab9adef..cfc1e9cca 100644 --- a/Builds/VisualStudio2012/beast.vcxproj.filters +++ b/Builds/VisualStudio2012/beast.vcxproj.filters @@ -291,6 +291,9 @@ {4e9c54da-1581-41d7-ac75-48140e4a13d4} + + {96d46096-0e7d-4ca8-9d81-72f6834f88f8} + @@ -1221,6 +1224,18 @@ beast_asio\http + + beast\chrono + + + beast\thread + + + beast\thread + + + beast\thread + @@ -1760,6 +1775,15 @@ beast_asio\http + + beast\chrono\impl + + + beast\thread\impl + + + beast\thread + diff --git a/beast/Chrono.h b/beast/Chrono.h index 26d809afd..728baa1f8 100644 --- a/beast/Chrono.h +++ b/beast/Chrono.h @@ -20,6 +20,7 @@ #ifndef BEAST_CHRONO_H_INCLUDED #define BEAST_CHRONO_H_INCLUDED +#include "chrono/CPUUsage.h" #include "chrono/RelativeTime.h" #endif diff --git a/beast/Thread.h b/beast/Thread.h index 33849429c..bc3e9428a 100644 --- a/beast/Thread.h +++ b/beast/Thread.h @@ -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" diff --git a/beast/chrono/CPUUsage.h b/beast/chrono/CPUUsage.h new file mode 100644 index 000000000..049c779ed --- /dev/null +++ b/beast/chrono/CPUUsage.h @@ -0,0 +1,44 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + 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 diff --git a/beast/chrono/Chrono.cpp b/beast/chrono/Chrono.cpp index 549f9ba99..a3030761f 100644 --- a/beast/chrono/Chrono.cpp +++ b/beast/chrono/Chrono.cpp @@ -19,4 +19,5 @@ #include "BeastConfig.h" +#include "impl/CPUUsage.cpp" #include "impl/RelativeTime.cpp" diff --git a/beast/chrono/impl/CPUUsage.cpp b/beast/chrono/impl/CPUUsage.cpp new file mode 100644 index 000000000..a27bcd4e0 --- /dev/null +++ b/beast/chrono/impl/CPUUsage.cpp @@ -0,0 +1,21 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + 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" + diff --git a/beast/thread/LockGuard.h b/beast/thread/LockGuard.h index b933a9e6f..44463df53 100644 --- a/beast/thread/LockGuard.h +++ b/beast/thread/LockGuard.h @@ -22,8 +22,7 @@ #include "../Uncopyable.h" -namespace beast -{ +namespace beast { template class LockGuard : public Uncopyable diff --git a/beast/thread/RecursiveMutex.h b/beast/thread/RecursiveMutex.h new file mode 100644 index 000000000..b1d45adcf --- /dev/null +++ b/beast/thread/RecursiveMutex.h @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + 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 +#endif + +namespace beast { + +class RecursiveMutex +{ +public: + typedef LockGuard ScopedLockType; + typedef UnlockGuard ScopedUnlockType; + typedef TryLockGuard 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 diff --git a/beast/thread/SharedData.h b/beast/thread/SharedData.h index 118e3a5f1..542054079 100644 --- a/beast/thread/SharedData.h +++ b/beast/thread/SharedData.h @@ -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 > + SharedMutexAdapter > class SharedData : public Uncopyable { private: diff --git a/beast/thread/SharedMutexAdapter.h b/beast/thread/SharedMutexAdapter.h index 90115901a..b6fea1890 100644 --- a/beast/thread/SharedMutexAdapter.h +++ b/beast/thread/SharedMutexAdapter.h @@ -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 diff --git a/beast/thread/Thread.cpp b/beast/thread/Thread.cpp new file mode 100644 index 000000000..79ff63a23 --- /dev/null +++ b/beast/thread/Thread.cpp @@ -0,0 +1,22 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + 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" diff --git a/beast/thread/TryLockGuard.h b/beast/thread/TryLockGuard.h new file mode 100644 index 000000000..0f9d65c14 --- /dev/null +++ b/beast/thread/TryLockGuard.h @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + 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 +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 diff --git a/beast/thread/UnlockGuard.h b/beast/thread/UnlockGuard.h new file mode 100644 index 000000000..c53b4d06a --- /dev/null +++ b/beast/thread/UnlockGuard.h @@ -0,0 +1,50 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + 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 +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 diff --git a/beast/thread/impl/RecursiveMutex.cpp b/beast/thread/impl/RecursiveMutex.cpp new file mode 100644 index 000000000..0fce37f4c --- /dev/null +++ b/beast/thread/impl/RecursiveMutex.cpp @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + 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 + +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 diff --git a/modules/beast_core/threads/CriticalSection.h b/modules/beast_core/threads/CriticalSection.h index 0ccef1042..a130cb318 100644 --- a/modules/beast_core/threads/CriticalSection.h +++ b/modules/beast_core/threads/CriticalSection.h @@ -82,7 +82,6 @@ public: */ void exit() const noexcept; - //============================================================================== /** Provides the type of scoped lock to use with a CriticalSection. */ typedef GenericScopedLock ScopedLockType;