diff options
author | mtklein <mtklein@chromium.org> | 2015-01-13 08:40:23 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-01-13 08:40:23 -0800 |
commit | 199ba8e19b0cafb37eea3beab162bc4b52728fff (patch) | |
tree | 143928c3d09c28f887ca752978b49cc0662b43f5 | |
parent | 60f8353f683277f360c5a74e2905c8de987a2dfe (diff) |
namespace {} trick for SK_DECLARE_STATIC_ONCE
Like all our other SK_DECLARE_STATIC_*, it's usually not a thread-safe
thing to put inside a function. Adding namespace {} prevents that
syntactically.
Needs https://codereview.chromium.org/841263004/ to land first.
BUG=chromium:447890
No public API changes.
TBR=reed@google.com
Review URL: https://codereview.chromium.org/806473006
-rw-r--r-- | include/core/SkOnce.h | 4 | ||||
-rw-r--r-- | src/ports/SkGlobalInitialization_chromium.cpp | 2 | ||||
-rw-r--r-- | src/ports/SkGlobalInitialization_default.cpp | 2 | ||||
-rw-r--r-- | src/utils/SkEventTracer.cpp | 2 | ||||
-rw-r--r-- | src/utils/win/SkDWrite.cpp | 3 | ||||
-rw-r--r-- | tests/OnceTest.cpp | 24 |
6 files changed, 18 insertions, 19 deletions
diff --git a/include/core/SkOnce.h b/include/core/SkOnce.h index 87bb277800..f025cc2abf 100644 --- a/include/core/SkOnce.h +++ b/include/core/SkOnce.h @@ -31,8 +31,8 @@ #include "SkThread.h" #include "SkTypes.h" -// This must be used in a global or function scope, not as a class member. -#define SK_DECLARE_STATIC_ONCE(name) static SkOnceFlag name +// This must be used in a global scope, not in fuction scope or as a class member. +#define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name class SkOnceFlag; diff --git a/src/ports/SkGlobalInitialization_chromium.cpp b/src/ports/SkGlobalInitialization_chromium.cpp index a5115d3df6..cc087c1063 100644 --- a/src/ports/SkGlobalInitialization_chromium.cpp +++ b/src/ports/SkGlobalInitialization_chromium.cpp @@ -120,7 +120,7 @@ public: } }; +SK_DECLARE_STATIC_ONCE(once); void SkFlattenable::InitializeFlattenablesIfNeeded() { - SK_DECLARE_STATIC_ONCE(once); SkOnce(&once, SkPrivateEffectInitializer::Init); } diff --git a/src/ports/SkGlobalInitialization_default.cpp b/src/ports/SkGlobalInitialization_default.cpp index a5115d3df6..cc087c1063 100644 --- a/src/ports/SkGlobalInitialization_default.cpp +++ b/src/ports/SkGlobalInitialization_default.cpp @@ -120,7 +120,7 @@ public: } }; +SK_DECLARE_STATIC_ONCE(once); void SkFlattenable::InitializeFlattenablesIfNeeded() { - SK_DECLARE_STATIC_ONCE(once); SkOnce(&once, SkPrivateEffectInitializer::Init); } diff --git a/src/utils/SkEventTracer.cpp b/src/utils/SkEventTracer.cpp index 4c0470b389..ef2a0552b5 100644 --- a/src/utils/SkEventTracer.cpp +++ b/src/utils/SkEventTracer.cpp @@ -51,8 +51,8 @@ static void intialize_default_tracer(SkEventTracer* current_instance) { } +SK_DECLARE_STATIC_ONCE(once); SkEventTracer* SkEventTracer::GetInstance() { - SK_DECLARE_STATIC_ONCE(once); SkOnce(&once, intialize_default_tracer, SkEventTracer::gInstance); SkASSERT(SkEventTracer::gInstance); return SkEventTracer::gInstance; diff --git a/src/utils/win/SkDWrite.cpp b/src/utils/win/SkDWrite.cpp index 7801059187..363ac438d9 100644 --- a/src/utils/win/SkDWrite.cpp +++ b/src/utils/win/SkDWrite.cpp @@ -41,10 +41,9 @@ static void create_dwrite_factory(IDWriteFactory** factory) { } +SK_DECLARE_STATIC_ONCE(once); IDWriteFactory* sk_get_dwrite_factory() { - SK_DECLARE_STATIC_ONCE(once); SkOnce(&once, create_dwrite_factory, &gDWriteFactory); - return gDWriteFactory; } diff --git a/tests/OnceTest.cpp b/tests/OnceTest.cpp index 6ab9cd27ac..1344cee5c4 100644 --- a/tests/OnceTest.cpp +++ b/tests/OnceTest.cpp @@ -14,16 +14,16 @@ static void add_five(int* x) { *x += 5; } +SK_DECLARE_STATIC_ONCE(st_once); DEF_TEST(SkOnce_Singlethreaded, r) { int x = 0; - SK_DECLARE_STATIC_ONCE(once); // No matter how many times we do this, x will be 5. - SkOnce(&once, add_five, &x); - SkOnce(&once, add_five, &x); - SkOnce(&once, add_five, &x); - SkOnce(&once, add_five, &x); - SkOnce(&once, add_five, &x); + SkOnce(&st_once, add_five, &x); + SkOnce(&st_once, add_five, &x); + SkOnce(&st_once, add_five, &x); + SkOnce(&st_once, add_five, &x); + SkOnce(&st_once, add_five, &x); REPORTER_ASSERT(r, 5 == x); } @@ -46,15 +46,15 @@ public: } // namespace +SK_DECLARE_STATIC_ONCE(mt_once); DEF_TEST(SkOnce_Multithreaded, r) { const int kTasks = 16; // Make a bunch of tasks that will race to be the first to add six to x. Racer racers[kTasks]; - SK_DECLARE_STATIC_ONCE(once); int x = 0; for (int i = 0; i < kTasks; i++) { - racers[i].once = &once; + racers[i].once = &mt_once; racers[i].ptr = &x; } @@ -72,10 +72,10 @@ DEF_TEST(SkOnce_Multithreaded, r) { static int gX = 0; static void inc_gX() { gX++; } +SK_DECLARE_STATIC_ONCE(noarg_once); DEF_TEST(SkOnce_NoArg, r) { - SK_DECLARE_STATIC_ONCE(once); - SkOnce(&once, inc_gX); - SkOnce(&once, inc_gX); - SkOnce(&once, inc_gX); + SkOnce(&noarg_once, inc_gX); + SkOnce(&noarg_once, inc_gX); + SkOnce(&noarg_once, inc_gX); REPORTER_ASSERT(r, 1 == gX); } |