Skip to content

Commit

Permalink
shm: fix initialization of rc segment when region is created externally
Browse files Browse the repository at this point in the history
  • Loading branch information
rbx committed Nov 24, 2023
1 parent ff1f9b9 commit faf5770
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
14 changes: 8 additions & 6 deletions fairmq/shmem/Manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ class Manager
}

const uint16_t id = cfg.id.value();
const uint64_t rcSegmentSize = cfg.rcSegmentSize;

std::lock_guard<std::mutex> lock(fLocalRegionsMtx);

Expand All @@ -340,6 +339,12 @@ class Manager
LOG(debug) << "Unmanaged region (view) already present, promoting to controller";
region->BecomeController(cfg);
} else {
// we need to update local config, if the region information already exists
auto info = fShmRegions->find(id);
if (info != fShmRegions->end()) {
cfg.rcSegmentSize = info->second.fRCSegmentSize;
}

auto res = fRegions.emplace(id, std::make_unique<UnmanagedRegion>(fShmId, size, true, cfg));
region = res.first->second.get();
}
Expand All @@ -348,7 +353,6 @@ class Manager
// start ack receiver only if a callback has been provided.
if (callback || bulkCallback) {
region->SetCallbacks(callback, bulkCallback);
region->InitializeRefCountSegment(rcSegmentSize);
region->InitializeQueues();
region->StartAckSender();
region->StartAckReceiver();
Expand Down Expand Up @@ -401,19 +405,18 @@ class Manager

try {
RegionConfig cfg;
const uint64_t rcSegmentSize = cfg.rcSegmentSize;
// get region info
{
boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> shmLock(*fShmMtx);
RegionInfo regionInfo = fShmRegions->at(id);
cfg.id = id;
cfg.creationFlags = regionInfo.fCreationFlags;
cfg.rcSegmentSize = regionInfo.fRCSegmentSize;
cfg.path = regionInfo.fPath.c_str();
}
// LOG(debug) << "Located remote region with id '" << id << "', path: '" << cfg.path << "', flags: '" << cfg.creationFlags << "'";

auto r = fRegions.emplace(id, std::make_unique<UnmanagedRegion>(fShmId, 0, false, std::move(cfg)));
r.first->second->InitializeRefCountSegment(rcSegmentSize);
r.first->second->InitializeQueues();
r.first->second->StartAckSender();
return r.first->second.get();
Expand Down Expand Up @@ -482,6 +485,7 @@ class Manager
cfg.id = info.id;
cfg.creationFlags = regionInfo.fCreationFlags;
cfg.path = regionInfo.fPath.c_str();
cfg.rcSegmentSize = regionInfo.fRCSegmentSize;
regionCfgs.emplace(info.id, cfg);
// fill the ptr+size info after shmLock is released, to avoid constructing local region under it
} else {
Expand All @@ -503,10 +507,8 @@ class Manager
if (it != fRegions.end()) {
region = it->second.get();
} else {
const uint64_t rcSegmentSize = cfgIt->second.rcSegmentSize;
auto r = fRegions.emplace(cfgIt->first, std::make_unique<UnmanagedRegion>(fShmId, 0, false, cfgIt->second));
region = r.first->second.get();
region->InitializeRefCountSegment(rcSegmentSize);
region->InitializeQueues();
region->StartAckSender();
}
Expand Down
2 changes: 2 additions & 0 deletions fairmq/shmem/UnmanagedRegion.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ struct UnmanagedRegion
LOG(debug) << "Successfully zeroed free memory of region " << id << ".";
}

InitializeRefCountSegment(cfg.rcSegmentSize);

if (fControlling && created) {
Register(shmId, cfg);
}
Expand Down

0 comments on commit faf5770

Please sign in to comment.