Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <rwlock_traits.hpp>
- #include <tpcc/stdlike/condition_variable.hpp>
- #include <mutex>
- namespace tpcc {
- namespace solutions {
- class ReaderWriterLock {
- public:
- // Reader section
- void ReaderLock() {
- std::unique_lock<std::mutex> lock (mutex_);
- while(write_acquire_ > 0) {
- cond_var_.wait(lock);
- }
- readers_++;
- }
- void ReaderUnlock() {
- std::unique_lock<std::mutex> lock (mutex_);
- readers_--;
- cond_var_.notify_all();
- }
- // Writer section
- void WriterLock() {
- std::unique_lock<std::mutex> lock (mutex_);
- write_acquire_++;
- while(readers_ > 0 || write_) {
- cond_var_.wait(lock);
- }
- write_ = true;
- }
- void WriterUnlock() {
- std::unique_lock<std::mutex> lock (mutex_);
- write_acquire_--;
- write_ = false;
- cond_var_.notify_all();
- }
- private:
- bool write_{false};
- size_t write_acquire_{0};
- size_t readers_{0};
- std::mutex mutex_;
- tpcc::condition_variable cond_var_;
- };
- } // namespace solutions
- } // namespace tpcc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement