aboutsummaryrefslogtreecommitdiff
path: root/src/profiling/SendThread.hpp
blob: af1a72bce5b828d1dd11dc2ae2b2093023940d15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// Copyright © 2020 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#pragma once

#include "IBufferManager.hpp"
#include "IConsumer.hpp"
#include "ICounterDirectory.hpp"
#include "ISendCounterPacket.hpp"
#include "ISendThread.hpp"
#include "IProfilingConnection.hpp"
#include "ProfilingStateMachine.hpp"
#include "ProfilingUtils.hpp"

#include <atomic>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <type_traits>

namespace armnn
{

namespace profiling
{

class SendThread : public ISendThread, public IConsumer
{
public:
    SendThread(ProfilingStateMachine& profilingStateMachine,
        IBufferManager& buffer, ISendCounterPacket& sendCounterPacket, int timeout= 1000);
    ~SendThread()
    {
        // Don't rethrow when destructing the object
        Stop(false);
    }
    void Start(IProfilingConnection& profilingConnection) override;

    void Stop(bool rethrowSendThreadExceptions = true) override;

    void SetReadyToRead() override;

    bool IsRunning() { return m_IsRunning.load(); }

    bool WaitForPacketSent(uint32_t timeout);

private:
    void Send(IProfilingConnection& profilingConnection);

    void FlushBuffer(IProfilingConnection& profilingConnection, bool notifyWatchers = true);

    ProfilingStateMachine& m_StateMachine;
    IBufferManager& m_BufferManager;
    ISendCounterPacket& m_SendCounterPacket;
    int m_Timeout;
    std::mutex m_WaitMutex;
    std::condition_variable m_WaitCondition;
    std::thread m_SendThread;
    std::atomic<bool> m_IsRunning;
    std::atomic<bool> m_KeepRunning;
    // m_ReadyToRead will be protected by m_WaitMutex
    bool m_ReadyToRead;
    // m_PacketSent will be protected by m_PacketSentWaitMutex
    bool m_PacketSent;
    std::exception_ptr m_SendThreadException;
    std::mutex m_PacketSentWaitMutex;
    std::condition_variable m_PacketSentWaitCondition;

};

} // namespace profiling

} // namespace armnn