diff --git a/sycl/source/detail/global_handler.cpp b/sycl/source/detail/global_handler.cpp index 5669fbdaacc50..078da1aab83ba 100644 --- a/sycl/source/detail/global_handler.cpp +++ b/sycl/source/detail/global_handler.cpp @@ -136,11 +136,11 @@ GlobalHandler &GlobalHandler::instance() { } template -T &GlobalHandler::getOrCreate(InstWithLock &IWL, Types... Args) { +T &GlobalHandler::getOrCreate(InstWithLock &IWL, Types &&...Args) { const LockGuard Lock{IWL.Lock}; if (!IWL.Inst) - IWL.Inst = std::make_unique(Args...); + IWL.Inst = std::make_unique(std::forward(Args)...); return *IWL.Inst; } @@ -182,40 +182,58 @@ void GlobalHandler::registerSchedulerUsage(bool ModifyCounter) { } ProgramManager &GlobalHandler::getProgramManager() { - return getOrCreate(MProgramManager); + static ProgramManager &PM = getOrCreate(MProgramManager); + return PM; } std::unordered_map & GlobalHandler::getPlatformToDefaultContextCache() { + // The optimization with static reference is not done because + // there are public methods of the GlobalHandler + // that can set the MPlatformToDefaultContextCache back to nullptr. + // So one time initialization is not possible and we need + // to call getOrCreate on every access. return getOrCreate(MPlatformToDefaultContextCache); } std::mutex &GlobalHandler::getPlatformToDefaultContextCacheMutex() { - return getOrCreate(MPlatformToDefaultContextCacheMutex); + static std::mutex &PlatformToDefaultContextCacheMutex = + getOrCreate(MPlatformToDefaultContextCacheMutex); + return PlatformToDefaultContextCacheMutex; } -Sync &GlobalHandler::getSync() { return getOrCreate(MSync); } +Sync &GlobalHandler::getSync() { + static Sync &sync = getOrCreate(MSync); + return sync; +} std::vector &GlobalHandler::getPlatformCache() { - return getOrCreate(MPlatformCache); + static std::vector &PlatformCache = + getOrCreate(MPlatformCache); + return PlatformCache; } std::mutex &GlobalHandler::getPlatformMapMutex() { - return getOrCreate(MPlatformMapMutex); + static std::mutex &PlatformMapMutex = getOrCreate(MPlatformMapMutex); + return PlatformMapMutex; } std::mutex &GlobalHandler::getFilterMutex() { - return getOrCreate(MFilterMutex); + static std::mutex &FilterMutex = getOrCreate(MFilterMutex); + return FilterMutex; } std::vector &GlobalHandler::getAdapters() { + static std::vector &adapters = getOrCreate(MAdapters); enableOnCrashStackPrinting(); - return getOrCreate(MAdapters); + return adapters; } ods_target_list & GlobalHandler::getOneapiDeviceSelectorTargets(const std::string &InitValue) { - return getOrCreate(MOneapiDeviceSelectorTargets, InitValue); + static ods_target_list &OneapiDeviceSelectorTargets = + getOrCreate(MOneapiDeviceSelectorTargets, InitValue); + return OneapiDeviceSelectorTargets; } XPTIRegistry &GlobalHandler::getXPTIRegistry() { @@ -223,9 +241,8 @@ XPTIRegistry &GlobalHandler::getXPTIRegistry() { } ThreadPool &GlobalHandler::getHostTaskThreadPool() { - int Size = SYCLConfig::get(); - ThreadPool &TP = getOrCreate(MHostTaskThreadPool, Size); - + static ThreadPool &TP = getOrCreate( + MHostTaskThreadPool, SYCLConfig::get()); return TP; } diff --git a/sycl/source/detail/global_handler.hpp b/sycl/source/detail/global_handler.hpp index 4b834927e3832..ef5bc32c0db0d 100644 --- a/sycl/source/detail/global_handler.hpp +++ b/sycl/source/detail/global_handler.hpp @@ -113,7 +113,7 @@ class GlobalHandler { }; template - T &getOrCreate(InstWithLock &IWL, Types... Args); + T &getOrCreate(InstWithLock &IWL, Types &&...Args); InstWithLock MScheduler; InstWithLock MProgramManager;