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