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