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