process.cc (12957:e54f9890363d) process.cc (12959:33d9a39e40a3)
1/*
2 * Copyright 2018 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: Gabe Black
28 */
29
30#include "systemc/core/process.hh"
31
32#include "base/logging.hh"
33#include "systemc/core/event.hh"
34#include "systemc/core/scheduler.hh"
35
36namespace sc_gem5
37{
38
1/*
2 * Copyright 2018 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: Gabe Black
28 */
29
30#include "systemc/core/process.hh"
31
32#include "base/logging.hh"
33#include "systemc/core/event.hh"
34#include "systemc/core/scheduler.hh"
35
36namespace sc_gem5
37{
38
39void
40Sensitivity::satisfy()
41{
42 warn_once("Ignoring suspended status for now.\n");
43 process->setDynamic(nullptr);
44 scheduler.ready(process);
45}
46
47SensitivityTimeout::SensitivityTimeout(Process *p, ::sc_core::sc_time t) :
48 Sensitivity(p), timeoutEvent(this), timeout(t)
49{
50 Tick when = scheduler.eventQueue().getCurTick() + timeout.value();
51 scheduler.eventQueue().schedule(&timeoutEvent, when);
52}
53
54SensitivityTimeout::~SensitivityTimeout()
55{
56 if (timeoutEvent.scheduled())
57 scheduler.eventQueue().deschedule(&timeoutEvent);
58}
59
60SensitivityEvent::SensitivityEvent(
61 Process *p, const ::sc_core::sc_event *e) : Sensitivity(p), event(e)
62{
63 Event::getFromScEvent(event)->addSensitivity(this);
64}
65
66SensitivityEvent::~SensitivityEvent()
67{
68 Event::getFromScEvent(event)->delSensitivity(this);
69}
70
71SensitivityEventAndList::SensitivityEventAndList(
72 Process *p, const ::sc_core::sc_event_and_list *list) :
73 Sensitivity(p), list(list), count(0)
74{
75 for (auto e: list->events)
76 Event::getFromScEvent(e)->addSensitivity(this);
77}
78
79SensitivityEventAndList::~SensitivityEventAndList()
80{
81 for (auto e: list->events)
82 Event::getFromScEvent(e)->delSensitivity(this);
83}
84
85void
86SensitivityEventAndList::notifyWork(Event *e)
87{
88 e->delSensitivity(this);
89 count++;
90 if (count == list->events.size())
39SensitivityTimeout::SensitivityTimeout(Process *p, ::sc_core::sc_time t) :
40 Sensitivity(p), timeoutEvent(this), timeout(t)
41{
42 Tick when = scheduler.eventQueue().getCurTick() + timeout.value();
43 scheduler.eventQueue().schedule(&timeoutEvent, when);
44}
45
46SensitivityTimeout::~SensitivityTimeout()
47{
48 if (timeoutEvent.scheduled())
49 scheduler.eventQueue().deschedule(&timeoutEvent);
50}
51
52SensitivityEvent::SensitivityEvent(
53 Process *p, const ::sc_core::sc_event *e) : Sensitivity(p), event(e)
54{
55 Event::getFromScEvent(event)->addSensitivity(this);
56}
57
58SensitivityEvent::~SensitivityEvent()
59{
60 Event::getFromScEvent(event)->delSensitivity(this);
61}
62
63SensitivityEventAndList::SensitivityEventAndList(
64 Process *p, const ::sc_core::sc_event_and_list *list) :
65 Sensitivity(p), list(list), count(0)
66{
67 for (auto e: list->events)
68 Event::getFromScEvent(e)->addSensitivity(this);
69}
70
71SensitivityEventAndList::~SensitivityEventAndList()
72{
73 for (auto e: list->events)
74 Event::getFromScEvent(e)->delSensitivity(this);
75}
76
77void
78SensitivityEventAndList::notifyWork(Event *e)
79{
80 e->delSensitivity(this);
81 count++;
82 if (count == list->events.size())
91 satisfy();
83 process->satisfySensitivity(this);
92}
93
94SensitivityEventOrList::SensitivityEventOrList(
95 Process *p, const ::sc_core::sc_event_or_list *list) :
96 Sensitivity(p), list(list)
97{
98 for (auto e: list->events)
99 Event::getFromScEvent(e)->addSensitivity(this);
100}
101
102SensitivityEventOrList::~SensitivityEventOrList()
103{
104 for (auto e: list->events)
105 Event::getFromScEvent(e)->delSensitivity(this);
106}
107
108
109class UnwindExceptionReset : public ::sc_core::sc_unwind_exception
110{
111 public:
112 const char *what() const throw() override { return "RESET"; }
113 bool is_reset() const override { return true; }
114};
115
116class UnwindExceptionKill : public ::sc_core::sc_unwind_exception
117{
118 public:
119 const char *what() const throw() override { return "KILL"; }
120 bool is_reset() const override { return false; }
121};
122
123template <typename T>
124struct BuiltinExceptionWrapper : public ExceptionWrapperBase
125{
126 public:
127 T t;
128 void throw_it() override { throw t; }
129};
130
131BuiltinExceptionWrapper<UnwindExceptionReset> resetException;
132BuiltinExceptionWrapper<UnwindExceptionKill> killException;
133
134
135void
136Process::forEachKid(const std::function<void(Process *)> &work)
137{
138 for (auto &kid: get_child_objects()) {
139 Process *p_kid = dynamic_cast<Process *>(kid);
140 if (p_kid)
141 work(p_kid);
142 }
143}
144
145void
146Process::suspend(bool inc_kids)
147{
148 if (inc_kids)
149 forEachKid([](Process *p) { p->suspend(true); });
150
151 if (!_suspended) {
152 _suspended = true;
84}
85
86SensitivityEventOrList::SensitivityEventOrList(
87 Process *p, const ::sc_core::sc_event_or_list *list) :
88 Sensitivity(p), list(list)
89{
90 for (auto e: list->events)
91 Event::getFromScEvent(e)->addSensitivity(this);
92}
93
94SensitivityEventOrList::~SensitivityEventOrList()
95{
96 for (auto e: list->events)
97 Event::getFromScEvent(e)->delSensitivity(this);
98}
99
100
101class UnwindExceptionReset : public ::sc_core::sc_unwind_exception
102{
103 public:
104 const char *what() const throw() override { return "RESET"; }
105 bool is_reset() const override { return true; }
106};
107
108class UnwindExceptionKill : public ::sc_core::sc_unwind_exception
109{
110 public:
111 const char *what() const throw() override { return "KILL"; }
112 bool is_reset() const override { return false; }
113};
114
115template <typename T>
116struct BuiltinExceptionWrapper : public ExceptionWrapperBase
117{
118 public:
119 T t;
120 void throw_it() override { throw t; }
121};
122
123BuiltinExceptionWrapper<UnwindExceptionReset> resetException;
124BuiltinExceptionWrapper<UnwindExceptionKill> killException;
125
126
127void
128Process::forEachKid(const std::function<void(Process *)> &work)
129{
130 for (auto &kid: get_child_objects()) {
131 Process *p_kid = dynamic_cast<Process *>(kid);
132 if (p_kid)
133 work(p_kid);
134 }
135}
136
137void
138Process::suspend(bool inc_kids)
139{
140 if (inc_kids)
141 forEachKid([](Process *p) { p->suspend(true); });
142
143 if (!_suspended) {
144 _suspended = true;
153 //TODO Suspend this process.
145 _suspendedReady = false;
154 }
155
156 if (procKind() != ::sc_core::SC_METHOD_PROC_ &&
157 scheduler.current() == this) {
158 scheduler.yield();
159 }
160}
161
162void
163Process::resume(bool inc_kids)
164{
165 if (inc_kids)
166 forEachKid([](Process *p) { p->resume(true); });
167
168 if (_suspended) {
169 _suspended = false;
146 }
147
148 if (procKind() != ::sc_core::SC_METHOD_PROC_ &&
149 scheduler.current() == this) {
150 scheduler.yield();
151 }
152}
153
154void
155Process::resume(bool inc_kids)
156{
157 if (inc_kids)
158 forEachKid([](Process *p) { p->resume(true); });
159
160 if (_suspended) {
161 _suspended = false;
170 //TODO Resume this process.
162 if (_suspendedReady)
163 ready();
164 _suspendedReady = false;
171 }
172}
173
174void
175Process::disable(bool inc_kids)
176{
177 if (inc_kids)
178 forEachKid([](Process *p) { p->disable(true); });
179
180 _disabled = true;
181}
182
183void
184Process::enable(bool inc_kids)
185{
186
187 if (inc_kids)
188 forEachKid([](Process *p) { p->enable(true); });
189
190 _disabled = false;
191}
192
193void
194Process::kill(bool inc_kids)
195{
196 // Update our state.
197 _terminated = true;
198 _isUnwinding = true;
199
200 // Propogate the kill to our children no matter what happens to us.
201 if (inc_kids)
202 forEachKid([](Process *p) { p->kill(true); });
203
204 // If we're in the middle of unwinding, ignore the kill request.
205 if (_isUnwinding)
206 return;
207
208 // Inject the kill exception into this process.
209 injectException(killException);
210
211 _terminatedEvent.notify();
212}
213
214void
215Process::reset(bool inc_kids)
216{
217 // Update our state.
218 _isUnwinding = true;
219
220 // Propogate the reset to our children no matter what happens to us.
221 if (inc_kids)
222 forEachKid([](Process *p) { p->reset(true); });
223
224 // If we're in the middle of unwinding, ignore the reset request.
225 if (_isUnwinding)
226 return;
227
228 // Inject the reset exception into this process.
229 injectException(resetException);
230
231 _resetEvent.notify();
232}
233
234void
235Process::throw_it(ExceptionWrapperBase &exc, bool inc_kids)
236{
237 if (inc_kids)
238 forEachKid([&exc](Process *p) { p->throw_it(exc, true); });
239}
240
241void
242Process::injectException(ExceptionWrapperBase &exc)
243{
244 excWrapper = &exc;
245 // Let this process preempt us.
246};
247
248void
249Process::syncResetOn(bool inc_kids)
250{
251 if (inc_kids)
252 forEachKid([](Process *p) { p->syncResetOn(true); });
253
254 _syncReset = true;
255}
256
257void
258Process::syncResetOff(bool inc_kids)
259{
260 if (inc_kids)
261 forEachKid([](Process *p) { p->syncResetOff(true); });
262
263 _syncReset = false;
264}
265
266void
267Process::dontInitialize()
268{
269 scheduler.dontInitialize(this);
270}
271
272void
273Process::finalize()
274{
275 for (auto &s: pendingStaticSensitivities) {
276 s->finalize(staticSensitivities);
277 delete s;
278 s = nullptr;
279 }
280 pendingStaticSensitivities.clear();
281};
282
283void
284Process::run()
285{
286 _running = true;
287 bool reset;
288 do {
289 reset = false;
290 try {
291 func->call();
292 } catch(::sc_core::sc_unwind_exception exc) {
293 reset = exc.is_reset();
294 }
295 } while (reset);
296 _running = false;
297}
298
299void
300Process::addStatic(PendingSensitivity *s)
301{
302 pendingStaticSensitivities.push_back(s);
303}
304
305void
306Process::setDynamic(Sensitivity *s)
307{
308 delete dynamicSensitivity;
309 dynamicSensitivity = s;
310}
311
165 }
166}
167
168void
169Process::disable(bool inc_kids)
170{
171 if (inc_kids)
172 forEachKid([](Process *p) { p->disable(true); });
173
174 _disabled = true;
175}
176
177void
178Process::enable(bool inc_kids)
179{
180
181 if (inc_kids)
182 forEachKid([](Process *p) { p->enable(true); });
183
184 _disabled = false;
185}
186
187void
188Process::kill(bool inc_kids)
189{
190 // Update our state.
191 _terminated = true;
192 _isUnwinding = true;
193
194 // Propogate the kill to our children no matter what happens to us.
195 if (inc_kids)
196 forEachKid([](Process *p) { p->kill(true); });
197
198 // If we're in the middle of unwinding, ignore the kill request.
199 if (_isUnwinding)
200 return;
201
202 // Inject the kill exception into this process.
203 injectException(killException);
204
205 _terminatedEvent.notify();
206}
207
208void
209Process::reset(bool inc_kids)
210{
211 // Update our state.
212 _isUnwinding = true;
213
214 // Propogate the reset to our children no matter what happens to us.
215 if (inc_kids)
216 forEachKid([](Process *p) { p->reset(true); });
217
218 // If we're in the middle of unwinding, ignore the reset request.
219 if (_isUnwinding)
220 return;
221
222 // Inject the reset exception into this process.
223 injectException(resetException);
224
225 _resetEvent.notify();
226}
227
228void
229Process::throw_it(ExceptionWrapperBase &exc, bool inc_kids)
230{
231 if (inc_kids)
232 forEachKid([&exc](Process *p) { p->throw_it(exc, true); });
233}
234
235void
236Process::injectException(ExceptionWrapperBase &exc)
237{
238 excWrapper = &exc;
239 // Let this process preempt us.
240};
241
242void
243Process::syncResetOn(bool inc_kids)
244{
245 if (inc_kids)
246 forEachKid([](Process *p) { p->syncResetOn(true); });
247
248 _syncReset = true;
249}
250
251void
252Process::syncResetOff(bool inc_kids)
253{
254 if (inc_kids)
255 forEachKid([](Process *p) { p->syncResetOff(true); });
256
257 _syncReset = false;
258}
259
260void
261Process::dontInitialize()
262{
263 scheduler.dontInitialize(this);
264}
265
266void
267Process::finalize()
268{
269 for (auto &s: pendingStaticSensitivities) {
270 s->finalize(staticSensitivities);
271 delete s;
272 s = nullptr;
273 }
274 pendingStaticSensitivities.clear();
275};
276
277void
278Process::run()
279{
280 _running = true;
281 bool reset;
282 do {
283 reset = false;
284 try {
285 func->call();
286 } catch(::sc_core::sc_unwind_exception exc) {
287 reset = exc.is_reset();
288 }
289 } while (reset);
290 _running = false;
291}
292
293void
294Process::addStatic(PendingSensitivity *s)
295{
296 pendingStaticSensitivities.push_back(s);
297}
298
299void
300Process::setDynamic(Sensitivity *s)
301{
302 delete dynamicSensitivity;
303 dynamicSensitivity = s;
304}
305
306void
307Process::satisfySensitivity(Sensitivity *s)
308{
309 // If there's a dynamic sensitivity and this wasn't it, ignore.
310 if (dynamicSensitivity && dynamicSensitivity != s)
311 return;
312
313 setDynamic(nullptr);
314 ready();
315}
316
317void
318Process::ready()
319{
320 if (suspended())
321 _suspendedReady = true;
322 else
323 scheduler.ready(this);
324}
325
312Process::Process(const char *name, ProcessFuncWrapper *func, bool _dynamic) :
313 ::sc_core::sc_object(name), excWrapper(nullptr), func(func),
314 _running(false), _dynamic(_dynamic), _isUnwinding(false),
315 _terminated(false), _suspended(false), _disabled(false),
316 _syncReset(false), refCount(0), stackSize(::Fiber::DefaultStackSize),
317 dynamicSensitivity(nullptr)
318{
319 _newest = this;
320 if (_dynamic)
321 finalize();
322 else
323 scheduler.reg(this);
324}
325
326Process *Process::_newest;
327
328void
329throw_it_wrapper(Process *p, ExceptionWrapperBase &exc, bool inc_kids)
330{
331 p->throw_it(exc, inc_kids);
332}
333
334} // namespace sc_gem5
326Process::Process(const char *name, ProcessFuncWrapper *func, bool _dynamic) :
327 ::sc_core::sc_object(name), excWrapper(nullptr), func(func),
328 _running(false), _dynamic(_dynamic), _isUnwinding(false),
329 _terminated(false), _suspended(false), _disabled(false),
330 _syncReset(false), refCount(0), stackSize(::Fiber::DefaultStackSize),
331 dynamicSensitivity(nullptr)
332{
333 _newest = this;
334 if (_dynamic)
335 finalize();
336 else
337 scheduler.reg(this);
338}
339
340Process *Process::_newest;
341
342void
343throw_it_wrapper(Process *p, ExceptionWrapperBase &exc, bool inc_kids)
344{
345 p->throw_it(exc, inc_kids);
346}
347
348} // namespace sc_gem5