blob: 1c411399bf17b6ade6da4090588f59318af955c9 [file] [log] [blame]
anjana_sreekumar@infosys.com991c2062020-01-08 11:42:57 +05301/*
2 * Copyright (c) 2019, Infosys Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <iostream>
18#include <string>
19#include "controlBlock.h"
20#include "event.h"
21#include "log.h"
22#include "tempDataBlock.h"
23#include <stateMachineEngine.h>
24
25using namespace std;
26
27namespace SM
28{
29 uint32_t ControlBlock::controlBlockArrayIdx = 0;
30
31 ControlBlock::ControlBlock():mutex_m(), cbIndex_m(++controlBlockArrayIdx),
32 cbState_m(FREE), data_p(NULL), pdb_mp(NULL),
33 tdb_mp(NULL), inProcQueue_m(false)
34 {
35 for (int i = 0; i < MAX_FAST_BLOCK_IDX; i++)
36 fadb_mpa[i] = NULL;
37
38 std::queue<Event> emptyQ;
39 std::swap( eventQ, emptyQ );
40
41 std::deque<debugEventInfo> emptyDQ;
42 std::swap(debugEventInfoQ, emptyDQ);
43 }
44
45 void ControlBlock::reset()
46 {
47 std::lock_guard<std::mutex> lock(mutex_m);
48
49 cbState_m = FREE;
50
51 data_p = NULL;
52 pdb_mp = NULL;
53
54 for (int i = 0; i < MAX_FAST_BLOCK_IDX; i++)
55 fadb_mpa[i] = NULL;
56
57 tdb_mp = NULL;
58
59 inProcQueue_m = false;
60
61 std::queue<Event> emptyQ;
62 std::swap( eventQ, emptyQ );
63
64 std::deque<debugEventInfo> emptyDQ;
65 std::swap(debugEventInfoQ, emptyDQ);
66 }
67
68 ControlBlock::~ControlBlock()
69 {
70 }
71
72 bool ControlBlock::getCurrentEvent(Event &evt)
73 {
74 std::lock_guard<std::mutex> lock(mutex_m);
75
76 if(eventQ.empty())
77 return false;
78
79 evt = eventQ.front();
80 eventQ.pop();
81
82 setMsgData(evt.getEventData());
83
84 return true;
85 }
86
87 State* ControlBlock::getCurrentState()
88 {
89 return tdb_mp->getCurrentState();
90 }
91
92 void ControlBlock::display()
93 {
94 log_msg(LOG_DEBUG,"Displaying control block");
95
96 pdb_mp->display();
97
98 tdb_mp->display();
99
100 }
101
102 void ControlBlock::addEventToProcQ(Event &event)
103 {
104 std::lock_guard<std::mutex> lock(mutex_m);
105
106 if (cbState_m == ALLOCATED)
107 {
108 eventQ.push(event);
109 if (inProcQueue_m == false)
110 {
111 inProcQueue_m = true;
112 if (!SM::StateMachineEngine::Instance()->addCBToProcQ(this))
113 {
114 inProcQueue_m = false;
115 }
116 }
117 }
118 }
119
120 void ControlBlock::setNextState(State* state)
121 {
122 if (tdb_mp != NULL)
123 tdb_mp->setNextState(state);
124 }
125
126 PermDataBlock* ControlBlock::getFastAccessBlock(unsigned short idx) const
127 {
128 PermDataBlock* permBlock_p = NULL;
129
130 if (idx < ControlBlock::MAX_FAST_BLOCK_IDX)
131 permBlock_p = fadb_mpa[idx];
132
133 return permBlock_p;
134 }
135
136 void ControlBlock::setFastAccessBlock(
137 PermDataBlock* pdb_p, unsigned short idx)
138 {
139 if (idx < ControlBlock::MAX_FAST_BLOCK_IDX)
140 fadb_mpa[idx] = pdb_p;
141 }
142
143 PermDataBlock* ControlBlock::getPermDataBlock() const
144 {
145 return pdb_mp;
146 }
147
148 void ControlBlock::setPermDataBlock(PermDataBlock* pdb_p)
149 {
150 pdb_mp = pdb_p;
151 }
152
153 TempDataBlock* ControlBlock::getTempDataBlock() const
154 {
155 return tdb_mp;
156 }
157
158 void ControlBlock::setTempDataBlock(TempDataBlock* tdb_p)
159 {
160 if (tdb_p == NULL)
161 return;
162
163 tdb_mp = tdb_p;
164 }
165
166 void ControlBlock::setCurrentTempDataBlock(TempDataBlock* tdb_p)
167 {
168 if (tdb_p == NULL)
169 return;
170
171 if (tdb_mp != NULL)
172 {
173 tdb_p->setNextTempDataBlock(tdb_mp);
174 }
175
176 tdb_mp = tdb_p;
177 }
178
179 void ControlBlock::addDebugInfo(debugEventInfo& eventInfo)
180 {
181 if(debugEventInfoQ.size()>=10)
182 {
183 debugEventInfoQ.pop_front();
184 }
185 debugEventInfoQ.push_back(eventInfo);
186 }
187
188 uint32_t ControlBlock::getCBIndex()
189 {
190 return cbIndex_m;
191 }
192
193 void ControlBlock::setControlBlockState(ControlBlockState state)
194 {
195 std::lock_guard<std::mutex> lock(mutex_m);
196
197 log_msg(LOG_INFO, "CB state transition from %d to %d\n", cbState_m, state);
198
199 cbState_m = state;
200 }
201
202 ControlBlockState ControlBlock::getControlBlockState()
203 {
204 std::lock_guard<std::mutex> lock(mutex_m);
205
206 return cbState_m;
207 }
208
209 void ControlBlock::setProcQueueFlag(bool flag)
210 {
211 std::lock_guard<std::mutex> lock(mutex_m);
212 inProcQueue_m = flag;
213 }
214
215 bool ControlBlock::isInProcQueue()
216 {
217 std::lock_guard<std::mutex> lock(mutex_m);
218 return inProcQueue_m;
219 }
220}