diff --git a/sycl/source/detail/global_handler.cpp b/sycl/source/detail/global_handler.cpp index c0174f81d6d80..5fef7ea4e478b 100644 --- a/sycl/source/detail/global_handler.cpp +++ b/sycl/source/detail/global_handler.cpp @@ -143,6 +143,12 @@ void shutdown() { // there's no need to load and unload plugins. if (GlobalHandler::instance().MPlugins.Inst) { for (plugin &Plugin : GlobalHandler::instance().getPlugins()) { + // shutdown() is called once and only when process is terminating. + // Till the time it is called all threads using RT must be closed so it + // should be safe to work with plugin without multi thread protection. + // Shutdown mode allows to skip some potentially unsafe code + // (lock/unlock). + Plugin.enableShutdownMode(); // PluginParameter is reserved for future use that can control // some parameters in the plugin tear-down process. // Currently, it is not used. diff --git a/sycl/source/detail/plugin.hpp b/sycl/source/detail/plugin.hpp index 0a5851175711c..ad566fd2126cc 100644 --- a/sycl/source/detail/plugin.hpp +++ b/sycl/source/detail/plugin.hpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -92,7 +93,7 @@ class plugin { plugin(const std::shared_ptr &Plugin, backend UseBackend, void *LibraryHandle) : MPlugin(Plugin), MBackend(UseBackend), MLibraryHandle(LibraryHandle), - TracingMutex(std::make_shared()), + MTracingSLock(std::make_shared()), MPluginMutex(std::make_shared()) {} plugin &operator=(const plugin &) = default; @@ -163,7 +164,13 @@ class plugin { #endif RT::PiResult R; if (pi::trace(pi::TraceLevel::PI_TRACE_CALLS)) { - std::lock_guard Guard(*TracingMutex); + // MShutdownModeEnabled signals if process is terminating now. It covers + // normal case and undefined behavior when it happens by initiative from + // underlying modules (calling exit(1)). In case of unexpected exit(1) + // call sync primitive can be still locked. + auto Guard = MShutdownModeEnabled + ? std::unique_lock() + : std::unique_lock(*MTracingSLock); const char *FnName = PiCallInfo.getFuncName(); std::cout << "---> " << FnName << "(" << std::endl; RT::printArgs(Args...); @@ -245,11 +252,13 @@ class plugin { std::shared_ptr getPluginMutex() { return MPluginMutex; } + void enableShutdownMode() { MShutdownModeEnabled = true; } + private: std::shared_ptr MPlugin; backend MBackend; void *MLibraryHandle; // the handle returned from dlopen - std::shared_ptr TracingMutex; + std::shared_ptr MTracingSLock; // Mutex to guard PiPlatforms and LastDeviceIds. // Note that this is a temporary solution until we implement the global // Device/Platform cache later. @@ -259,6 +268,8 @@ class plugin { // represents the unique ids of the last device of each platform // index of this vector corresponds to the index in PiPlatforms vector. std::vector LastDeviceIds; + + bool MShutdownModeEnabled = false; }; // class plugin } // namespace detail } // namespace sycl