Advertisement
den4ik2003

Untitled

Aug 31st, 2023
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. #include <wheels/test/framework.hpp>
  2.  
  3. #include "../barrier.hpp"
  4.  
  5. // https://gitlab.com/Lipovsky/tinyfibers
  6. #include <tf/sched/spawn.hpp>
  7. #include <tf/sched/yield.hpp>
  8. #include <tf/sync/mutex.hpp>
  9. #include <tf/sync/wait_group.hpp>
  10.  
  11. using tf::Mutex;
  12. using tf::Spawn;
  13. using tf::WaitGroup;
  14. using tf::Yield;
  15.  
  16. void TwoFibersDeadLock() {
  17. // Mutexes
  18. Mutex a;
  19. Mutex b;
  20.  
  21. // Fibers
  22.  
  23. auto first = [&] {
  24. a.Lock();
  25.  
  26. Yield();
  27.  
  28. b.Lock();
  29.  
  30. a.Unlock();
  31. b.Unlock();
  32. };
  33.  
  34. auto second = [&] {
  35. b.Lock();
  36.  
  37. Yield();
  38.  
  39. a.Lock();
  40.  
  41. a.Unlock();
  42. b.Unlock();
  43. };
  44.  
  45. // No deadlock with one fiber
  46.  
  47. // No deadlock expected here
  48. // Run routine twice to check that
  49. // routine leaves mutexes in unlocked state
  50. Spawn(first).Join();
  51. Spawn(first).Join();
  52.  
  53. // Same for `second`
  54. Spawn(second).Join();
  55. Spawn(second).Join();
  56.  
  57. ReadyToDeadLock();
  58.  
  59. // Deadlock with two fibers
  60. WaitGroup wg;
  61. wg.Spawn(first).Spawn(second).Wait();
  62.  
  63. // We do not expect to reach this line
  64. FAIL_TEST("No deadlock =(");
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement