process.hh revision 13308
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    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    ::sc_core::sc_event &resetEvent() { return _resetEvent; }
101    ::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
164    bool _internal;
165
166    // Needed to support the deprecated "timed_out" function.
167    bool _timedOut;
168
169    bool _dontInitialize;
170
171    bool _needsStart;
172    bool _dynamic;
173    bool _isUnwinding;
174    bool _terminated;
175
176    void terminate();
177
178    bool _suspended;
179    bool _suspendedReady;
180    bool _disabled;
181
182    bool _syncReset;
183
184    int syncResetCount;
185    int asyncResetCount;
186
187    int _waitCount;
188
189    int refCount;
190
191    size_t stackSize;
192
193    StaticSensitivities staticSensitivities;
194    DynamicSensitivity *dynamicSensitivity;
195    std::vector<Reset *> resets;
196
197    std::unique_ptr<::sc_core::sc_report> _lastReport;
198
199    std::vector<::sc_core::sc_join *> joinWaiters;
200
201    UniqueNameGen nameGen;
202};
203
204class Reset
205{
206  public:
207    Reset(Process *p, bool s, bool v) :
208        _process(p), _signal(nullptr), _sync(s), _value(v)
209    {}
210
211    bool
212    install(const sc_core::sc_signal_in_if<bool> *s)
213    {
214        _signal = s;
215
216        if (_signal->_addReset(this)) {
217            _process->addReset(this);
218            if (_signal->read() == _value)
219                update();
220            return true;
221        }
222        return false;
223    }
224    void update() { _process->signalReset(_signal->read() == _value, _sync); }
225
226    Process *process() { return _process; }
227    const sc_core::sc_signal_in_if<bool> *signal() { return _signal; }
228    bool sync() { return _sync; }
229    bool value() { return _value; }
230
231  private:
232    Process *_process;
233    const sc_core::sc_signal_in_if<bool> *_signal;
234    bool _sync;
235    bool _value;
236};
237
238void newReset(const sc_core::sc_port_base *pb, Process *p, bool s, bool v);
239void newReset(const sc_core::sc_signal_in_if<bool> *sig, Process *p,
240        bool s, bool v);
241
242} // namespace sc_gem5
243
244#endif  //__SYSTEMC_CORE_PROCESS_HH__
245