Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 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" |
Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 10 | #include "ISendThread.hpp" |
| 11 | #include "IProfilingConnection.hpp" |
| 12 | #include "ProfilingStateMachine.hpp" |
| 13 | #include "ProfilingUtils.hpp" |
| 14 | |
Jim Flynn | 2776183 | 2022-03-20 21:52:17 +0000 | [diff] [blame] | 15 | #include <client/include/ISendCounterPacket.hpp> |
| 16 | |
Jim Flynn | c454ac9 | 2022-03-16 18:43:18 +0000 | [diff] [blame] | 17 | #include <common/include/ICounterDirectory.hpp> |
| 18 | |
Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 19 | #include <atomic> |
Jim Flynn | e195a04 | 2022-04-12 17:19:28 +0100 | [diff] [blame] | 20 | #if !defined(ARMNN_DISABLE_THREADS) |
Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 21 | #include <condition_variable> |
| 22 | #include <mutex> |
| 23 | #include <thread> |
Jim Flynn | e195a04 | 2022-04-12 17:19:28 +0100 | [diff] [blame] | 24 | #endif |
Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 25 | #include <type_traits> |
| 26 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 27 | namespace arm |
Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 28 | { |
| 29 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 30 | namespace pipe |
Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 31 | { |
| 32 | |
| 33 | class SendThread : public ISendThread, public IConsumer |
| 34 | { |
| 35 | public: |
| 36 | SendThread(ProfilingStateMachine& profilingStateMachine, |
| 37 | IBufferManager& buffer, ISendCounterPacket& sendCounterPacket, int timeout= 1000); |
| 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 | |
| 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; |
Jim Flynn | e195a04 | 2022-04-12 17:19:28 +0100 | [diff] [blame] | 62 | #if !defined(ARMNN_DISABLE_THREADS) |
Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 63 | std::mutex m_WaitMutex; |
| 64 | std::condition_variable m_WaitCondition; |
| 65 | std::thread m_SendThread; |
Jim Flynn | e195a04 | 2022-04-12 17:19:28 +0100 | [diff] [blame] | 66 | #endif |
Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 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; |
Jim Flynn | e195a04 | 2022-04-12 17:19:28 +0100 | [diff] [blame] | 74 | #if !defined(ARMNN_DISABLE_THREADS) |
Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 75 | std::mutex m_PacketSentWaitMutex; |
| 76 | std::condition_variable m_PacketSentWaitCondition; |
Jim Flynn | e195a04 | 2022-04-12 17:19:28 +0100 | [diff] [blame] | 77 | #endif |
Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 80 | } // namespace pipe |
Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 81 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 82 | } // namespace arm |