Advertisement
MystMe

Untitled

Feb 21st, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <atomic.hpp>
  4. #include <vector>
  5.  
  6. namespace tpcc {
  7.     namespace solutions {
  8.         class TournamentTreeLock {
  9.         public:
  10.             explicit TournamentTreeLock(size_t num_threads) {
  11.               num = num_threads;
  12.             }
  13.  
  14.             void Lock(size_t thread_index) {
  15.               for (size_t level = 1; level < num; level++) {
  16.                 levels[thread_index] = level;
  17.                 victim[level] = thread_index;
  18.  
  19.                 bool conflicts_exist = true;
  20.                 while(conflicts_exist) {
  21.                   conflicts_exist = false;
  22.                   for(int another_thread = 1; another_thread < num; another_thread++) {
  23.                     if(another_thread!=thread_index && levels[another_thread].load() >= level && victim[level].load() == thread_index) {
  24.                       conflicts_exist = true;
  25.                       break;
  26.                     }
  27.                   }
  28.                 }
  29.               }
  30.             }
  31.  
  32.             void Unlock(size_t thread_index) {
  33.               levels[thread_index] = 0;
  34.             }
  35.  
  36.         private:
  37.             std::atomic<size_t> num;
  38.             atomic<size_t> levels[1000];
  39.             atomic<size_t> victim[1000];
  40.         };
  41.  
  42.     } // namespace solutions
  43. } // namespace tpcc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement