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" 37 38namespace sc_gem5 39{ 40 41SensitivityTimeout::SensitivityTimeout(Process *p, ::sc_core::sc_time t) :
| 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" 37 38namespace sc_gem5 39{ 40 41SensitivityTimeout::SensitivityTimeout(Process *p, ::sc_core::sc_time t) :
|
42 Sensitivity(p), timeoutEvent(this), time(t)
| 42 Sensitivity(p), timeoutEvent([this]() { this->timeout(); })
|
43{
| 43{
|
44 Tick when = scheduler.getCurTick() + time.value(); 45 scheduler.schedule(&timeoutEvent, when);
| 44 scheduler.schedule(&timeoutEvent, t);
|
46} 47 48SensitivityTimeout::~SensitivityTimeout() 49{ 50 if (timeoutEvent.scheduled()) 51 scheduler.deschedule(&timeoutEvent); 52} 53 54void 55SensitivityTimeout::timeout() 56{
| 45} 46 47SensitivityTimeout::~SensitivityTimeout() 48{ 49 if (timeoutEvent.scheduled()) 50 scheduler.deschedule(&timeoutEvent); 51} 52 53void 54SensitivityTimeout::timeout() 55{
|
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 if (!::sc_core::sc_allow_process_control_corners && 182 dynamic_cast<SensitivityTimeout *>(dynamicSensitivity)) { 183 std::string message("attempt to disable a thread with timeout wait: "); 184 message += name(); 185 SC_REPORT_ERROR("Undefined process control interaction", 186 message.c_str()); 187 } 188 189 _disabled = true; 190} 191 192void 193Process::enable(bool inc_kids) 194{ 195 196 if (inc_kids) 197 forEachKid([](Process *p) { p->enable(true); }); 198 199 _disabled = false; 200} 201 202void 203Process::kill(bool inc_kids) 204{ 205 // Propogate the kill to our children no matter what happens to us. 206 if (inc_kids) 207 forEachKid([](Process *p) { p->kill(true); }); 208 209 // If we're in the middle of unwinding, ignore the kill request. 210 if (_isUnwinding) 211 return; 212 213 // Update our state. 214 terminate(); 215 _isUnwinding = true; 216 217 // Make sure this process isn't marked ready 218 popListNode(); 219 220 // Inject the kill exception into this process if it's started. 221 if (!_needsStart) 222 injectException(killException); 223} 224 225void 226Process::reset(bool inc_kids) 227{ 228 // Propogate the reset to our children no matter what happens to us. 229 if (inc_kids) 230 forEachKid([](Process *p) { p->reset(true); }); 231 232 // If we're in the middle of unwinding, ignore the reset request. 233 if (_isUnwinding) 234 return; 235 236 237 if (_needsStart) { 238 scheduler.runNow(this); 239 } else { 240 _isUnwinding = true; 241 injectException(resetException); 242 } 243 244 _resetEvent.notify(); 245} 246 247void 248Process::throw_it(ExceptionWrapperBase &exc, bool inc_kids) 249{ 250 if (inc_kids) 251 forEachKid([&exc](Process *p) { p->throw_it(exc, true); }); 252 253 // Only inject an exception into threads that have started. 254 if (!_needsStart) 255 injectException(exc); 256} 257 258void 259Process::injectException(ExceptionWrapperBase &exc) 260{ 261 excWrapper = &exc; 262 scheduler.runNow(this); 263}; 264 265void 266Process::syncResetOn(bool inc_kids) 267{ 268 if (inc_kids) 269 forEachKid([](Process *p) { p->syncResetOn(true); }); 270 271 _syncReset = true; 272} 273 274void 275Process::syncResetOff(bool inc_kids) 276{ 277 if (inc_kids) 278 forEachKid([](Process *p) { p->syncResetOff(true); }); 279 280 _syncReset = false; 281} 282 283void 284Process::dontInitialize() 285{ 286 scheduler.dontInitialize(this); 287} 288 289void 290Process::finalize() 291{ 292 for (auto &s: pendingStaticSensitivities) { 293 s->finalize(staticSensitivities); 294 delete s; 295 s = nullptr; 296 } 297 pendingStaticSensitivities.clear(); 298}; 299 300void 301Process::run() 302{ 303 bool reset; 304 do { 305 reset = false; 306 try { 307 func->call(); 308 } catch(const ::sc_core::sc_unwind_exception &exc) { 309 reset = exc.is_reset(); 310 _isUnwinding = false; 311 } 312 } while (reset); 313} 314 315void 316Process::addStatic(PendingSensitivity *s) 317{ 318 pendingStaticSensitivities.push_back(s); 319} 320 321void 322Process::setDynamic(Sensitivity *s) 323{ 324 delete dynamicSensitivity; 325 dynamicSensitivity = s; 326} 327 328void 329Process::satisfySensitivity(Sensitivity *s) 330{ 331 // If there's a dynamic sensitivity and this wasn't it, ignore. 332 if (dynamicSensitivity && dynamicSensitivity != s) 333 return; 334 335 setDynamic(nullptr); 336 ready(); 337} 338 339void 340Process::ready() 341{ 342 if (disabled()) 343 return; 344 if (suspended()) 345 _suspendedReady = true; 346 else 347 scheduler.ready(this); 348} 349 350void 351Process::lastReport(::sc_core::sc_report *report) 352{ 353 if (report) { 354 _lastReport = std::unique_ptr<::sc_core::sc_report>( 355 new ::sc_core::sc_report(*report)); 356 } else { 357 _lastReport = nullptr; 358 } 359} 360 361::sc_core::sc_report *Process::lastReport() const { return _lastReport.get(); } 362 363Process::Process(const char *name, ProcessFuncWrapper *func, bool _dynamic) : 364 ::sc_core::sc_object(name), excWrapper(nullptr), func(func), 365 _needsStart(true), _dynamic(_dynamic), _isUnwinding(false), 366 _terminated(false), _suspended(false), _disabled(false), 367 _syncReset(false), refCount(0), stackSize(::Fiber::DefaultStackSize), 368 dynamicSensitivity(nullptr) 369{ 370 _newest = this; 371} 372 373void 374Process::terminate() 375{ 376 _terminated = true; 377 _suspendedReady = false; 378 _suspended = false; 379 _syncReset = false; 380 delete dynamicSensitivity; 381 dynamicSensitivity = nullptr; 382 for (auto s: staticSensitivities) 383 delete s; 384 staticSensitivities.clear(); 385 386 _terminatedEvent.notify(); 387} 388 389Process *Process::_newest; 390 391void 392throw_it_wrapper(Process *p, ExceptionWrapperBase &exc, bool inc_kids) 393{ 394 p->throw_it(exc, inc_kids); 395} 396 397} // namespace sc_gem5
| 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 if (!::sc_core::sc_allow_process_control_corners && 180 dynamic_cast<SensitivityTimeout *>(dynamicSensitivity)) { 181 std::string message("attempt to disable a thread with timeout wait: "); 182 message += name(); 183 SC_REPORT_ERROR("Undefined process control interaction", 184 message.c_str()); 185 } 186 187 _disabled = true; 188} 189 190void 191Process::enable(bool inc_kids) 192{ 193 194 if (inc_kids) 195 forEachKid([](Process *p) { p->enable(true); }); 196 197 _disabled = false; 198} 199 200void 201Process::kill(bool inc_kids) 202{ 203 // Propogate the kill to our children no matter what happens to us. 204 if (inc_kids) 205 forEachKid([](Process *p) { p->kill(true); }); 206 207 // If we're in the middle of unwinding, ignore the kill request. 208 if (_isUnwinding) 209 return; 210 211 // Update our state. 212 terminate(); 213 _isUnwinding = true; 214 215 // Make sure this process isn't marked ready 216 popListNode(); 217 218 // Inject the kill exception into this process if it's started. 219 if (!_needsStart) 220 injectException(killException); 221} 222 223void 224Process::reset(bool inc_kids) 225{ 226 // Propogate the reset to our children no matter what happens to us. 227 if (inc_kids) 228 forEachKid([](Process *p) { p->reset(true); }); 229 230 // If we're in the middle of unwinding, ignore the reset request. 231 if (_isUnwinding) 232 return; 233 234 235 if (_needsStart) { 236 scheduler.runNow(this); 237 } else { 238 _isUnwinding = true; 239 injectException(resetException); 240 } 241 242 _resetEvent.notify(); 243} 244 245void 246Process::throw_it(ExceptionWrapperBase &exc, bool inc_kids) 247{ 248 if (inc_kids) 249 forEachKid([&exc](Process *p) { p->throw_it(exc, true); }); 250 251 // Only inject an exception into threads that have started. 252 if (!_needsStart) 253 injectException(exc); 254} 255 256void 257Process::injectException(ExceptionWrapperBase &exc) 258{ 259 excWrapper = &exc; 260 scheduler.runNow(this); 261}; 262 263void 264Process::syncResetOn(bool inc_kids) 265{ 266 if (inc_kids) 267 forEachKid([](Process *p) { p->syncResetOn(true); }); 268 269 _syncReset = true; 270} 271 272void 273Process::syncResetOff(bool inc_kids) 274{ 275 if (inc_kids) 276 forEachKid([](Process *p) { p->syncResetOff(true); }); 277 278 _syncReset = false; 279} 280 281void 282Process::dontInitialize() 283{ 284 scheduler.dontInitialize(this); 285} 286 287void 288Process::finalize() 289{ 290 for (auto &s: pendingStaticSensitivities) { 291 s->finalize(staticSensitivities); 292 delete s; 293 s = nullptr; 294 } 295 pendingStaticSensitivities.clear(); 296}; 297 298void 299Process::run() 300{ 301 bool reset; 302 do { 303 reset = false; 304 try { 305 func->call(); 306 } catch(const ::sc_core::sc_unwind_exception &exc) { 307 reset = exc.is_reset(); 308 _isUnwinding = false; 309 } 310 } while (reset); 311} 312 313void 314Process::addStatic(PendingSensitivity *s) 315{ 316 pendingStaticSensitivities.push_back(s); 317} 318 319void 320Process::setDynamic(Sensitivity *s) 321{ 322 delete dynamicSensitivity; 323 dynamicSensitivity = s; 324} 325 326void 327Process::satisfySensitivity(Sensitivity *s) 328{ 329 // If there's a dynamic sensitivity and this wasn't it, ignore. 330 if (dynamicSensitivity && dynamicSensitivity != s) 331 return; 332 333 setDynamic(nullptr); 334 ready(); 335} 336 337void 338Process::ready() 339{ 340 if (disabled()) 341 return; 342 if (suspended()) 343 _suspendedReady = true; 344 else 345 scheduler.ready(this); 346} 347 348void 349Process::lastReport(::sc_core::sc_report *report) 350{ 351 if (report) { 352 _lastReport = std::unique_ptr<::sc_core::sc_report>( 353 new ::sc_core::sc_report(*report)); 354 } else { 355 _lastReport = nullptr; 356 } 357} 358 359::sc_core::sc_report *Process::lastReport() const { return _lastReport.get(); } 360 361Process::Process(const char *name, ProcessFuncWrapper *func, bool _dynamic) : 362 ::sc_core::sc_object(name), excWrapper(nullptr), func(func), 363 _needsStart(true), _dynamic(_dynamic), _isUnwinding(false), 364 _terminated(false), _suspended(false), _disabled(false), 365 _syncReset(false), refCount(0), stackSize(::Fiber::DefaultStackSize), 366 dynamicSensitivity(nullptr) 367{ 368 _newest = this; 369} 370 371void 372Process::terminate() 373{ 374 _terminated = true; 375 _suspendedReady = false; 376 _suspended = false; 377 _syncReset = false; 378 delete dynamicSensitivity; 379 dynamicSensitivity = nullptr; 380 for (auto s: staticSensitivities) 381 delete s; 382 staticSensitivities.clear(); 383 384 _terminatedEvent.notify(); 385} 386 387Process *Process::_newest; 388 389void 390throw_it_wrapper(Process *p, ExceptionWrapperBase &exc, bool inc_kids) 391{ 392 p->throw_it(exc, inc_kids); 393} 394 395} // namespace sc_gem5
|