Update mutex, now using std::shared_mutex allowing reload of word list while server is running

This commit is contained in:
Dario 2020-06-05 13:20:00 +02:00
parent 7030333e91
commit 004d019819
2 changed files with 19 additions and 7 deletions

View File

@ -4,6 +4,7 @@
#include <memory>
#include <cstring>
#include <assert.h>
#include <mutex>
#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<std::shared_mutex> _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<std::shared_mutex> _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<std::shared_mutex> _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<std::shared_mutex> _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<std::shared_mutex> _lock(mWorkingMutex);
std::list<std::string> words;
for (auto it = mWordHashIndices.begin(); it != mWordHashIndices.end(); it++) {
words.push_back(mWords[it->second]);

View File

@ -12,8 +12,8 @@
*/
#include "../lib/DRHashList.h"
#include "Poco/Mutex.h"
#include <string>
#include <shared_mutex>
#include <map>
#include <list>
@ -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<std::shared_mutex> _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<std::shared_mutex> _lock(mWorkingMutex);
return getWordIndex(word.data()) != -1;
}
// using only for debugging
std::string getCompleteWordList();
@ -55,7 +62,7 @@ protected:
typedef std::pair<std::string, unsigned short> HashCollideWordEntry;
std::map<DHASH, unsigned short> mWordHashIndices;
std::map<DHASH, std::map<std::string, unsigned short>> mHashCollisionWords;
Poco::Mutex mWorkingMutex;
std::shared_mutex mWorkingMutex;
};