process.cc (13196:4b5ab2c22743) process.cc (13206:c944ef4abb48)
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#include "systemc/ext/core/sc_join.hh"
36#include "systemc/ext/core/sc_main.hh"
37#include "systemc/ext/core/sc_process_handle.hh"
38#include "systemc/ext/utils/sc_report_handler.hh"
39
40namespace sc_gem5
41{
42
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#include "systemc/ext/core/sc_join.hh"
36#include "systemc/ext/core/sc_main.hh"
37#include "systemc/ext/core/sc_process_handle.hh"
38#include "systemc/ext/utils/sc_report_handler.hh"
39
40namespace sc_gem5
41{
42
43SensitivityTimeout::SensitivityTimeout(Process *p, ::sc_core::sc_time t) :
44 Sensitivity(p), timeoutEvent([this]() { this->timeout(); })
45{
46 scheduler.schedule(&timeoutEvent, t);
47}
48
49SensitivityTimeout::~SensitivityTimeout()
50{
51 if (timeoutEvent.scheduled())
52 scheduler.deschedule(&timeoutEvent);
53}
54
55void
56SensitivityTimeout::timeout()
57{
58 notify();
59}
60
61SensitivityEvent::SensitivityEvent(
62 Process *p, const ::sc_core::sc_event *e) : Sensitivity(p), event(e)
63{
64 Event::getFromScEvent(event)->addSensitivity(this);
65}
66
67SensitivityEvent::~SensitivityEvent()
68{
69 Event::getFromScEvent(event)->delSensitivity(this);
70}
71
72SensitivityEventAndList::SensitivityEventAndList(
73 Process *p, const ::sc_core::sc_event_and_list *list) :
74 Sensitivity(p), list(list), count(0)
75{
76 for (auto e: list->events)
77 Event::getFromScEvent(e)->addSensitivity(this);
78}
79
80SensitivityEventAndList::~SensitivityEventAndList()
81{
82 for (auto e: list->events)
83 Event::getFromScEvent(e)->delSensitivity(this);
84}
85
86void
87SensitivityEventAndList::notifyWork(Event *e)
88{
89 e->delSensitivity(this);
90 count++;
91 if (count == list->events.size())
92 satisfy();
93}
94
95SensitivityEventOrList::SensitivityEventOrList(
96 Process *p, const ::sc_core::sc_event_or_list *list) :
97 Sensitivity(p), list(list)
98{
99 for (auto e: list->events)
100 Event::getFromScEvent(e)->addSensitivity(this);
101}
102
103SensitivityEventOrList::~SensitivityEventOrList()
104{
105 for (auto e: list->events)
106 Event::getFromScEvent(e)->delSensitivity(this);
107}
108
109void
110SensitivityTimeoutAndEventAndList::notifyWork(Event *e)
111{
112 if (e) {
113 // An event went off which must be part of the sc_event_and_list.
114 SensitivityEventAndList::notifyWork(e);
115 } else {
116 // There's no inciting event, so this must be a timeout.
117 satisfy(true);
118 }
119}
120
121
122class UnwindExceptionReset : public ::sc_core::sc_unwind_exception
123{
124 public:
125 UnwindExceptionReset() { _isReset = true; }
126};
127
128class UnwindExceptionKill : public ::sc_core::sc_unwind_exception
129{
130 public:
131 UnwindExceptionKill() {}
132};
133
134template <typename T>
135struct BuiltinExceptionWrapper : public ExceptionWrapperBase
136{
137 public:
138 T t;
139 void throw_it() override { throw t; }
140};
141
142BuiltinExceptionWrapper<UnwindExceptionReset> resetException;
143BuiltinExceptionWrapper<UnwindExceptionKill> killException;
144
145
146void
147Process::forEachKid(const std::function<void(Process *)> &work)
148{
149 for (auto &kid: get_child_objects()) {
150 Process *p_kid = dynamic_cast<Process *>(kid);
151 if (p_kid)
152 work(p_kid);
153 }
154}
155
156void
157Process::suspend(bool inc_kids)
158{
159 if (inc_kids)
160 forEachKid([](Process *p) { p->suspend(true); });
161
162 if (!_suspended) {
163 _suspended = true;
164 _suspendedReady = scheduler.suspend(this);
165
166 if (procKind() != ::sc_core::SC_METHOD_PROC_ &&
167 scheduler.current() == this) {
168 // This isn't in the spec, but Accellera says that a thread that
169 // self suspends should be marked ready immediately when it's
170 // resumed.
171 _suspendedReady = true;
172 scheduler.yield();
173 }
174 }
175}
176
177void
178Process::resume(bool inc_kids)
179{
180 if (inc_kids)
181 forEachKid([](Process *p) { p->resume(true); });
182
183 if (_suspended) {
184 _suspended = false;
185 if (_suspendedReady)
186 scheduler.resume(this);
187 _suspendedReady = false;
188 }
189}
190
191void
192Process::disable(bool inc_kids)
193{
194 if (inc_kids)
195 forEachKid([](Process *p) { p->disable(true); });
196
197 if (!::sc_core::sc_allow_process_control_corners &&
43class UnwindExceptionReset : public ::sc_core::sc_unwind_exception
44{
45 public:
46 UnwindExceptionReset() { _isReset = true; }
47};
48
49class UnwindExceptionKill : public ::sc_core::sc_unwind_exception
50{
51 public:
52 UnwindExceptionKill() {}
53};
54
55template <typename T>
56struct BuiltinExceptionWrapper : public ExceptionWrapperBase
57{
58 public:
59 T t;
60 void throw_it() override { throw t; }
61};
62
63BuiltinExceptionWrapper<UnwindExceptionReset> resetException;
64BuiltinExceptionWrapper<UnwindExceptionKill> killException;
65
66
67void
68Process::forEachKid(const std::function<void(Process *)> &work)
69{
70 for (auto &kid: get_child_objects()) {
71 Process *p_kid = dynamic_cast<Process *>(kid);
72 if (p_kid)
73 work(p_kid);
74 }
75}
76
77void
78Process::suspend(bool inc_kids)
79{
80 if (inc_kids)
81 forEachKid([](Process *p) { p->suspend(true); });
82
83 if (!_suspended) {
84 _suspended = true;
85 _suspendedReady = scheduler.suspend(this);
86
87 if (procKind() != ::sc_core::SC_METHOD_PROC_ &&
88 scheduler.current() == this) {
89 // This isn't in the spec, but Accellera says that a thread that
90 // self suspends should be marked ready immediately when it's
91 // resumed.
92 _suspendedReady = true;
93 scheduler.yield();
94 }
95 }
96}
97
98void
99Process::resume(bool inc_kids)
100{
101 if (inc_kids)
102 forEachKid([](Process *p) { p->resume(true); });
103
104 if (_suspended) {
105 _suspended = false;
106 if (_suspendedReady)
107 scheduler.resume(this);
108 _suspendedReady = false;
109 }
110}
111
112void
113Process::disable(bool inc_kids)
114{
115 if (inc_kids)
116 forEachKid([](Process *p) { p->disable(true); });
117
118 if (!::sc_core::sc_allow_process_control_corners &&
198 dynamic_cast<SensitivityTimeout *>(dynamicSensitivity)) {
119 timeoutEvent.scheduled()) {
199 std::string message("attempt to disable a thread with timeout wait: ");
200 message += name();
201 SC_REPORT_ERROR("Undefined process control interaction",
202 message.c_str());
203 }
204
205 _disabled = true;
206}
207
208void
209Process::enable(bool inc_kids)
210{
211
212 if (inc_kids)
213 forEachKid([](Process *p) { p->enable(true); });
214
215 _disabled = false;
216}
217
218void
219Process::kill(bool inc_kids)
220{
221 if (::sc_core::sc_get_status() != ::sc_core::SC_RUNNING) {
222 SC_REPORT_ERROR(
223 "(E572) a process may not be killed before it is initialized",
224 name());
225 }
226
227 // Propogate the kill to our children no matter what happens to us.
228 if (inc_kids)
229 forEachKid([](Process *p) { p->kill(true); });
230
231 // If we're in the middle of unwinding, ignore the kill request.
232 if (_isUnwinding)
233 return;
234
235 // Update our state.
236 terminate();
237 _isUnwinding = true;
238
239 // Make sure this process isn't marked ready
240 popListNode();
241
242 // Inject the kill exception into this process if it's started.
243 if (!_needsStart)
244 injectException(killException);
245}
246
247void
248Process::reset(bool inc_kids)
249{
250 if (::sc_core::sc_get_status() != ::sc_core::SC_RUNNING) {
251 SC_REPORT_ERROR(
252 "(E573) a process may not be asynchronously reset while"
253 "the simulation is not running", name());
254 }
255
256 // Propogate the reset to our children no matter what happens to us.
257 if (inc_kids)
258 forEachKid([](Process *p) { p->reset(true); });
259
260 // If we're in the middle of unwinding, ignore the reset request.
261 if (_isUnwinding)
262 return;
263
264
265 if (_needsStart) {
266 scheduler.runNow(this);
267 } else {
268 _isUnwinding = true;
269 injectException(resetException);
270 }
271
272 _resetEvent.notify();
273}
274
275void
276Process::throw_it(ExceptionWrapperBase &exc, bool inc_kids)
277{
278 if (::sc_core::sc_get_status() != ::sc_core::SC_RUNNING) {
279 SC_REPORT_ERROR(
280 "(E574) throw_it not allowed unless simulation is running ",
281 name());
282 }
283
284 if (inc_kids)
285 forEachKid([&exc](Process *p) { p->throw_it(exc, true); });
286
287 // Only inject an exception into threads that have started.
288 if (!_needsStart)
289 injectException(exc);
290}
291
292void
293Process::injectException(ExceptionWrapperBase &exc)
294{
295 excWrapper = &exc;
296 scheduler.runNow(this);
297};
298
299void
300Process::syncResetOn(bool inc_kids)
301{
302 if (inc_kids)
303 forEachKid([](Process *p) { p->syncResetOn(true); });
304
305 _syncReset = true;
306}
307
308void
309Process::syncResetOff(bool inc_kids)
310{
311 if (inc_kids)
312 forEachKid([](Process *p) { p->syncResetOff(true); });
313
314 _syncReset = false;
315}
316
317void
318Process::finalize()
319{
120 std::string message("attempt to disable a thread with timeout wait: ");
121 message += name();
122 SC_REPORT_ERROR("Undefined process control interaction",
123 message.c_str());
124 }
125
126 _disabled = true;
127}
128
129void
130Process::enable(bool inc_kids)
131{
132
133 if (inc_kids)
134 forEachKid([](Process *p) { p->enable(true); });
135
136 _disabled = false;
137}
138
139void
140Process::kill(bool inc_kids)
141{
142 if (::sc_core::sc_get_status() != ::sc_core::SC_RUNNING) {
143 SC_REPORT_ERROR(
144 "(E572) a process may not be killed before it is initialized",
145 name());
146 }
147
148 // Propogate the kill to our children no matter what happens to us.
149 if (inc_kids)
150 forEachKid([](Process *p) { p->kill(true); });
151
152 // If we're in the middle of unwinding, ignore the kill request.
153 if (_isUnwinding)
154 return;
155
156 // Update our state.
157 terminate();
158 _isUnwinding = true;
159
160 // Make sure this process isn't marked ready
161 popListNode();
162
163 // Inject the kill exception into this process if it's started.
164 if (!_needsStart)
165 injectException(killException);
166}
167
168void
169Process::reset(bool inc_kids)
170{
171 if (::sc_core::sc_get_status() != ::sc_core::SC_RUNNING) {
172 SC_REPORT_ERROR(
173 "(E573) a process may not be asynchronously reset while"
174 "the simulation is not running", name());
175 }
176
177 // Propogate the reset to our children no matter what happens to us.
178 if (inc_kids)
179 forEachKid([](Process *p) { p->reset(true); });
180
181 // If we're in the middle of unwinding, ignore the reset request.
182 if (_isUnwinding)
183 return;
184
185
186 if (_needsStart) {
187 scheduler.runNow(this);
188 } else {
189 _isUnwinding = true;
190 injectException(resetException);
191 }
192
193 _resetEvent.notify();
194}
195
196void
197Process::throw_it(ExceptionWrapperBase &exc, bool inc_kids)
198{
199 if (::sc_core::sc_get_status() != ::sc_core::SC_RUNNING) {
200 SC_REPORT_ERROR(
201 "(E574) throw_it not allowed unless simulation is running ",
202 name());
203 }
204
205 if (inc_kids)
206 forEachKid([&exc](Process *p) { p->throw_it(exc, true); });
207
208 // Only inject an exception into threads that have started.
209 if (!_needsStart)
210 injectException(exc);
211}
212
213void
214Process::injectException(ExceptionWrapperBase &exc)
215{
216 excWrapper = &exc;
217 scheduler.runNow(this);
218};
219
220void
221Process::syncResetOn(bool inc_kids)
222{
223 if (inc_kids)
224 forEachKid([](Process *p) { p->syncResetOn(true); });
225
226 _syncReset = true;
227}
228
229void
230Process::syncResetOff(bool inc_kids)
231{
232 if (inc_kids)
233 forEachKid([](Process *p) { p->syncResetOff(true); });
234
235 _syncReset = false;
236}
237
238void
239Process::finalize()
240{
320 for (auto &s: pendingStaticSensitivities) {
321 s->finalize(staticSensitivities);
322 delete s;
323 s = nullptr;
324 }
325 pendingStaticSensitivities.clear();
241 for (auto s: staticSensitivities)
242 s->finalize();
326};
327
328void
329Process::run()
330{
331 bool reset;
332 do {
333 reset = false;
334 try {
335 func->call();
336 } catch(ScHalt) {
337 std::cout << "Terminating process " << name() << std::endl;
338 } catch(const ::sc_core::sc_unwind_exception &exc) {
339 reset = exc.is_reset();
340 _isUnwinding = false;
341 } catch (...) {
342 throw;
343 }
344 } while (reset);
345 needsStart(true);
346}
347
348void
243};
244
245void
246Process::run()
247{
248 bool reset;
249 do {
250 reset = false;
251 try {
252 func->call();
253 } catch(ScHalt) {
254 std::cout << "Terminating process " << name() << std::endl;
255 } catch(const ::sc_core::sc_unwind_exception &exc) {
256 reset = exc.is_reset();
257 _isUnwinding = false;
258 } catch (...) {
259 throw;
260 }
261 } while (reset);
262 needsStart(true);
263}
264
265void
349Process::addStatic(PendingSensitivity *s)
266Process::addStatic(StaticSensitivity *s)
350{
267{
351 pendingStaticSensitivities.push_back(s);
268 staticSensitivities.push_back(s);
352}
353
354void
269}
270
271void
355Process::setDynamic(Sensitivity *s)
272Process::setDynamic(DynamicSensitivity *s)
356{
273{
357 delete dynamicSensitivity;
274 if (dynamicSensitivity) {
275 dynamicSensitivity->clear();
276 delete dynamicSensitivity;
277 }
358 dynamicSensitivity = s;
278 dynamicSensitivity = s;
279 if (dynamicSensitivity)
280 dynamicSensitivity->finalize();
359}
360
361void
281}
282
283void
284Process::cancelTimeout()
285{
286 if (timeoutEvent.scheduled())
287 scheduler.deschedule(&timeoutEvent);
288}
289
290void
291Process::setTimeout(::sc_core::sc_time t)
292{
293 cancelTimeout();
294 scheduler.schedule(&timeoutEvent, t);
295}
296
297void
298Process::timeout()
299{
300 // A process is considered timed_out only if it was also waiting for an
301 // event but got a timeout instead.
302 _timedOut = (dynamicSensitivity != nullptr);
303
304 setDynamic(nullptr);
305 if (disabled())
306 return;
307
308 ready();
309}
310
311void
362Process::satisfySensitivity(Sensitivity *s)
363{
364 // If there's a dynamic sensitivity and this wasn't it, ignore.
312Process::satisfySensitivity(Sensitivity *s)
313{
314 // If there's a dynamic sensitivity and this wasn't it, ignore.
365 if (dynamicSensitivity && dynamicSensitivity != s)
315 if ((dynamicSensitivity || timeoutEvent.scheduled()) &&
316 dynamicSensitivity != s) {
366 return;
317 return;
318 }
367
319
368 setDynamic(nullptr);
320 _timedOut = false;
321 // This sensitivity should already be cleared by this point, or the event
322 // which triggered it will take care of it.
323 delete dynamicSensitivity;
324 dynamicSensitivity = nullptr;
325 cancelTimeout();
369 ready();
370}
371
372void
373Process::ready()
374{
375 if (disabled())
376 return;
377 if (suspended())
378 _suspendedReady = true;
379 else
380 scheduler.ready(this);
381}
382
383void
384Process::lastReport(::sc_core::sc_report *report)
385{
386 if (report) {
387 _lastReport = std::unique_ptr<::sc_core::sc_report>(
388 new ::sc_core::sc_report(*report));
389 } else {
390 _lastReport = nullptr;
391 }
392}
393
394::sc_core::sc_report *Process::lastReport() const { return _lastReport.get(); }
395
396Process::Process(const char *name, ProcessFuncWrapper *func, bool internal) :
326 ready();
327}
328
329void
330Process::ready()
331{
332 if (disabled())
333 return;
334 if (suspended())
335 _suspendedReady = true;
336 else
337 scheduler.ready(this);
338}
339
340void
341Process::lastReport(::sc_core::sc_report *report)
342{
343 if (report) {
344 _lastReport = std::unique_ptr<::sc_core::sc_report>(
345 new ::sc_core::sc_report(*report));
346 } else {
347 _lastReport = nullptr;
348 }
349}
350
351::sc_core::sc_report *Process::lastReport() const { return _lastReport.get(); }
352
353Process::Process(const char *name, ProcessFuncWrapper *func, bool internal) :
397 ::sc_core::sc_process_b(name), excWrapper(nullptr), func(func),
398 _internal(internal), _timedOut(false), _dontInitialize(false),
354 ::sc_core::sc_process_b(name), excWrapper(nullptr),
355 timeoutEvent([this]() { this->timeout(); }),
356 func(func), _internal(internal), _timedOut(false), _dontInitialize(false),
399 _needsStart(true), _isUnwinding(false), _terminated(false),
400 _suspended(false), _disabled(false), _syncReset(false), refCount(0),
401 stackSize(::Fiber::DefaultStackSize), dynamicSensitivity(nullptr)
402{
403 _dynamic =
404 (::sc_core::sc_get_status() >
405 ::sc_core::SC_BEFORE_END_OF_ELABORATION);
406 _newest = this;
407}
408
409void
410Process::terminate()
411{
412 _terminated = true;
413 _suspendedReady = false;
414 _suspended = false;
415 _syncReset = false;
357 _needsStart(true), _isUnwinding(false), _terminated(false),
358 _suspended(false), _disabled(false), _syncReset(false), refCount(0),
359 stackSize(::Fiber::DefaultStackSize), dynamicSensitivity(nullptr)
360{
361 _dynamic =
362 (::sc_core::sc_get_status() >
363 ::sc_core::SC_BEFORE_END_OF_ELABORATION);
364 _newest = this;
365}
366
367void
368Process::terminate()
369{
370 _terminated = true;
371 _suspendedReady = false;
372 _suspended = false;
373 _syncReset = false;
416 delete dynamicSensitivity;
417 dynamicSensitivity = nullptr;
418 for (auto s: staticSensitivities)
374 clearDynamic();
375 cancelTimeout();
376 for (auto s: staticSensitivities) {
377 s->clear();
419 delete s;
378 delete s;
379 }
420 staticSensitivities.clear();
421
422 _terminatedEvent.notify();
423
424 for (auto jw: joinWaiters)
425 jw->signal();
426 joinWaiters.clear();
427}
428
429Process *Process::_newest;
430
431void
432throw_it_wrapper(Process *p, ExceptionWrapperBase &exc, bool inc_kids)
433{
434 p->throw_it(exc, inc_kids);
435}
436
437} // namespace sc_gem5
380 staticSensitivities.clear();
381
382 _terminatedEvent.notify();
383
384 for (auto jw: joinWaiters)
385 jw->signal();
386 joinWaiters.clear();
387}
388
389Process *Process::_newest;
390
391void
392throw_it_wrapper(Process *p, ExceptionWrapperBase &exc, bool inc_kids)
393{
394 p->throw_it(exc, inc_kids);
395}
396
397} // namespace sc_gem5