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 | |
| 11 | #include "SkAtomics.h" |
| 12 | #include "SkSemaphore.h" |
| 13 | #include "SkTypes.h" |
herb | 966e3d3 | 2015-09-18 07:00:48 -0700 | [diff] [blame^] | 14 | |
| 15 | #ifdef SK_DEBUG |
| 16 | #include "SkMutex.h" |
| 17 | #include "../private/SkUniquePtr.h" |
| 18 | #endif // SK_DEBUG |
| 19 | |
| 20 | // There are two shared lock implementations one debug the other is high performance. They implement |
| 21 | // an interface similar to pthread's rwlocks. |
| 22 | // This is a shared lock implementation similar to pthreads rwlocks. The high performance |
| 23 | // implementation is cribbed from Preshing's article: |
herb | c782b2a | 2015-06-29 13:46:55 -0700 | [diff] [blame] | 24 | // http://preshing.com/20150316/semaphores-are-surprisingly-versatile/ |
| 25 | // |
| 26 | // This lock does not obey strict queue ordering. It will always alternate between readers and |
| 27 | // a single writer. |
| 28 | class SkSharedMutex { |
| 29 | public: |
| 30 | SkSharedMutex(); |
herb | dec1afc | 2015-06-30 14:12:16 -0700 | [diff] [blame] | 31 | ~SkSharedMutex(); |
herb | c782b2a | 2015-06-29 13:46:55 -0700 | [diff] [blame] | 32 | // Acquire lock for exclusive use. |
| 33 | void acquire(); |
| 34 | |
| 35 | // Release lock for exclusive use. |
| 36 | void release(); |
| 37 | |
herb | ab42ec7 | 2015-08-19 13:40:12 -0700 | [diff] [blame] | 38 | // Fail if exclusive is not held. |
herb | ab42ec7 | 2015-08-19 13:40:12 -0700 | [diff] [blame] | 39 | void assertHeld() const; |
herb | ab42ec7 | 2015-08-19 13:40:12 -0700 | [diff] [blame] | 40 | |
herb | c782b2a | 2015-06-29 13:46:55 -0700 | [diff] [blame] | 41 | // Acquire lock for shared use. |
| 42 | void acquireShared(); |
| 43 | |
| 44 | // Release lock for shared use. |
| 45 | void releaseShared(); |
| 46 | |
herb | ab42ec7 | 2015-08-19 13:40:12 -0700 | [diff] [blame] | 47 | // Fail if shared lock not held. |
herb | ab42ec7 | 2015-08-19 13:40:12 -0700 | [diff] [blame] | 48 | void assertHeldShared() const; |
herb | ab42ec7 | 2015-08-19 13:40:12 -0700 | [diff] [blame] | 49 | |
herb | c782b2a | 2015-06-29 13:46:55 -0700 | [diff] [blame] | 50 | private: |
herb | 966e3d3 | 2015-09-18 07:00:48 -0700 | [diff] [blame^] | 51 | #ifdef SK_DEBUG |
| 52 | class ThreadIDSet; |
| 53 | skstd::unique_ptr<ThreadIDSet> fCurrentShared; |
| 54 | skstd::unique_ptr<ThreadIDSet> fWaitingExclusive; |
| 55 | skstd::unique_ptr<ThreadIDSet> fWaitingShared; |
| 56 | int fSharedQueueSelect{0}; |
| 57 | mutable SkMutex fMu; |
| 58 | SkSemaphore fSharedQueue[2]; |
herb | c782b2a | 2015-06-29 13:46:55 -0700 | [diff] [blame] | 59 | SkSemaphore fExclusiveQueue; |
herb | 966e3d3 | 2015-09-18 07:00:48 -0700 | [diff] [blame^] | 60 | #else |
| 61 | SkAtomic<int32_t> fQueueCounts; |
| 62 | SkSemaphore fSharedQueue; |
| 63 | SkSemaphore fExclusiveQueue; |
| 64 | #endif // SK_DEBUG |
herb | c782b2a | 2015-06-29 13:46:55 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
herb | 966e3d3 | 2015-09-18 07:00:48 -0700 | [diff] [blame^] | 67 | #ifndef SK_DEBUG |
| 68 | inline void SkSharedMutex::assertHeld() const {}; |
| 69 | inline void SkSharedMutex::assertHeldShared() const {}; |
| 70 | #endif // SK_DEBUG |
herb | ab42ec7 | 2015-08-19 13:40:12 -0700 | [diff] [blame] | 71 | |
herb | c782b2a | 2015-06-29 13:46:55 -0700 | [diff] [blame] | 72 | #endif // SkSharedLock_DEFINED |