fibers.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <atomic>
  5. #include <cstdlib>
  6. #include <functional>
  7. #include <memory>
  8. #include <thread>
  9. #include <unordered_map>
  10. #include <vector>
  11. #include <catch2/catch.hpp>
  12. #include <math.h>
  13. #include "common/common_types.h"
  14. #include "common/fiber.h"
  15. #include "common/spin_lock.h"
  16. namespace Common {
  17. class TestControl1 {
  18. public:
  19. TestControl1() = default;
  20. void DoWork();
  21. void ExecuteThread(u32 id);
  22. std::unordered_map<std::thread::id, u32> ids;
  23. std::vector<std::shared_ptr<Common::Fiber>> thread_fibers;
  24. std::vector<std::shared_ptr<Common::Fiber>> work_fibers;
  25. std::vector<u32> items;
  26. std::vector<u32> results;
  27. };
  28. static void WorkControl1(void* control) {
  29. TestControl1* test_control = static_cast<TestControl1*>(control);
  30. test_control->DoWork();
  31. }
  32. void TestControl1::DoWork() {
  33. std::thread::id this_id = std::this_thread::get_id();
  34. u32 id = ids[this_id];
  35. u32 value = items[id];
  36. for (u32 i = 0; i < id; i++) {
  37. value++;
  38. }
  39. results[id] = value;
  40. Fiber::YieldTo(work_fibers[id], thread_fibers[id]);
  41. }
  42. void TestControl1::ExecuteThread(u32 id) {
  43. std::thread::id this_id = std::this_thread::get_id();
  44. ids[this_id] = id;
  45. auto thread_fiber = Fiber::ThreadToFiber();
  46. thread_fibers[id] = thread_fiber;
  47. work_fibers[id] = std::make_shared<Fiber>(std::function<void(void*)>{WorkControl1}, this);
  48. items[id] = rand() % 256;
  49. Fiber::YieldTo(thread_fibers[id], work_fibers[id]);
  50. thread_fibers[id]->Exit();
  51. }
  52. static void ThreadStart1(u32 id, TestControl1& test_control) {
  53. test_control.ExecuteThread(id);
  54. }
  55. /** This test checks for fiber setup configuration and validates that fibers are
  56. * doing all the work required.
  57. */
  58. TEST_CASE("Fibers::Setup", "[common]") {
  59. constexpr u32 num_threads = 7;
  60. TestControl1 test_control{};
  61. test_control.thread_fibers.resize(num_threads, nullptr);
  62. test_control.work_fibers.resize(num_threads, nullptr);
  63. test_control.items.resize(num_threads, 0);
  64. test_control.results.resize(num_threads, 0);
  65. std::vector<std::thread> threads;
  66. for (u32 i = 0; i < num_threads; i++) {
  67. threads.emplace_back(ThreadStart1, i, std::ref(test_control));
  68. }
  69. for (u32 i = 0; i < num_threads; i++) {
  70. threads[i].join();
  71. }
  72. for (u32 i = 0; i < num_threads; i++) {
  73. REQUIRE(test_control.items[i] + i == test_control.results[i]);
  74. }
  75. }
  76. class TestControl2 {
  77. public:
  78. TestControl2() = default;
  79. void DoWork1() {
  80. trap2 = false;
  81. while (trap.load());
  82. for (u32 i = 0; i < 12000; i++) {
  83. value1 += i;
  84. }
  85. Fiber::YieldTo(fiber1, fiber3);
  86. std::thread::id this_id = std::this_thread::get_id();
  87. u32 id = ids[this_id];
  88. assert1 = id == 1;
  89. value2 += 5000;
  90. Fiber::YieldTo(fiber1, thread_fibers[id]);
  91. }
  92. void DoWork2() {
  93. while (trap2.load());
  94. value2 = 2000;
  95. trap = false;
  96. Fiber::YieldTo(fiber2, fiber1);
  97. assert3 = false;
  98. }
  99. void DoWork3() {
  100. std::thread::id this_id = std::this_thread::get_id();
  101. u32 id = ids[this_id];
  102. assert2 = id == 0;
  103. value1 += 1000;
  104. Fiber::YieldTo(fiber3, thread_fibers[id]);
  105. }
  106. void ExecuteThread(u32 id);
  107. void CallFiber1() {
  108. std::thread::id this_id = std::this_thread::get_id();
  109. u32 id = ids[this_id];
  110. Fiber::YieldTo(thread_fibers[id], fiber1);
  111. }
  112. void CallFiber2() {
  113. std::thread::id this_id = std::this_thread::get_id();
  114. u32 id = ids[this_id];
  115. Fiber::YieldTo(thread_fibers[id], fiber2);
  116. }
  117. void Exit();
  118. bool assert1{};
  119. bool assert2{};
  120. bool assert3{true};
  121. u32 value1{};
  122. u32 value2{};
  123. std::atomic<bool> trap{true};
  124. std::atomic<bool> trap2{true};
  125. std::unordered_map<std::thread::id, u32> ids;
  126. std::vector<std::shared_ptr<Common::Fiber>> thread_fibers;
  127. std::shared_ptr<Common::Fiber> fiber1;
  128. std::shared_ptr<Common::Fiber> fiber2;
  129. std::shared_ptr<Common::Fiber> fiber3;
  130. };
  131. static void WorkControl2_1(void* control) {
  132. TestControl2* test_control = static_cast<TestControl2*>(control);
  133. test_control->DoWork1();
  134. }
  135. static void WorkControl2_2(void* control) {
  136. TestControl2* test_control = static_cast<TestControl2*>(control);
  137. test_control->DoWork2();
  138. }
  139. static void WorkControl2_3(void* control) {
  140. TestControl2* test_control = static_cast<TestControl2*>(control);
  141. test_control->DoWork3();
  142. }
  143. void TestControl2::ExecuteThread(u32 id) {
  144. std::thread::id this_id = std::this_thread::get_id();
  145. ids[this_id] = id;
  146. auto thread_fiber = Fiber::ThreadToFiber();
  147. thread_fibers[id] = thread_fiber;
  148. }
  149. void TestControl2::Exit() {
  150. std::thread::id this_id = std::this_thread::get_id();
  151. u32 id = ids[this_id];
  152. thread_fibers[id]->Exit();
  153. }
  154. static void ThreadStart2_1(u32 id, TestControl2& test_control) {
  155. test_control.ExecuteThread(id);
  156. test_control.CallFiber1();
  157. test_control.Exit();
  158. }
  159. static void ThreadStart2_2(u32 id, TestControl2& test_control) {
  160. test_control.ExecuteThread(id);
  161. test_control.CallFiber2();
  162. test_control.Exit();
  163. }
  164. /** This test checks for fiber thread exchange configuration and validates that fibers are
  165. * that a fiber has been succesfully transfered from one thread to another and that the TLS
  166. * region of the thread is kept while changing fibers.
  167. */
  168. TEST_CASE("Fibers::InterExchange", "[common]") {
  169. TestControl2 test_control{};
  170. test_control.thread_fibers.resize(2, nullptr);
  171. test_control.fiber1 = std::make_shared<Fiber>(std::function<void(void*)>{WorkControl2_1}, &test_control);
  172. test_control.fiber2 = std::make_shared<Fiber>(std::function<void(void*)>{WorkControl2_2}, &test_control);
  173. test_control.fiber3 = std::make_shared<Fiber>(std::function<void(void*)>{WorkControl2_3}, &test_control);
  174. std::thread thread1(ThreadStart2_1, 0, std::ref(test_control));
  175. std::thread thread2(ThreadStart2_2, 1, std::ref(test_control));
  176. thread1.join();
  177. thread2.join();
  178. REQUIRE(test_control.assert1);
  179. REQUIRE(test_control.assert2);
  180. REQUIRE(test_control.assert3);
  181. REQUIRE(test_control.value2 == 7000);
  182. u32 cal_value = 0;
  183. for (u32 i = 0; i < 12000; i++) {
  184. cal_value += i;
  185. }
  186. cal_value += 1000;
  187. REQUIRE(test_control.value1 == cal_value);
  188. }
  189. class TestControl3 {
  190. public:
  191. TestControl3() = default;
  192. void DoWork1() {
  193. value1 += 1;
  194. Fiber::YieldTo(fiber1, fiber2);
  195. std::thread::id this_id = std::this_thread::get_id();
  196. u32 id = ids[this_id];
  197. value3 += 1;
  198. Fiber::YieldTo(fiber1, thread_fibers[id]);
  199. }
  200. void DoWork2() {
  201. value2 += 1;
  202. std::thread::id this_id = std::this_thread::get_id();
  203. u32 id = ids[this_id];
  204. Fiber::YieldTo(fiber2, thread_fibers[id]);
  205. }
  206. void ExecuteThread(u32 id);
  207. void CallFiber1() {
  208. std::thread::id this_id = std::this_thread::get_id();
  209. u32 id = ids[this_id];
  210. Fiber::YieldTo(thread_fibers[id], fiber1);
  211. }
  212. void Exit();
  213. u32 value1{};
  214. u32 value2{};
  215. u32 value3{};
  216. std::unordered_map<std::thread::id, u32> ids;
  217. std::vector<std::shared_ptr<Common::Fiber>> thread_fibers;
  218. std::shared_ptr<Common::Fiber> fiber1;
  219. std::shared_ptr<Common::Fiber> fiber2;
  220. };
  221. static void WorkControl3_1(void* control) {
  222. TestControl3* test_control = static_cast<TestControl3*>(control);
  223. test_control->DoWork1();
  224. }
  225. static void WorkControl3_2(void* control) {
  226. TestControl3* test_control = static_cast<TestControl3*>(control);
  227. test_control->DoWork2();
  228. }
  229. void TestControl3::ExecuteThread(u32 id) {
  230. std::thread::id this_id = std::this_thread::get_id();
  231. ids[this_id] = id;
  232. auto thread_fiber = Fiber::ThreadToFiber();
  233. thread_fibers[id] = thread_fiber;
  234. }
  235. void TestControl3::Exit() {
  236. std::thread::id this_id = std::this_thread::get_id();
  237. u32 id = ids[this_id];
  238. thread_fibers[id]->Exit();
  239. }
  240. static void ThreadStart3(u32 id, TestControl3& test_control) {
  241. test_control.ExecuteThread(id);
  242. test_control.CallFiber1();
  243. test_control.Exit();
  244. }
  245. /** This test checks for one two threads racing for starting the same fiber.
  246. * It checks execution occured in an ordered manner and by no time there were
  247. * two contexts at the same time.
  248. */
  249. TEST_CASE("Fibers::StartRace", "[common]") {
  250. TestControl3 test_control{};
  251. test_control.thread_fibers.resize(2, nullptr);
  252. test_control.fiber1 = std::make_shared<Fiber>(std::function<void(void*)>{WorkControl3_1}, &test_control);
  253. test_control.fiber2 = std::make_shared<Fiber>(std::function<void(void*)>{WorkControl3_2}, &test_control);
  254. std::thread thread1(ThreadStart3, 0, std::ref(test_control));
  255. std::thread thread2(ThreadStart3, 1, std::ref(test_control));
  256. thread1.join();
  257. thread2.join();
  258. REQUIRE(test_control.value1 == 1);
  259. REQUIRE(test_control.value2 == 1);
  260. REQUIRE(test_control.value3 == 1);
  261. }
  262. } // namespace Common