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

Side by Side Diff: tests/OnceTest.cpp

Issue 806473006: namespace {} trick for SK_DECLARE_STATIC_ONCE (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 11 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
« src/utils/win/SkDWrite.cpp ('K') | « src/utils/win/SkDWrite.cpp ('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
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkOnce.h" 8 #include "SkOnce.h"
9 #include "SkRunnable.h" 9 #include "SkRunnable.h"
10 #include "SkTaskGroup.h" 10 #include "SkTaskGroup.h"
11 #include "Test.h" 11 #include "Test.h"
12 12
13 static void add_five(int* x) { 13 static void add_five(int* x) {
14 *x += 5; 14 *x += 5;
15 } 15 }
16 16
17 SK_DECLARE_STATIC_ONCE(st_once);
17 DEF_TEST(SkOnce_Singlethreaded, r) { 18 DEF_TEST(SkOnce_Singlethreaded, r) {
18 int x = 0; 19 int x = 0;
19 20
20 SK_DECLARE_STATIC_ONCE(once);
21 // No matter how many times we do this, x will be 5. 21 // No matter how many times we do this, x will be 5.
22 SkOnce(&once, add_five, &x); 22 SkOnce(&st_once, add_five, &x);
23 SkOnce(&once, add_five, &x); 23 SkOnce(&st_once, add_five, &x);
24 SkOnce(&once, add_five, &x); 24 SkOnce(&st_once, add_five, &x);
25 SkOnce(&once, add_five, &x); 25 SkOnce(&st_once, add_five, &x);
26 SkOnce(&once, add_five, &x); 26 SkOnce(&st_once, add_five, &x);
27 27
28 REPORTER_ASSERT(r, 5 == x); 28 REPORTER_ASSERT(r, 5 == x);
29 } 29 }
30 30
31 static void add_six(int* x) { 31 static void add_six(int* x) {
32 *x += 6; 32 *x += 6;
33 } 33 }
34 34
35 namespace { 35 namespace {
36 36
37 class Racer : public SkRunnable { 37 class Racer : public SkRunnable {
38 public: 38 public:
39 SkOnceFlag* once; 39 SkOnceFlag* once;
40 int* ptr; 40 int* ptr;
41 41
42 void run() SK_OVERRIDE { 42 void run() SK_OVERRIDE {
43 SkOnce(once, add_six, ptr); 43 SkOnce(once, add_six, ptr);
44 } 44 }
45 }; 45 };
46 46
47 } // namespace 47 } // namespace
48 48
49 SK_DECLARE_STATIC_ONCE(mt_once);
49 DEF_TEST(SkOnce_Multithreaded, r) { 50 DEF_TEST(SkOnce_Multithreaded, r) {
50 const int kTasks = 16; 51 const int kTasks = 16;
51 52
52 // Make a bunch of tasks that will race to be the first to add six to x. 53 // Make a bunch of tasks that will race to be the first to add six to x.
53 Racer racers[kTasks]; 54 Racer racers[kTasks];
54 SK_DECLARE_STATIC_ONCE(once);
55 int x = 0; 55 int x = 0;
56 for (int i = 0; i < kTasks; i++) { 56 for (int i = 0; i < kTasks; i++) {
57 racers[i].once = &once; 57 racers[i].once = &mt_once;
58 racers[i].ptr = &x; 58 racers[i].ptr = &x;
59 } 59 }
60 60
61 // Let them race. 61 // Let them race.
62 SkTaskGroup tg; 62 SkTaskGroup tg;
63 for (int i = 0; i < kTasks; i++) { 63 for (int i = 0; i < kTasks; i++) {
64 tg.add(&racers[i]); 64 tg.add(&racers[i]);
65 } 65 }
66 tg.wait(); 66 tg.wait();
67 67
68 // Only one should have done the +=. 68 // Only one should have done the +=.
69 REPORTER_ASSERT(r, 6 == x); 69 REPORTER_ASSERT(r, 6 == x);
70 } 70 }
71 71
72 static int gX = 0; 72 static int gX = 0;
73 static void inc_gX() { gX++; } 73 static void inc_gX() { gX++; }
74 74
75 SK_DECLARE_STATIC_ONCE(noarg_once);
75 DEF_TEST(SkOnce_NoArg, r) { 76 DEF_TEST(SkOnce_NoArg, r) {
76 SK_DECLARE_STATIC_ONCE(once); 77 SkOnce(&noarg_once, inc_gX);
77 SkOnce(&once, inc_gX); 78 SkOnce(&noarg_once, inc_gX);
78 SkOnce(&once, inc_gX); 79 SkOnce(&noarg_once, inc_gX);
79 SkOnce(&once, inc_gX);
80 REPORTER_ASSERT(r, 1 == gX); 80 REPORTER_ASSERT(r, 1 == gX);
81 } 81 }
OLDNEW
« src/utils/win/SkDWrite.cpp ('K') | « src/utils/win/SkDWrite.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698