process.hh revision 13303:045f002c325c
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 "systemc/core/list.hh"
39#include "systemc/core/module.hh"
40#include "systemc/core/object.hh"
41#include "systemc/core/sched_event.hh"
42#include "systemc/core/sensitivity.hh"
43#include "systemc/ext/channel/sc_signal_in_if.hh"
44#include "systemc/ext/core/sc_event.hh"
45#include "systemc/ext/core/sc_module.hh"
46#include "systemc/ext/core/sc_process_handle.hh"
47
48namespace sc_core
49{
50
51class sc_join;
52
53} // namespace sc_core
54
55namespace sc_gem5
56{
57
58class ScHalt
59{};
60
61class Process;
62class Reset;
63
64class Process : public ::sc_core::sc_process_b, public ListNode
65{
66  public:
67    virtual ::sc_core::sc_curr_proc_kind procKind() const = 0;
68    bool needsStart() const { return _needsStart; }
69    void needsStart(bool ns) { _needsStart = ns; }
70    bool dynamic() const { return _dynamic; }
71    bool isUnwinding() const { return _isUnwinding; }
72    void isUnwinding(bool v) { _isUnwinding = v; }
73    bool terminated() const { return _terminated; }
74
75    void forEachKid(const std::function<void(Process *)> &work);
76
77    bool suspended() const { return _suspended; }
78    bool disabled() const { return _disabled; }
79
80    void suspend(bool inc_kids);
81    void resume(bool inc_kids);
82    void disable(bool inc_kids);
83    void enable(bool inc_kids);
84
85    void kill(bool inc_kids);
86    void reset(bool inc_kids);
87    virtual void throw_it(ExceptionWrapperBase &exc, bool inc_kids);
88
89    void injectException(ExceptionWrapperBase &exc);
90    ExceptionWrapperBase *excWrapper;
91
92    void syncResetOn(bool inc_kids);
93    void syncResetOff(bool inc_kids);
94
95    void signalReset(bool set, bool sync);
96
97    void incref() { refCount++; }
98    void decref() { refCount--; }
99
100    const ::sc_core::sc_event &resetEvent() { return _resetEvent; }
101    const ::sc_core::sc_event &terminatedEvent() { return _terminatedEvent; }
102
103    void setStackSize(size_t size) { stackSize = size; }
104
105    void run();
106
107    void addStatic(StaticSensitivity *);
108    void setDynamic(DynamicSensitivity *);
109    void clearDynamic() { setDynamic(nullptr); }
110    void addReset(Reset *);
111
112    ScEvent timeoutEvent;
113    void setTimeout(::sc_core::sc_time t);
114    void cancelTimeout();
115
116    void satisfySensitivity(Sensitivity *);
117
118    void ready();
119
120    virtual Fiber *fiber() { return Fiber::primaryFiber(); }
121
122    static Process *newest() { return _newest; }
123
124    void lastReport(::sc_core::sc_report *report);
125    ::sc_core::sc_report *lastReport() const;
126
127    bool hasStaticSensitivities() { return !staticSensitivities.empty(); }
128    bool internal() { return _internal; }
129    bool timedOut() { return _timedOut; }
130    bool inReset() { return _syncReset || syncResetCount || asyncResetCount; }
131
132    bool dontInitialize() { return _dontInitialize; }
133    void dontInitialize(bool di) { _dontInitialize = di; }
134
135    void joinWait(::sc_core::sc_join *join) { joinWaiters.push_back(join); }
136
137    void waitCount(int count) { _waitCount = count; }
138
139    const char *uniqueName(const char *seed) { return nameGen.gen(seed); }
140
141  protected:
142    void timeout();
143
144    Process(const char *name, ProcessFuncWrapper *func, bool internal=false);
145
146    static Process *_newest;
147
148    virtual ~Process()
149    {
150        popListNode();
151        delete func;
152        for (auto s: staticSensitivities) {
153            s->clear();
154            delete s;
155        }
156        clearDynamic();
157    }
158
159    InternalScEvent _resetEvent;
160    InternalScEvent _terminatedEvent;
161
162    ProcessFuncWrapper *func;
163    sc_core::sc_curr_proc_kind _procKind;
164
165    bool _internal;
166
167    // Needed to support the deprecated "timed_out" function.
168    bool _timedOut;
169
170    bool _dontInitialize;
171
172    bool _needsStart;
173    bool _dynamic;
174    bool _isUnwinding;
175    bool _terminated;
176
177    void terminate();
178
179    bool _suspended;
180    bool _suspendedReady;
181    bool _disabled;
182
183    bool _syncReset;
184
185    int syncResetCount;
186    int asyncResetCount;
187
188    int _waitCount;
189
190    int refCount;
191
192    size_t stackSize;
193
194    StaticSensitivities staticSensitivities;
195    DynamicSensitivity *dynamicSensitivity;
196    std::vector<Reset *> resets;
197
198    std::unique_ptr<::sc_core::sc_report> _lastReport;
199
200    std::vector<::sc_core::sc_join *> joinWaiters;
201
202    UniqueNameGen nameGen;
203};
204
205class Reset
206{
207  public:
208    Reset(Process *p, bool s, bool v) :
209        _process(p), _signal(nullptr), _sync(s), _value(v)
210    {}
211
212    bool
213    install(const sc_core::sc_signal_in_if<bool> *s)
214    {
215        _signal = s;
216
217        if (_signal->_addReset(this)) {
218            _process->addReset(this);
219            if (_signal->read() == _value)
220                update();
221            return true;
222        }
223        return false;
224    }
225    void update() { _process->signalReset(_signal->read() == _value, _sync); }
226
227    Process *process() { return _process; }
228    const sc_core::sc_signal_in_if<bool> *signal() { return _signal; }
229    bool sync() { return _sync; }
230    bool value() { return _value; }
231
232  private:
233    Process *_process;
234    const sc_core::sc_signal_in_if<bool> *_signal;
235    bool _sync;
236    bool _value;
237};
238
239void newReset(const sc_core::sc_port_base *pb, Process *p, bool s, bool v);
240void newReset(const sc_core::sc_signal_in_if<bool> *sig, Process *p,
241        bool s, bool v);
242
243} // namespace sc_gem5
244
245#endif  //__SYSTEMC_CORE_PROCESS_HH__
246