Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 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 #include "SkSharedLock.h" | |
| 9 #include "SkTaskGroup.h" | |
| 10 | |
| 11 #include "Test.h" | |
| 12 | |
| 13 DEF_TEST(SkSharedLockBasic, r) { | |
| 14 SkSharedLock sm; | |
|
mtklein
2015/06/29 16:29:22
Given that we've already got the precent of 'SkMut
| |
| 15 sm.acquire(); | |
| 16 sm.release(); | |
| 17 sm.acquireShared(); | |
| 18 sm.releaseShared(); | |
| 19 } | |
| 20 | |
| 21 class LockTester { | |
| 22 public: | |
| 23 LockTester(SkSharedLock* sharedLock, int* shared, int size) | |
| 24 : fValue(0) | |
| 25 , fSharedLock(sharedLock) | |
| 26 , fShared(shared) | |
| 27 , fSize(size) { } | |
| 28 | |
| 29 void operator()(int threadIndex) const { | |
| 30 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.
| |
| 31 for (int c = 0; c < 100000; ++c) { | |
| 32 fSharedLock->acquireShared(); | |
| 33 int v = fShared[0]; | |
| 34 for (int i = 1; i < fSize; ++i) { | |
| 35 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.
| |
| 36 } | |
| 37 fSharedLock->releaseShared(); | |
| 38 } | |
| 39 } else { | |
| 40 for (int c = 0; c < 100000; ++c) { | |
| 41 fSharedLock->acquire(); | |
| 42 fValue += 1; | |
| 43 for (int i = 0; i < fSize; ++i) { | |
| 44 fShared[i] = fValue; | |
| 45 } | |
| 46 fSharedLock->release(); | |
| 47 } | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 private: | |
| 52 mutable int fValue; | |
| 53 mutable SkSharedLock* fSharedLock; | |
| 54 int* const fShared; | |
| 55 const int fSize; | |
| 56 }; | |
| 57 | |
| 58 DEF_TEST(SkSharedLockMultiThreaded, r) { | |
| 59 SkSharedLock sm; | |
| 60 static const int kSharedSize = 10; | |
| 61 int shared[kSharedSize]; | |
| 62 for (int i = 0; i < kSharedSize; ++i) { | |
| 63 shared[i] = 0; | |
| 64 } | |
| 65 LockTester t(&sm, shared, kSharedSize); | |
| 66 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.
| |
| 67 } | |
| OLD | NEW |