diff --git a/include/easy/profiler.h b/include/easy/profiler.h index ba7defa..d71c763 100644 --- a/include/easy/profiler.h +++ b/include/easy/profiler.h @@ -556,7 +556,7 @@ namespace profiler { */ PROFILER_API const char* getContextSwitchLogFilename(); - PROFILER_API void startListen(uint16_t _port = profiler::DEFAULT_PORT); + PROFILER_API void startListen(uint16_t _port = ::profiler::DEFAULT_PORT); PROFILER_API void stopListen(); /** Returns current major version. @@ -604,7 +604,7 @@ namespace profiler { inline void setLowPriorityEventTracing(bool) { } inline void setContextSwitchLogFilename(const char*) { } inline const char* getContextSwitchLogFilename() { return ""; } - inline void startListen(uint16_t _port = profiler::DEFAULT_PORT) { } + inline void startListen(uint16_t = ::profiler::DEFAULT_PORT) { } inline void stopListen() { } inline uint8_t versionMajor() { return 0; } inline uint8_t versionMinor() { return 0; } diff --git a/src/profile_manager.cpp b/src/profile_manager.cpp index 458a044..e06d53b 100644 --- a/src/profile_manager.cpp +++ b/src/profile_manager.cpp @@ -175,12 +175,12 @@ extern "C" { PROFILER_API void startListen(uint16_t _port) { - return MANAGER.startListenSignalToCapture(_port); + return MANAGER.startListen(_port); } PROFILER_API void stopListen() { - return MANAGER.stopListenSignalToCapture(); + return MANAGER.stopListen(); } #else PROFILER_API const BaseBlockDescriptor* registerDescription(EasyBlockStatus, const char*, const char*, const char*, int, block_type_t, color_t) { return reinterpret_cast(0xbad); } @@ -195,8 +195,8 @@ extern "C" { PROFILER_API void setLowPriorityEventTracing(bool) { } PROFILER_API void setContextSwitchLogFilename(const char*) { } PROFILER_API const char* getContextSwitchLogFilename() { return ""; } - PROFILER_API void startListenSignalToCapture() { } - PROFILER_API void stopListenSignalToCapture() { } + PROFILER_API void startListen(uint16_t) { } + PROFILER_API void stopListen() { } #endif PROFILER_API uint8_t versionMajor() @@ -396,13 +396,14 @@ ProfileManager::ProfileManager() { m_isEnabled = ATOMIC_VAR_INIT(false); m_isEventTracingEnabled = ATOMIC_VAR_INIT(EASY_EVENT_TRACING_ENABLED); + m_isAlreadyListening = ATOMIC_VAR_INIT(false); m_stopListen = ATOMIC_VAR_INIT(false); } ProfileManager::~ProfileManager() { #ifndef EASY_PROFILER_API_DISABLED - stopListenSignalToCapture(); + stopListen(); #endif for (auto desc : m_descriptors) @@ -909,22 +910,21 @@ void ProfileManager::setBlockStatus(block_id_t _id, EasyBlockStatus _status) } } -void ProfileManager::startListenSignalToCapture(uint16_t _port) +void ProfileManager::startListen(uint16_t _port) { - if (!m_isAlreadyListened) + if (!m_isAlreadyListening.exchange(true, std::memory_order_release)) { m_stopListen.store(false, std::memory_order_release); m_listenThread = std::move(std::thread(&ProfileManager::listen, this, _port)); - m_isAlreadyListened = true; } } -void ProfileManager::stopListenSignalToCapture() +void ProfileManager::stopListen() { m_stopListen.store(true, std::memory_order_release); if (m_listenThread.joinable()) m_listenThread.join(); - m_isAlreadyListened = false; + m_isAlreadyListening.store(false, std::memory_order_release); } ////////////////////////////////////////////////////////////////////////// diff --git a/src/profile_manager.h b/src/profile_manager.h index 50285ae..87cc5ad 100644 --- a/src/profile_manager.h +++ b/src/profile_manager.h @@ -360,6 +360,7 @@ class ProfileManager profiler::spin_lock m_storedSpin; std::atomic_bool m_isEnabled; std::atomic_bool m_isEventTracingEnabled; + std::atomic_bool m_isAlreadyListening; std::string m_csInfoFilename = "/tmp/cs_profiling_info.log"; @@ -367,7 +368,6 @@ class ProfileManager void setBlockStatus(profiler::block_id_t _id, profiler::EasyBlockStatus _status); std::thread m_listenThread; - bool m_isAlreadyListened = false; void listen(uint16_t _port); int m_socket = 0;//TODO crossplatform @@ -409,8 +409,8 @@ public: void beginContextSwitch(profiler::thread_id_t _thread_id, profiler::timestamp_t _time, profiler::thread_id_t _target_thread_id, const char* _target_process, bool _lockSpin = true); void storeContextSwitch(profiler::thread_id_t _thread_id, profiler::timestamp_t _time, profiler::thread_id_t _target_thread_id, bool _lockSpin = true); void endContextSwitch(profiler::thread_id_t _thread_id, profiler::timestamp_t _endtime, bool _lockSpin = true); - void startListenSignalToCapture(uint16_t _port); - void stopListenSignalToCapture(); + void startListen(uint16_t _port); + void stopListen(); private: