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 // This is a shared lock implementation similar to pthreads rwlocks. This implem
entation is | 15 #ifdef SK_DEBUG |
16 // cribbed from Preshing's article: | 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 perform
ance. They implement |
| 21 // an interface similar to pthread's rwlocks. |
| 22 // This is a shared lock implementation similar to pthreads rwlocks. The high pe
rformance |
| 23 // implementation is cribbed from Preshing's article: |
17 // http://preshing.com/20150316/semaphores-are-surprisingly-versatile/ | 24 // http://preshing.com/20150316/semaphores-are-surprisingly-versatile/ |
18 // | 25 // |
19 // 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 |
20 // a single writer. | 27 // a single writer. |
21 class SkSharedMutex { | 28 class SkSharedMutex { |
22 public: | 29 public: |
23 SkSharedMutex(); | 30 SkSharedMutex(); |
24 ~SkSharedMutex(); | 31 ~SkSharedMutex(); |
25 // Acquire lock for exclusive use. | 32 // Acquire lock for exclusive use. |
26 void acquire(); | 33 void acquire(); |
27 | 34 |
28 // Release lock for exclusive use. | 35 // Release lock for exclusive use. |
29 void release(); | 36 void release(); |
30 | 37 |
31 // Fail if exclusive is not held. | 38 // Fail if exclusive is not held. |
32 #ifdef SK_DEBUG | |
33 void assertHeld() const; | 39 void assertHeld() const; |
34 #else | |
35 void assertHeld() const {} | |
36 #endif | |
37 | 40 |
38 // Acquire lock for shared use. | 41 // Acquire lock for shared use. |
39 void acquireShared(); | 42 void acquireShared(); |
40 | 43 |
41 // Release lock for shared use. | 44 // Release lock for shared use. |
42 void releaseShared(); | 45 void releaseShared(); |
43 | 46 |
44 // Fail if shared lock not held. | 47 // Fail if shared lock not held. |
45 #ifdef SK_DEBUG | |
46 void assertHeldShared() const; | 48 void assertHeldShared() const; |
47 #else | |
48 void assertHeldShared() const {} | |
49 #endif | |
50 | 49 |
51 private: | 50 private: |
| 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]; |
| 59 SkSemaphore fExclusiveQueue; |
| 60 #else |
52 SkAtomic<int32_t> fQueueCounts; | 61 SkAtomic<int32_t> fQueueCounts; |
53 SkSemaphore fSharedQueue; | 62 SkSemaphore fSharedQueue; |
54 SkSemaphore fExclusiveQueue; | 63 SkSemaphore fExclusiveQueue; |
| 64 #endif // SK_DEBUG |
55 }; | 65 }; |
56 | 66 |
| 67 #ifndef SK_DEBUG |
| 68 inline void SkSharedMutex::assertHeld() const {}; |
| 69 inline void SkSharedMutex::assertHeldShared() const {}; |
| 70 #endif // SK_DEBUG |
57 | 71 |
58 #endif // SkSharedLock_DEFINED | 72 #endif // SkSharedLock_DEFINED |
OLD | NEW |