Skip to content

Commit a77410b

Browse files
authored
Update NamedMutex (#1202)
NamedMutex stores global array of mutexes provided via reference. Wrap them into unique_ptr to allow safely reordered elements in container. Relates-To: OLPSUP-14070 Signed-off-by: Iuliia Moroz <[email protected]>
1 parent c7a9f9d commit a77410b

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

olp-cpp-sdk-dataservice-read/src/repositories/NamedMutex.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919

2020
#include "NamedMutex.h"
2121

22+
#include <memory>
2223
#include <unordered_map>
2324

25+
#include <olp/core/porting/make_unique.h>
26+
2427
namespace olp {
2528
namespace dataservice {
2629
namespace read {
@@ -31,7 +34,9 @@ namespace {
3134
static std::mutex gMutex;
3235

3336
struct RefCounterMutex {
34-
std::mutex mutex;
37+
explicit RefCounterMutex() : mutex(std::make_unique<std::mutex>()) {}
38+
39+
std::unique_ptr<std::mutex> mutex;
3540
uint32_t use_count{0};
3641
};
3742

@@ -41,7 +46,7 @@ std::mutex& AquireLock(const std::string& resource) {
4146
std::unique_lock<std::mutex> lock(gMutex);
4247
RefCounterMutex& ref_mutex = gMutexes[resource];
4348
ref_mutex.use_count++;
44-
return ref_mutex.mutex;
49+
return *ref_mutex.mutex;
4550
}
4651

4752
void ReleaseLock(const std::string& resource) {

0 commit comments

Comments
 (0)