Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <atomic.hpp>
- #include <vector>
- namespace tpcc {
- namespace solutions {
- class TournamentTreeLock {
- public:
- explicit TournamentTreeLock(size_t num_threads) {
- num = num_threads;
- }
- void Lock(size_t thread_index) {
- for (size_t level = 1; level < num; level++) {
- levels[thread_index] = level;
- victim[level] = thread_index;
- bool conflicts_exist = true;
- while(conflicts_exist) {
- conflicts_exist = false;
- for(int another_thread = 1; another_thread < num; another_thread++) {
- if(another_thread!=thread_index && levels[another_thread].load() >= level && victim[level].load() == thread_index) {
- conflicts_exist = true;
- break;
- }
- }
- }
- }
- }
- void Unlock(size_t thread_index) {
- levels[thread_index] = 0;
- }
- private:
- std::atomic<size_t> num;
- atomic<size_t> levels[1000];
- atomic<size_t> victim[1000];
- };
- } // namespace solutions
- } // namespace tpcc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement