aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/private/SkMutex.h
diff options
context:
space:
mode:
authorGravatar mtklein <mtklein@chromium.org>2016-05-04 11:31:29 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-05-04 11:31:29 -0700
commit427c2819d9237d7d7729c59238036cfc73c072ea (patch)
tree90d1d7ebba7d16bfbe56a6f59c6cf135c7161acf /include/private/SkMutex.h
parent325474dd42db6d5a16cc4cf18f06dad4e0d60e9f (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/private/SkMutex.h')
-rw-r--r--include/private/SkMutex.h56
1 files changed, 16 insertions, 40 deletions
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