summaryrefslogtreecommitdiff
path: root/Source/UnitTests/Common/BusyLoopTest.cpp
blob: 50d11acb316f399e39389cbfe45a79afcac2f404 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2014 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#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;
	for (int i = 0; i < 100; i++)
	{
		loop.Prepare();
		std::thread loop_thread(
		[&]() {
			loop.Run(
			[&]() {
				e.Set();
			});
		});

		// Ping - Pong
		for (int j = 0; j < 10; j++)
		{
			loop.Wakeup();
			e.Wait();

			// Just waste some time. So the main loop did fall back to the sleep state much more likely.
			Common::SleepCurrentThread(1);
		}

		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();
		}

		loop.Stop();
		loop_thread.join();
	}
}