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 "ISendThread.hpp" 11 #include "IProfilingConnection.hpp" 12 #include "ProfilingStateMachine.hpp" 13 #include "ProfilingUtils.hpp" 14 15 #include <client/include/ISendCounterPacket.hpp> 16 17 #include <common/include/ICounterDirectory.hpp> 18 19 #include <atomic> 20 #if !defined(ARMNN_DISABLE_THREADS) 21 #include <condition_variable> 22 #include <mutex> 23 #include <thread> 24 #endif 25 #include <type_traits> 26 27 namespace arm 28 { 29 30 namespace pipe 31 { 32 33 class SendThread : public ISendThread, public IConsumer 34 { 35 public: 36 SendThread(ProfilingStateMachine& profilingStateMachine, 37 IBufferManager& buffer, ISendCounterPacket& sendCounterPacket, int timeout= 1000); ~SendThread()38 ~SendThread() 39 { 40 // Don't rethrow when destructing the object 41 Stop(false); 42 } 43 void Start(IProfilingConnection& profilingConnection) override; 44 45 void Stop(bool rethrowSendThreadExceptions = true) override; 46 47 void SetReadyToRead() override; 48 IsRunning()49 bool IsRunning() { return m_IsRunning.load(); } 50 51 bool WaitForPacketSent(uint32_t timeout); 52 53 private: 54 void Send(IProfilingConnection& profilingConnection); 55 56 void FlushBuffer(IProfilingConnection& profilingConnection, bool notifyWatchers = true); 57 58 ProfilingStateMachine& m_StateMachine; 59 IBufferManager& m_BufferManager; 60 ISendCounterPacket& m_SendCounterPacket; 61 int m_Timeout; 62 #if !defined(ARMNN_DISABLE_THREADS) 63 std::mutex m_WaitMutex; 64 std::condition_variable m_WaitCondition; 65 std::thread m_SendThread; 66 #endif 67 std::atomic<bool> m_IsRunning; 68 std::atomic<bool> m_KeepRunning; 69 // m_ReadyToRead will be protected by m_WaitMutex 70 bool m_ReadyToRead; 71 // m_PacketSent will be protected by m_PacketSentWaitMutex 72 bool m_PacketSent; 73 std::exception_ptr m_SendThreadException; 74 #if !defined(ARMNN_DISABLE_THREADS) 75 std::mutex m_PacketSentWaitMutex; 76 std::condition_variable m_PacketSentWaitCondition; 77 #endif 78 }; 79 80 } // namespace pipe 81 82 } // namespace arm 83