From 95f643cd9c14f3eb8ca3a265d0d0ea8f4b7c8865 Mon Sep 17 00:00:00 2001 From: Michael Stahl Date: Wed, 29 Nov 2017 15:18:12 +0100 Subject: tdf#114025 framework: avoid deadlock between Desktop init ... ... and SolarMutex: the problem is that rtl::StaticWithArg will first lock the implicit mutex of the C++11 static variable, and then the SolarMutex. So if one thread creates the Desktop singleton with SolarMutex locked and another thread without SolarMutex locked, this can deadlock. If we use rtl_Instance directly with SolarMutex, then there is still a static variable, but the SolarMutex will always be locked first, preventing this deadlock. Change-Id: Ibd37fdfa96a4a2b57f661be3814dd597eb52d338 Reviewed-on: https://gerrit.libreoffice.org/45508 Reviewed-by: Michael Meeks Tested-by: Jenkins (cherry picked from commit fa9c083c6071a0a4dc812f3c34731f347ddbabf7) Reviewed-on: https://gerrit.libreoffice.org/45557 Reviewed-by: Michael Stahl --- framework/source/services/desktop.cxx | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'framework/source/services') diff --git a/framework/source/services/desktop.cxx b/framework/source/services/desktop.cxx index fd52af489998..119dd5d69697 100644 --- a/framework/source/services/desktop.cxx +++ b/framework/source/services/desktop.cxx @@ -1796,10 +1796,30 @@ struct Instance { rtl::Reference instance; }; -struct Singleton: - public rtl::StaticWithArg< - Instance, css::uno::Reference, Singleton> -{}; +struct InstanceInit { + Instance * operator() (css::uno::Reference const& xContext) { + static Instance instance(xContext); + return &instance; + } +}; + +struct GetSolarMutex { + comphelper::SolarMutex * operator() () + { + return &Application::GetSolarMutex(); + } +}; + +Instance & getInstance(css::uno::Reference const& xContext) +{ + // tdf#114025 init with SolarMutex to avoid deadlock + return *rtl_Instance, + GetSolarMutex, + css::uno::Reference const> + ::create(InstanceInit(), GetSolarMutex(), xContext); +} } @@ -1808,7 +1828,7 @@ com_sun_star_comp_framework_Desktop_get_implementation( css::uno::XComponentContext *context, css::uno::Sequence const &) { - return cppu::acquire(Singleton::get(context).instance.get()); + return cppu::acquire(getInstance(context).instance.get()); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit