Using pthreads to avoid concurrent access to the collection

This commit is contained in:
Felipe Zimmerle 2017-06-03 16:07:35 -03:00
parent 37868d1534
commit 8fbb9e8128
No known key found for this signature in database
GPG Key ID: E6DFB08CE8B11277
2 changed files with 17 additions and 0 deletions

View File

@ -24,6 +24,8 @@
#include <memory>
#endif
#include <pthread.h>
#include "modsecurity/collection/variable.h"
#include "src/utils/regex.h"
#include "src/utils/string.h"
@ -36,14 +38,21 @@ namespace backend {
InMemoryPerProcess::InMemoryPerProcess() {
this->reserve(1000);
if (pthread_mutex_init(&m_lock, NULL) != 0)
{
printf("\n mutex init failed\n");
}
}
InMemoryPerProcess::~InMemoryPerProcess() {
this->clear();
pthread_mutex_destroy(&m_lock);
}
void InMemoryPerProcess::store(std::string key, std::string value) {
pthread_mutex_lock(&m_lock);
this->emplace(key, value);
pthread_mutex_unlock(&m_lock);
}
@ -58,18 +67,23 @@ bool InMemoryPerProcess::storeOrUpdateFirst(const std::string &key,
bool InMemoryPerProcess::updateFirst(const std::string &key,
const std::string &value) {
pthread_mutex_lock(&m_lock);
auto range = this->equal_range(key);
for (auto it = range.first; it != range.second; ++it) {
it->second = value;
pthread_mutex_unlock(&m_lock);
return true;
}
pthread_mutex_unlock(&m_lock);
return false;
}
void InMemoryPerProcess::del(const std::string& key) {
pthread_mutex_lock(&m_lock);
this->erase(key);
pthread_mutex_unlock(&m_lock);
}

View File

@ -91,6 +91,9 @@ class InMemoryPerProcess :
std::vector<const Variable *> *l) override;
void resolveRegularExpression(const std::string& var,
std::vector<const Variable *> *l) override;
private:
pthread_mutex_t m_lock;
};
} // namespace backend