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

Side by Side Diff: tests/OnceTest.cpp

Issue 26563002: Sketch of SK_ONCE (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: undef ANNOTATE_BENIGN_RACE when done with it 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 unified diff | Download patch | Annotate | Revision Log
« src/core/SkOnce.h ('K') | « src/core/SkOnce.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include "SkOnce.h"
2 #include "SkRunnable.h"
3 #include "SkThreadPool.h"
4 #include "Test.h"
5 #include "TestClassDef.h"
6
7 DEF_SK_ONCE(add_five, int* x) {
8 *x += 5;
9 }
10
11 DEF_TEST(SkOnce_Singlethreaded, r) {
12 int x = 0;
13
14 // No matter how many times we do this, x will be 5.
15 SK_ONCE(add_five, &x);
16 SK_ONCE(add_five, &x);
17 SK_ONCE(add_five, &x);
18 SK_ONCE(add_five, &x);
19 SK_ONCE(add_five, &x);
20
21 REPORTER_ASSERT(r, 5 == x);
22 }
23
24
25 DEF_SK_ONCE(add_six, int* x) {
26 *x += 6;
27 }
28
29 DEF_TEST(SkOnce_Multithreaded, r) {
30 const int kTasks = 16, kThreads = 4;
31
32 // Make a bunch of tasks that will race to be the first to add six.
33 struct : public SkRunnable {
34 int* ptr;
35 virtual void run() SK_OVERRIDE {
36 SK_ONCE(add_six, ptr);
37 }
38 } racers[kTasks];
39
40 // Point them at x.
41 int x = 0;
42 for (int i = 0; i < kTasks; i++) {
43 racers[i].ptr = &x;
44 }
45
46 // Let them race.
47 SkAutoTDelete<SkThreadPool> pool(new SkThreadPool(kThreads));
48 for (int i = 0; i < kTasks; i++) {
49 pool->add(&racers[i]);
50 }
51 pool.free(); // Blocks until all threads are done.
52
53 // Only one should have done the +=.
54 REPORTER_ASSERT(r, 6 == x);
55 }
OLDNEW
« 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