process.hh revision 13207:034ca389a810
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 incref() { refCount++; }
91    void decref() { refCount--; }
92
93    const ::sc_core::sc_event &resetEvent() { return _resetEvent; }
94    const ::sc_core::sc_event &terminatedEvent() { return _terminatedEvent; }
95
96    void setStackSize(size_t size) { stackSize = size; }
97
98    void run();
99
100    void addStatic(StaticSensitivity *);
101    void setDynamic(DynamicSensitivity *);
102    void clearDynamic() { setDynamic(nullptr); }
103
104    ScEvent timeoutEvent;
105    void setTimeout(::sc_core::sc_time t);
106    void cancelTimeout();
107
108    void satisfySensitivity(Sensitivity *);
109
110    void ready();
111
112    virtual Fiber *fiber() { return Fiber::primaryFiber(); }
113
114    static Process *newest() { return _newest; }
115
116    void lastReport(::sc_core::sc_report *report);
117    ::sc_core::sc_report *lastReport() const;
118
119    bool hasStaticSensitivities() { return !staticSensitivities.empty(); }
120    bool internal() { return _internal; }
121    bool timedOut() { return _timedOut; }
122
123    bool dontInitialize() { return _dontInitialize; }
124    void dontInitialize(bool di) { _dontInitialize = di; }
125
126    void joinWait(::sc_core::sc_join *join) { joinWaiters.push_back(join); }
127
128  protected:
129    void timeout();
130
131    Process(const char *name, ProcessFuncWrapper *func, bool internal=false);
132
133    static Process *_newest;
134
135    virtual ~Process()
136    {
137        popListNode();
138        delete func;
139        for (auto s: staticSensitivities) {
140            s->clear();
141            delete s;
142        }
143        clearDynamic();
144    }
145
146    ::sc_core::sc_event _resetEvent;
147    ::sc_core::sc_event _terminatedEvent;
148
149    ProcessFuncWrapper *func;
150    sc_core::sc_curr_proc_kind _procKind;
151
152    bool _internal;
153
154    // Needed to support the deprecated "timed_out" function.
155    bool _timedOut;
156
157    bool _dontInitialize;
158
159    bool _needsStart;
160    bool _dynamic;
161    bool _isUnwinding;
162    bool _terminated;
163
164    void terminate();
165
166    bool _suspended;
167    bool _suspendedReady;
168    bool _disabled;
169
170    bool _syncReset;
171
172    int refCount;
173
174    size_t stackSize;
175
176    StaticSensitivities staticSensitivities;
177    DynamicSensitivity *dynamicSensitivity;
178
179    std::unique_ptr<::sc_core::sc_report> _lastReport;
180
181    std::vector<::sc_core::sc_join *> joinWaiters;
182};
183
184} // namespace sc_gem5
185
186#endif  //__SYSTEMC_CORE_PROCESS_HH__
187