process.hh revision 13132:1fb4a87f550f
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#ifndef __SYSTEMC_CORE_PROCESS_HH__
31#define __SYSTEMC_CORE_PROCESS_HH__
32
33#include <functional>
34#include <memory>
35#include <vector>
36
37#include "base/fiber.hh"
38#include "sim/eventq.hh"
39#include "systemc/core/bindinfo.hh"
40#include "systemc/core/event.hh"
41#include "systemc/core/list.hh"
42#include "systemc/core/object.hh"
43#include "systemc/core/sched_event.hh"
44#include "systemc/ext/core/sc_event.hh"
45#include "systemc/ext/core/sc_export.hh"
46#include "systemc/ext/core/sc_interface.hh"
47#include "systemc/ext/core/sc_module.hh"
48#include "systemc/ext/core/sc_port.hh"
49#include "systemc/ext/core/sc_process_handle.hh"
50#include "systemc/ext/utils/sc_report.hh"
51
52namespace sc_gem5
53{
54
55class Sensitivity
56{
57  protected:
58    Process *process;
59
60  public:
61    Sensitivity(Process *p) : process(p) {}
62    virtual ~Sensitivity() {}
63
64    virtual void notifyWork(Event *e);
65    void notify(Event *e);
66    void notify() { notify(nullptr); }
67
68    const std::string name();
69};
70
71class SensitivityTimeout : virtual public Sensitivity
72{
73  private:
74    void timeout();
75    ScEvent timeoutEvent;
76
77  public:
78    SensitivityTimeout(Process *p, ::sc_core::sc_time t);
79    ~SensitivityTimeout();
80};
81
82class SensitivityEvent : virtual public Sensitivity
83{
84  private:
85    const ::sc_core::sc_event *event;
86
87  public:
88    SensitivityEvent(Process *p, const ::sc_core::sc_event *e);
89    ~SensitivityEvent();
90};
91
92//XXX This sensitivity can't be reused. To reset it, it has to be deleted and
93//recreated. That works for dynamic sensitivities, but not for static.
94//Fortunately processes can't be statically sensitive to sc_event_and_lists.
95class SensitivityEventAndList : virtual public Sensitivity
96{
97  private:
98    const ::sc_core::sc_event_and_list *list;
99    int count;
100
101  public:
102    SensitivityEventAndList(
103            Process *p, const ::sc_core::sc_event_and_list *list);
104    ~SensitivityEventAndList();
105
106    void notifyWork(Event *e) override;
107};
108
109class SensitivityEventOrList : virtual public Sensitivity
110{
111  private:
112    const ::sc_core::sc_event_or_list *list;
113
114  public:
115    SensitivityEventOrList(
116            Process *p, const ::sc_core::sc_event_or_list *list);
117    ~SensitivityEventOrList();
118};
119
120// Combined sensitivities. These trigger when any of their parts do.
121
122class SensitivityTimeoutAndEvent :
123    public SensitivityTimeout, public SensitivityEvent
124{
125  public:
126    SensitivityTimeoutAndEvent(
127            Process *p, ::sc_core::sc_time t, const ::sc_core::sc_event *e) :
128        Sensitivity(p), SensitivityTimeout(p, t), SensitivityEvent(p, e)
129    {}
130};
131
132class SensitivityTimeoutAndEventAndList :
133    public SensitivityTimeout, public SensitivityEventAndList
134{
135  public:
136    SensitivityTimeoutAndEventAndList(
137            Process *p, ::sc_core::sc_time t,
138            const ::sc_core::sc_event_and_list *eal) :
139        Sensitivity(p), SensitivityTimeout(p, t),
140        SensitivityEventAndList(p, eal)
141    {}
142
143    void notifyWork(Event *e) override;
144};
145
146class SensitivityTimeoutAndEventOrList :
147    public SensitivityTimeout, public SensitivityEventOrList
148{
149  public:
150    SensitivityTimeoutAndEventOrList(
151            Process *p, ::sc_core::sc_time t,
152            const ::sc_core::sc_event_or_list *eol) :
153        Sensitivity(p), SensitivityTimeout(p, t),
154        SensitivityEventOrList(p, eol)
155    {}
156};
157
158typedef std::vector<Sensitivity *> Sensitivities;
159
160
161/*
162 * Pending sensitivities. These are records of sensitivities to install later,
163 * once all the information to configure them is available.
164 */
165
166class PendingSensitivity
167{
168  protected:
169    Process *process;
170
171  public:
172    virtual void finalize(Sensitivities &s) = 0;
173    PendingSensitivity(Process *p) : process(p) {}
174    virtual ~PendingSensitivity() {}
175};
176
177class PendingSensitivityEvent : public PendingSensitivity
178{
179  private:
180    const sc_core::sc_event *event;
181
182  public:
183    PendingSensitivityEvent(Process *p, const sc_core::sc_event *e) :
184        PendingSensitivity(p), event(e) {}
185
186    void
187    finalize(Sensitivities &s) override
188    {
189        s.push_back(new SensitivityEvent(process, event));
190    }
191};
192
193class PendingSensitivityInterface : public PendingSensitivity
194{
195  private:
196    const sc_core::sc_interface *interface;
197
198  public:
199    PendingSensitivityInterface(Process *p, const sc_core::sc_interface *i) :
200        PendingSensitivity(p), interface(i)
201    {}
202
203    void
204    finalize(Sensitivities &s) override
205    {
206        s.push_back(new SensitivityEvent(process,
207                                         &interface->default_event()));
208    }
209};
210
211class PendingSensitivityPort : public PendingSensitivity
212{
213  private:
214    const sc_core::sc_port_base *port;
215
216  public:
217    PendingSensitivityPort(Process *p, const sc_core::sc_port_base *pb) :
218        PendingSensitivity(p), port(pb)
219    {}
220
221    void
222    finalize(Sensitivities &s) override
223    {
224        for (int i = 0; i < port->size(); i++) {
225            const ::sc_core::sc_event *e =
226                &port->_gem5Interface(i)->default_event();
227            s.push_back(new SensitivityEvent(process, e));
228        }
229    }
230};
231
232class PendingSensitivityExport : public PendingSensitivity
233{
234  private:
235    const sc_core::sc_export_base *exp;
236
237  public:
238    PendingSensitivityExport(Process *p, const sc_core::sc_export_base *exp) :
239        PendingSensitivity(p), exp(exp)
240    {}
241
242    void
243    finalize(Sensitivities &s) override
244    {
245        s.push_back(new SensitivityEvent(process,
246                    &exp->get_interface()->default_event()));
247    }
248};
249
250class PendingSensitivityFinder : public PendingSensitivity
251{
252  private:
253    const sc_core::sc_event_finder *finder;
254
255  public:
256    PendingSensitivityFinder(Process *p, const sc_core::sc_event_finder *f) :
257        PendingSensitivity(p), finder(f)
258    {}
259
260    void
261    finalize(Sensitivities &s) override
262    {
263        const ::sc_core::sc_port_base *port = finder->port();
264        int size = port->size();
265        for (int i = 0; i < size; i++) {
266            ::sc_core::sc_interface *interface = port->_gem5Interface(i);
267            const ::sc_core::sc_event *event = &finder->find_event(interface);
268            s.push_back(new SensitivityEvent(process, event));
269        }
270    }
271};
272
273typedef std::vector<PendingSensitivity *> PendingSensitivities;
274
275
276class Process : public ::sc_core::sc_process_b, public ListNode
277{
278  public:
279    virtual ::sc_core::sc_curr_proc_kind procKind() const = 0;
280    bool needsStart() const { return _needsStart; }
281    void needsStart(bool ns) { _needsStart = ns; }
282    bool dynamic() const { return _dynamic; }
283    bool isUnwinding() const { return _isUnwinding; }
284    void isUnwinding(bool v) { _isUnwinding = v; }
285    bool terminated() const { return _terminated; }
286
287    void forEachKid(const std::function<void(Process *)> &work);
288
289    bool suspended() const { return _suspended; }
290    bool disabled() const { return _disabled; }
291
292    void suspend(bool inc_kids);
293    void resume(bool inc_kids);
294    void disable(bool inc_kids);
295    void enable(bool inc_kids);
296
297    void kill(bool inc_kids);
298    void reset(bool inc_kids);
299    virtual void throw_it(ExceptionWrapperBase &exc, bool inc_kids);
300
301    void injectException(ExceptionWrapperBase &exc);
302    ExceptionWrapperBase *excWrapper;
303
304    void syncResetOn(bool inc_kids);
305    void syncResetOff(bool inc_kids);
306
307    void incref() { refCount++; }
308    void decref() { refCount--; }
309
310    const ::sc_core::sc_event &resetEvent() { return _resetEvent; }
311    const ::sc_core::sc_event &terminatedEvent() { return _terminatedEvent; }
312
313    // This should only be called before initialization.
314    void dontInitialize();
315
316    void setStackSize(size_t size) { stackSize = size; }
317
318    void finalize();
319
320    void run();
321
322    void addStatic(PendingSensitivity *);
323    void setDynamic(Sensitivity *);
324
325    void satisfySensitivity(Sensitivity *);
326
327    void ready();
328
329    virtual Fiber *fiber() { return Fiber::primaryFiber(); }
330
331    static Process *newest() { return _newest; }
332
333    void lastReport(::sc_core::sc_report *report);
334    ::sc_core::sc_report *lastReport() const;
335
336  protected:
337    Process(const char *name, ProcessFuncWrapper *func);
338
339    static Process *_newest;
340
341    virtual ~Process()
342    {
343        popListNode();
344        delete func;
345        for (auto s: staticSensitivities)
346            delete s;
347    }
348
349    ::sc_core::sc_event _resetEvent;
350    ::sc_core::sc_event _terminatedEvent;
351
352    ProcessFuncWrapper *func;
353    sc_core::sc_curr_proc_kind _procKind;
354    bool _needsStart;
355    bool _dynamic;
356    bool _isUnwinding;
357    bool _terminated;
358
359    void terminate();
360
361    bool _suspended;
362    bool _suspendedReady;
363    bool _disabled;
364
365    bool _syncReset;
366
367    int refCount;
368
369    size_t stackSize;
370
371    Sensitivities staticSensitivities;
372    PendingSensitivities pendingStaticSensitivities;
373
374    Sensitivity *dynamicSensitivity;
375
376    std::unique_ptr<::sc_core::sc_report> _lastReport;
377};
378
379inline void
380Sensitivity::notifyWork(Event *e)
381{
382    process->satisfySensitivity(this);
383}
384
385inline void
386Sensitivity::notify(Event *e)
387{
388    if (!process->disabled())
389        notifyWork(e);
390}
391
392inline const std::string
393Sensitivity::name()
394{
395    return std::string(process->name()) + ".timeout";
396}
397
398} // namespace sc_gem5
399
400#endif  //__SYSTEMC_CORE_PROCESS_HH__
401