Deleted Added
sdiff udiff text old ( 12957:e54f9890363d ) new ( 12959:33d9a39e40a3 )
full compact
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())
91 satisfy();
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;
153 //TODO Suspend this process.
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;
170 //TODO Resume this process.
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
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