From 004d01981905f420287bb0c61045d957553c0ac3 Mon Sep 17 00:00:00 2001 From: Dario Date: Fri, 5 Jun 2020 13:20:00 +0200 Subject: [PATCH] Update mutex, now using std::shared_mutex allowing reload of word list while server is running --- src/cpp/Crypto/mnemonic.cpp | 9 +++++++-- src/cpp/Crypto/mnemonic.h | 17 ++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/cpp/Crypto/mnemonic.cpp b/src/cpp/Crypto/mnemonic.cpp index 53393ad3c..e9147ae99 100644 --- a/src/cpp/Crypto/mnemonic.cpp +++ b/src/cpp/Crypto/mnemonic.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "../dependencies/tinf/src/tinf.h" #include "DRRandom.h" @@ -23,7 +24,7 @@ Mnemonic::~Mnemonic() int Mnemonic::init(void(*fill_words_func)(unsigned char*), unsigned int original_size, unsigned int compressed_size) { - Poco::Mutex::ScopedLock _lock(mWorkingMutex, 500); + std::unique_lock _lock(mWorkingMutex); clear(); unsigned char* buffer = (unsigned char*)malloc(compressed_size); @@ -152,8 +153,9 @@ int Mnemonic::init(void(*fill_words_func)(unsigned char*), unsigned int original return -5; } -short Mnemonic::getWordIndex(const char* word) const +short Mnemonic::getWordIndex(const char* word) { + std::shared_lock _lock(mWorkingMutex); DHASH word_hash = DRMakeStringHash(word); auto it = mWordHashIndices.find(word_hash); if (it != mWordHashIndices.end()) { @@ -193,6 +195,7 @@ void Mnemonic::clear() std::string Mnemonic::getCompleteWordList() { + std::shared_lock _lock(mWorkingMutex); std::string result(""); for (int i = 0; i < 2048; i++) { if (mWords[i]) { @@ -207,6 +210,7 @@ std::string Mnemonic::getCompleteWordList() void Mnemonic::printToFile(const char* filename) { + std::shared_lock _lock(mWorkingMutex); FILE* f = fopen(filename, "wt"); auto words = getCompleteWordList(); fwrite(words.data(), 1, words.size(), f); @@ -215,6 +219,7 @@ void Mnemonic::printToFile(const char* filename) Poco::JSON::Array Mnemonic::getSortedWordList() { + std::shared_lock _lock(mWorkingMutex); std::list words; for (auto it = mWordHashIndices.begin(); it != mWordHashIndices.end(); it++) { words.push_back(mWords[it->second]); diff --git a/src/cpp/Crypto/mnemonic.h b/src/cpp/Crypto/mnemonic.h index 216e122ee..afc708ac1 100644 --- a/src/cpp/Crypto/mnemonic.h +++ b/src/cpp/Crypto/mnemonic.h @@ -12,8 +12,8 @@ */ #include "../lib/DRHashList.h" -#include "Poco/Mutex.h" #include +#include #include #include @@ -30,9 +30,16 @@ public: int init(void(*fill_words_func)(unsigned char*), unsigned int original_size, unsigned int compressed_size); - inline const char* getWord(short index) const { if (index < 2048 && index >= 0) return mWords[index]; return nullptr; } - short getWordIndex(const char* word) const; - inline bool isWordExist(const std::string& word) const { return getWordIndex(word.data()) != -1; } + inline const char* getWord(short index) { + std::shared_lock _lock(mWorkingMutex); + if (index < 2048 && index >= 0) return mWords[index]; + return nullptr; + } + short getWordIndex(const char* word); + inline bool isWordExist(const std::string& word) { + std::shared_lock _lock(mWorkingMutex); + return getWordIndex(word.data()) != -1; + } // using only for debugging std::string getCompleteWordList(); @@ -55,7 +62,7 @@ protected: typedef std::pair HashCollideWordEntry; std::map mWordHashIndices; std::map> mHashCollisionWords; - Poco::Mutex mWorkingMutex; + std::shared_mutex mWorkingMutex; };