summaryrefslogtreecommitdiffstats
path: root/xmrstak/backend/globalStates.hpp
diff options
context:
space:
mode:
authorpsychocrypt <psychocryptHPC@gmail.com>2018-05-03 20:35:56 +0200
committerTimothy Pearson <tpearson@raptorengineering.com>2018-06-04 21:07:11 +0000
commitd43548fe1b0ccda74477a149379b852f5abc36f7 (patch)
treecd472b2e5a7776da256b313bb66ec90c937642a9 /xmrstak/backend/globalStates.hpp
parentd68036dfe013c4949fdc636a5f60e599555fe2ca (diff)
downloadxmr-stak-d43548fe1b0ccda74477a149379b852f5abc36f7.zip
xmr-stak-d43548fe1b0ccda74477a149379b852f5abc36f7.tar.gz
add read write lock class
add log class from Will Zhang: Package: cpputil Source: https://github.com/willzhang4a58/cpputil License: MIT License
Diffstat (limited to 'xmrstak/backend/globalStates.hpp')
-rw-r--r--xmrstak/backend/globalStates.hpp58
1 files changed, 58 insertions, 0 deletions
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