Deleted Added
sdiff udiff text old ( 12952:94fca7e8120b ) new ( 12953:ddfd5e4643a9 )
full compact
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#include "systemc/core/process.hh"
31#include "systemc/core/scheduler.hh"
32
33namespace sc_gem5
34{
35
36class UnwindExceptionReset : public ::sc_core::sc_unwind_exception
37{
38 public:
39 const char *what() const throw() override { return "RESET"; }
40 bool is_reset() const override { return true; }
41};
42
43class UnwindExceptionKill : public ::sc_core::sc_unwind_exception
44{
45 public:
46 const char *what() const throw() override { return "KILL"; }
47 bool is_reset() const override { return false; }
48};
49
50template <typename T>
51struct BuiltinExceptionWrapper : public ExceptionWrapperBase
52{
53 public:
54 T t;
55 void throw_it() override { throw t; }
56};
57
58BuiltinExceptionWrapper<UnwindExceptionReset> resetException;
59BuiltinExceptionWrapper<UnwindExceptionKill> killException;
60
61
62void
63Process::forEachKid(const std::function<void(Process *)> &work)
64{
65 for (auto &kid: get_child_objects()) {
66 Process *p_kid = dynamic_cast<Process *>(kid);
67 if (p_kid)
68 work(p_kid);
69 }
70}
71
72void
73Process::suspend(bool inc_kids)
74{
75 if (inc_kids)
76 forEachKid([](Process *p) { p->suspend(true); });
77
78 if (!_suspended) {
79 _suspended = true;
80 //TODO Suspend this process.
81 }
82
83 if (procKind() != ::sc_core::SC_METHOD_PROC_ &&
84 scheduler.current() == this) {
85 scheduler.yield();
86 }
87}
88
89void
90Process::resume(bool inc_kids)
91{
92 if (inc_kids)
93 forEachKid([](Process *p) { p->resume(true); });
94
95 if (_suspended) {
96 _suspended = false;
97 //TODO Resume this process.
98 }
99}
100
101void
102Process::disable(bool inc_kids)
103{
104 if (inc_kids)
105 forEachKid([](Process *p) { p->disable(true); });
106
107 _disabled = true;
108}
109
110void
111Process::enable(bool inc_kids)
112{
113
114 if (inc_kids)
115 forEachKid([](Process *p) { p->enable(true); });
116
117 _disabled = false;
118}
119
120void
121Process::kill(bool inc_kids)
122{
123 // Update our state.
124 _terminated = true;
125 _isUnwinding = true;
126
127 // Propogate the kill to our children no matter what happens to us.
128 if (inc_kids)
129 forEachKid([](Process *p) { p->kill(true); });
130
131 // If we're in the middle of unwinding, ignore the kill request.
132 if (_isUnwinding)
133 return;
134
135 // Inject the kill exception into this process.
136 injectException(killException);
137
138 _terminatedEvent.notify();
139}
140
141void
142Process::reset(bool inc_kids)
143{
144 // Update our state.
145 _isUnwinding = true;
146
147 // Propogate the reset to our children no matter what happens to us.
148 if (inc_kids)
149 forEachKid([](Process *p) { p->reset(true); });
150
151 // If we're in the middle of unwinding, ignore the reset request.
152 if (_isUnwinding)
153 return;
154
155 // Inject the reset exception into this process.
156 injectException(resetException);
157
158 _resetEvent.notify();
159}
160
161void
162Process::throw_it(ExceptionWrapperBase &exc, bool inc_kids)
163{
164 if (inc_kids)
165 forEachKid([&exc](Process *p) { p->throw_it(exc, true); });
166}
167
168void
169Process::injectException(ExceptionWrapperBase &exc)
170{
171 excWrapper = &exc;
172 // Let this process preempt us.
173};
174
175void
176Process::syncResetOn(bool inc_kids)
177{
178 if (inc_kids)
179 forEachKid([](Process *p) { p->syncResetOn(true); });
180
181 _syncReset = true;
182}
183
184void
185Process::syncResetOff(bool inc_kids)
186{
187 if (inc_kids)
188 forEachKid([](Process *p) { p->syncResetOff(true); });
189
190 _syncReset = false;
191}
192
193void
194Process::run()
195{
196 _running = true;
197 bool reset;
198 do {
199 reset = false;
200 try {
201 func->call();
202 } catch(::sc_core::sc_unwind_exception exc) {
203 reset = exc.is_reset();
204 }
205 } while (reset);
206 _running = false;
207}
208
209Process::Process(const char *name, ProcessFuncWrapper *func, bool _dynamic) :
210 ::sc_core::sc_object(name), excWrapper(nullptr), func(func),
211 _running(false), _dynamic(_dynamic), _isUnwinding(false),
212 _terminated(false), _suspended(false), _disabled(false),
213 _syncReset(false), refCount(0), stackSize(::Fiber::DefaultStackSize)
214{
215 _newest = this;
216 if (!_dynamic)
217 scheduler.init(this);
218}
219
220Process *Process::_newest;
221
222void
223throw_it_wrapper(Process *p, ExceptionWrapperBase &exc, bool inc_kids)
224{
225 p->throw_it(exc, inc_kids);
226}
227
228} // namespace sc_gem5