aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar kkinnunen <kkinnunen@nvidia.com>2015-11-04 06:30:17 -0800
committerGravatar Commit bot <commit-bot@chromium.org>2015-11-04 06:30:17 -0800
commit7f97a76b805b4cc05f83a965063767f0256ea270 (patch)
tree8bfd8b01dddd43d7f5044a5f97cd61fcc7e3e7f1
parentf57ef1c05f7ff20a80a8ef01a3fb3d4666612de2 (diff)
Avoid hang in OncePtr test with --threads 1
Avoid hang in OncePtr test when using "dm --threads 1". The test will hang the threads until sk_num_cores() threads have run the code. This requires that sk_num_cores() threads to be run in parallel, which the global thread pool will not do if the thread count is smaller than sk_num_cores(). BUG=skia: Review URL: https://codereview.chromium.org/1419593004
-rw-r--r--src/core/SkTaskGroup.cpp7
-rw-r--r--src/core/SkTaskGroup.h2
-rw-r--r--tests/OncePtrTest.cpp6
3 files changed, 13 insertions, 2 deletions
diff --git a/src/core/SkTaskGroup.cpp b/src/core/SkTaskGroup.cpp
index 97e3ff44a8..863195cfd3 100644
--- a/src/core/SkTaskGroup.cpp
+++ b/src/core/SkTaskGroup.cpp
@@ -196,6 +196,7 @@ private:
static ThreadPool* gGlobal;
friend struct SkTaskGroup::Enabler;
+ friend int ::sk_parallel_for_thread_count();
};
ThreadPool* ThreadPool::gGlobal = nullptr;
@@ -219,3 +220,9 @@ void SkTaskGroup::batch (void (*fn)(void*), void* args, int N, size_t stride) {
ThreadPool::Batch(fn, args, N, stride, &fPending);
}
+int sk_parallel_for_thread_count() {
+ if (ThreadPool::gGlobal != nullptr) {
+ return ThreadPool::gGlobal->fThreads.count();
+ }
+ return 0;
+}
diff --git a/src/core/SkTaskGroup.h b/src/core/SkTaskGroup.h
index 76afe9d92f..f68c528a1f 100644
--- a/src/core/SkTaskGroup.h
+++ b/src/core/SkTaskGroup.h
@@ -53,6 +53,8 @@ private:
// Returns best estimate of number of CPU cores available to use.
int sk_num_cores();
+int sk_parallel_for_thread_count();
+
// Call f(i) for i in [0, end).
template <typename Func>
void sk_parallel_for(int end, const Func& f) {
diff --git a/tests/OncePtrTest.cpp b/tests/OncePtrTest.cpp
index b1e4e5d1ae..103172751a 100644
--- a/tests/OncePtrTest.cpp
+++ b/tests/OncePtrTest.cpp
@@ -18,8 +18,10 @@ DEF_TEST(OncePtr, r) {
return new int(5);
};
- SkAtomic<int> force_a_race(sk_num_cores());
-
+ SkAtomic<int> force_a_race(sk_parallel_for_thread_count());
+ if (force_a_race < 1) {
+ return;
+ }
sk_parallel_for(sk_num_cores()*4, [&](size_t) {
force_a_race.fetch_add(-1);
while (force_a_race.load() > 0);