diff options
author | mtklein@google.com <mtklein@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-10-09 16:12:23 +0000 |
---|---|---|
committer | mtklein@google.com <mtklein@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-10-09 16:12:23 +0000 |
commit | 3a19fb58a68183ea855439bdc92da4b90592c6a1 (patch) | |
tree | bea79c847d666238bb1a6c618cd9de549360b438 /tests/OnceTest.cpp | |
parent | 7d14ceff7bcedd48e585a5c1a45390defe8a2d89 (diff) |
Sketch of SK_ONCE
BUG=
R=bungeman@google.com
Review URL: https://codereview.chromium.org/26563002
git-svn-id: http://skia.googlecode.com/svn/trunk@11674 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'tests/OnceTest.cpp')
-rw-r--r-- | tests/OnceTest.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/OnceTest.cpp b/tests/OnceTest.cpp new file mode 100644 index 0000000000..0b2aa9a857 --- /dev/null +++ b/tests/OnceTest.cpp @@ -0,0 +1,67 @@ +/* + * Copyright 2013 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkOnce.h" +#include "SkRunnable.h" +#include "SkThreadPool.h" +#include "Test.h" +#include "TestClassDef.h" + +DEF_SK_ONCE(add_five, int* x) { + *x += 5; +} + +DEF_TEST(SkOnce_Singlethreaded, r) { + int x = 0; + + // No matter how many times we do this, x will be 5. + SK_ONCE(add_five, &x); + SK_ONCE(add_five, &x); + SK_ONCE(add_five, &x); + SK_ONCE(add_five, &x); + SK_ONCE(add_five, &x); + + REPORTER_ASSERT(r, 5 == x); +} + + +DEF_SK_ONCE(add_six, int* x) { + *x += 6; +} + +namespace { + +class Racer : public SkRunnable { +public: + int* ptr; + virtual void run() SK_OVERRIDE { + SK_ONCE(add_six, ptr); + } +}; + +} // namespace + +DEF_TEST(SkOnce_Multithreaded, r) { + const int kTasks = 16, kThreads = 4; + + // Make a bunch of tasks that will race to be the first to add six to x. + Racer racers[kTasks]; + int x = 0; + for (int i = 0; i < kTasks; i++) { + racers[i].ptr = &x; + } + + // Let them race. + SkAutoTDelete<SkThreadPool> pool(new SkThreadPool(kThreads)); + for (int i = 0; i < kTasks; i++) { + pool->add(&racers[i]); + } + pool.free(); // Blocks until all threads are done. + + // Only one should have done the +=. + REPORTER_ASSERT(r, 6 == x); +} |