Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/actions/spelling/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ deliveryoptimization
deliveryoptimizationerrors
DENYWR
desktopappinstaller
devblogs
devhome
DFX
dic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,43 @@ namespace AppInstaller::Repository::Microsoft

struct CachedInstalledIndex
{
// https://devblogs.microsoft.com/oldnewthing/20210215-00/?p=104865
struct Singleton
{
struct Holder : public winrt::implements<Holder, winrt::Windows::Foundation::IInspectable>
{
static constexpr std::wstring_view Guid{ L"{48c47064-4fff-4eca-812c-dbb4f33a8fcb}" };
std::shared_ptr<CachedInstalledIndex> m_shared{ std::make_shared<CachedInstalledIndex>() };
};

std::weak_ptr<CachedInstalledIndex> m_weak;
winrt::slim_mutex m_lock;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: everything except Get() can go to private: section?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a "private" struct anyway, being declared in the .cpp.


std::shared_ptr<CachedInstalledIndex> Get()
{
{
const std::shared_lock lock{ m_lock };
if (auto cachedIndex = m_weak.lock())
{
return cachedIndex;
}
}

auto value = winrt::make_self<Holder>();

const std::shared_lock lock{ m_lock };
if (auto cachedIndex = m_weak.lock())
{
return cachedIndex;
}

winrt::Windows::ApplicationModel::Core::CoreApplication::Properties().Insert(Holder::Guid, value.as<winrt::Windows::Foundation::IInspectable>());

m_weak = value->m_shared;
return value->m_shared;
}
};

CachedInstalledIndex()
{
ARPHelper arpHelper;
Expand Down Expand Up @@ -340,7 +377,7 @@ namespace AppInstaller::Repository::Microsoft

if (PredefinedInstalledSourceFactory::StringToFilter(m_details.Arg) == PredefinedInstalledSourceFactory::Filter::NoneWithForcedCacheUpdate)
{
GetCachedInstalledIndex().ForceNextUpdate();
GetCachedInstalledIndex()->ForceNextUpdate();
}
}

Expand All @@ -359,9 +396,9 @@ namespace AppInstaller::Repository::Microsoft
// Only cache for the unfiltered install data
if (filter == PredefinedInstalledSourceFactory::Filter::None || filter == PredefinedInstalledSourceFactory::Filter::NoneWithForcedCacheUpdate)
{
CachedInstalledIndex& cachedIndex = GetCachedInstalledIndex();
cachedIndex.UpdateIndexIfNeeded();
return std::make_shared<SQLiteIndexSource>(m_details, cachedIndex.GetCopy(), true);
std::shared_ptr<CachedInstalledIndex> cachedIndex = GetCachedInstalledIndex();
cachedIndex->UpdateIndexIfNeeded();
return std::make_shared<SQLiteIndexSource>(m_details, cachedIndex->GetCopy(), true);
}
else
{
Expand All @@ -370,10 +407,10 @@ namespace AppInstaller::Repository::Microsoft
}

private:
CachedInstalledIndex& GetCachedInstalledIndex()
std::shared_ptr<CachedInstalledIndex> GetCachedInstalledIndex()
{
static CachedInstalledIndex s_installedIndex;
return s_installedIndex;
static CachedInstalledIndex::Singleton s_installedIndex;
return s_installedIndex.Get();
}

SourceDetails m_details;
Expand Down
6 changes: 4 additions & 2 deletions src/AppInstallerRepositoryCore/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

#include <winsqlite/winsqlite3.h>

#include <winrt/Windows.ApplicationModel.h>
#include <winrt/Windows.ApplicationModel.h>
#include <winrt/Windows.ApplicationModel.Core.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Management.Deployment.h>
Expand All @@ -41,7 +42,8 @@
#include <memory>
#include <optional>
#include <random>
#include <set>
#include <set>
#include <shared_mutex>
#include <string>
#include <string_view>
#include <sstream>
Expand Down
Loading