| 1 | // Copyright 2014 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include <thread> |
| 5 | |
| 6 | #include <gtest/gtest.h> |
| 7 | |
| 8 | #include "Common/BlockingLoop.h" |
| 9 | #include "Common/Thread.h" |
| 10 | |
| 11 | TEST(BusyLoopTest, MultiThreaded) |
| 12 | { |
| 13 | Common::BlockingLoop loop; |
| 14 | Common::Event e; |
| 15 | for (int i = 0; i < 10; i++) |
| 16 | { |
| 17 | loop.Prepare(); |
| 18 | std::thread loop_thread([&] { loop.Run([&] { e.Set(); }); }); |
| 19 | |
| 20 | // Ping - Pong |
| 21 | for (int j = 0; j < 10; j++) |
| 22 | { |
| 23 | loop.Wakeup(); |
| 24 | e.Wait(); |
| 25 | |
| 26 | // Just waste some time. So the main loop did fall back to the sleep state much more likely. |
| 27 | Common::SleepCurrentThread(1); |
| 28 | } |
| 29 | |
| 30 | for (int j = 0; j < 100; j++) |
| 31 | { |
| 32 | // We normally have to call Wakeup to assure the Event is triggered. |
| 33 | // But this check is for an internal feature of the BlockingLoop. |
| 34 | // It's implemented to fall back to a busy loop regularly. |
| 35 | // If we're in the busy loop, the payload (and so the Event) is called all the time. |
| 36 | // loop.Wakeup(); |
| 37 | e.Wait(); |
| 38 | } |
| 39 | |
| 40 | loop.Stop(); |
| 41 | loop_thread.join(); |
| 42 | } |
| 43 | } |