aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkSharedMutex.h
diff options
context:
space:
mode:
authorGravatar herb <herb@google.com>2015-06-29 13:46:55 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2015-06-29 13:46:55 -0700
commitc782b2a046763dc427fa47d4e33afca59c994d60 (patch)
treea5344a19cc994f00f2ff0cae427c998b684e7235 /src/core/SkSharedMutex.h
parent8cd8f9429a8a7d5c09c48f7cc9ab34073e01d945 (diff)
Implement shared locks in the style of pthread's rwlock.
Diffstat (limited to 'src/core/SkSharedMutex.h')
-rw-r--r--src/core/SkSharedMutex.h43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/core/SkSharedMutex.h b/src/core/SkSharedMutex.h
new file mode 100644
index 0000000000..a020f9e1be
--- /dev/null
+++ b/src/core/SkSharedMutex.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkSharedLock_DEFINED
+#define SkSharedLock_DEFINED
+
+#include "SkAtomics.h"
+#include "SkSemaphore.h"
+#include "SkTypes.h"
+
+// This is a shared lock implementation similar to pthreads rwlocks. This implementation is
+// cribbed from Preshing's article:
+// http://preshing.com/20150316/semaphores-are-surprisingly-versatile/
+//
+// This lock does not obey strict queue ordering. It will always alternate between readers and
+// a single writer.
+class SkSharedMutex {
+public:
+ SkSharedMutex();
+
+ // Acquire lock for exclusive use.
+ void acquire();
+
+ // Release lock for exclusive use.
+ void release();
+
+ // Acquire lock for shared use.
+ void acquireShared();
+
+ // Release lock for shared use.
+ void releaseShared();
+
+private:
+ SkAtomic<int32_t> fQueueCounts;
+ SkSemaphore fSharedQueue;
+ SkSemaphore fExclusiveQueue;
+};
+
+#endif // SkSharedLock_DEFINED