2015-06-29 20:46:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkSharedMutex.h"
|
|
|
|
#include "src/core/SkTaskGroup.h"
|
2015-06-29 20:46:55 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tests/Test.h"
|
2015-06-29 20:46:55 +00:00
|
|
|
|
|
|
|
DEF_TEST(SkSharedMutexBasic, r) {
|
|
|
|
SkSharedMutex sm;
|
|
|
|
sm.acquire();
|
2015-08-19 20:40:12 +00:00
|
|
|
sm.assertHeld();
|
2015-06-29 20:46:55 +00:00
|
|
|
sm.release();
|
|
|
|
sm.acquireShared();
|
2015-08-19 20:40:12 +00:00
|
|
|
sm.assertHeldShared();
|
2015-06-29 20:46:55 +00:00
|
|
|
sm.releaseShared();
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(SkSharedMutexMultiThreaded, r) {
|
|
|
|
SkSharedMutex sm;
|
|
|
|
static const int kSharedSize = 10;
|
|
|
|
int shared[kSharedSize];
|
|
|
|
int value = 0;
|
|
|
|
for (int i = 0; i < kSharedSize; ++i) {
|
|
|
|
shared[i] = 0;
|
|
|
|
}
|
2016-01-05 03:13:19 +00:00
|
|
|
SkTaskGroup().batch(8, [&](int threadIndex) {
|
2015-06-29 20:46:55 +00:00
|
|
|
if (threadIndex % 4 != 0) {
|
|
|
|
for (int c = 0; c < 100000; ++c) {
|
|
|
|
sm.acquireShared();
|
2015-08-19 20:40:12 +00:00
|
|
|
sm.assertHeldShared();
|
2015-06-29 20:46:55 +00:00
|
|
|
int v = shared[0];
|
|
|
|
for (int i = 1; i < kSharedSize; ++i) {
|
|
|
|
REPORTER_ASSERT(r, v == shared[i]);
|
|
|
|
}
|
|
|
|
sm.releaseShared();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int c = 0; c < 100000; ++c) {
|
|
|
|
sm.acquire();
|
2015-08-19 20:40:12 +00:00
|
|
|
sm.assertHeld();
|
2015-06-29 20:46:55 +00:00
|
|
|
value += 1;
|
|
|
|
for (int i = 0; i < kSharedSize; ++i) {
|
|
|
|
shared[i] = value;
|
|
|
|
}
|
|
|
|
sm.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|