SHARE
TWEET

Untitled

a guest Apr 21st, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2. #include <iostream>
  3. #include "thread_local.hpp"
  4.  
  5. #include <twist/stdlike/atomic.hpp>
  6. #include <twist/threading/spin_wait.hpp>
  7.  
  8. namespace solutions {
  9.  
  10. class RCULock {
  11.  public:
  12.   void ReadLock() {
  13.     tickets_->fetch_add(1);
  14.   }
  15.  
  16.   void ReadUnlock() {
  17.     tickets_->fetch_add(1);
  18.   }
  19.  
  20.   void Synchronize() {
  21.     std::vector<size_t> v;
  22.     for (auto& ticket : tickets_) {
  23.       v.push_back(ticket.load());
  24.     }
  25.  
  26.     twist::SpinWait spin_wait;
  27.     size_t i = 0;
  28.     for (auto& ticket : tickets_) {
  29.       while (v[i] % 2 == 1 && ticket.load() == v[i]) {
  30.         spin_wait();
  31.       }
  32.       i++;
  33.     }
  34.   }
  35.  
  36.   void lock_shared() {  // NOLINT
  37.     ReadLock();
  38.   }
  39.  
  40.   void unlock_shared() {  // NOLINT
  41.     ReadUnlock();
  42.   }
  43.  
  44.  private:
  45.   ThreadLocal<twist::atomic<size_t>> tickets_;
  46. };
  47.  
  48. }  // namespace solutions
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top