aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
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 /tests
parent8cd8f9429a8a7d5c09c48f7cc9ab34073e01d945 (diff)
Implement shared locks in the style of pthread's rwlock.
Diffstat (limited to 'tests')
-rw-r--r--tests/SkSharedMutexTest.cpp50
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/SkSharedMutexTest.cpp b/tests/SkSharedMutexTest.cpp
new file mode 100644
index 0000000000..49f01abfe4
--- /dev/null
+++ b/tests/SkSharedMutexTest.cpp
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkSharedMutex.h"
+#include "SkTaskGroup.h"
+
+#include "Test.h"
+
+DEF_TEST(SkSharedMutexBasic, r) {
+ SkSharedMutex sm;
+ sm.acquire();
+ sm.release();
+ sm.acquireShared();
+ sm.releaseShared();
+}
+
+DEF_TEST(SkSharedMutexMultiThreaded, r) {
+ SkSharedMutex sm;
+ static const int kSharedSize = 10;
+ int shared[kSharedSize];
+ int value = 0;
+ for (int i = 0; i < kSharedSize; ++i) {
+ shared[i] = 0;
+ }
+ sk_parallel_for(8, [&](int threadIndex) {
+ if (threadIndex % 4 != 0) {
+ for (int c = 0; c < 100000; ++c) {
+ sm.acquireShared();
+ int v = shared[0];
+ for (int i = 1; i < kSharedSize; ++i) {
+ REPORTER_ASSERT(r, v == shared[i]);
+ }
+ sm.releaseShared();
+ }
+ } else {
+ for (int c = 0; c < 100000; ++c) {
+ sm.acquire();
+ value += 1;
+ for (int i = 0; i < kSharedSize; ++i) {
+ shared[i] = value;
+ }
+ sm.release();
+ }
+ }
+ });
+}