Advertisement
den4ik2003

Untitled

Aug 31st, 2023
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.40 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "atomic.hpp"
  4.  
  5. #include <twist/ed/wait/spin.hpp>
  6.  
  7. // Naive Test-and-Set (TAS) spinlock
  8.  
  9. class TASSpinLock {
  10. public:
  11. void Lock() {
  12. while (locked_.Exchange(1) == 1) {
  13. twist::ed::CpuRelax();
  14. }
  15. }
  16.  
  17. bool TryLock() {
  18. return (locked_.Exchange(1) == 0);
  19. }
  20.  
  21. void Unlock() {
  22. locked_.Store(0);
  23. }
  24.  
  25. private:
  26. stdlike::Atomic locked_{0};
  27. };
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement