diff --git a/libs/prof/include/psemek/prof/profiler.hpp b/libs/prof/include/psemek/prof/profiler.hpp index 0d5293e2..bd1796c8 100644 --- a/libs/prof/include/psemek/prof/profiler.hpp +++ b/libs/prof/include/psemek/prof/profiler.hpp @@ -3,7 +3,7 @@ #include #include -#include +#include namespace psemek::prof { @@ -13,8 +13,13 @@ namespace psemek::prof struct profiler { profiler(std::string const & name); + profiler(profiler && other); + profiler(profiler const & other) = delete; - ~ profiler(); + profiler & operator = (profiler && other); + profiler & operator = (profiler const & other) = delete; + + ~profiler(); static void start_frame(std::string const & name); static void push(std::string const & name, std::chrono::duration duration, std::size_t count = 1); @@ -33,6 +38,9 @@ namespace psemek::prof private: util::clock, std::chrono::high_resolution_clock> clock_; + bool moved_out_ = false; + + void report(); }; } diff --git a/libs/prof/source/profiler.cpp b/libs/prof/source/profiler.cpp index e99a6e65..a7f03b03 100644 --- a/libs/prof/source/profiler.cpp +++ b/libs/prof/source/profiler.cpp @@ -183,12 +183,25 @@ namespace psemek::prof clock_.restart(); } + profiler::profiler(profiler && other) + : clock_(other.clock_) + , moved_out_(other.moved_out_) + { + other.moved_out_ = true; + } + + profiler & profiler::operator = (profiler && other) + { + report(); + clock_ = other.clock_; + moved_out_ = other.moved_out_; + other.moved_out_ = true; + return *this; + } + profiler::~profiler() { - auto count = clock_.count(); - std::lock_guard lock{*(current->mutex)}; - current->execution_time.push(count); - current = current->parent; + report(); } void profiler::start_frame(std::string const & name) @@ -214,4 +227,15 @@ namespace psemek::prof current = current->parent; } + void profiler::report() + { + if (moved_out_) + return; + + auto count = clock_.count(); + std::lock_guard lock{*(current->mutex)}; + current->execution_time.push(count); + current = current->parent; + } + }