process.hh revision 13260:4d18f1d20093
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/object.hh"
40#include "systemc/core/sched_event.hh"
41#include "systemc/core/sensitivity.hh"
42#include "systemc/ext/core/sc_event.hh"
43#include "systemc/ext/core/sc_module.hh"
44#include "systemc/ext/core/sc_process_handle.hh"
45
46namespace sc_core
47{
48
49class sc_join;
50
51} // namespace sc_core
52
53namespace sc_gem5
54{
55
56class ScHalt
57{};
58
59class Process : public ::sc_core::sc_process_b, public ListNode
60{
61  public:
62    virtual ::sc_core::sc_curr_proc_kind procKind() const = 0;
63    bool needsStart() const { return _needsStart; }
64    void needsStart(bool ns) { _needsStart = ns; }
65    bool dynamic() const { return _dynamic; }
66    bool isUnwinding() const { return _isUnwinding; }
67    void isUnwinding(bool v) { _isUnwinding = v; }
68    bool terminated() const { return _terminated; }
69
70    void forEachKid(const std::function<void(Process *)> &work);
71
72    bool suspended() const { return _suspended; }
73    bool disabled() const { return _disabled; }
74
75    void suspend(bool inc_kids);
76    void resume(bool inc_kids);
77    void disable(bool inc_kids);
78    void enable(bool inc_kids);
79
80    void kill(bool inc_kids);
81    void reset(bool inc_kids);
82    virtual void throw_it(ExceptionWrapperBase &exc, bool inc_kids);
83
84    void injectException(ExceptionWrapperBase &exc);
85    ExceptionWrapperBase *excWrapper;
86
87    void syncResetOn(bool inc_kids);
88    void syncResetOff(bool inc_kids);
89
90    void signalReset(bool set, bool sync);
91
92    void incref() { refCount++; }
93    void decref() { refCount--; }
94
95    const ::sc_core::sc_event &resetEvent() { return _resetEvent; }
96    const ::sc_core::sc_event &terminatedEvent() { return _terminatedEvent; }
97
98    void setStackSize(size_t size) { stackSize = size; }
99
100    void run();
101
102    void addStatic(StaticSensitivity *);
103    void setDynamic(DynamicSensitivity *);
104    void clearDynamic() { setDynamic(nullptr); }
105    void addReset(ResetSensitivity *);
106
107    ScEvent timeoutEvent;
108    void setTimeout(::sc_core::sc_time t);
109    void cancelTimeout();
110
111    void satisfySensitivity(Sensitivity *);
112
113    void ready();
114
115    virtual Fiber *fiber() { return Fiber::primaryFiber(); }
116
117    static Process *newest() { return _newest; }
118
119    void lastReport(::sc_core::sc_report *report);
120    ::sc_core::sc_report *lastReport() const;
121
122    bool hasStaticSensitivities() { return !staticSensitivities.empty(); }
123    bool internal() { return _internal; }
124    bool timedOut() { return _timedOut; }
125    bool inReset() { return _syncReset || syncResetCount || asyncResetCount; }
126
127    bool dontInitialize() { return _dontInitialize; }
128    void dontInitialize(bool di) { _dontInitialize = di; }
129
130    void joinWait(::sc_core::sc_join *join) { joinWaiters.push_back(join); }
131
132    void waitCount(int count) { _waitCount = count; }
133
134  protected:
135    void timeout();
136
137    Process(const char *name, ProcessFuncWrapper *func, bool internal=false);
138
139    static Process *_newest;
140
141    virtual ~Process()
142    {
143        popListNode();
144        delete func;
145        for (auto s: staticSensitivities) {
146            s->clear();
147            delete s;
148        }
149        clearDynamic();
150    }
151
152    ::sc_core::sc_event _resetEvent;
153    ::sc_core::sc_event _terminatedEvent;
154
155    ProcessFuncWrapper *func;
156    sc_core::sc_curr_proc_kind _procKind;
157
158    bool _internal;
159
160    // Needed to support the deprecated "timed_out" function.
161    bool _timedOut;
162
163    bool _dontInitialize;
164
165    bool _needsStart;
166    bool _dynamic;
167    bool _isUnwinding;
168    bool _terminated;
169
170    void terminate();
171
172    bool _suspended;
173    bool _suspendedReady;
174    bool _disabled;
175
176    bool _syncReset;
177
178    int syncResetCount;
179    int asyncResetCount;
180
181    int _waitCount;
182
183    int refCount;
184
185    size_t stackSize;
186
187    StaticSensitivities staticSensitivities;
188    DynamicSensitivity *dynamicSensitivity;
189    ResetSensitivities resetSensitivities;
190
191    std::unique_ptr<::sc_core::sc_report> _lastReport;
192
193    std::vector<::sc_core::sc_join *> joinWaiters;
194};
195
196} // namespace sc_gem5
197
198#endif  //__SYSTEMC_CORE_PROCESS_HH__
199