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