diff options
author | mtklein <mtklein@chromium.org> | 2016-05-04 11:31:29 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-05-04 11:31:29 -0700 |
commit | 427c2819d9237d7d7729c59238036cfc73c072ea (patch) | |
tree | 90d1d7ebba7d16bfbe56a6f59c6cf135c7161acf /include | |
parent | 325474dd42db6d5a16cc4cf18f06dad4e0d60e9f (diff) |
Modernize SkMutex and SkSemaphore.
- use <atomic>
- fuse SkMutex and SkBaseMutex
- fuse SkSemaphore and SkBaseSemaphore
Still TODO:
- replace SK_DECLARE_STATIC_MUTEX(name) with static SkMutex name
I just didn't want to bother fixing all that up until I know this CL sticks.
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1947153002
No public API changes.
TBR=reed@google.com
Review-Url: https://codereview.chromium.org/1947153002
Diffstat (limited to 'include')
-rw-r--r-- | include/core/SkPixelRef.h | 2 | ||||
-rw-r--r-- | include/ports/SkFontConfigInterface.h | 1 | ||||
-rw-r--r-- | include/private/SkMutex.h | 56 | ||||
-rw-r--r-- | include/private/SkSemaphore.h | 96 |
4 files changed, 60 insertions, 95 deletions
diff --git a/include/core/SkPixelRef.h b/include/core/SkPixelRef.h index 90e0de540d..d836b2fd7c 100644 --- a/include/core/SkPixelRef.h +++ b/include/core/SkPixelRef.h @@ -342,7 +342,7 @@ protected: /** Return the mutex associated with this pixelref. This value is assigned in the constructor, and cannot change during the lifetime of the object. */ - SkBaseMutex* mutex() const { return &fMutex; } + SkMutex* mutex() const { return &fMutex; } // only call from constructor. Flags this to always be locked, removing // the need to grab the mutex and call onLockPixels/onUnlockPixels. diff --git a/include/ports/SkFontConfigInterface.h b/include/ports/SkFontConfigInterface.h index 72cad0ad6b..9f98e356d2 100644 --- a/include/ports/SkFontConfigInterface.h +++ b/include/ports/SkFontConfigInterface.h @@ -13,7 +13,6 @@ #include "SkRefCnt.h" #include "SkTypeface.h" -struct SkBaseMutex; class SkFontMgr; /** diff --git a/include/private/SkMutex.h b/include/private/SkMutex.h index 8c78e1205c..b9af00f8a8 100644 --- a/include/private/SkMutex.h +++ b/include/private/SkMutex.h @@ -8,33 +8,20 @@ #ifndef SkMutex_DEFINED #define SkMutex_DEFINED -// This file is not part of the public Skia API. #include "../private/SkSemaphore.h" +#include "../private/SkThreadID.h" #include "SkTypes.h" -#ifdef SK_DEBUG - #include "../private/SkThreadID.h" -#endif - -#define SK_MUTEX_SEMAPHORE_INIT {1, {0}} - -#ifdef SK_DEBUG - #define SK_BASE_MUTEX_INIT {SK_MUTEX_SEMAPHORE_INIT, 0} -#else - #define SK_BASE_MUTEX_INIT {SK_MUTEX_SEMAPHORE_INIT} -#endif - -// Using POD-style initialization prevents the generation of a static initializer. -// -// Without magic statics there are no thread safety guarantees on initialization -// of local statics (even POD). As a result, it is illegal to use -// SK_DECLARE_STATIC_MUTEX in a function. -// -// Because SkBaseMutex is not a primitive, a static SkBaseMutex cannot be -// initialized in a class with this macro. -#define SK_DECLARE_STATIC_MUTEX(name) namespace {} static SkBaseMutex name = SK_BASE_MUTEX_INIT; - -struct SkBaseMutex { +// TODO: no need for this anymore. +#define SK_DECLARE_STATIC_MUTEX(name) static SkMutex name; + +class SkMutex { +public: + constexpr SkMutex() = default; + + SkMutex(const SkMutex&) = delete; + SkMutex& operator=(const SkMutex&) = delete; + void acquire() { fSemaphore.wait(); SkDEBUGCODE(fOwner = SkGetThreadID();) @@ -50,20 +37,9 @@ struct SkBaseMutex { SkASSERT(fOwner == SkGetThreadID()); } - SkBaseSemaphore fSemaphore; - SkDEBUGCODE(SkThreadID fOwner;) -}; - -// This needs to use subclassing instead of encapsulation to make SkAutoMutexAcquire to work. -class SkMutex : public SkBaseMutex { -public: - SkMutex () { - fSemaphore = SK_MUTEX_SEMAPHORE_INIT; - SkDEBUGCODE(fOwner = kIllegalThreadID); - } - ~SkMutex () { fSemaphore.deleteSemaphore(); } - SkMutex(const SkMutex&) = delete; - SkMutex& operator=(const SkMutex&) = delete; +private: + SkSemaphore fSemaphore{1}; + SkDEBUGCODE(SkThreadID fOwner{kIllegalThreadID};) }; template <typename Lock> @@ -116,10 +92,10 @@ private: Lock &fLock; }; -typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire; +typedef SkAutoTAcquire<SkMutex> SkAutoMutexAcquire; #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) -typedef SkAutoTExclusive<SkBaseMutex> SkAutoMutexExclusive; +typedef SkAutoTExclusive<SkMutex> SkAutoMutexExclusive; #define SkAutoMutexExclusive(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexExclusive) #endif//SkMutex_DEFINED diff --git a/include/private/SkSemaphore.h b/include/private/SkSemaphore.h index cb2f58da4a..adee574bb7 100644 --- a/include/private/SkSemaphore.h +++ b/include/private/SkSemaphore.h @@ -8,43 +8,26 @@ #ifndef SkSemaphore_DEFINED #define SkSemaphore_DEFINED +#include "../private/SkOnce.h" #include "SkTypes.h" -#include "../private/SkAtomics.h" -#include "../private/SkOncePtr.h" +#include <atomic> -struct SkBaseSemaphore { +class SkSemaphore { +public: + constexpr SkSemaphore(int count = 0) + : fCount(count), fOSSemaphore(nullptr) {} - // Increment the counter by 1. - // This is a specialization for supporting SkMutex. - void signal() { - // Since this fetches the value before the add, 0 indicates that this thread is running and - // no threads are waiting, -1 and below means that threads are waiting, but only signal 1 - // thread to run. - if (sk_atomic_fetch_add(&fCount, 1, sk_memory_order_release) < 0) { - this->osSignal(1); - } - } + ~SkSemaphore(); - // Increment the counter N times. - // Generally it's better to call signal(N) instead of signal() N times. - void signal(int N); + // Increment the counter n times. + // Generally it's better to call signal(n) instead of signal() n times. + void signal(int n = 1); // Decrement the counter by 1, // then if the counter is <= 0, sleep this thread until the counter is > 0. - void wait() { - // Since this fetches the value before the subtract, zero and below means that there are no - // resources left, so the thread needs to wait. - if (sk_atomic_fetch_sub(&fCount, 1, sk_memory_order_acquire) <= 0) { - this->osWait(); - } - } - - struct OSSemaphore; - - void osSignal(int n); - void osWait(); - void deleteSemaphore(); + void wait(); +private: // This implementation follows the general strategy of // 'A Lightweight Semaphore with Partial Spinning' // found here @@ -54,33 +37,40 @@ struct SkBaseSemaphore { // We wrap an OS-provided semaphore with a user-space atomic counter that // lets us avoid interacting with the OS semaphore unless strictly required: // moving the count from >0 to <=0 or vice-versa, i.e. sleeping or waking threads. - int fCount; - SkBaseOncePtr<OSSemaphore> fOSSemaphore; -}; + struct OSSemaphore; -/** - * SkSemaphore is a fast mostly-user-space semaphore. - * - * A semaphore is logically an atomic integer with a few special properties: - * - The integer always starts at 0. - * - You can only increment or decrement it, never read or write it. - * - Increment is spelled 'signal()'; decrement is spelled 'wait()'. - * - If a call to wait() decrements the counter to <= 0, - * the calling thread sleeps until another thread signal()s it back above 0. - */ -class SkSemaphore : SkNoncopyable { -public: - // Initializes the counter to 0. - // (Though all current implementations could start from an arbitrary value.) - SkSemaphore(); - ~SkSemaphore(); + void osSignal(int n); + void osWait(); - void wait(); + std::atomic<int> fCount; + SkOnce fOSSemaphoreOnce; + OSSemaphore* fOSSemaphore; +}; - void signal(int n = 1); +inline void SkSemaphore::signal(int n) { + int prev = fCount.fetch_add(n, std::memory_order_release); -private: - SkBaseSemaphore fBaseSemaphore; -}; + // We only want to call the OS semaphore when our logical count crosses + // from <= 0 to >0 (when we need to wake sleeping threads). + // + // This is easiest to think about with specific examples of prev and n. + // If n == 5 and prev == -3, there are 3 threads sleeping and we signal + // SkTMin(-(-3), 5) == 3 times on the OS semaphore, leaving the count at 2. + // + // If prev >= 0, no threads are waiting, SkTMin(-prev, n) is always <= 0, + // so we don't call the OS semaphore, leaving the count at (prev + n). + int toSignal = SkTMin(-prev, n); + if (toSignal > 0) { + this->osSignal(toSignal); + } +} + +inline void SkSemaphore::wait() { + // Since this fetches the value before the subtract, zero and below means that there are no + // resources left, so the thread needs to wait. + if (fCount.fetch_sub(1, std::memory_order_acquire) <= 0) { + this->osWait(); + } +} #endif//SkSemaphore_DEFINED |