Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- #include <atomic.hpp>
- #include <support.hpp>
- namespace tpcc {
- namespace solutions {
- class PetersonLock {
- public:
- PetersonLock() {
- flag[0].store(false);
- flag[1].store(false);
- victim.store(0);
- }
- void Lock(const size_t thread_index) {
- const size_t another_thread = 1 - thread_index;
- flag[thread_index].store(true);
- victim.store(thread_index);
- while(flag[another_thread].load() && victim.load() == thread_index) {};
- }
- void Unlock(const size_t thread_index) {
- flag[thread_index].store(false);
- }
- private:
- std::atomic<bool> flag[2];
- std::atomic<size_t> victim;
- };
- class TournamentTreeLock {
- public:
- explicit TournamentTreeLock(const size_t num_threads) {
- return;
- }
- void Lock(const size_t thread_index) {
- //std::cerr << "TRY LOCK " << thread_index << std::endl;
- pt.Lock(thread_index);
- //std::cerr << "GET LOCK " << thread_index << std::endl;
- }
- void Unlock(const size_t thread_index) {
- //std::cerr << "TRY UNLOCK " << thread_index << std::endl;
- pt.Unlock(thread_index);
- //std::cerr << "GET UNLOCK " << thread_index << std::endl;
- }
- private:
- PetersonLock pt;
- };
- } // namespace solutions
- } // namespace tpcc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement