From c5582317b35f2d333c750246bbf10b58ff2f7fa9 Mon Sep 17 00:00:00 2001 From: Mike Klein Date: Mon, 1 Mar 2021 11:24:27 -0600 Subject: [PATCH] make SkExecutor::GetDefault() a little threadsafe We note SetDefault() isn't threadsafe, but don't mention anything about GetDefault(). So it feels like it ought to be thread safe. But it's not threadsafe as-is, since it writes to gDefaultExecutor. With tiny tweaks we can make it instead only read gDefaultExecutor. Change-Id: If229afda3d0bfb4fe491137fd2fc2226d0eafafa Reviewed-on: https://skia-review.googlesource.com/c/skia/+/377918 Reviewed-by: Brian Osman Commit-Queue: Mike Klein --- src/core/SkExecutor.cpp | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/core/SkExecutor.cpp b/src/core/SkExecutor.cpp index 6ed366f55e..8b12b467ab 100644 --- a/src/core/SkExecutor.cpp +++ b/src/core/SkExecutor.cpp @@ -36,24 +36,22 @@ class SkTrivialExecutor final : public SkExecutor { } }; +static SkExecutor& trivial_executor() { + static auto* executor = new SkTrivialExecutor(); + return *executor; +} + static SkExecutor* gDefaultExecutor = nullptr; -void SetDefaultTrivialExecutor() { - static SkTrivialExecutor *gTrivial = new SkTrivialExecutor(); - gDefaultExecutor = gTrivial; -} SkExecutor& SkExecutor::GetDefault() { - if (!gDefaultExecutor) { - SetDefaultTrivialExecutor(); + if (gDefaultExecutor) { + return *gDefaultExecutor; } - return *gDefaultExecutor; + return trivial_executor(); } + void SkExecutor::SetDefault(SkExecutor* executor) { - if (executor) { - gDefaultExecutor = executor; - } else { - SetDefaultTrivialExecutor(); - } + gDefaultExecutor = executor; } // We'll always push_back() new work, but pop from the front of deques or the back of SkTArray.