Chromium Code Reviews| Index: third_party/WebKit/Source/core/css/off/MultiThreadedTestUtil.h |
| diff --git a/third_party/WebKit/Source/core/css/off/MultiThreadedTestUtil.h b/third_party/WebKit/Source/core/css/off/MultiThreadedTestUtil.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d00ab16ea0606294bdaedd9a20d3153893400f86 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/css/off/MultiThreadedTestUtil.h |
| @@ -0,0 +1,62 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +#include "platform/CrossThreadFunctional.h" |
| +#include "platform/WaitableEvent.h" |
| +#include "platform/WebTaskRunner.h" |
| +#include "platform/scheduler/child/web_scheduler.h" |
| +#include "platform/wtf/Functional.h" |
| +#include "platform/wtf/RefCounted.h" |
| +#include "public/platform/Platform.h" |
| +#include "public/platform/WebThread.h" |
| + |
| +namespace blink { |
| + |
| +class MultiThreadedTest : public testing::Test { |
| + public: |
| + // RunOnThreads run a closure num_threads * callbacks_per_thread times. |
| + // The default for this is 10*100 = 1000 times. |
| + |
| + template <typename FunctionType, typename... Ps> |
| + void RunOnThreads(FunctionType function, Ps&&... parameters) { |
| + Vector<std::unique_ptr<WebThread>> threads; |
| + Vector<std::unique_ptr<WaitableEvent>> waits; |
| + |
| + for (int i = 0; i < num_threads_; ++i) { |
| + threads.push_back(Platform::Current()->CreateThread("one")); |
|
Justin Novosad
2017/05/26 19:13:13
thread name...
fserb
2017/05/26 20:49:48
done.
|
| + waits.push_back(WTF::MakeUnique<WaitableEvent>()); |
| + } |
| + |
| + for (int i = 0; i < num_threads_; ++i) { |
| + WebTaskRunner* task_runner = threads[i]->GetWebTaskRunner(); |
| + |
| + for (int j = 0; j < callbacks_per_thread_; ++j) { |
| + std::unique_ptr<CrossThreadClosure> func = |
| + CrossThreadBind(function, parameters...); |
| + task_runner->PostTask( |
| + FROM_HERE, CrossThreadBind( |
|
Justin Novosad
2017/05/26 19:13:13
See if you can eliminate nested CrossThreadBinds
fserb
2017/05/26 20:49:48
done.
|
| + [](const std::unique_ptr<CrossThreadClosure>& func) { |
| + (*func)(); |
| + }, |
| + std::move(func))); |
| + } |
| + |
| + task_runner->PostTask( |
| + FROM_HERE, CrossThreadBind([](WaitableEvent* w) { w->Signal(); }, |
| + CrossThreadUnretained(waits[i].get()))); |
| + } |
| + |
| + for (int i = 0; i < num_threads_; ++i) { |
| + waits[i]->Wait(); |
| + } |
| + } |
| + |
| + protected: |
| + int num_threads_ = 10; |
| + int callbacks_per_thread_ = 100; |
| +}; |
| + |
| +} // namespace blink |