SkSharedMutexTest.cpp 1.4 KB

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