Chromium Code Reviews| 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("one")); | |
|
Justin Novosad
2017/05/26 19:13:13
thread name...
fserb
2017/05/26 20:49:48
done.
| |
| 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 std::unique_ptr<CrossThreadClosure> func = | |
| 38 CrossThreadBind(function, parameters...); | |
| 39 task_runner->PostTask( | |
| 40 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.
| |
| 41 [](const std::unique_ptr<CrossThreadClosure>& func) { | |
| 42 (*func)(); | |
| 43 }, | |
| 44 std::move(func))); | |
| 45 } | |
| 46 | |
| 47 task_runner->PostTask( | |
| 48 FROM_HERE, CrossThreadBind([](WaitableEvent* w) { w->Signal(); }, | |
| 49 CrossThreadUnretained(waits[i].get()))); | |
| 50 } | |
| 51 | |
| 52 for (int i = 0; i < num_threads_; ++i) { | |
| 53 waits[i]->Wait(); | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 protected: | |
| 58 int num_threads_ = 10; | |
| 59 int callbacks_per_thread_ = 100; | |
| 60 }; | |
| 61 | |
| 62 } // namespace blink | |
| OLD | NEW |