blob: 6512c8aac64ffb485018da338c3dad27a2b90ecb [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"
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
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000023namespace arm
Sadik Armagan3896b472020-02-10 12:24:15 +000024{
25
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000026namespace pipe
Sadik Armagan3896b472020-02-10 12:24:15 +000027{
28
29class SendThread : public ISendThread, public IConsumer
30{
31public:
32 SendThread(ProfilingStateMachine& profilingStateMachine,
33 IBufferManager& buffer, ISendCounterPacket& sendCounterPacket, int timeout= 1000);
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
45 bool IsRunning() { return m_IsRunning.load(); }
46
47 bool WaitForPacketSent(uint32_t timeout);
48
49private:
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
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000073} // namespace pipe
Sadik Armagan3896b472020-02-10 12:24:15 +000074
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000075} // namespace arm