diff --git a/strings/base_activation.h b/strings/base_activation.h index 942b3b342..8d8eb5e92 100644 --- a/strings/base_activation.h +++ b/strings/base_activation.h @@ -233,19 +233,23 @@ namespace winrt::impl explicit factory_count_guard(size_t& count) noexcept : m_count(count) { +#ifndef WINRT_NO_MODULE_LOCK #ifdef _WIN64 _InterlockedIncrement64((int64_t*)&m_count); #else _InterlockedIncrement((long*)&m_count); +#endif #endif } ~factory_count_guard() noexcept { +#ifndef WINRT_NO_MODULE_LOCK #ifdef _WIN64 _InterlockedDecrement64((int64_t*)&m_count); #else _InterlockedDecrement((long*)&m_count); +#endif #endif } @@ -362,7 +366,9 @@ namespace winrt::impl if (nullptr == _InterlockedCompareExchangePointer(reinterpret_cast(&m_value.object), *reinterpret_cast(&object), nullptr)) { *reinterpret_cast(&object) = nullptr; +#ifndef WINRT_NO_MODULE_LOCK get_factory_cache().add(this); +#endif } return callback(*reinterpret_cast const*>(&m_value.object)); diff --git a/test/test_module_lock_none/main.cpp b/test/test_module_lock_none/main.cpp index a5001ce13..1a4b19fa7 100644 --- a/test/test_module_lock_none/main.cpp +++ b/test/test_module_lock_none/main.cpp @@ -6,6 +6,7 @@ #define WINRT_NO_MODULE_LOCK #include "winrt/Windows.Foundation.h" +#include "winrt/Windows.System.Diagnostics.h" namespace { @@ -26,9 +27,31 @@ TEST_CASE("module_lock_none") REQUIRE(--winrt::get_module_lock() == 0); REQUIRE(--winrt::get_module_lock() == 0); - // Just validates that you can still construct an implementation without a module lock. + // Validates that you can still construct an implementation without a module lock. winrt::make(); + + // Validates that clear_factory_cache is still callable, but has no effect. + // Windows.System.Diagnostics is used because it is implemented in C++/WinRT that + // unlike WRL, does not cache factories inside the implementation. Client-side + // caching is far more effective. + + void* first = nullptr; + void* second = nullptr; + + { + auto factory = winrt::get_activation_factory(); + first = winrt::get_abi(factory); + } + winrt::clear_factory_cache(); + + { + auto factory = winrt::get_activation_factory(); + second = winrt::get_abi(factory); + } + winrt::clear_factory_cache(); + + REQUIRE(first == second); } int main(int const argc, char** argv)