| OLD | NEW | 
|---|
| 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 SkSharedLock_DEFINED | 8 #ifndef SkSharedLock_DEFINED | 
| 9 #define SkSharedLock_DEFINED | 9 #define SkSharedLock_DEFINED | 
| 10 | 10 | 
| 11 #include "SkAtomics.h" | 11 #include "SkAtomics.h" | 
| 12 #include "SkSemaphore.h" | 12 #include "SkSemaphore.h" | 
| 13 #include "SkTypes.h" | 13 #include "SkTypes.h" | 
| 14 | 14 | 
| 15 #ifdef SK_DEBUG | 15 #ifdef SK_DEBUG | 
| 16     #include "SkMutex.h" | 16     #include "SkMutex.h" | 
| 17     #include "SkUniquePtr.h" | 17     #include <memory> | 
| 18 #endif  // SK_DEBUG | 18 #endif  // SK_DEBUG | 
| 19 | 19 | 
| 20 // There are two shared lock implementations one debug the other is high perform
    ance. They implement | 20 // There are two shared lock implementations one debug the other is high perform
    ance. They implement | 
| 21 // an interface similar to pthread's rwlocks. | 21 // an interface similar to pthread's rwlocks. | 
| 22 // This is a shared lock implementation similar to pthreads rwlocks. The high pe
    rformance | 22 // This is a shared lock implementation similar to pthreads rwlocks. The high pe
    rformance | 
| 23 // implementation is cribbed from Preshing's article: | 23 // implementation is cribbed from Preshing's article: | 
| 24 // http://preshing.com/20150316/semaphores-are-surprisingly-versatile/ | 24 // http://preshing.com/20150316/semaphores-are-surprisingly-versatile/ | 
| 25 // | 25 // | 
| 26 // This lock does not obey strict queue ordering. It will always alternate betwe
    en readers and | 26 // This lock does not obey strict queue ordering. It will always alternate betwe
    en readers and | 
| 27 // a single writer. | 27 // a single writer. | 
| (...skipping 15 matching lines...) Expand all  Loading... | 
| 43 | 43 | 
| 44     // Release lock for shared use. | 44     // Release lock for shared use. | 
| 45     void releaseShared(); | 45     void releaseShared(); | 
| 46 | 46 | 
| 47     // Fail if shared lock not held. | 47     // Fail if shared lock not held. | 
| 48     void assertHeldShared() const; | 48     void assertHeldShared() const; | 
| 49 | 49 | 
| 50 private: | 50 private: | 
| 51 #ifdef SK_DEBUG | 51 #ifdef SK_DEBUG | 
| 52     class ThreadIDSet; | 52     class ThreadIDSet; | 
| 53     skstd::unique_ptr<ThreadIDSet> fCurrentShared; | 53     std::unique_ptr<ThreadIDSet> fCurrentShared; | 
| 54     skstd::unique_ptr<ThreadIDSet> fWaitingExclusive; | 54     std::unique_ptr<ThreadIDSet> fWaitingExclusive; | 
| 55     skstd::unique_ptr<ThreadIDSet> fWaitingShared; | 55     std::unique_ptr<ThreadIDSet> fWaitingShared; | 
| 56     int fSharedQueueSelect{0}; | 56     int fSharedQueueSelect{0}; | 
| 57     mutable SkMutex fMu; | 57     mutable SkMutex fMu; | 
| 58     SkSemaphore fSharedQueue[2]; | 58     SkSemaphore fSharedQueue[2]; | 
| 59     SkSemaphore fExclusiveQueue; | 59     SkSemaphore fExclusiveQueue; | 
| 60 #else | 60 #else | 
| 61     SkAtomic<int32_t> fQueueCounts; | 61     SkAtomic<int32_t> fQueueCounts; | 
| 62     SkSemaphore       fSharedQueue; | 62     SkSemaphore       fSharedQueue; | 
| 63     SkSemaphore       fExclusiveQueue; | 63     SkSemaphore       fExclusiveQueue; | 
| 64 #endif  // SK_DEBUG | 64 #endif  // SK_DEBUG | 
| 65 }; | 65 }; | 
| 66 | 66 | 
| 67 #ifndef SK_DEBUG | 67 #ifndef SK_DEBUG | 
| 68 inline void SkSharedMutex::assertHeld() const {}; | 68 inline void SkSharedMutex::assertHeld() const {}; | 
| 69 inline void SkSharedMutex::assertHeldShared() const {}; | 69 inline void SkSharedMutex::assertHeldShared() const {}; | 
| 70 #endif  // SK_DEBUG | 70 #endif  // SK_DEBUG | 
| 71 | 71 | 
| 72 class SkAutoSharedMutexShared { | 72 class SkAutoSharedMutexShared { | 
| 73 public: | 73 public: | 
| 74     SkAutoSharedMutexShared(SkSharedMutex& lock) : fLock(lock) { lock.acquireSha
    red(); } | 74     SkAutoSharedMutexShared(SkSharedMutex& lock) : fLock(lock) { lock.acquireSha
    red(); } | 
| 75     ~SkAutoSharedMutexShared() { fLock.releaseShared(); } | 75     ~SkAutoSharedMutexShared() { fLock.releaseShared(); } | 
| 76 private: | 76 private: | 
| 77     SkSharedMutex& fLock; | 77     SkSharedMutex& fLock; | 
| 78 }; | 78 }; | 
| 79 | 79 | 
| 80 #define SkAutoSharedMutexShared(...) SK_REQUIRE_LOCAL_VAR(SkAutoSharedMutexShare
    d) | 80 #define SkAutoSharedMutexShared(...) SK_REQUIRE_LOCAL_VAR(SkAutoSharedMutexShare
    d) | 
| 81 | 81 | 
| 82 #endif // SkSharedLock_DEFINED | 82 #endif // SkSharedLock_DEFINED | 
| OLD | NEW | 
|---|