herb | c782b2a | 2015-06-29 13:46:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkSharedLock_DEFINED |
| 9 | #define SkSharedLock_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkTypes.h" |
| 12 | #include "include/private/SkMacros.h" |
| 13 | #include "include/private/SkSemaphore.h" |
Herb Derby | 209ebc0 | 2019-05-13 13:38:09 -0400 | [diff] [blame] | 14 | #include "include/private/SkThreadAnnotations.h" |
Mike Klein | 015c899 | 2018-08-09 12:23:19 -0400 | [diff] [blame] | 15 | #include <atomic> |
herb | 966e3d3 | 2015-09-18 07:00:48 -0700 | [diff] [blame] | 16 | |
| 17 | #ifdef SK_DEBUG |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "include/private/SkMutex.h" |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 19 | #include <memory> |
herb | 966e3d3 | 2015-09-18 07:00:48 -0700 | [diff] [blame] | 20 | #endif // SK_DEBUG |
| 21 | |
| 22 | // There are two shared lock implementations one debug the other is high performance. They implement |
| 23 | // an interface similar to pthread's rwlocks. |
| 24 | // This is a shared lock implementation similar to pthreads rwlocks. The high performance |
| 25 | // implementation is cribbed from Preshing's article: |
herb | c782b2a | 2015-06-29 13:46:55 -0700 | [diff] [blame] | 26 | // http://preshing.com/20150316/semaphores-are-surprisingly-versatile/ |
| 27 | // |
| 28 | // This lock does not obey strict queue ordering. It will always alternate between readers and |
| 29 | // a single writer. |
Herb Derby | 209ebc0 | 2019-05-13 13:38:09 -0400 | [diff] [blame] | 30 | class SK_CAPABILITY("mutex") SkSharedMutex { |
herb | c782b2a | 2015-06-29 13:46:55 -0700 | [diff] [blame] | 31 | public: |
| 32 | SkSharedMutex(); |
herb | dec1afc | 2015-06-30 14:12:16 -0700 | [diff] [blame] | 33 | ~SkSharedMutex(); |
herb | c782b2a | 2015-06-29 13:46:55 -0700 | [diff] [blame] | 34 | // Acquire lock for exclusive use. |
Herb Derby | 209ebc0 | 2019-05-13 13:38:09 -0400 | [diff] [blame] | 35 | void acquire() SK_ACQUIRE(); |
herb | c782b2a | 2015-06-29 13:46:55 -0700 | [diff] [blame] | 36 | |
| 37 | // Release lock for exclusive use. |
Herb Derby | 209ebc0 | 2019-05-13 13:38:09 -0400 | [diff] [blame] | 38 | void release() SK_RELEASE_CAPABILITY(); |
herb | c782b2a | 2015-06-29 13:46:55 -0700 | [diff] [blame] | 39 | |
herb | ab42ec7 | 2015-08-19 13:40:12 -0700 | [diff] [blame] | 40 | // Fail if exclusive is not held. |
Herb Derby | 209ebc0 | 2019-05-13 13:38:09 -0400 | [diff] [blame] | 41 | void assertHeld() const SK_ASSERT_CAPABILITY(this); |
herb | ab42ec7 | 2015-08-19 13:40:12 -0700 | [diff] [blame] | 42 | |
herb | c782b2a | 2015-06-29 13:46:55 -0700 | [diff] [blame] | 43 | // Acquire lock for shared use. |
Herb Derby | 209ebc0 | 2019-05-13 13:38:09 -0400 | [diff] [blame] | 44 | void acquireShared() SK_ACQUIRE_SHARED(); |
herb | c782b2a | 2015-06-29 13:46:55 -0700 | [diff] [blame] | 45 | |
| 46 | // Release lock for shared use. |
Herb Derby | 209ebc0 | 2019-05-13 13:38:09 -0400 | [diff] [blame] | 47 | void releaseShared() SK_RELEASE_SHARED_CAPABILITY(); |
herb | c782b2a | 2015-06-29 13:46:55 -0700 | [diff] [blame] | 48 | |
herb | ab42ec7 | 2015-08-19 13:40:12 -0700 | [diff] [blame] | 49 | // Fail if shared lock not held. |
Herb Derby | 209ebc0 | 2019-05-13 13:38:09 -0400 | [diff] [blame] | 50 | void assertHeldShared() const SK_ASSERT_SHARED_CAPABILITY(this); |
herb | ab42ec7 | 2015-08-19 13:40:12 -0700 | [diff] [blame] | 51 | |
herb | c782b2a | 2015-06-29 13:46:55 -0700 | [diff] [blame] | 52 | private: |
herb | 966e3d3 | 2015-09-18 07:00:48 -0700 | [diff] [blame] | 53 | #ifdef SK_DEBUG |
| 54 | class ThreadIDSet; |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 55 | std::unique_ptr<ThreadIDSet> fCurrentShared; |
| 56 | std::unique_ptr<ThreadIDSet> fWaitingExclusive; |
| 57 | std::unique_ptr<ThreadIDSet> fWaitingShared; |
herb | 966e3d3 | 2015-09-18 07:00:48 -0700 | [diff] [blame] | 58 | int fSharedQueueSelect{0}; |
| 59 | mutable SkMutex fMu; |
| 60 | SkSemaphore fSharedQueue[2]; |
herb | c782b2a | 2015-06-29 13:46:55 -0700 | [diff] [blame] | 61 | SkSemaphore fExclusiveQueue; |
herb | 966e3d3 | 2015-09-18 07:00:48 -0700 | [diff] [blame] | 62 | #else |
Mike Klein | 015c899 | 2018-08-09 12:23:19 -0400 | [diff] [blame] | 63 | std::atomic<int32_t> fQueueCounts; |
| 64 | SkSemaphore fSharedQueue; |
| 65 | SkSemaphore fExclusiveQueue; |
herb | 966e3d3 | 2015-09-18 07:00:48 -0700 | [diff] [blame] | 66 | #endif // SK_DEBUG |
herb | c782b2a | 2015-06-29 13:46:55 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
herb | 966e3d3 | 2015-09-18 07:00:48 -0700 | [diff] [blame] | 69 | #ifndef SK_DEBUG |
| 70 | inline void SkSharedMutex::assertHeld() const {}; |
| 71 | inline void SkSharedMutex::assertHeldShared() const {}; |
| 72 | #endif // SK_DEBUG |
herb | ab42ec7 | 2015-08-19 13:40:12 -0700 | [diff] [blame] | 73 | |
Herb Derby | 209ebc0 | 2019-05-13 13:38:09 -0400 | [diff] [blame] | 74 | class SK_SCOPED_CAPABILITY SkAutoSharedMutexExclusive { |
herb | 12449a9 | 2015-10-21 19:11:13 -0700 | [diff] [blame] | 75 | public: |
Herb Derby | 209ebc0 | 2019-05-13 13:38:09 -0400 | [diff] [blame] | 76 | explicit SkAutoSharedMutexExclusive(SkSharedMutex& lock) SK_ACQUIRE(lock) |
| 77 | : fLock(lock) { |
| 78 | lock.acquire(); |
| 79 | } |
| 80 | ~SkAutoSharedMutexExclusive() SK_RELEASE_CAPABILITY() { fLock.release(); } |
| 81 | |
| 82 | private: |
| 83 | SkSharedMutex& fLock; |
| 84 | }; |
| 85 | |
| 86 | #define SkAutoSharedMutexExclusive(...) SK_REQUIRE_LOCAL_VAR(SkAutoSharedMutexExclusive) |
| 87 | |
| 88 | class SK_SCOPED_CAPABILITY SkAutoSharedMutexShared { |
| 89 | public: |
| 90 | explicit SkAutoSharedMutexShared(SkSharedMutex& lock) SK_ACQUIRE_SHARED(lock) |
| 91 | : fLock(lock) { |
| 92 | lock.acquireShared(); |
| 93 | } |
| 94 | |
| 95 | // You would think this should be SK_RELEASE_SHARED_CAPABILITY, but SK_SCOPED_CAPABILITY |
| 96 | // doesn't fully understand the difference between shared and exclusive. |
| 97 | // Please review https://reviews.llvm.org/D52578 for more information. |
| 98 | ~SkAutoSharedMutexShared() SK_RELEASE_CAPABILITY() { fLock.releaseShared(); } |
| 99 | |
herb | 12449a9 | 2015-10-21 19:11:13 -0700 | [diff] [blame] | 100 | private: |
| 101 | SkSharedMutex& fLock; |
| 102 | }; |
| 103 | |
| 104 | #define SkAutoSharedMutexShared(...) SK_REQUIRE_LOCAL_VAR(SkAutoSharedMutexShared) |
| 105 | |
herb | c782b2a | 2015-06-29 13:46:55 -0700 | [diff] [blame] | 106 | #endif // SkSharedLock_DEFINED |