From b0145091a7a7503178fe47e6bfbe0de03caa4ba0 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Thu, 23 Feb 2017 14:48:51 -0800 Subject: [PATCH] Fix multiton issue I385a05a3ca01258e44fe3b37ef77e4aaff547b26 broke Singleton in the same way that 544e3e3606abebd2e5016bdb33a3ed05a1650e5b had already fixed once. Fix it again, the next CL will add tests. This affected cases where two libraries referenced the same singleton, the one that was supposed to define the singleton was already loaded, and then the second library was dlopen'd. Bug: 35674422 Test: out/host/linux-x86/nativetest64/libutils_tests/libutils_tests from later CL Change-Id: I87c64f95ed294a887e67a6c11be3072299789f01 --- libutils/include/utils/Singleton.h | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/libutils/include/utils/Singleton.h b/libutils/include/utils/Singleton.h index 7cc4c18be..a989a4703 100644 --- a/libutils/include/utils/Singleton.h +++ b/libutils/include/utils/Singleton.h @@ -26,6 +26,16 @@ namespace android { // --------------------------------------------------------------------------- +// Singleton may be used in multiple libraries, only one of which should +// define the static member variables using ANDROID_SINGLETON_STATIC_INSTANCE. +// Turn off -Wundefined-var-template so other users don't get: +// instantiation of variable 'android::Singleton::sLock' required here, +// but no definition is available +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wundefined-var-template" +#endif + template class ANDROID_API Singleton { @@ -56,11 +66,9 @@ private: static TYPE* sInstance; }; -template -Mutex Singleton::sLock; - -template -TYPE* Singleton::sInstance; +#if defined(__clang__) +#pragma clang diagnostic pop +#endif /* * use ANDROID_SINGLETON_STATIC_INSTANCE(TYPE) in your implementation file