aboutsummaryrefslogtreecommitdiff
path: root/src/profiling/ProfilingStateMachine.cpp
blob: 682e1b8894d29eb109865360bfe4583ee038ce44 (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
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "ProfilingStateMachine.hpp"

#include <armnn/Exceptions.hpp>

namespace armnn
{

namespace profiling
{

ProfilingState ProfilingStateMachine::GetCurrentState() const
{
    return m_State;
}

void ProfilingStateMachine::TransitionToState(ProfilingState newState)
{
     switch (newState)
     {
         case ProfilingState::Uninitialised:
         {
             ProfilingState expectedState = m_State.load(std::memory_order::memory_order_relaxed);
             do {
                 if (!IsOneOfStates(expectedState, ProfilingState::Uninitialised))
                 {
                     throw armnn::Exception(std::string("Cannot transition from state [")
                                            + GetProfilingStateName(expectedState)
                                            +"] to [" + GetProfilingStateName(newState) + "]");
                 }
             } while (!m_State.compare_exchange_strong(expectedState, newState,
                      std::memory_order::memory_order_relaxed));

             break;
         }
         case  ProfilingState::NotConnected:
         {
             ProfilingState expectedState = m_State.load(std::memory_order::memory_order_relaxed);
             do {
                 if (!IsOneOfStates(expectedState, ProfilingState::Uninitialised, ProfilingState::NotConnected,
                                    ProfilingState::Active))
                 {
                     throw armnn::Exception(std::string("Cannot transition from state [")
                                            + GetProfilingStateName(expectedState)
                                            +"] to [" + GetProfilingStateName(newState) + "]");
                 }
             } while (!m_State.compare_exchange_strong(expectedState, newState,
                      std::memory_order::memory_order_relaxed));

             break;
         }
         case ProfilingState::WaitingForAck:
         {
             ProfilingState expectedState = m_State.load(std::memory_order::memory_order_relaxed);
             do {
                 if (!IsOneOfStates(expectedState, ProfilingState::NotConnected, ProfilingState::WaitingForAck))
                 {
                     throw armnn::Exception(std::string("Cannot transition from state [")
                                            + GetProfilingStateName(expectedState)
                                            +"] to [" + GetProfilingStateName(newState) + "]");
                 }
             } while (!m_State.compare_exchange_strong(expectedState, newState,
                      std::memory_order::memory_order_relaxed));

             break;
         }
         case ProfilingState::Active:
         {
             ProfilingState expectedState = m_State.load(std::memory_order::memory_order_relaxed);
             do {
                 if (!IsOneOfStates(expectedState, ProfilingState::WaitingForAck, ProfilingState::Active))
                 {
                     throw armnn::Exception(std::string("Cannot transition from state [")
                                            + GetProfilingStateName(expectedState)
                                            +"] to [" + GetProfilingStateName(newState) + "]");
                 }
             } while (!m_State.compare_exchange_strong(expectedState, newState,
                      std::memory_order::memory_order_relaxed));

             break;
         }
         default:
             break;
     }
}

} //namespace profiling

} //namespace armnn