Attempt to fix

This commit is contained in:
Ivan Lezhankin 2020-02-03 20:06:59 +03:00
parent 88095b636e
commit 634d3bedd3
2 changed files with 9 additions and 24 deletions

View File

@ -156,17 +156,13 @@ void ThreadStatus::finalizePerformanceCounters()
void ThreadStatus::initQueryProfiler()
{
/// query profilers are useless without trace collector
if (!global_context)
if (!global_context || !ext::Singleton<TraceCollector>::isInitialized())
return;
const auto & settings = query_context->getSettingsRef();
try
{
/// Force initialization of TraceCollector instance before setting signal handlers,
/// because the constructor is not async-signal-safe.
ext::Singleton<TraceCollector> initialize __attribute__((unused));
if (settings.query_profiler_real_time_period_ns > 0)
query_profiler_real = std::make_unique<QueryProfilerReal>(thread_id,
/* period */ static_cast<UInt32>(settings.query_profiler_real_time_period_ns));

View File

@ -1,33 +1,18 @@
#pragma once
#include <memory>
#include <type_traits>
namespace ext {
template <class T, typename DefaultConstructable = void>
class Singleton;
/// For default-constructable type we don't need to implement |create()|
/// and may use "arrow" operator immediately.
template <class T>
class Singleton<T, std::enable_if_t<std::is_default_constructible_v<T>>>
class Singleton
{
public:
T * operator->()
Singleton()
{
static T instance;
return &instance;
if (!instance)
instance.reset(new T);
}
};
/// For custom-constructed type we have to construct |Singleton| object with non-default constructor once
/// before any use of "arrow" operator.
template <class T>
class Singleton<T, std::enable_if_t<!std::is_default_constructible_v<T>>>
{
public:
Singleton() = default;
template <typename ... Args>
Singleton(const Args & ... args)
@ -41,6 +26,10 @@ public:
return instance.get();
}
static bool isInitialized() {
return !!instance;
}
private:
inline static std::unique_ptr<T> instance{};
};