File: BlockingLoopTest.cpp

package info (click to toggle)
dolphin-emu 2512%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 76,328 kB
  • sloc: cpp: 499,023; ansic: 119,674; python: 6,547; sh: 2,338; makefile: 1,093; asm: 726; pascal: 257; javascript: 183; perl: 97; objc: 75; xml: 30
file content (79 lines) | stat: -rw-r--r-- 1,690 bytes parent folder | download
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2014 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <atomic>
#include <thread>

#include <gtest/gtest.h>

#include "Common/BlockingLoop.h"

TEST(BlockingLoop, MultiThreaded)
{
  Common::BlockingLoop loop;
  std::atomic signaled_a(0);
  std::atomic received_a(0);
  std::atomic signaled_b(0);
  std::atomic received_b(0);
  for (int i = 0; i < 100; i++)
  {
    // Invalidate the current state.
    received_a.store(signaled_a.load() + 1);
    received_b.store(signaled_b.load() + 123);

    // Must not block as the loop is stopped.
    loop.Wait();

    std::thread loop_thread([&] {
      loop.Run([&] {
        received_a.store(signaled_a.load());
        received_b.store(signaled_b.load());
      });
    });

    // Now Wait must block.
    loop.Prepare();

    // The payload must run at least once on startup.
    loop.Wait();
    EXPECT_EQ(signaled_a.load(), received_a.load());
    EXPECT_EQ(signaled_b.load(), received_b.load());

    std::thread run_a_thread([&] {
      for (int j = 0; j < 100; j++)
      {
        for (int k = 0; k < 100; k++)
        {
          signaled_a++;
          loop.Wakeup();
        }

        loop.Wait();
        EXPECT_EQ(signaled_a.load(), received_a.load());
      }
    });
    std::thread run_b_thread([&] {
      for (int j = 0; j < 100; j++)
      {
        for (int k = 0; k < 100; k++)
        {
          signaled_b++;
          loop.Wakeup();
        }

        loop.Wait();
        EXPECT_EQ(signaled_b.load(), received_b.load());
      }
    });

    run_a_thread.join();
    run_b_thread.join();

    loop.Stop();

    // Must not block
    loop.Wait();

    loop_thread.join();
  }
}