blob: b96a6d54f3c9b883513e8eaf35e6a75c27658367 [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>
20#include <condition_variable>
21#include <mutex>
22#include <thread>
23#include <type_traits>
24
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000025namespace arm
Sadik Armagan3896b472020-02-10 12:24:15 +000026{
27
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000028namespace pipe
Sadik Armagan3896b472020-02-10 12:24:15 +000029{
30
31class SendThread : public ISendThread, public IConsumer
32{
33public:
34 SendThread(ProfilingStateMachine& profilingStateMachine,
35 IBufferManager& buffer, ISendCounterPacket& sendCounterPacket, int timeout= 1000);
36 ~SendThread()
37 {
38 // Don't rethrow when destructing the object
39 Stop(false);
40 }
41 void Start(IProfilingConnection& profilingConnection) override;
42
43 void Stop(bool rethrowSendThreadExceptions = true) override;
44
45 void SetReadyToRead() override;
46
47 bool IsRunning() { return m_IsRunning.load(); }
48
49 bool WaitForPacketSent(uint32_t timeout);
50
51private:
52 void Send(IProfilingConnection& profilingConnection);
53
54 void FlushBuffer(IProfilingConnection& profilingConnection, bool notifyWatchers = true);
55
56 ProfilingStateMachine& m_StateMachine;
57 IBufferManager& m_BufferManager;
58 ISendCounterPacket& m_SendCounterPacket;
59 int m_Timeout;
60 std::mutex m_WaitMutex;
61 std::condition_variable m_WaitCondition;
62 std::thread m_SendThread;
63 std::atomic<bool> m_IsRunning;
64 std::atomic<bool> m_KeepRunning;
65 // m_ReadyToRead will be protected by m_WaitMutex
66 bool m_ReadyToRead;
67 // m_PacketSent will be protected by m_PacketSentWaitMutex
68 bool m_PacketSent;
69 std::exception_ptr m_SendThreadException;
70 std::mutex m_PacketSentWaitMutex;
71 std::condition_variable m_PacketSentWaitCondition;
72
73};
74
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000075} // namespace pipe
Sadik Armagan3896b472020-02-10 12:24:15 +000076
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000077} // namespace arm