From: Gunnar Beutner Date: Wed, 12 Nov 2014 05:48:23 +0000 (+0100) Subject: Allow thin mutex calls to be inlined X-Git-Tag: v2.2.0~57 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=83a2e4c35077e19d301c6cc83159c4867ac6a2d6;p=icinga2 Allow thin mutex calls to be inlined refs #7622 --- diff --git a/lib/base/thinmutex.cpp b/lib/base/thinmutex.cpp index b2eedd430..4276b1a69 100644 --- a/lib/base/thinmutex.cpp +++ b/lib/base/thinmutex.cpp @@ -52,6 +52,39 @@ static void InitThinMutex(void) INITIALIZE_ONCE(&InitThinMutex); #endif /* _DEBUG */ +void ThinMutex::LockSlowPath(void) +{ + LockSlowPath(false); +} + +void ThinMutex::LockSlowPath(bool make_native) +{ + unsigned int it = 0; + +#ifdef _WIN32 +# ifdef _WIN64 + while (InterlockedCompareExchange64(&m_Data, THINLOCK_LOCKED, THINLOCK_UNLOCKED) != THINLOCK_UNLOCKED) { +# else /* _WIN64 */ + while (InterlockedCompareExchange(&m_Data, THINLOCK_LOCKED, THINLOCK_UNLOCKED) != THINLOCK_UNLOCKED) { +# endif /* _WIN64 */ +#else /* _WIN32 */ + while (!__sync_bool_compare_and_swap(&m_Data, THINLOCK_UNLOCKED, THINLOCK_LOCKED)) { +#endif /* _WIN32 */ + if (m_Data > THINLOCK_LOCKED) { + LockNative(); + return; + } + + make_native = true; + + Spin(it); + it++; + } + + if (make_native) + MakeNative(); +} + void ThinMutex::MakeNative(void) { boost::mutex *mtx = new boost::mutex(); diff --git a/lib/base/thinmutex.hpp b/lib/base/thinmutex.hpp index 7605937fa..b560f8f5a 100644 --- a/lib/base/thinmutex.hpp +++ b/lib/base/thinmutex.hpp @@ -69,33 +69,22 @@ public: inline void Lock(bool make_native = false) { - bool contended = false; - unsigned int it = 0; - #ifdef _WIN32 # ifdef _WIN64 - while (InterlockedCompareExchange64(&m_Data, THINLOCK_LOCKED, THINLOCK_UNLOCKED) != THINLOCK_UNLOCKED) { + if (InterlockedCompareExchange64(&m_Data, THINLOCK_LOCKED, THINLOCK_UNLOCKED) != THINLOCK_UNLOCKED) { # else /* _WIN64 */ - while (InterlockedCompareExchange(&m_Data, THINLOCK_LOCKED, THINLOCK_UNLOCKED) != THINLOCK_UNLOCKED) { + if (InterlockedCompareExchange(&m_Data, THINLOCK_LOCKED, THINLOCK_UNLOCKED) != THINLOCK_UNLOCKED) { # endif /* _WIN64 */ #else /* _WIN32 */ - while (!__sync_bool_compare_and_swap(&m_Data, THINLOCK_UNLOCKED, THINLOCK_LOCKED)) { + if (!__sync_bool_compare_and_swap(&m_Data, THINLOCK_UNLOCKED, THINLOCK_LOCKED)) { #endif /* _WIN32 */ - if (m_Data > THINLOCK_LOCKED) { - LockNative(); - return; - } - - contended = true; - - Spin(it); - it++; + LockSlowPath(); } - - if (contended || make_native) - MakeNative(); } + void LockSlowPath(void); + void LockSlowPath(bool make_native); + inline void Unlock(void) { #ifdef _WIN32 @@ -112,7 +101,7 @@ public: inline void Inflate(void) { - Lock(true); + LockSlowPath(true); Unlock(); }