Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(662)

Side by Side Diff: include/private/SkMutex.h

Issue 1948233002: Revert "Modernize SkMutex and SkSemaphore." (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/ports/SkFontConfigInterface.h ('k') | include/private/SkSemaphore.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef SkMutex_DEFINED 8 #ifndef SkMutex_DEFINED
9 #define SkMutex_DEFINED 9 #define SkMutex_DEFINED
10 10
11 // This file is not part of the public Skia API.
11 #include "../private/SkSemaphore.h" 12 #include "../private/SkSemaphore.h"
12 #include "../private/SkThreadID.h"
13 #include "SkTypes.h" 13 #include "SkTypes.h"
14 14
15 class SkMutex { 15 #ifdef SK_DEBUG
16 public: 16 #include "../private/SkThreadID.h"
17 constexpr SkMutex() = default; 17 #endif
18 18
19 SkMutex(const SkMutex&) = delete; 19 #define SK_MUTEX_SEMAPHORE_INIT {1, {0}}
20 SkMutex& operator=(const SkMutex&) = delete;
21 20
21 #ifdef SK_DEBUG
22 #define SK_BASE_MUTEX_INIT {SK_MUTEX_SEMAPHORE_INIT, 0}
23 #else
24 #define SK_BASE_MUTEX_INIT {SK_MUTEX_SEMAPHORE_INIT}
25 #endif
26
27 // Using POD-style initialization prevents the generation of a static initialize r.
28 //
29 // Without magic statics there are no thread safety guarantees on initialization
30 // of local statics (even POD). As a result, it is illegal to use
31 // SK_DECLARE_STATIC_MUTEX in a function.
32 //
33 // Because SkBaseMutex is not a primitive, a static SkBaseMutex cannot be
34 // initialized in a class with this macro.
35 #define SK_DECLARE_STATIC_MUTEX(name) namespace {} static SkBaseMutex name = SK_ BASE_MUTEX_INIT;
36
37 struct SkBaseMutex {
22 void acquire() { 38 void acquire() {
23 fSemaphore.wait(); 39 fSemaphore.wait();
24 SkDEBUGCODE(fOwner = SkGetThreadID();) 40 SkDEBUGCODE(fOwner = SkGetThreadID();)
25 } 41 }
26 42
27 void release() { 43 void release() {
28 this->assertHeld(); 44 this->assertHeld();
29 SkDEBUGCODE(fOwner = kIllegalThreadID;) 45 SkDEBUGCODE(fOwner = kIllegalThreadID;)
30 fSemaphore.signal(); 46 fSemaphore.signal();
31 } 47 }
32 48
33 void assertHeld() { 49 void assertHeld() {
34 SkASSERT(fOwner == SkGetThreadID()); 50 SkASSERT(fOwner == SkGetThreadID());
35 } 51 }
36 52
37 private: 53 SkBaseSemaphore fSemaphore;
38 SkSemaphore fSemaphore{1}; 54 SkDEBUGCODE(SkThreadID fOwner;)
39 SkDEBUGCODE(SkThreadID fOwner{kIllegalThreadID};) 55 };
56
57 // This needs to use subclassing instead of encapsulation to make SkAutoMutexAcq uire to work.
58 class SkMutex : public SkBaseMutex {
59 public:
60 SkMutex () {
61 fSemaphore = SK_MUTEX_SEMAPHORE_INIT;
62 SkDEBUGCODE(fOwner = kIllegalThreadID);
63 }
64 ~SkMutex () { fSemaphore.deleteSemaphore(); }
65 SkMutex(const SkMutex&) = delete;
66 SkMutex& operator=(const SkMutex&) = delete;
40 }; 67 };
41 68
42 template <typename Lock> 69 template <typename Lock>
43 class SkAutoTAcquire : SkNoncopyable { 70 class SkAutoTAcquire : SkNoncopyable {
44 public: 71 public:
45 explicit SkAutoTAcquire(Lock& mutex) : fMutex(&mutex) { 72 explicit SkAutoTAcquire(Lock& mutex) : fMutex(&mutex) {
46 SkASSERT(fMutex != nullptr); 73 SkASSERT(fMutex != nullptr);
47 mutex.acquire(); 74 mutex.acquire();
48 } 75 }
49 76
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 // mutex, thus removing the check for the null pointer. 109 // mutex, thus removing the check for the null pointer.
83 template <typename Lock> 110 template <typename Lock>
84 class SkAutoTExclusive { 111 class SkAutoTExclusive {
85 public: 112 public:
86 SkAutoTExclusive(Lock& lock) : fLock(lock) { lock.acquire(); } 113 SkAutoTExclusive(Lock& lock) : fLock(lock) { lock.acquire(); }
87 ~SkAutoTExclusive() { fLock.release(); } 114 ~SkAutoTExclusive() { fLock.release(); }
88 private: 115 private:
89 Lock &fLock; 116 Lock &fLock;
90 }; 117 };
91 118
92 typedef SkAutoTAcquire<SkMutex> SkAutoMutexAcquire; 119 typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire;
93 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) 120 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire)
94 121
95 typedef SkAutoTExclusive<SkMutex> SkAutoMutexExclusive; 122 typedef SkAutoTExclusive<SkBaseMutex> SkAutoMutexExclusive;
96 #define SkAutoMutexExclusive(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexExclusive) 123 #define SkAutoMutexExclusive(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexExclusive)
97 124
98 #endif//SkMutex_DEFINED 125 #endif//SkMutex_DEFINED
OLDNEW
« no previous file with comments | « include/ports/SkFontConfigInterface.h ('k') | include/private/SkSemaphore.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698