Skip to content

Commit

Permalink
Use atomic functions to ensure single initialization of tracking cata…
Browse files Browse the repository at this point in the history
…log (#4592)

Fixes #4587 

## Change
Hold the tracking catalog object in a `shared_ptr` and use the atomic
functions to ensure that only one thread can initialize it.
  • Loading branch information
JohnMcPMS authored Jun 27, 2024
1 parent 986f240 commit a7f7c21
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,6 @@ namespace AppInstaller::Repository
bool m_isComposite = false;
std::optional<TimeSpan> m_backgroundUpdateInterval;
bool m_installedPackageInformationOnly = false;
mutable PackageTrackingCatalog m_trackingCatalog;
mutable std::shared_ptr<PackageTrackingCatalog> m_trackingCatalog;
};
}
13 changes: 10 additions & 3 deletions src/AppInstallerRepositoryCore/RepositorySource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,12 +983,19 @@ namespace AppInstaller::Repository

PackageTrackingCatalog Source::GetTrackingCatalog() const
{
if (!m_trackingCatalog)
// With C++20, consider removing the shared_ptr here and making the one inside PackageTrackingCatalog atomic.
std::shared_ptr<PackageTrackingCatalog> currentTrackingCatalog = std::atomic_load(&m_trackingCatalog);
if (!currentTrackingCatalog)
{
m_trackingCatalog = PackageTrackingCatalog::CreateForSource(*this);
std::shared_ptr<PackageTrackingCatalog> newTrackingCatalog = std::make_shared<PackageTrackingCatalog>(PackageTrackingCatalog::CreateForSource(*this));

if (std::atomic_compare_exchange_strong(&m_trackingCatalog, &currentTrackingCatalog, newTrackingCatalog))
{
currentTrackingCatalog = newTrackingCatalog;
}
}

return m_trackingCatalog;
return *currentTrackingCatalog;
}

std::vector<SourceDetails> Source::GetCurrentSources()
Expand Down

0 comments on commit a7f7c21

Please sign in to comment.