blob: f462ae07d822ac73f6e3dbe5821c76d1c042893b [file] [log] [blame]
Sadik Armagan3896b472020-02-10 12:24:15 +00001//
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 Armagan3896b472020-02-10 12:24:15 +000010#include "ISendThread.hpp"
11#include "IProfilingConnection.hpp"
12#include "ProfilingStateMachine.hpp"
13#include "ProfilingUtils.hpp"
14
Jim Flynn27761832022-03-20 21:52:17 +000015#include <client/include/ISendCounterPacket.hpp>
16
Jim Flynnc454ac92022-03-16 18:43:18 +000017#include <common/include/ICounterDirectory.hpp>
18
Sadik Armagan3896b472020-02-10 12:24:15 +000019#include <atomic>
Jim Flynne195a042022-04-12 17:19:28 +010020#if !defined(ARMNN_DISABLE_THREADS)
Sadik Armagan3896b472020-02-10 12:24:15 +000021#include <condition_variable>
22#include <mutex>
23#include <thread>
Jim Flynne195a042022-04-12 17:19:28 +010024#endif
Sadik Armagan3896b472020-02-10 12:24:15 +000025#include <type_traits>
26
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000027namespace arm
Sadik Armagan3896b472020-02-10 12:24:15 +000028{
29
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000030namespace pipe
Sadik Armagan3896b472020-02-10 12:24:15 +000031{
32
33class SendThread : public ISendThread, public IConsumer
34{
35public:
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
53private:
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 Flynne195a042022-04-12 17:19:28 +010062#if !defined(ARMNN_DISABLE_THREADS)
Sadik Armagan3896b472020-02-10 12:24:15 +000063 std::mutex m_WaitMutex;
64 std::condition_variable m_WaitCondition;
65 std::thread m_SendThread;
Jim Flynne195a042022-04-12 17:19:28 +010066#endif
Sadik Armagan3896b472020-02-10 12:24:15 +000067 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 Flynne195a042022-04-12 17:19:28 +010074#if !defined(ARMNN_DISABLE_THREADS)
Sadik Armagan3896b472020-02-10 12:24:15 +000075 std::mutex m_PacketSentWaitMutex;
76 std::condition_variable m_PacketSentWaitCondition;
Jim Flynne195a042022-04-12 17:19:28 +010077#endif
Sadik Armagan3896b472020-02-10 12:24:15 +000078};
79
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000080} // namespace pipe
Sadik Armagan3896b472020-02-10 12:24:15 +000081
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000082} // namespace arm