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

#include "ProfilingStateMachine.hpp"

#include <common/include/ProfilingException.hpp>

#include <sstream>

namespace arm
{

namespace pipe
{

namespace
{

void ThrowStateTransitionException(ProfilingState expectedState, ProfilingState newState)
{
    std::stringstream ss;
    ss << "Cannot transition from state [" << GetProfilingStateName(expectedState) << "] "
       << "to state [" << GetProfilingStateName(newState) << "]";
    throw arm::pipe::ProfilingException(ss.str());
}

} // Anonymous namespace

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

void ProfilingStateMachine::TransitionToState(ProfilingState newState)
{
    ProfilingState currentState = m_State.load(std::memory_order::memory_order_relaxed);

    switch (newState)
    {
    case ProfilingState::Uninitialised:
        do
        {
            if (!IsOneOfStates(currentState, ProfilingState::Uninitialised))
            {
                ThrowStateTransitionException(currentState, newState);
            }
        }
        while (!m_State.compare_exchange_strong(currentState, newState, std::memory_order::memory_order_relaxed));
        break;
    case  ProfilingState::NotConnected:
        do
        {
            if (!IsOneOfStates(currentState, ProfilingState::Uninitialised, ProfilingState::NotConnected,
                               ProfilingState::Active, ProfilingState::WaitingForAck))
            {
                ThrowStateTransitionException(currentState, newState);
            }
        }
        while (!m_State.compare_exchange_strong(currentState, newState, std::memory_order::memory_order_relaxed));
        break;
    case ProfilingState::WaitingForAck:
        do
        {
            if (!IsOneOfStates(currentState, ProfilingState::NotConnected, ProfilingState::WaitingForAck))
            {
                ThrowStateTransitionException(currentState, newState);
            }
        }
        while (!m_State.compare_exchange_strong(currentState, newState, std::memory_order::memory_order_relaxed));
        break;
    case ProfilingState::Active:
        do
        {
            if (!IsOneOfStates(currentState, ProfilingState::WaitingForAck, ProfilingState::Active))
            {
                ThrowStateTransitionException(currentState, newState);
            }
        }
        while (!m_State.compare_exchange_strong(currentState, newState, std::memory_order::memory_order_relaxed));
        break;
    default:
        break;
    }
}

void ProfilingStateMachine::Reset()
{
    m_State.store(ProfilingState::Uninitialised);
}

} // namespace pipe

} // namespace arm