diff options
author | psychocrypt <psychocryptHPC@gmail.com> | 2018-05-03 20:40:49 +0200 |
---|---|---|
committer | Timothy Pearson <tpearson@raptorengineering.com> | 2018-06-04 21:07:11 +0000 |
commit | c68e94fb7e18d988aeed3924e505d9ccec87363f (patch) | |
tree | d4f5c00ab4b978e8db64f159e81b12690227f478 /xmrstak/backend | |
parent | 74b4e22aabf38286125316c151fbd3bb72d87bcd (diff) | |
download | xmr-stak-c68e94fb7e18d988aeed3924e505d9ccec87363f.zip xmr-stak-c68e94fb7e18d988aeed3924e505d9ccec87363f.tar.gz |
use read write locks to secure job updates
user read write locks to be sure that no job is consumend during the job update
Diffstat (limited to 'xmrstak/backend')
-rw-r--r-- | xmrstak/backend/globalStates.cpp | 8 | ||||
-rw-r--r-- | xmrstak/backend/globalStates.hpp | 59 |
2 files changed, 6 insertions, 61 deletions
diff --git a/xmrstak/backend/globalStates.cpp b/xmrstak/backend/globalStates.cpp index 6058b7a..8de6bfe 100644 --- a/xmrstak/backend/globalStates.cpp +++ b/xmrstak/backend/globalStates.cpp @@ -35,17 +35,17 @@ namespace xmrstak void globalStates::consume_work( miner_work& threadWork, uint64_t& currentJobId) { - jobLock.rdlock(); + jobLock.ReadLock(); threadWork = oGlobalWork; currentJobId = iGlobalJobNo.load(std::memory_order_relaxed); - jobLock.unlock(); + jobLock.UnLock(); } void globalStates::switch_work(miner_work& pWork, pool_data& dat) { - jobLock.wrlock(); + jobLock.WriteLock(); // this notifies all threads that the job has changed iGlobalJobNo++; @@ -62,7 +62,7 @@ void globalStates::switch_work(miner_work& pWork, pool_data& dat) dat.iSavedNonce = iGlobalNonce.exchange(dat.iSavedNonce, std::memory_order_relaxed); oGlobalWork = pWork; - jobLock.unlock(); + jobLock.UnLock(); } } // namespace xmrstak diff --git a/xmrstak/backend/globalStates.hpp b/xmrstak/backend/globalStates.hpp index 3add4e4..c8d6917 100644 --- a/xmrstak/backend/globalStates.hpp +++ b/xmrstak/backend/globalStates.hpp @@ -4,68 +4,13 @@ #include "xmrstak/misc/environment.hpp" #include "xmrstak/misc/console.hpp" #include "xmrstak/backend/pool_data.hpp" +#include "xmrstak/cpputil/read_write_lock.h" #include <atomic> -#include <condition_variable> namespace xmrstak { - -class RWLock { -public: - RWLock() : _status(0), _waiting_readers(0), _waiting_writers(0) {} - RWLock(const RWLock&) = delete; - RWLock(RWLock&&) = delete; - RWLock& operator = (const RWLock&) = delete; - RWLock& operator = (RWLock&&) = delete; - - void rdlock() { - std::unique_lock<std::mutex> lck(_mtx); - _waiting_readers += 1; - _read_cv.wait(lck, [&]() { return _waiting_writers == 0 && _status >= 0; }); - _waiting_readers -= 1; - _status += 1; - } - - void wrlock() { - std::unique_lock<std::mutex> lck(_mtx); - _waiting_writers += 1; - _write_cv.wait(lck, [&]() { return _status == 0; }); - _waiting_writers -= 1; - _status = -1; - } - - void unlock() { - std::unique_lock<std::mutex> lck(_mtx); - if (_status == -1) { - _status = 0; - } else { - _status -= 1; - } - if (_waiting_writers > 0) { - if (_status == 0) { - _write_cv.notify_one(); - } - } else { - _read_cv.notify_all(); - } - } - -private: - // -1 : one writer - // 0 : no reader and no writer - // n > 0 : n reader - int32_t _status; - int32_t _waiting_readers; - int32_t _waiting_writers; - std::mutex _mtx; - std::condition_variable _read_cv; - std::condition_variable _write_cv; -}; - - - struct globalStates { static inline globalStates& inst() @@ -101,7 +46,7 @@ private: { } - RWLock jobLock; + ::cpputil::RWLock jobLock; }; } // namespace xmrstak |