fibers.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 <mutex>
  9. #include <stdexcept>
  10. #include <thread>
  11. #include <unordered_map>
  12. #include <vector>
  13. #include <catch2/catch.hpp>
  14. #include "common/common_types.h"
  15. #include "common/fiber.h"
  16. namespace Common {
  17. class ThreadIds {
  18. public:
  19. void Register(u32 id) {
  20. const auto thread_id = std::this_thread::get_id();
  21. std::scoped_lock lock{mutex};
  22. if (ids.contains(thread_id)) {
  23. throw std::logic_error{"Registering the same thread twice"};
  24. }
  25. ids.emplace(thread_id, id);
  26. }
  27. [[nodiscard]] u32 Get() const {
  28. std::scoped_lock lock{mutex};
  29. return ids.at(std::this_thread::get_id());
  30. }
  31. private:
  32. mutable std::mutex mutex;
  33. std::unordered_map<std::thread::id, u32> ids;
  34. };
  35. class TestControl1 {
  36. public:
  37. TestControl1() = default;
  38. void DoWork();
  39. void ExecuteThread(u32 id);
  40. ThreadIds thread_ids;
  41. std::vector<std::shared_ptr<Common::Fiber>> thread_fibers;
  42. std::vector<std::shared_ptr<Common::Fiber>> work_fibers;
  43. std::vector<u32> items;
  44. std::vector<u32> results;
  45. };
  46. static void WorkControl1(void* control) {
  47. auto* test_control = static_cast<TestControl1*>(control);
  48. test_control->DoWork();
  49. }
  50. void TestControl1::DoWork() {
  51. const u32 id = thread_ids.Get();
  52. u32 value = items[id];
  53. for (u32 i = 0; i < id; i++) {
  54. value++;
  55. }
  56. results[id] = value;
  57. Fiber::YieldTo(work_fibers[id], thread_fibers[id]);
  58. }
  59. void TestControl1::ExecuteThread(u32 id) {
  60. thread_ids.Register(id);
  61. auto thread_fiber = Fiber::ThreadToFiber();
  62. thread_fibers[id] = thread_fiber;
  63. work_fibers[id] = std::make_shared<Fiber>(std::function<void(void*)>{WorkControl1}, this);
  64. items[id] = rand() % 256;
  65. Fiber::YieldTo(thread_fibers[id], work_fibers[id]);
  66. thread_fibers[id]->Exit();
  67. }
  68. static void ThreadStart1(u32 id, TestControl1& test_control) {
  69. test_control.ExecuteThread(id);
  70. }
  71. /** This test checks for fiber setup configuration and validates that fibers are
  72. * doing all the work required.
  73. */
  74. TEST_CASE("Fibers::Setup", "[common]") {
  75. constexpr std::size_t num_threads = 7;
  76. TestControl1 test_control{};
  77. test_control.thread_fibers.resize(num_threads);
  78. test_control.work_fibers.resize(num_threads);
  79. test_control.items.resize(num_threads, 0);
  80. test_control.results.resize(num_threads, 0);
  81. std::vector<std::thread> threads;
  82. for (u32 i = 0; i < num_threads; i++) {
  83. threads.emplace_back(ThreadStart1, i, std::ref(test_control));
  84. }
  85. for (u32 i = 0; i < num_threads; i++) {
  86. threads[i].join();
  87. }
  88. for (u32 i = 0; i < num_threads; i++) {
  89. REQUIRE(test_control.items[i] + i == test_control.results[i]);
  90. }
  91. }
  92. class TestControl2 {
  93. public:
  94. TestControl2() = default;
  95. void DoWork1() {
  96. trap2 = false;
  97. while (trap.load())
  98. ;
  99. for (u32 i = 0; i < 12000; i++) {
  100. value1 += i;
  101. }
  102. Fiber::YieldTo(fiber1, fiber3);
  103. const u32 id = thread_ids.Get();
  104. assert1 = id == 1;
  105. value2 += 5000;
  106. Fiber::YieldTo(fiber1, thread_fibers[id]);
  107. }
  108. void DoWork2() {
  109. while (trap2.load())
  110. ;
  111. value2 = 2000;
  112. trap = false;
  113. Fiber::YieldTo(fiber2, fiber1);
  114. assert3 = false;
  115. }
  116. void DoWork3() {
  117. const u32 id = thread_ids.Get();
  118. assert2 = id == 0;
  119. value1 += 1000;
  120. Fiber::YieldTo(fiber3, thread_fibers[id]);
  121. }
  122. void ExecuteThread(u32 id);
  123. void CallFiber1() {
  124. const u32 id = thread_ids.Get();
  125. Fiber::YieldTo(thread_fibers[id], fiber1);
  126. }
  127. void CallFiber2() {
  128. const u32 id = thread_ids.Get();
  129. Fiber::YieldTo(thread_fibers[id], fiber2);
  130. }
  131. void Exit();
  132. bool assert1{};
  133. bool assert2{};
  134. bool assert3{true};
  135. u32 value1{};
  136. u32 value2{};
  137. std::atomic<bool> trap{true};
  138. std::atomic<bool> trap2{true};
  139. ThreadIds thread_ids;
  140. std::vector<std::shared_ptr<Common::Fiber>> thread_fibers;
  141. std::shared_ptr<Common::Fiber> fiber1;
  142. std::shared_ptr<Common::Fiber> fiber2;
  143. std::shared_ptr<Common::Fiber> fiber3;
  144. };
  145. static void WorkControl2_1(void* control) {
  146. auto* test_control = static_cast<TestControl2*>(control);
  147. test_control->DoWork1();
  148. }
  149. static void WorkControl2_2(void* control) {
  150. auto* test_control = static_cast<TestControl2*>(control);
  151. test_control->DoWork2();
  152. }
  153. static void WorkControl2_3(void* control) {
  154. auto* test_control = static_cast<TestControl2*>(control);
  155. test_control->DoWork3();
  156. }
  157. void TestControl2::ExecuteThread(u32 id) {
  158. thread_ids.Register(id);
  159. auto thread_fiber = Fiber::ThreadToFiber();
  160. thread_fibers[id] = thread_fiber;
  161. }
  162. void TestControl2::Exit() {
  163. const u32 id = thread_ids.Get();
  164. thread_fibers[id]->Exit();
  165. }
  166. static void ThreadStart2_1(u32 id, TestControl2& test_control) {
  167. test_control.ExecuteThread(id);
  168. test_control.CallFiber1();
  169. test_control.Exit();
  170. }
  171. static void ThreadStart2_2(u32 id, TestControl2& test_control) {
  172. test_control.ExecuteThread(id);
  173. test_control.CallFiber2();
  174. test_control.Exit();
  175. }
  176. /** This test checks for fiber thread exchange configuration and validates that fibers are
  177. * that a fiber has been successfully transferred from one thread to another and that the TLS
  178. * region of the thread is kept while changing fibers.
  179. */
  180. TEST_CASE("Fibers::InterExchange", "[common]") {
  181. TestControl2 test_control{};
  182. test_control.thread_fibers.resize(2);
  183. test_control.fiber1 =
  184. std::make_shared<Fiber>(std::function<void(void*)>{WorkControl2_1}, &test_control);
  185. test_control.fiber2 =
  186. std::make_shared<Fiber>(std::function<void(void*)>{WorkControl2_2}, &test_control);
  187. test_control.fiber3 =
  188. std::make_shared<Fiber>(std::function<void(void*)>{WorkControl2_3}, &test_control);
  189. std::thread thread1(ThreadStart2_1, 0, std::ref(test_control));
  190. std::thread thread2(ThreadStart2_2, 1, std::ref(test_control));
  191. thread1.join();
  192. thread2.join();
  193. REQUIRE(test_control.assert1);
  194. REQUIRE(test_control.assert2);
  195. REQUIRE(test_control.assert3);
  196. REQUIRE(test_control.value2 == 7000);
  197. u32 cal_value = 0;
  198. for (u32 i = 0; i < 12000; i++) {
  199. cal_value += i;
  200. }
  201. cal_value += 1000;
  202. REQUIRE(test_control.value1 == cal_value);
  203. }
  204. class TestControl3 {
  205. public:
  206. TestControl3() = default;
  207. void DoWork1() {
  208. value1 += 1;
  209. Fiber::YieldTo(fiber1, fiber2);
  210. const u32 id = thread_ids.Get();
  211. value3 += 1;
  212. Fiber::YieldTo(fiber1, thread_fibers[id]);
  213. }
  214. void DoWork2() {
  215. value2 += 1;
  216. const u32 id = thread_ids.Get();
  217. Fiber::YieldTo(fiber2, thread_fibers[id]);
  218. }
  219. void ExecuteThread(u32 id);
  220. void CallFiber1() {
  221. const u32 id = thread_ids.Get();
  222. Fiber::YieldTo(thread_fibers[id], fiber1);
  223. }
  224. void Exit();
  225. u32 value1{};
  226. u32 value2{};
  227. u32 value3{};
  228. ThreadIds thread_ids;
  229. std::vector<std::shared_ptr<Common::Fiber>> thread_fibers;
  230. std::shared_ptr<Common::Fiber> fiber1;
  231. std::shared_ptr<Common::Fiber> fiber2;
  232. };
  233. static void WorkControl3_1(void* control) {
  234. auto* test_control = static_cast<TestControl3*>(control);
  235. test_control->DoWork1();
  236. }
  237. static void WorkControl3_2(void* control) {
  238. auto* test_control = static_cast<TestControl3*>(control);
  239. test_control->DoWork2();
  240. }
  241. void TestControl3::ExecuteThread(u32 id) {
  242. thread_ids.Register(id);
  243. auto thread_fiber = Fiber::ThreadToFiber();
  244. thread_fibers[id] = thread_fiber;
  245. }
  246. void TestControl3::Exit() {
  247. const u32 id = thread_ids.Get();
  248. thread_fibers[id]->Exit();
  249. }
  250. static void ThreadStart3(u32 id, TestControl3& test_control) {
  251. test_control.ExecuteThread(id);
  252. test_control.CallFiber1();
  253. test_control.Exit();
  254. }
  255. /** This test checks for one two threads racing for starting the same fiber.
  256. * It checks execution occurred in an ordered manner and by no time there were
  257. * two contexts at the same time.
  258. */
  259. TEST_CASE("Fibers::StartRace", "[common]") {
  260. TestControl3 test_control{};
  261. test_control.thread_fibers.resize(2);
  262. test_control.fiber1 =
  263. std::make_shared<Fiber>(std::function<void(void*)>{WorkControl3_1}, &test_control);
  264. test_control.fiber2 =
  265. std::make_shared<Fiber>(std::function<void(void*)>{WorkControl3_2}, &test_control);
  266. std::thread thread1(ThreadStart3, 0, std::ref(test_control));
  267. std::thread thread2(ThreadStart3, 1, std::ref(test_control));
  268. thread1.join();
  269. thread2.join();
  270. REQUIRE(test_control.value1 == 1);
  271. REQUIRE(test_control.value2 == 1);
  272. REQUIRE(test_control.value3 == 1);
  273. }
  274. class TestControl4;
  275. static void WorkControl4(void* control);
  276. class TestControl4 {
  277. public:
  278. TestControl4() {
  279. fiber1 = std::make_shared<Fiber>(std::function<void(void*)>{WorkControl4}, this);
  280. goal_reached = false;
  281. rewinded = false;
  282. }
  283. void Execute() {
  284. thread_fiber = Fiber::ThreadToFiber();
  285. Fiber::YieldTo(thread_fiber, fiber1);
  286. thread_fiber->Exit();
  287. }
  288. void DoWork() {
  289. fiber1->SetRewindPoint(std::function<void(void*)>{WorkControl4}, this);
  290. if (rewinded) {
  291. goal_reached = true;
  292. Fiber::YieldTo(fiber1, thread_fiber);
  293. }
  294. rewinded = true;
  295. fiber1->Rewind();
  296. }
  297. std::shared_ptr<Common::Fiber> fiber1;
  298. std::shared_ptr<Common::Fiber> thread_fiber;
  299. bool goal_reached;
  300. bool rewinded;
  301. };
  302. static void WorkControl4(void* control) {
  303. auto* test_control = static_cast<TestControl4*>(control);
  304. test_control->DoWork();
  305. }
  306. TEST_CASE("Fibers::Rewind", "[common]") {
  307. TestControl4 test_control{};
  308. test_control.Execute();
  309. REQUIRE(test_control.goal_reached);
  310. REQUIRE(test_control.rewinded);
  311. }
  312. } // namespace Common