From 70f6c41ff77dcb2347138eebf3e41aa6c93cc754 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Fri, 4 Oct 2013 11:33:32 -0700 Subject: [PATCH] Move ServiceQueue, ThreadLocalValue, SpinLock --- .../Builds/VisualStudio2012/beast.vcxproj | 11 ++++- .../VisualStudio2012/beast.vcxproj.filters | 18 ++++++--- src/beast/beast/Thread.h | 3 ++ .../thread/ServiceQueue.h | 10 ++++- .../threads => beast/thread}/SpinLock.h | 40 +++++++++++++------ src/beast/beast/thread/Thread.cpp | 1 + .../thread}/ThreadLocalValue.h | 11 +++-- .../beast/thread/impl/RecursiveMutex.cpp | 5 +++ .../thread/impl}/ServiceQueue.cpp | 4 ++ src/beast/beast/thread/impl/WaitableEvent.cpp | 5 +++ src/beast/modules/beast_core/beast_core.cpp | 2 - src/beast/modules/beast_core/beast_core.h | 4 -- 12 files changed, 82 insertions(+), 32 deletions(-) rename src/beast/{modules/beast_core => beast}/thread/ServiceQueue.h (98%) rename src/beast/{modules/beast_core/threads => beast/thread}/SpinLock.h (82%) rename src/beast/{modules/beast_core/threads => beast/thread}/ThreadLocalValue.h (97%) rename src/beast/{modules/beast_core/thread => beast/thread/impl}/ServiceQueue.cpp (99%) diff --git a/src/beast/Builds/VisualStudio2012/beast.vcxproj b/src/beast/Builds/VisualStudio2012/beast.vcxproj index 38133a41f6..29b8ea3748 100644 --- a/src/beast/Builds/VisualStudio2012/beast.vcxproj +++ b/src/beast/Builds/VisualStudio2012/beast.vcxproj @@ -154,10 +154,13 @@ + + + @@ -329,9 +332,7 @@ - - @@ -511,6 +512,12 @@ true true + + true + true + true + true + true true diff --git a/src/beast/Builds/VisualStudio2012/beast.vcxproj.filters b/src/beast/Builds/VisualStudio2012/beast.vcxproj.filters index b5ca3dc0e5..2d991fef43 100644 --- a/src/beast/Builds/VisualStudio2012/beast.vcxproj.filters +++ b/src/beast/Builds/VisualStudio2012/beast.vcxproj.filters @@ -497,15 +497,9 @@ beast_core\threads - - beast_core\threads - beast_core\threads - - beast_core\threads - beast_core\threads @@ -1239,6 +1233,15 @@ beast\thread + + beast\thread + + + beast\thread + + + beast\thread + @@ -1790,6 +1793,9 @@ beast\thread\impl + + beast\thread\impl + diff --git a/src/beast/beast/Thread.h b/src/beast/beast/Thread.h index 208caed58c..72e83d69f0 100644 --- a/src/beast/beast/Thread.h +++ b/src/beast/beast/Thread.h @@ -26,6 +26,9 @@ #include "thread/SharedLockGuard.h" #include "thread/SharedMutexAdapter.h" #include "thread/SharedData.h" +#include "thread/ServiceQueue.h" +#include "thread/SpinLock.h" +#include "thread/ThreadLocalValue.h" #include "thread/WaitableEvent.h" #endif diff --git a/src/beast/modules/beast_core/thread/ServiceQueue.h b/src/beast/beast/thread/ServiceQueue.h similarity index 98% rename from src/beast/modules/beast_core/thread/ServiceQueue.h rename to src/beast/beast/thread/ServiceQueue.h index de921f0e41..1f54540513 100644 --- a/src/beast/modules/beast_core/thread/ServiceQueue.h +++ b/src/beast/beast/thread/ServiceQueue.h @@ -17,8 +17,14 @@ */ //============================================================================== -#ifndef BEAST_SERVICEQUEUE_H_INCLUDED -#define BEAST_SERVICEQUEUE_H_INCLUDED +#ifndef BEAST_THREAD_SERVICEQUEUE_H_INCLUDED +#define BEAST_THREAD_SERVICEQUEUE_H_INCLUDED + +#include "../intrusive/List.h" +#include "../intrusive/LockFreeStack.h" +#include "SharedData.h" +#include "ThreadLocalValue.h" +#include "WaitableEvent.h" namespace beast { diff --git a/src/beast/modules/beast_core/threads/SpinLock.h b/src/beast/beast/thread/SpinLock.h similarity index 82% rename from src/beast/modules/beast_core/threads/SpinLock.h rename to src/beast/beast/thread/SpinLock.h index 87b9c2abd2..37501659c8 100644 --- a/src/beast/modules/beast_core/threads/SpinLock.h +++ b/src/beast/beast/thread/SpinLock.h @@ -21,8 +21,14 @@ */ //============================================================================== -#ifndef BEAST_SPINLOCK_H_INCLUDED -#define BEAST_SPINLOCK_H_INCLUDED +#ifndef BEAST_THREAD_SPINLOCK_H_INCLUDED +#define BEAST_THREAD_SPINLOCK_H_INCLUDED + +#include "../Atomic.h" +#include "LockGuard.h" +#include "UnlockGuard.h" + +namespace beast { //============================================================================== /** @@ -40,6 +46,12 @@ class BEAST_API SpinLock : public Uncopyable { public: + /** Provides the type of scoped lock to use for locking a SpinLock. */ + typedef LockGuard ScopedLockType; + + /** Provides the type of scoped unlocker to use with a SpinLock. */ + typedef UnlockGuard ScopedUnlockType; + inline SpinLock() noexcept {} inline ~SpinLock() noexcept {} @@ -57,27 +69,29 @@ public: /** Attempts to acquire the lock, returning true if this was successful. */ inline bool tryEnter() const noexcept { - return lock.compareAndSetBool (1, 0); + return m_lock.compareAndSetBool (1, 0); } /** Releases the lock. */ inline void exit() const noexcept { - bassert (lock.value == 1); // Agh! Releasing a lock that isn't currently held! - lock = 0; + bassert (m_lock.value == 1); // Agh! Releasing a lock that isn't currently held! + m_lock = 0; } - //============================================================================== - /** Provides the type of scoped lock to use for locking a SpinLock. */ - typedef GenericScopedLock ScopedLockType; - - /** Provides the type of scoped unlocker to use with a SpinLock. */ - typedef GenericScopedUnlock ScopedUnlockType; + void lock () const + { enter(); } + void unlock () const + { exit(); } + bool try_lock () const + { return tryEnter(); } private: //============================================================================== - mutable Atomic lock; + mutable Atomic m_lock; }; +} + +#endif -#endif // BEAST_SPINLOCK_H_INCLUDED diff --git a/src/beast/beast/thread/Thread.cpp b/src/beast/beast/thread/Thread.cpp index d44ea8210c..7bd7438ec9 100644 --- a/src/beast/beast/thread/Thread.cpp +++ b/src/beast/beast/thread/Thread.cpp @@ -20,4 +20,5 @@ #include "BeastConfig.h" #include "impl/RecursiveMutex.cpp" +#include "impl/ServiceQueue.cpp" #include "impl/WaitableEvent.cpp" diff --git a/src/beast/modules/beast_core/threads/ThreadLocalValue.h b/src/beast/beast/thread/ThreadLocalValue.h similarity index 97% rename from src/beast/modules/beast_core/threads/ThreadLocalValue.h rename to src/beast/beast/thread/ThreadLocalValue.h index af623adbed..4b7e4908d5 100644 --- a/src/beast/modules/beast_core/threads/ThreadLocalValue.h +++ b/src/beast/beast/thread/ThreadLocalValue.h @@ -21,8 +21,12 @@ */ //============================================================================== -#ifndef BEAST_THREADLOCALVALUE_H_INCLUDED -#define BEAST_THREADLOCALVALUE_H_INCLUDED +#ifndef BEAST_THREAD_THREADLOCALVALUE_H_INCLUDED +#define BEAST_THREAD_THREADLOCALVALUE_H_INCLUDED + +#include "../Config.h" + +namespace beast { // (NB: on win32, native thread-locals aren't possible in a dynamically loaded DLL in XP). #if ! ((BEAST_MSVC && (BEAST_64BIT || ! defined (BeastPlugin_PluginCode))) \ @@ -186,5 +190,6 @@ private: #endif }; +} -#endif // BEAST_THREADLOCALVALUE_H_INCLUDED +#endif diff --git a/src/beast/beast/thread/impl/RecursiveMutex.cpp b/src/beast/beast/thread/impl/RecursiveMutex.cpp index 0fce37f4c0..414f883c91 100644 --- a/src/beast/beast/thread/impl/RecursiveMutex.cpp +++ b/src/beast/beast/thread/impl/RecursiveMutex.cpp @@ -28,6 +28,11 @@ #include "../StaticAssert.h" #include +#undef check +#undef direct +#undef max +#undef min +#undef TYPE_BOOL namespace beast { diff --git a/src/beast/modules/beast_core/thread/ServiceQueue.cpp b/src/beast/beast/thread/impl/ServiceQueue.cpp similarity index 99% rename from src/beast/modules/beast_core/thread/ServiceQueue.cpp rename to src/beast/beast/thread/impl/ServiceQueue.cpp index e03b59071c..ddf9bd4f0d 100644 --- a/src/beast/modules/beast_core/thread/ServiceQueue.cpp +++ b/src/beast/beast/thread/impl/ServiceQueue.cpp @@ -17,6 +17,10 @@ */ //============================================================================== +#include "../ServiceQueue.h" + +#include "../../../modules/beast_core/beast_core.h" // for UnitTest + namespace beast { namespace detail { diff --git a/src/beast/beast/thread/impl/WaitableEvent.cpp b/src/beast/beast/thread/impl/WaitableEvent.cpp index 0a976c783a..9e882c499b 100644 --- a/src/beast/beast/thread/impl/WaitableEvent.cpp +++ b/src/beast/beast/thread/impl/WaitableEvent.cpp @@ -26,6 +26,11 @@ #if BEAST_WINDOWS #include +#undef check +#undef direct +#undef max +#undef min +#undef TYPE_BOOL namespace beast { diff --git a/src/beast/modules/beast_core/beast_core.cpp b/src/beast/modules/beast_core/beast_core.cpp index ba83844d7f..da04f4dbe1 100644 --- a/src/beast/modules/beast_core/beast_core.cpp +++ b/src/beast/modules/beast_core/beast_core.cpp @@ -274,8 +274,6 @@ namespace beast } -#include "thread/ServiceQueue.cpp" - // Has to be outside the beast namespace extern "C" { void beast_reportFatalError (char const* message, char const* fileName, int lineNumber) diff --git a/src/beast/modules/beast_core/beast_core.h b/src/beast/modules/beast_core/beast_core.h index 1ea063eb4e..aa34fde6cb 100644 --- a/src/beast/modules/beast_core/beast_core.h +++ b/src/beast/modules/beast_core/beast_core.h @@ -149,8 +149,6 @@ class FileOutputStream; #include "memory/CacheLine.h" #include "threads/ReadWriteMutex.h" #include "threads/Thread.h" -#include "threads/SpinLock.h" -#include "threads/ThreadLocalValue.h" #include "thread/MutexTraits.h" #include "thread/TrackedMutex.h" #include "diagnostic/FatalError.h" @@ -245,8 +243,6 @@ class FileOutputStream; } -#include "thread/ServiceQueue.h" - #if BEAST_MSVC #pragma warning (pop) #endif