process.cc revision 12961:9bd3a469fd11
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
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())
83        process->satisfySensitivity(this);
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;
145        _suspendedReady = 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;
162        if (_suspendedReady)
163            ready();
164        _suspendedReady = false;
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    bool reset;
281    do {
282        reset = false;
283        try {
284            func->call();
285        } catch(::sc_core::sc_unwind_exception exc) {
286            reset = exc.is_reset();
287        }
288    } while (reset);
289    _terminated = true;
290}
291
292void
293Process::addStatic(PendingSensitivity *s)
294{
295    pendingStaticSensitivities.push_back(s);
296}
297
298void
299Process::setDynamic(Sensitivity *s)
300{
301    delete dynamicSensitivity;
302    dynamicSensitivity = s;
303}
304
305void
306Process::satisfySensitivity(Sensitivity *s)
307{
308    // If there's a dynamic sensitivity and this wasn't it, ignore.
309    if (dynamicSensitivity && dynamicSensitivity != s)
310        return;
311
312    setDynamic(nullptr);
313    ready();
314}
315
316void
317Process::ready()
318{
319    if (suspended())
320        _suspendedReady = true;
321    else
322        scheduler.ready(this);
323}
324
325Process::Process(const char *name, ProcessFuncWrapper *func,
326        bool _dynamic, bool needs_start) :
327    ::sc_core::sc_object(name), excWrapper(nullptr), func(func),
328    _needsStart(needs_start), _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
349