2015-05-28 07:00:53 +00:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2015-05-28 07:00:53 +00:00
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include "Common/BlockingLoop.h"
|
|
|
|
#include "Common/Thread.h"
|
|
|
|
|
|
|
|
TEST(BusyLoopTest, MultiThreaded)
|
|
|
|
{
|
|
|
|
Common::BlockingLoop loop;
|
|
|
|
Common::Event e;
|
2016-06-27 20:06:52 +00:00
|
|
|
for (int i = 0; i < 10; i++)
|
2015-05-28 07:00:53 +00:00
|
|
|
{
|
|
|
|
loop.Prepare();
|
|
|
|
std::thread loop_thread([&]() { loop.Run([&]() { e.Set(); }); });
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-05-28 07:00:53 +00:00
|
|
|
// Ping - Pong
|
|
|
|
for (int j = 0; j < 10; j++)
|
|
|
|
{
|
|
|
|
loop.Wakeup();
|
|
|
|
e.Wait();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-05-28 07:00:53 +00:00
|
|
|
// Just waste some time. So the main loop did fall back to the sleep state much more likely.
|
|
|
|
Common::SleepCurrentThread(1);
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-05-28 07:00:53 +00:00
|
|
|
for (int j = 0; j < 100; j++)
|
|
|
|
{
|
|
|
|
// We normally have to call Wakeup to assure the Event is triggered.
|
|
|
|
// But this check is for an internal feature of the BlockingLoop.
|
|
|
|
// It's implemented to fall back to a busy loop regulary.
|
|
|
|
// If we're in the busy loop, the payload (and so the Event) is called all the time.
|
|
|
|
// loop.Wakeup();
|
|
|
|
e.Wait();
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-05-28 07:00:53 +00:00
|
|
|
loop.Stop();
|
|
|
|
loop_thread.join();
|
|
|
|
}
|
|
|
|
}
|