| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "testing/gtest/include/gtest/gtest.h" |
| 6 |
| 7 #include "platform/CrossThreadFunctional.h" |
| 8 #include "platform/WaitableEvent.h" |
| 9 #include "platform/WebTaskRunner.h" |
| 10 #include "platform/scheduler/child/web_scheduler.h" |
| 11 #include "platform/wtf/Functional.h" |
| 12 #include "platform/wtf/RefCounted.h" |
| 13 #include "public/platform/Platform.h" |
| 14 #include "public/platform/WebThread.h" |
| 15 |
| 16 namespace blink { |
| 17 |
| 18 class MultiThreadedTest : public testing::Test { |
| 19 public: |
| 20 // RunOnThreads run a closure num_threads * callbacks_per_thread times. |
| 21 // The default for this is 10*100 = 1000 times. |
| 22 |
| 23 template <typename FunctionType, typename... Ps> |
| 24 void RunOnThreads(FunctionType function, Ps&&... parameters) { |
| 25 Vector<std::unique_ptr<WebThread>> threads; |
| 26 Vector<std::unique_ptr<WaitableEvent>> waits; |
| 27 |
| 28 for (int i = 0; i < num_threads_; ++i) { |
| 29 threads.push_back(Platform::Current()->CreateThread("")); |
| 30 waits.push_back(WTF::MakeUnique<WaitableEvent>()); |
| 31 } |
| 32 |
| 33 for (int i = 0; i < num_threads_; ++i) { |
| 34 WebTaskRunner* task_runner = threads[i]->GetWebTaskRunner(); |
| 35 |
| 36 for (int j = 0; j < callbacks_per_thread_; ++j) { |
| 37 task_runner->PostTask(FROM_HERE, |
| 38 CrossThreadBind(function, parameters...)); |
| 39 } |
| 40 |
| 41 task_runner->PostTask( |
| 42 FROM_HERE, CrossThreadBind([](WaitableEvent* w) { w->Signal(); }, |
| 43 CrossThreadUnretained(waits[i].get()))); |
| 44 } |
| 45 |
| 46 for (int i = 0; i < num_threads_; ++i) { |
| 47 waits[i]->Wait(); |
| 48 } |
| 49 } |
| 50 |
| 51 protected: |
| 52 int num_threads_ = 10; |
| 53 int callbacks_per_thread_ = 100; |
| 54 }; |
| 55 |
| 56 } // namespace blink |
| OLD | NEW |