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.
|
+} |