Skip to content

Commit 3da4aee

Browse files
author
David Goodwin
committed
SharedMemoryBlockManager must be thread safe
1 parent 7fc9a0f commit 3da4aee

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/servers/shared_memory_block_manager.cc

+12
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ SharedMemoryBlockManager::CpuCreate(
4343
{
4444
*smb = nullptr;
4545

46+
std::lock_guard<std::mutex> lock(mu_);
47+
4648
if (blocks_.find(name) != blocks_.end()) {
4749
return TRTSERVER_ErrorNew(
4850
TRTSERVER_ERROR_ALREADY_EXISTS,
@@ -67,6 +69,8 @@ SharedMemoryBlockManager::GpuCreate(
6769
{
6870
*smb = nullptr;
6971

72+
std::lock_guard<std::mutex> lock(mu_);
73+
7074
if (blocks_.find(name) != blocks_.end()) {
7175
return TRTSERVER_ErrorNew(
7276
TRTSERVER_ERROR_ALREADY_EXISTS,
@@ -89,6 +93,8 @@ SharedMemoryBlockManager::Get(
8993
{
9094
*smb = nullptr;
9195

96+
std::lock_guard<std::mutex> lock(mu_);
97+
9298
auto itr = blocks_.find(name);
9399
if (itr == blocks_.end()) {
94100
return TRTSERVER_ErrorNew(
@@ -108,6 +114,8 @@ SharedMemoryBlockManager::Find(
108114
{
109115
*smb = nullptr;
110116

117+
std::lock_guard<std::mutex> lock(mu_);
118+
111119
auto itr = blocks_.find(name);
112120
if (itr != blocks_.end()) {
113121
*smb = itr->second;
@@ -122,6 +130,8 @@ SharedMemoryBlockManager::Remove(
122130
{
123131
*smb = nullptr;
124132

133+
std::lock_guard<std::mutex> lock(mu_);
134+
125135
auto itr = blocks_.find(name);
126136
if (itr != blocks_.end()) {
127137
*smb = itr->second;
@@ -136,6 +146,8 @@ SharedMemoryBlockManager::Clear()
136146
{
137147
std::string failed_blocks;
138148

149+
std::lock_guard<std::mutex> lock(mu_);
150+
139151
auto it = blocks_.begin();
140152
while (it != blocks_.cend()) {
141153
TRTSERVER_Error* err = TRTSERVER_SharedMemoryBlockDelete(it->second);

src/servers/shared_memory_block_manager.h

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class SharedMemoryBlockManager {
102102
TRTSERVER_Error* Clear();
103103

104104
private:
105+
std::mutex mu_;
105106
std::unordered_map<std::string, TRTSERVER_SharedMemoryBlock*> blocks_;
106107
};
107108

0 commit comments

Comments
 (0)