1 // 2 // Copyright © 2020 Arm Ltd. All rights reserved. 3 // SPDX-License-Identifier: MIT 4 // 5 6 #pragma once 7 8 #include "IBufferManager.hpp" 9 #include "IConsumer.hpp" 10 #include "ICounterDirectory.hpp" 11 #include "ISendCounterPacket.hpp" 12 #include "ISendThread.hpp" 13 #include "IProfilingConnection.hpp" 14 #include "ProfilingStateMachine.hpp" 15 #include "ProfilingUtils.hpp" 16 17 #include <atomic> 18 #include <condition_variable> 19 #include <mutex> 20 #include <thread> 21 #include <type_traits> 22 23 namespace armnn 24 { 25 26 namespace profiling 27 { 28 29 class SendThread : public ISendThread, public IConsumer 30 { 31 public: 32 SendThread(ProfilingStateMachine& profilingStateMachine, 33 IBufferManager& buffer, ISendCounterPacket& sendCounterPacket, int timeout= 1000); ~SendThread()34 ~SendThread() 35 { 36 // Don't rethrow when destructing the object 37 Stop(false); 38 } 39 void Start(IProfilingConnection& profilingConnection) override; 40 41 void Stop(bool rethrowSendThreadExceptions = true) override; 42 43 void SetReadyToRead() override; 44 IsRunning()45 bool IsRunning() { return m_IsRunning.load(); } 46 47 bool WaitForPacketSent(uint32_t timeout); 48 49 private: 50 void Send(IProfilingConnection& profilingConnection); 51 52 void FlushBuffer(IProfilingConnection& profilingConnection, bool notifyWatchers = true); 53 54 ProfilingStateMachine& m_StateMachine; 55 IBufferManager& m_BufferManager; 56 ISendCounterPacket& m_SendCounterPacket; 57 int m_Timeout; 58 std::mutex m_WaitMutex; 59 std::condition_variable m_WaitCondition; 60 std::thread m_SendThread; 61 std::atomic<bool> m_IsRunning; 62 std::atomic<bool> m_KeepRunning; 63 // m_ReadyToRead will be protected by m_WaitMutex 64 bool m_ReadyToRead; 65 // m_PacketSent will be protected by m_PacketSentWaitMutex 66 bool m_PacketSent; 67 std::exception_ptr m_SendThreadException; 68 std::mutex m_PacketSentWaitMutex; 69 std::condition_variable m_PacketSentWaitCondition; 70 71 }; 72 73 } // namespace profiling 74 75 } // namespace armnn 76