MystMe

Untitled

Mar 19th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <atomic.hpp>
  4. #include "futex_like.hpp"
  5.  
  6. namespace tpcc {
  7.   namespace solutions {
  8.  
  9.     class AdaptiveLock {
  10.       public:
  11.           AdaptiveLock() {
  12.             state_.store(false);
  13.  
  14.             tpcc::atomic <bool> tmp;
  15.             tmp.store(false);
  16.             futex_ = new FutexLike<bool> (tmp);
  17.           }
  18.  
  19.           ~AdaptiveLock() {
  20.             delete futex_;
  21.           }
  22.  
  23.           void Lock() {
  24.             while (state_.exchange(true)) {
  25.               futex_->Wait(false);
  26.             }
  27.           }
  28.  
  29.           void Unlock() {
  30.             state_.store(false);
  31.             futex_->WakeOne();
  32.           }
  33.  
  34.       private:
  35.           tpcc::atomic<bool> state_;
  36.           FutexLike<bool> *futex_;
  37.       };
  38.  
  39.   } // namespace solutions
  40. } // namespace tpcc
Advertisement
Add Comment
Please, Sign In to add comment