diff options
author | 2015-09-24 07:34:49 -0700 | |
---|---|---|
committer | 2015-09-24 07:34:49 -0700 | |
commit | 7f0a3d7523377097184309152f883f1fcb12a4b8 (patch) | |
tree | b7f6d9ee1208ead229a40240d26d765dd446c2ae /include/private | |
parent | c6363ef7b4763b36a9a0d255bc775a973d2fc7a5 (diff) |
Make mutex semaphore based.
This implementation improves performance of SkMutex acquire / release pair from 42ns -> 13 ns.
SkSharedMutex and SkSpinlock have the same performance.
It also removes specialized windows and linux/mac code.
BUG=skia:
Review URL: https://codereview.chromium.org/1359733002
Diffstat (limited to 'include/private')
-rw-r--r-- | include/private/SkOncePtr.h | 17 | ||||
-rw-r--r-- | include/private/SkSemaphore.h | 86 |
2 files changed, 94 insertions, 9 deletions
diff --git a/include/private/SkOncePtr.h b/include/private/SkOncePtr.h index ee30d8f8db..a1c028c4af 100644 --- a/include/private/SkOncePtr.h +++ b/include/private/SkOncePtr.h @@ -14,7 +14,7 @@ template <typename T> class SkBaseOncePtr; // Use this to create a global static pointer that's intialized exactly once when you call get(). -#define SK_DECLARE_STATIC_ONCE_PTR(type, name) namespace {} static SkBaseOncePtr<type> name +#define SK_DECLARE_STATIC_ONCE_PTR(type, name) namespace {} static SkBaseOncePtr<type> name; // Use this for a local or member pointer that's initialized exactly once when you call get(). template <typename T, typename Delete = skstd::default_delete<T>> @@ -61,17 +61,17 @@ class SkBaseOncePtr { public: template <typename F> T* get(const F& f) const { - uintptr_t state = fState.load(sk_memory_order_acquire); + uintptr_t state = sk_atomic_load(&fState, sk_memory_order_acquire); if (state < 2) { if (state == 0) { // It looks like no one has tried to create our pointer yet. // We try to claim that task by atomically swapping our state from '0' to '1'. - if (fState.compare_exchange(&state, 1, sk_memory_order_relaxed, - sk_memory_order_relaxed)) { + if (sk_atomic_compare_exchange( + &fState, &state, (uintptr_t)1, sk_memory_order_relaxed, sk_memory_order_relaxed)) { // We've claimed it. Create our pointer and store it into fState. state = (uintptr_t)f(); SkASSERT(state > 1); - fState.store(state, sk_memory_order_release); + sk_atomic_store(&fState, state, sk_memory_order_release); } else { // Someone else claimed it. // We fall through to the spin loop just below to wait for them to finish. @@ -82,7 +82,7 @@ public: // State '1' is our busy-but-not-done state. // Some other thread has claimed the job of creating our pointer. // We just need to wait for it to finish. - state = fState.load(sk_memory_order_acquire); + state = sk_atomic_load(&fState, sk_memory_order_acquire); } // We shouldn't be able to get here without having created our pointer. @@ -92,16 +92,15 @@ public: } operator T*() const { - auto state = fState.load(sk_memory_order_acquire); + auto state = sk_atomic_load(&fState, sk_memory_order_acquire); return state < 2 ? nullptr : (T*)state; // TODO: If state == 1 spin until it's not? } -private: // fState == 0 --> we have not created our ptr yet // fState == 1 --> someone is in the middle of creating our ptr // else --> (T*)fState is our ptr - mutable SkAtomic<uintptr_t> fState; + mutable uintptr_t fState; }; #endif//SkOncePtr_DEFINED diff --git a/include/private/SkSemaphore.h b/include/private/SkSemaphore.h new file mode 100644 index 0000000000..b5b5ea5957 --- /dev/null +++ b/include/private/SkSemaphore.h @@ -0,0 +1,86 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkSemaphore_DEFINED +#define SkSemaphore_DEFINED + +#include "SkTypes.h" +#include "SkAtomics.h" +#include "../private/SkOncePtr.h" + +struct SkBaseSemaphore { + + // 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); + } + } + + // Increment the counter N times. + // Generally it's better to call signal(N) instead of signal() N times. + void signal(int N); + + // 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(); + + // This implementation follows the general strategy of + // 'A Lightweight Semaphore with Partial Spinning' + // found here + // http://preshing.com/20150316/semaphores-are-surprisingly-versatile/ + // That article (and entire blog) are very much worth reading. + // + // 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; +}; + +/** + * 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 wait(); + + void signal(int n = 1); + +private: + SkBaseSemaphore fBaseSemaphore; +}; + +#endif//SkSemaphore_DEFINED |