Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Unified Diff: tests/SkSharedLockTest.cpp

Issue 1215503005: Implement shared locks in the style of pthread's rwlock. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« src/core/SkSharedLock.h ('K') | « src/core/SkSharedLock.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/SkSharedLockTest.cpp
diff --git a/tests/SkSharedLockTest.cpp b/tests/SkSharedLockTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..dd2e8fa4324483c551204301f2590ff73f80d2f9
--- /dev/null
+++ b/tests/SkSharedLockTest.cpp
@@ -0,0 +1,67 @@
+/*
+ * 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 "SkSharedLock.h"
+#include "SkTaskGroup.h"
+
+#include "Test.h"
+
+DEF_TEST(SkSharedLockBasic, r) {
+ SkSharedLock sm;
mtklein 2015/06/29 16:29:22 Given that we've already got the precent of 'SkMut
+ sm.acquire();
+ sm.release();
+ sm.acquireShared();
+ sm.releaseShared();
+}
+
+class LockTester {
+public:
+ LockTester(SkSharedLock* sharedLock, int* shared, int size)
+ : fValue(0)
+ , fSharedLock(sharedLock)
+ , fShared(shared)
+ , fSize(size) { }
+
+ void operator()(int threadIndex) const {
+ if (threadIndex % 4 != 0) {
mtklein 2015/06/29 16:29:22 Time to switch your editor to 4 space indents. :)
herb_g 2015/06/29 20:37:06 Done.
+ for (int c = 0; c < 100000; ++c) {
+ fSharedLock->acquireShared();
+ int v = fShared[0];
+ for (int i = 1; i < fSize; ++i) {
+ SkASSERTF(v == fShared[i], "v: %d i: %d, s[i]: %d", v, i, fShared[i]);
mtklein 2015/06/29 16:29:22 Generally we'd use REPORTER_ASSERT(r, v == fShared
herb_g 2015/06/29 20:37:05 Done.
+ }
+ fSharedLock->releaseShared();
+ }
+ } else {
+ for (int c = 0; c < 100000; ++c) {
+ fSharedLock->acquire();
+ fValue += 1;
+ for (int i = 0; i < fSize; ++i) {
+ fShared[i] = fValue;
+ }
+ fSharedLock->release();
+ }
+ }
+ }
+
+private:
+ mutable int fValue;
+ mutable SkSharedLock* fSharedLock;
+ int* const fShared;
+ const int fSize;
+};
+
+DEF_TEST(SkSharedLockMultiThreaded, r) {
+ SkSharedLock sm;
+ static const int kSharedSize = 10;
+ int shared[kSharedSize];
+ for (int i = 0; i < kSharedSize; ++i) {
+ shared[i] = 0;
+ }
+ LockTester t(&sm, shared, kSharedSize);
+ sk_parallel_for(8, t);
mtklein 2015/06/29 16:29:22 Any reason to not write this all inline? DEF_TEST
herb_g 2015/06/29 20:37:06 Done.
+}
« src/core/SkSharedLock.h ('K') | « src/core/SkSharedLock.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698