IVGCVSW-3431 Create Profiling Service State Machine
[platform/upstream/armnn.git] / src / profiling / ProfilingStateMachine.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "ProfilingStateMachine.hpp"
7
8 #include <armnn/Exceptions.hpp>
9
10 namespace armnn
11 {
12
13 namespace profiling
14 {
15
16 ProfilingState ProfilingStateMachine::GetCurrentState() const
17 {
18     return m_State;
19 }
20
21 void ProfilingStateMachine::TransitionToState(ProfilingState newState)
22 {
23      switch (newState)
24      {
25          case ProfilingState::Uninitialised:
26          {
27              ProfilingState expectedState = m_State.load(std::memory_order::memory_order_relaxed);
28              do {
29                  if (!IsOneOfStates(expectedState, ProfilingState::Uninitialised))
30                  {
31                      throw armnn::Exception(std::string("Cannot transition from state [")
32                                             + GetProfilingStateName(expectedState)
33                                             +"] to [" + GetProfilingStateName(newState) + "]");
34                  }
35              } while (!m_State.compare_exchange_strong(expectedState, newState,
36                       std::memory_order::memory_order_relaxed));
37
38              break;
39          }
40          case  ProfilingState::NotConnected:
41          {
42              ProfilingState expectedState = m_State.load(std::memory_order::memory_order_relaxed);
43              do {
44                  if (!IsOneOfStates(expectedState, ProfilingState::Uninitialised, ProfilingState::NotConnected,
45                                     ProfilingState::Active))
46                  {
47                      throw armnn::Exception(std::string("Cannot transition from state [")
48                                             + GetProfilingStateName(expectedState)
49                                             +"] to [" + GetProfilingStateName(newState) + "]");
50                  }
51              } while (!m_State.compare_exchange_strong(expectedState, newState,
52                       std::memory_order::memory_order_relaxed));
53
54              break;
55          }
56          case ProfilingState::WaitingForAck:
57          {
58              ProfilingState expectedState = m_State.load(std::memory_order::memory_order_relaxed);
59              do {
60                  if (!IsOneOfStates(expectedState, ProfilingState::NotConnected, ProfilingState::WaitingForAck))
61                  {
62                      throw armnn::Exception(std::string("Cannot transition from state [")
63                                             + GetProfilingStateName(expectedState)
64                                             +"] to [" + GetProfilingStateName(newState) + "]");
65                  }
66              } while (!m_State.compare_exchange_strong(expectedState, newState,
67                       std::memory_order::memory_order_relaxed));
68
69              break;
70          }
71          case ProfilingState::Active:
72          {
73              ProfilingState expectedState = m_State.load(std::memory_order::memory_order_relaxed);
74              do {
75                  if (!IsOneOfStates(expectedState, ProfilingState::WaitingForAck, ProfilingState::Active))
76                  {
77                      throw armnn::Exception(std::string("Cannot transition from state [")
78                                             + GetProfilingStateName(expectedState)
79                                             +"] to [" + GetProfilingStateName(newState) + "]");
80                  }
81              } while (!m_State.compare_exchange_strong(expectedState, newState,
82                       std::memory_order::memory_order_relaxed));
83
84              break;
85          }
86          default:
87              break;
88      }
89 }
90
91 } //namespace profiling
92
93 } //namespace armnn