mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-08-15 23:55:03 +03:00
Using pthreads to avoid concurrent access to the collection
This commit is contained in:
parent
37868d1534
commit
8fbb9e8128
@ -24,6 +24,8 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
#include "modsecurity/collection/variable.h"
|
#include "modsecurity/collection/variable.h"
|
||||||
#include "src/utils/regex.h"
|
#include "src/utils/regex.h"
|
||||||
#include "src/utils/string.h"
|
#include "src/utils/string.h"
|
||||||
@ -36,14 +38,21 @@ namespace backend {
|
|||||||
|
|
||||||
InMemoryPerProcess::InMemoryPerProcess() {
|
InMemoryPerProcess::InMemoryPerProcess() {
|
||||||
this->reserve(1000);
|
this->reserve(1000);
|
||||||
|
if (pthread_mutex_init(&m_lock, NULL) != 0)
|
||||||
|
{
|
||||||
|
printf("\n mutex init failed\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
InMemoryPerProcess::~InMemoryPerProcess() {
|
InMemoryPerProcess::~InMemoryPerProcess() {
|
||||||
this->clear();
|
this->clear();
|
||||||
|
pthread_mutex_destroy(&m_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InMemoryPerProcess::store(std::string key, std::string value) {
|
void InMemoryPerProcess::store(std::string key, std::string value) {
|
||||||
|
pthread_mutex_lock(&m_lock);
|
||||||
this->emplace(key, value);
|
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,
|
bool InMemoryPerProcess::updateFirst(const std::string &key,
|
||||||
const std::string &value) {
|
const std::string &value) {
|
||||||
|
pthread_mutex_lock(&m_lock);
|
||||||
auto range = this->equal_range(key);
|
auto range = this->equal_range(key);
|
||||||
|
|
||||||
for (auto it = range.first; it != range.second; ++it) {
|
for (auto it = range.first; it != range.second; ++it) {
|
||||||
it->second = value;
|
it->second = value;
|
||||||
|
pthread_mutex_unlock(&m_lock);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
pthread_mutex_unlock(&m_lock);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void InMemoryPerProcess::del(const std::string& key) {
|
void InMemoryPerProcess::del(const std::string& key) {
|
||||||
|
pthread_mutex_lock(&m_lock);
|
||||||
this->erase(key);
|
this->erase(key);
|
||||||
|
pthread_mutex_unlock(&m_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -91,6 +91,9 @@ class InMemoryPerProcess :
|
|||||||
std::vector<const Variable *> *l) override;
|
std::vector<const Variable *> *l) override;
|
||||||
void resolveRegularExpression(const std::string& var,
|
void resolveRegularExpression(const std::string& var,
|
||||||
std::vector<const Variable *> *l) override;
|
std::vector<const Variable *> *l) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
pthread_mutex_t m_lock;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace backend
|
} // namespace backend
|
||||||
|
Loading…
x
Reference in New Issue
Block a user