aboutsummaryrefslogtreecommitdiff
path: root/src/profiling/CommandThread.cpp
blob: bd4aa96c7ca0dbb53fbef54e07eb0f23403e6775 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// Copyright © 2019 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include <atomic>
#include "CommandThread.hpp"

namespace armnn
{

namespace profiling
{

CommandThread::CommandThread(uint32_t timeout,
                             bool stopAfterTimeout,
                             CommandHandlerRegistry& commandHandlerRegistry,
                             PacketVersionResolver& packetVersionResolver,
                             IProfilingConnection& socketProfilingConnection)
    : m_Timeout(timeout)
    , m_StopAfterTimeout(stopAfterTimeout)
    , m_IsRunning(false)
    , m_CommandHandlerRegistry(commandHandlerRegistry)
    , m_PacketVersionResolver(packetVersionResolver)
    , m_SocketProfilingConnection(socketProfilingConnection)
{};

void CommandThread::WaitForPacket()
{
    do {
        try
        {
            Packet packet = m_SocketProfilingConnection.ReadPacket(m_Timeout);
            Version version = m_PacketVersionResolver.ResolvePacketVersion(packet.GetPacketId());

            CommandHandlerFunctor* commandHandlerFunctor =
                m_CommandHandlerRegistry.GetFunctor(packet.GetPacketId(), version.GetEncodedValue());
            commandHandlerFunctor->operator()(packet);
        }
        catch(const armnn::TimeoutException&)
        {
            if(m_StopAfterTimeout)
            {
                m_IsRunning.store(false, std::memory_order_relaxed);
                return;
            }
        }
        catch(...)
        {
            //might want to differentiate the errors more
            m_IsRunning.store(false, std::memory_order_relaxed);
            return;
        }

    } while(m_KeepRunning.load(std::memory_order_relaxed));

    m_IsRunning.store(false, std::memory_order_relaxed);
}

void CommandThread::Start()
{
    if (!m_CommandThread.joinable() && !IsRunning())
    {
        m_IsRunning.store(true, std::memory_order_relaxed);
        m_KeepRunning.store(true, std::memory_order_relaxed);
        m_CommandThread = std::thread(&CommandThread::WaitForPacket, this);
    }
}

void CommandThread::Stop()
{
    m_KeepRunning.store(false, std::memory_order_relaxed);
}

void CommandThread::Join()
{
    m_CommandThread.join();
}

bool CommandThread::IsRunning() const
{
    return m_IsRunning.load(std::memory_order_relaxed);
}

bool CommandThread::StopAfterTimeout(bool stopAfterTimeout)
{
    if (!IsRunning())
    {
        m_StopAfterTimeout = stopAfterTimeout;
        return true;
    }
    return false;
}

}//namespace profiling

}//namespace armnn