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 32#include "base/logging.hh" 33#include "systemc/core/event.hh" 34#include "systemc/core/scheduler.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 32#include "base/logging.hh" 33#include "systemc/core/event.hh" 34#include "systemc/core/scheduler.hh"
|
| 35#include "systemc/ext/core/sc_process_handle.hh" 36#include "systemc/ext/utils/sc_report_handler.hh"
|
35 36namespace sc_gem5 37{ 38 39SensitivityTimeout::SensitivityTimeout(Process *p, ::sc_core::sc_time t) : 40 Sensitivity(p), timeoutEvent(this), time(t) 41{ 42 Tick when = scheduler.getCurTick() + time.value(); 43 scheduler.schedule(&timeoutEvent, when); 44} 45 46SensitivityTimeout::~SensitivityTimeout() 47{ 48 if (timeoutEvent.scheduled()) 49 scheduler.deschedule(&timeoutEvent); 50} 51 52void 53SensitivityTimeout::timeout() 54{ 55 scheduler.eventHappened(); 56 notify(); 57} 58 59SensitivityEvent::SensitivityEvent( 60 Process *p, const ::sc_core::sc_event *e) : Sensitivity(p), event(e) 61{ 62 Event::getFromScEvent(event)->addSensitivity(this); 63} 64 65SensitivityEvent::~SensitivityEvent() 66{ 67 Event::getFromScEvent(event)->delSensitivity(this); 68} 69 70SensitivityEventAndList::SensitivityEventAndList( 71 Process *p, const ::sc_core::sc_event_and_list *list) : 72 Sensitivity(p), list(list), count(0) 73{ 74 for (auto e: list->events) 75 Event::getFromScEvent(e)->addSensitivity(this); 76} 77 78SensitivityEventAndList::~SensitivityEventAndList() 79{ 80 for (auto e: list->events) 81 Event::getFromScEvent(e)->delSensitivity(this); 82} 83 84void 85SensitivityEventAndList::notifyWork(Event *e) 86{ 87 e->delSensitivity(this); 88 count++; 89 if (count == list->events.size()) 90 process->satisfySensitivity(this); 91} 92 93SensitivityEventOrList::SensitivityEventOrList( 94 Process *p, const ::sc_core::sc_event_or_list *list) : 95 Sensitivity(p), list(list) 96{ 97 for (auto e: list->events) 98 Event::getFromScEvent(e)->addSensitivity(this); 99} 100 101SensitivityEventOrList::~SensitivityEventOrList() 102{ 103 for (auto e: list->events) 104 Event::getFromScEvent(e)->delSensitivity(this); 105} 106 107 108class UnwindExceptionReset : public ::sc_core::sc_unwind_exception 109{ 110 public: 111 UnwindExceptionReset() { _isReset = true; } 112}; 113 114class UnwindExceptionKill : public ::sc_core::sc_unwind_exception 115{ 116 public: 117 UnwindExceptionKill() {} 118}; 119 120template <typename T> 121struct BuiltinExceptionWrapper : public ExceptionWrapperBase 122{ 123 public: 124 T t; 125 void throw_it() override { throw t; } 126}; 127 128BuiltinExceptionWrapper<UnwindExceptionReset> resetException; 129BuiltinExceptionWrapper<UnwindExceptionKill> killException; 130 131 132void 133Process::forEachKid(const std::function<void(Process *)> &work) 134{ 135 for (auto &kid: get_child_objects()) { 136 Process *p_kid = dynamic_cast<Process *>(kid); 137 if (p_kid) 138 work(p_kid); 139 } 140} 141 142void 143Process::suspend(bool inc_kids) 144{ 145 if (inc_kids) 146 forEachKid([](Process *p) { p->suspend(true); }); 147 148 if (!_suspended) { 149 _suspended = true; 150 _suspendedReady = false; 151 } 152 153 if (procKind() != ::sc_core::SC_METHOD_PROC_ && 154 scheduler.current() == this) { 155 scheduler.yield(); 156 } 157} 158 159void 160Process::resume(bool inc_kids) 161{ 162 if (inc_kids) 163 forEachKid([](Process *p) { p->resume(true); }); 164 165 if (_suspended) { 166 _suspended = false; 167 if (_suspendedReady) 168 ready(); 169 _suspendedReady = false; 170 } 171} 172 173void 174Process::disable(bool inc_kids) 175{ 176 if (inc_kids) 177 forEachKid([](Process *p) { p->disable(true); }); 178 179 _disabled = true; 180} 181 182void 183Process::enable(bool inc_kids) 184{ 185 186 if (inc_kids) 187 forEachKid([](Process *p) { p->enable(true); }); 188 189 _disabled = false; 190} 191 192void 193Process::kill(bool inc_kids) 194{ 195 // Propogate the kill to our children no matter what happens to us. 196 if (inc_kids) 197 forEachKid([](Process *p) { p->kill(true); }); 198 199 // If we're in the middle of unwinding, ignore the kill request. 200 if (_isUnwinding) 201 return; 202 203 // Update our state.
| 37 38namespace sc_gem5 39{ 40 41SensitivityTimeout::SensitivityTimeout(Process *p, ::sc_core::sc_time t) : 42 Sensitivity(p), timeoutEvent(this), time(t) 43{ 44 Tick when = scheduler.getCurTick() + time.value(); 45 scheduler.schedule(&timeoutEvent, when); 46} 47 48SensitivityTimeout::~SensitivityTimeout() 49{ 50 if (timeoutEvent.scheduled()) 51 scheduler.deschedule(&timeoutEvent); 52} 53 54void 55SensitivityTimeout::timeout() 56{ 57 scheduler.eventHappened(); 58 notify(); 59} 60 61SensitivityEvent::SensitivityEvent( 62 Process *p, const ::sc_core::sc_event *e) : Sensitivity(p), event(e) 63{ 64 Event::getFromScEvent(event)->addSensitivity(this); 65} 66 67SensitivityEvent::~SensitivityEvent() 68{ 69 Event::getFromScEvent(event)->delSensitivity(this); 70} 71 72SensitivityEventAndList::SensitivityEventAndList( 73 Process *p, const ::sc_core::sc_event_and_list *list) : 74 Sensitivity(p), list(list), count(0) 75{ 76 for (auto e: list->events) 77 Event::getFromScEvent(e)->addSensitivity(this); 78} 79 80SensitivityEventAndList::~SensitivityEventAndList() 81{ 82 for (auto e: list->events) 83 Event::getFromScEvent(e)->delSensitivity(this); 84} 85 86void 87SensitivityEventAndList::notifyWork(Event *e) 88{ 89 e->delSensitivity(this); 90 count++; 91 if (count == list->events.size()) 92 process->satisfySensitivity(this); 93} 94 95SensitivityEventOrList::SensitivityEventOrList( 96 Process *p, const ::sc_core::sc_event_or_list *list) : 97 Sensitivity(p), list(list) 98{ 99 for (auto e: list->events) 100 Event::getFromScEvent(e)->addSensitivity(this); 101} 102 103SensitivityEventOrList::~SensitivityEventOrList() 104{ 105 for (auto e: list->events) 106 Event::getFromScEvent(e)->delSensitivity(this); 107} 108 109 110class UnwindExceptionReset : public ::sc_core::sc_unwind_exception 111{ 112 public: 113 UnwindExceptionReset() { _isReset = true; } 114}; 115 116class UnwindExceptionKill : public ::sc_core::sc_unwind_exception 117{ 118 public: 119 UnwindExceptionKill() {} 120}; 121 122template <typename T> 123struct BuiltinExceptionWrapper : public ExceptionWrapperBase 124{ 125 public: 126 T t; 127 void throw_it() override { throw t; } 128}; 129 130BuiltinExceptionWrapper<UnwindExceptionReset> resetException; 131BuiltinExceptionWrapper<UnwindExceptionKill> killException; 132 133 134void 135Process::forEachKid(const std::function<void(Process *)> &work) 136{ 137 for (auto &kid: get_child_objects()) { 138 Process *p_kid = dynamic_cast<Process *>(kid); 139 if (p_kid) 140 work(p_kid); 141 } 142} 143 144void 145Process::suspend(bool inc_kids) 146{ 147 if (inc_kids) 148 forEachKid([](Process *p) { p->suspend(true); }); 149 150 if (!_suspended) { 151 _suspended = true; 152 _suspendedReady = false; 153 } 154 155 if (procKind() != ::sc_core::SC_METHOD_PROC_ && 156 scheduler.current() == this) { 157 scheduler.yield(); 158 } 159} 160 161void 162Process::resume(bool inc_kids) 163{ 164 if (inc_kids) 165 forEachKid([](Process *p) { p->resume(true); }); 166 167 if (_suspended) { 168 _suspended = false; 169 if (_suspendedReady) 170 ready(); 171 _suspendedReady = false; 172 } 173} 174 175void 176Process::disable(bool inc_kids) 177{ 178 if (inc_kids) 179 forEachKid([](Process *p) { p->disable(true); }); 180 181 _disabled = true; 182} 183 184void 185Process::enable(bool inc_kids) 186{ 187 188 if (inc_kids) 189 forEachKid([](Process *p) { p->enable(true); }); 190 191 _disabled = false; 192} 193 194void 195Process::kill(bool inc_kids) 196{ 197 // Propogate the kill to our children no matter what happens to us. 198 if (inc_kids) 199 forEachKid([](Process *p) { p->kill(true); }); 200 201 // If we're in the middle of unwinding, ignore the kill request. 202 if (_isUnwinding) 203 return; 204 205 // Update our state.
|
204 _terminated = true;
| 206 terminate();
|
205 _isUnwinding = true;
| 207 _isUnwinding = true;
|
206 _suspendedReady = false; 207 _suspended = false; 208 _syncReset = false;
| |
209
| 208
|
210 // Inject the kill exception into this process. 211 injectException(killException);
| 209 // Make sure this process isn't marked ready 210 popListNode();
|
212
| 211
|
| 212 // Inject the kill exception into this process if it's started. 213 if (!_needsStart) 214 injectException(killException); 215
|
213 _terminatedEvent.notify(); 214} 215 216void 217Process::reset(bool inc_kids) 218{ 219 // Propogate the reset to our children no matter what happens to us. 220 if (inc_kids) 221 forEachKid([](Process *p) { p->reset(true); }); 222 223 // If we're in the middle of unwinding, ignore the reset request. 224 if (_isUnwinding) 225 return; 226
| 216 _terminatedEvent.notify(); 217} 218 219void 220Process::reset(bool inc_kids) 221{ 222 // Propogate the reset to our children no matter what happens to us. 223 if (inc_kids) 224 forEachKid([](Process *p) { p->reset(true); }); 225 226 // If we're in the middle of unwinding, ignore the reset request. 227 if (_isUnwinding) 228 return; 229
|
227 // Update our state. 228 _isUnwinding = true;
| |
229
| 230
|
230 // Inject the reset exception into this process. 231 injectException(resetException);
| 231 if (_needsStart) { 232 scheduler.runNow(this); 233 } else { 234 _isUnwinding = true; 235 injectException(resetException); 236 }
|
232 233 _resetEvent.notify(); 234} 235 236void 237Process::throw_it(ExceptionWrapperBase &exc, bool inc_kids) 238{ 239 if (inc_kids) 240 forEachKid([&exc](Process *p) { p->throw_it(exc, true); });
| 237 238 _resetEvent.notify(); 239} 240 241void 242Process::throw_it(ExceptionWrapperBase &exc, bool inc_kids) 243{ 244 if (inc_kids) 245 forEachKid([&exc](Process *p) { p->throw_it(exc, true); });
|
| 246 247 // Only inject an exception into threads that have started. 248 if (!_needsStart) 249 injectException(exc);
|
241} 242 243void 244Process::injectException(ExceptionWrapperBase &exc) 245{ 246 excWrapper = &exc; 247 scheduler.runNow(this); 248}; 249 250void 251Process::syncResetOn(bool inc_kids) 252{ 253 if (inc_kids) 254 forEachKid([](Process *p) { p->syncResetOn(true); }); 255 256 _syncReset = true; 257} 258 259void 260Process::syncResetOff(bool inc_kids) 261{ 262 if (inc_kids) 263 forEachKid([](Process *p) { p->syncResetOff(true); }); 264 265 _syncReset = false; 266} 267 268void 269Process::dontInitialize() 270{ 271 scheduler.dontInitialize(this); 272} 273 274void 275Process::finalize() 276{ 277 for (auto &s: pendingStaticSensitivities) { 278 s->finalize(staticSensitivities); 279 delete s; 280 s = nullptr; 281 } 282 pendingStaticSensitivities.clear(); 283}; 284 285void 286Process::run() 287{ 288 bool reset; 289 do { 290 reset = false; 291 try { 292 func->call(); 293 } catch(const ::sc_core::sc_unwind_exception &exc) { 294 reset = exc.is_reset(); 295 _isUnwinding = false; 296 } 297 } while (reset);
| 250} 251 252void 253Process::injectException(ExceptionWrapperBase &exc) 254{ 255 excWrapper = &exc; 256 scheduler.runNow(this); 257}; 258 259void 260Process::syncResetOn(bool inc_kids) 261{ 262 if (inc_kids) 263 forEachKid([](Process *p) { p->syncResetOn(true); }); 264 265 _syncReset = true; 266} 267 268void 269Process::syncResetOff(bool inc_kids) 270{ 271 if (inc_kids) 272 forEachKid([](Process *p) { p->syncResetOff(true); }); 273 274 _syncReset = false; 275} 276 277void 278Process::dontInitialize() 279{ 280 scheduler.dontInitialize(this); 281} 282 283void 284Process::finalize() 285{ 286 for (auto &s: pendingStaticSensitivities) { 287 s->finalize(staticSensitivities); 288 delete s; 289 s = nullptr; 290 } 291 pendingStaticSensitivities.clear(); 292}; 293 294void 295Process::run() 296{ 297 bool reset; 298 do { 299 reset = false; 300 try { 301 func->call(); 302 } catch(const ::sc_core::sc_unwind_exception &exc) { 303 reset = exc.is_reset(); 304 _isUnwinding = false; 305 } 306 } while (reset);
|
298 _terminated = true;
| |
299} 300 301void 302Process::addStatic(PendingSensitivity *s) 303{ 304 pendingStaticSensitivities.push_back(s); 305} 306 307void 308Process::setDynamic(Sensitivity *s) 309{ 310 delete dynamicSensitivity; 311 dynamicSensitivity = s; 312} 313 314void 315Process::satisfySensitivity(Sensitivity *s) 316{ 317 // If there's a dynamic sensitivity and this wasn't it, ignore. 318 if (dynamicSensitivity && dynamicSensitivity != s) 319 return; 320 321 setDynamic(nullptr); 322 ready(); 323} 324 325void 326Process::ready() 327{ 328 if (disabled()) 329 return; 330 if (suspended()) 331 _suspendedReady = true; 332 else 333 scheduler.ready(this); 334} 335 336void 337Process::lastReport(::sc_core::sc_report *report) 338{ 339 if (report) { 340 _lastReport = std::unique_ptr<::sc_core::sc_report>( 341 new ::sc_core::sc_report(*report)); 342 } else { 343 _lastReport = nullptr; 344 } 345} 346 347::sc_core::sc_report *Process::lastReport() const { return _lastReport.get(); } 348
| 307} 308 309void 310Process::addStatic(PendingSensitivity *s) 311{ 312 pendingStaticSensitivities.push_back(s); 313} 314 315void 316Process::setDynamic(Sensitivity *s) 317{ 318 delete dynamicSensitivity; 319 dynamicSensitivity = s; 320} 321 322void 323Process::satisfySensitivity(Sensitivity *s) 324{ 325 // If there's a dynamic sensitivity and this wasn't it, ignore. 326 if (dynamicSensitivity && dynamicSensitivity != s) 327 return; 328 329 setDynamic(nullptr); 330 ready(); 331} 332 333void 334Process::ready() 335{ 336 if (disabled()) 337 return; 338 if (suspended()) 339 _suspendedReady = true; 340 else 341 scheduler.ready(this); 342} 343 344void 345Process::lastReport(::sc_core::sc_report *report) 346{ 347 if (report) { 348 _lastReport = std::unique_ptr<::sc_core::sc_report>( 349 new ::sc_core::sc_report(*report)); 350 } else { 351 _lastReport = nullptr; 352 } 353} 354 355::sc_core::sc_report *Process::lastReport() const { return _lastReport.get(); } 356
|
349Process::Process(const char *name, ProcessFuncWrapper *func, 350 bool _dynamic, bool needs_start) :
| 357Process::Process(const char *name, ProcessFuncWrapper *func, bool _dynamic) :
|
351 ::sc_core::sc_object(name), excWrapper(nullptr), func(func),
| 358 ::sc_core::sc_object(name), excWrapper(nullptr), func(func),
|
352 _needsStart(needs_start), _dynamic(_dynamic), _isUnwinding(false),
| 359 _needsStart(true), _dynamic(_dynamic), _isUnwinding(false),
|
353 _terminated(false), _suspended(false), _disabled(false), 354 _syncReset(false), refCount(0), stackSize(::Fiber::DefaultStackSize), 355 dynamicSensitivity(nullptr) 356{ 357 _newest = this; 358} 359
| 360 _terminated(false), _suspended(false), _disabled(false), 361 _syncReset(false), refCount(0), stackSize(::Fiber::DefaultStackSize), 362 dynamicSensitivity(nullptr) 363{ 364 _newest = this; 365} 366
|
| 367void 368Process::terminate() 369{ 370 _terminated = true; 371 _suspendedReady = false; 372 _suspended = false; 373 _syncReset = false; 374 delete dynamicSensitivity; 375 dynamicSensitivity = nullptr; 376 for (auto s: staticSensitivities) 377 delete s; 378 staticSensitivities.clear(); 379} 380
|
360Process *Process::_newest; 361 362void 363throw_it_wrapper(Process *p, ExceptionWrapperBase &exc, bool inc_kids) 364{ 365 p->throw_it(exc, inc_kids); 366} 367 368} // namespace sc_gem5
| 381Process *Process::_newest; 382 383void 384throw_it_wrapper(Process *p, ExceptionWrapperBase &exc, bool inc_kids) 385{ 386 p->throw_it(exc, inc_kids); 387} 388 389} // namespace sc_gem5
|