Advertisement
den4ik2003

Untitled

Aug 31st, 2023
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. #include <wheels/test/framework.hpp>
  2.  
  3. // https://gitlab.com/Lipovsky/tinyfibers
  4. #include <tf/sched/spawn.hpp>
  5. #include <tf/sched/yield.hpp>
  6. #include <tf/sync/wait_group.hpp>
  7.  
  8. using tf::WaitGroup;
  9. using tf::Yield;
  10.  
  11. void LiveLock() {
  12. static const size_t kIterations = 100;
  13.  
  14. size_t cs_count = 0;
  15.  
  16. // TrickyLock state
  17. size_t thread_count = 0;
  18.  
  19. auto contender = [&] {
  20. for (size_t i = 0; i < kIterations; ++i) {
  21. // TrickyLock::Lock
  22. while (thread_count++ > 0) {
  23. Yield();
  24. --thread_count;
  25. }
  26.  
  27. Yield();
  28.  
  29. // Spinlock acquired
  30.  
  31. {
  32. // Critical section
  33. ++cs_count;
  34. ASSERT_TRUE_M(cs_count < 3, "Too many critical sections");
  35. // End of critical section
  36. }
  37.  
  38. // TrickyLock::Unlock
  39. --thread_count;
  40. // Spinlock released
  41. }
  42. };
  43.  
  44. // Spawn two fibers
  45. WaitGroup wg;
  46. wg.Spawn(contender).Spawn(contender).Wait();
  47. };
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement