blob: 2544ee605a47cb0eb38c43b797c7a06211cd46e2 [file] [log] [blame]
herbc782b2a2015-06-29 13:46:55 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkTypes.h"
12#include "include/private/SkMacros.h"
13#include "include/private/SkSemaphore.h"
Herb Derby209ebc02019-05-13 13:38:09 -040014#include "include/private/SkThreadAnnotations.h"
Mike Klein015c8992018-08-09 12:23:19 -040015#include <atomic>
herb966e3d32015-09-18 07:00:48 -070016
17#ifdef SK_DEBUG
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018 #include "include/private/SkMutex.h"
mtklein5f939ab2016-03-16 10:28:35 -070019 #include <memory>
herb966e3d32015-09-18 07:00:48 -070020#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:
herbc782b2a2015-06-29 13:46:55 -070026// 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 Derby209ebc02019-05-13 13:38:09 -040030class SK_CAPABILITY("mutex") SkSharedMutex {
herbc782b2a2015-06-29 13:46:55 -070031public:
32 SkSharedMutex();
herbdec1afc2015-06-30 14:12:16 -070033 ~SkSharedMutex();
herbc782b2a2015-06-29 13:46:55 -070034 // Acquire lock for exclusive use.
Herb Derby209ebc02019-05-13 13:38:09 -040035 void acquire() SK_ACQUIRE();
herbc782b2a2015-06-29 13:46:55 -070036
37 // Release lock for exclusive use.
Herb Derby209ebc02019-05-13 13:38:09 -040038 void release() SK_RELEASE_CAPABILITY();
herbc782b2a2015-06-29 13:46:55 -070039
herbab42ec72015-08-19 13:40:12 -070040 // Fail if exclusive is not held.
Herb Derby209ebc02019-05-13 13:38:09 -040041 void assertHeld() const SK_ASSERT_CAPABILITY(this);
herbab42ec72015-08-19 13:40:12 -070042
herbc782b2a2015-06-29 13:46:55 -070043 // Acquire lock for shared use.
Herb Derby209ebc02019-05-13 13:38:09 -040044 void acquireShared() SK_ACQUIRE_SHARED();
herbc782b2a2015-06-29 13:46:55 -070045
46 // Release lock for shared use.
Herb Derby209ebc02019-05-13 13:38:09 -040047 void releaseShared() SK_RELEASE_SHARED_CAPABILITY();
herbc782b2a2015-06-29 13:46:55 -070048
herbab42ec72015-08-19 13:40:12 -070049 // Fail if shared lock not held.
Herb Derby209ebc02019-05-13 13:38:09 -040050 void assertHeldShared() const SK_ASSERT_SHARED_CAPABILITY(this);
herbab42ec72015-08-19 13:40:12 -070051
herbc782b2a2015-06-29 13:46:55 -070052private:
herb966e3d32015-09-18 07:00:48 -070053#ifdef SK_DEBUG
54 class ThreadIDSet;
mtklein5f939ab2016-03-16 10:28:35 -070055 std::unique_ptr<ThreadIDSet> fCurrentShared;
56 std::unique_ptr<ThreadIDSet> fWaitingExclusive;
57 std::unique_ptr<ThreadIDSet> fWaitingShared;
herb966e3d32015-09-18 07:00:48 -070058 int fSharedQueueSelect{0};
59 mutable SkMutex fMu;
60 SkSemaphore fSharedQueue[2];
herbc782b2a2015-06-29 13:46:55 -070061 SkSemaphore fExclusiveQueue;
herb966e3d32015-09-18 07:00:48 -070062#else
Mike Klein015c8992018-08-09 12:23:19 -040063 std::atomic<int32_t> fQueueCounts;
64 SkSemaphore fSharedQueue;
65 SkSemaphore fExclusiveQueue;
herb966e3d32015-09-18 07:00:48 -070066#endif // SK_DEBUG
herbc782b2a2015-06-29 13:46:55 -070067};
68
herb966e3d32015-09-18 07:00:48 -070069#ifndef SK_DEBUG
70inline void SkSharedMutex::assertHeld() const {};
71inline void SkSharedMutex::assertHeldShared() const {};
72#endif // SK_DEBUG
herbab42ec72015-08-19 13:40:12 -070073
Herb Derby209ebc02019-05-13 13:38:09 -040074class SK_SCOPED_CAPABILITY SkAutoSharedMutexExclusive {
herb12449a92015-10-21 19:11:13 -070075public:
Herb Derby209ebc02019-05-13 13:38:09 -040076 explicit SkAutoSharedMutexExclusive(SkSharedMutex& lock) SK_ACQUIRE(lock)
77 : fLock(lock) {
78 lock.acquire();
79 }
80 ~SkAutoSharedMutexExclusive() SK_RELEASE_CAPABILITY() { fLock.release(); }
81
82private:
83 SkSharedMutex& fLock;
84};
85
86#define SkAutoSharedMutexExclusive(...) SK_REQUIRE_LOCAL_VAR(SkAutoSharedMutexExclusive)
87
88class SK_SCOPED_CAPABILITY SkAutoSharedMutexShared {
89public:
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
herb12449a92015-10-21 19:11:13 -0700100private:
101 SkSharedMutex& fLock;
102};
103
104#define SkAutoSharedMutexShared(...) SK_REQUIRE_LOCAL_VAR(SkAutoSharedMutexShared)
105
herbc782b2a2015-06-29 13:46:55 -0700106#endif // SkSharedLock_DEFINED