summaryrefslogtreecommitdiffstats
path: root/xmrstak/backend
diff options
context:
space:
mode:
Diffstat (limited to 'xmrstak/backend')
-rw-r--r--xmrstak/backend/globalStates.cpp51
-rw-r--r--xmrstak/backend/globalStates.hpp58
2 files changed, 64 insertions, 45 deletions
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
OpenPOWER on IntegriCloud