Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(555)

Unified Diff: tests/OnceTest.cpp

Issue 26563002: Sketch of SK_ONCE (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: comments and windows Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« src/core/SkOnce.h ('K') | « src/core/SkOnce.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/OnceTest.cpp
diff --git a/tests/OnceTest.cpp b/tests/OnceTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..0f62cf2483016f6f8a18c3dc6212e34f717fa169
--- /dev/null
+++ b/tests/OnceTest.cpp
@@ -0,0 +1,55 @@
+#include "SkOnce.h"
robertphillips 2013/10/09 15:31:13 Header
mtklein 2013/10/09 15:33:57 Done.
+#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;
+}
+
+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.
+ struct : public SkRunnable {
+ int* ptr;
+ virtual void run() SK_OVERRIDE {
+ SK_ONCE(add_six, ptr);
+ }
+ } racers[kTasks];
+
+ // Point them at x.
+ 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);
+}
« src/core/SkOnce.h ('K') | « src/core/SkOnce.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698