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 "src/base/SkSharedMutex.h" 9 #include "src/core/SkTaskGroup.h" 10 #include "tests/Test.h" 11 12 #include <functional> 13 DEF_TEST(SkSharedMutexBasic,r)14DEF_TEST(SkSharedMutexBasic, r) { 15 SkSharedMutex sm; 16 sm.acquire(); 17 sm.assertHeld(); 18 sm.release(); 19 sm.acquireShared(); 20 sm.assertHeldShared(); 21 sm.releaseShared(); 22 } 23 DEF_TEST(SkSharedMutexMultiThreaded,r)24DEF_TEST(SkSharedMutexMultiThreaded, r) { 25 SkSharedMutex sm; 26 static const int kSharedSize = 10; 27 int shared[kSharedSize]; 28 int value = 0; 29 for (int i = 0; i < kSharedSize; ++i) { 30 shared[i] = 0; 31 } 32 SkTaskGroup().batch(8, [&](int threadIndex) { 33 if (threadIndex % 4 != 0) { 34 for (int c = 0; c < 100000; ++c) { 35 sm.acquireShared(); 36 sm.assertHeldShared(); 37 int v = shared[0]; 38 for (int i = 1; i < kSharedSize; ++i) { 39 REPORTER_ASSERT(r, v == shared[i]); 40 } 41 sm.releaseShared(); 42 } 43 } else { 44 for (int c = 0; c < 100000; ++c) { 45 sm.acquire(); 46 sm.assertHeld(); 47 value += 1; 48 for (int i = 0; i < kSharedSize; ++i) { 49 shared[i] = value; 50 } 51 sm.release(); 52 } 53 } 54 }); 55 } 56