Skip to content

Commit 8fbb9e8

Browse files
author
Felipe Zimmerle
committed
Using pthreads to avoid concurrent access to the collection
1 parent 37868d1 commit 8fbb9e8

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/collection/backend/in_memory-per_process.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include <memory>
2525
#endif
2626

27+
#include <pthread.h>
28+
2729
#include "modsecurity/collection/variable.h"
2830
#include "src/utils/regex.h"
2931
#include "src/utils/string.h"
@@ -36,14 +38,21 @@ namespace backend {
3638

3739
InMemoryPerProcess::InMemoryPerProcess() {
3840
this->reserve(1000);
41+
if (pthread_mutex_init(&m_lock, NULL) != 0)
42+
{
43+
printf("\n mutex init failed\n");
44+
}
3945
}
4046

4147
InMemoryPerProcess::~InMemoryPerProcess() {
4248
this->clear();
49+
pthread_mutex_destroy(&m_lock);
4350
}
4451

4552
void InMemoryPerProcess::store(std::string key, std::string value) {
53+
pthread_mutex_lock(&m_lock);
4654
this->emplace(key, value);
55+
pthread_mutex_unlock(&m_lock);
4756
}
4857

4958

@@ -58,18 +67,23 @@ bool InMemoryPerProcess::storeOrUpdateFirst(const std::string &key,
5867

5968
bool InMemoryPerProcess::updateFirst(const std::string &key,
6069
const std::string &value) {
70+
pthread_mutex_lock(&m_lock);
6171
auto range = this->equal_range(key);
6272

6373
for (auto it = range.first; it != range.second; ++it) {
6474
it->second = value;
75+
pthread_mutex_unlock(&m_lock);
6576
return true;
6677
}
78+
pthread_mutex_unlock(&m_lock);
6779
return false;
6880
}
6981

7082

7183
void InMemoryPerProcess::del(const std::string& key) {
84+
pthread_mutex_lock(&m_lock);
7285
this->erase(key);
86+
pthread_mutex_unlock(&m_lock);
7387
}
7488

7589

src/collection/backend/in_memory-per_process.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ class InMemoryPerProcess :
9191
std::vector<const Variable *> *l) override;
9292
void resolveRegularExpression(const std::string& var,
9393
std::vector<const Variable *> *l) override;
94+
95+
private:
96+
pthread_mutex_t m_lock;
9497
};
9598

9699
} // namespace backend

0 commit comments

Comments
 (0)