diff options
-rw-r--r-- | THIRD-PARTY-LICENSES | 9 | ||||
-rw-r--r-- | xmrstak/backend/globalStates.cpp | 51 | ||||
-rw-r--r-- | xmrstak/backend/globalStates.hpp | 58 | ||||
-rw-r--r-- | xmrstak/cpputil/LICENSE.txt | 21 | ||||
-rw-r--r-- | xmrstak/cpputil/read_write_lock.h | 63 |
5 files changed, 156 insertions, 46 deletions
diff --git a/THIRD-PARTY-LICENSES b/THIRD-PARTY-LICENSES index d3202f4..e3f574f 100644 --- a/THIRD-PARTY-LICENSES +++ b/THIRD-PARTY-LICENSES @@ -5,7 +5,7 @@ This application bundles the following third-party software in accordance with t Package: Original NVidia mining code Authors: tsiv and KlausT License: GNU GPLv3 -Notes: Improvements are (c) of Xmr-Stak team +Notes: Improvements are (c) of Xmr-Stak team team and are covered by GNU GPLv3 ------------------------------------------------------------------------- @@ -27,3 +27,10 @@ Authors: okdshin License: MIT License ------------------------------------------------------------------------- + +Package: cpputil +Authors: Will Zhang +Source: https://github.com/willzhang4a58/cpputil +License: MIT License + +------------------------------------------------------------------------- diff --git a/xmrstak/backend/globalStates.cpp b/xmrstak/backend/globalStates.cpp index e60db8f..6058b7a 100644 --- a/xmrstak/backend/globalStates.cpp +++ b/xmrstak/backend/globalStates.cpp @@ -35,57 +35,18 @@ namespace xmrstak void globalStates::consume_work( miner_work& threadWork, uint64_t& currentJobId) { - /* Only the executer thread which updates the job is ever setting iConsumeCnt - * to 1000. In this case each consumer must wait until the job is fully updated. - */ - uint64_t numConsumer = 0; - - /* Take care that we not consume a job if the job is updated. - * If we leave the loop we have increased iConsumeCnt so that - * the job will not be updated until we leave the method. - */ - do{ - numConsumer = iConsumeCnt.load(std::memory_order_relaxed); - if(numConsumer < 1000) - { - // register that thread try consume job data - numConsumer = ++iConsumeCnt; - if(numConsumer >= 1000) - { - iConsumeCnt--; - // 11 is a arbitrary chosen prime number - std::this_thread::sleep_for(std::chrono::milliseconds(11)); - } - } - else - { - // an other thread is preparing a new job, 11 is a arbitrary chosen prime number - std::this_thread::sleep_for(std::chrono::milliseconds(11)); - } - } - while(numConsumer >= 1000); + jobLock.rdlock(); threadWork = oGlobalWork; currentJobId = iGlobalJobNo.load(std::memory_order_relaxed); - // signal that thread consumed work - iConsumeCnt--; + jobLock.unlock(); } void globalStates::switch_work(miner_work& pWork, pool_data& dat) { - /* 1000 is used to notify that the the job will be updated as soon - * as all consumer (which currently coping oGlobalWork has copied - * all data) - */ - iConsumeCnt += 1000; - // wait until all threads which entered consume_work are finished - while (iConsumeCnt.load(std::memory_order_relaxed) > 1000) - { - // 7 is a arbitrary chosen prime number which is smaller than the consumer waiting time - std::this_thread::sleep_for(std::chrono::milliseconds(7)); - } - // BEGIN CRITICAL SECTION + jobLock.wrlock(); + // this notifies all threads that the job has changed iGlobalJobNo++; @@ -100,8 +61,8 @@ void globalStates::switch_work(miner_work& pWork, pool_data& dat) */ dat.iSavedNonce = iGlobalNonce.exchange(dat.iSavedNonce, std::memory_order_relaxed); oGlobalWork = pWork; - // END CRITICAL SECTION: allow job consume - iConsumeCnt -= 1000; + + jobLock.unlock(); } } // namespace xmrstak diff --git a/xmrstak/backend/globalStates.hpp b/xmrstak/backend/globalStates.hpp index 44b6e0b..3add4e4 100644 --- a/xmrstak/backend/globalStates.hpp +++ b/xmrstak/backend/globalStates.hpp @@ -6,10 +6,66 @@ #include "xmrstak/backend/pool_data.hpp" #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() @@ -44,6 +100,8 @@ private: globalStates() : iThreadCount(0), iGlobalJobNo(0), iConsumeCnt(0) { } + + RWLock jobLock; }; } // namespace xmrstak diff --git a/xmrstak/cpputil/LICENSE.txt b/xmrstak/cpputil/LICENSE.txt new file mode 100644 index 0000000..7615431 --- /dev/null +++ b/xmrstak/cpputil/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Will Zhang + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/xmrstak/cpputil/read_write_lock.h b/xmrstak/cpputil/read_write_lock.h new file mode 100644 index 0000000..b2db609 --- /dev/null +++ b/xmrstak/cpputil/read_write_lock.h @@ -0,0 +1,63 @@ +#ifndef CPPUTIL_READ_WRITE_LOCK_H_ +#define CPPUTIL_READ_WRITE_LOCK_H_ + +#include <mutex> +#include <condition_variable> + +namespace cpputil { + +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 ReadLock() { + 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 WriteLock() { + 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_; +}; + +} // namespace cpputil + +#endif // CPPUTIL_READ_WRITE_LOCK_H_ |