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