process.cc revision 12999
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) 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 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 _terminatedEvent.notify(); 225} 226 227void 228Process::reset(bool inc_kids) 229{ 230 // Propogate the reset to our children no matter what happens to us. 231 if (inc_kids) 232 forEachKid([](Process *p) { p->reset(true); }); 233 234 // If we're in the middle of unwinding, ignore the reset request. 235 if (_isUnwinding) 236 return; 237 238 239 if (_needsStart) { 240 scheduler.runNow(this); 241 } else { 242 _isUnwinding = true; 243 injectException(resetException); 244 } 245 246 _resetEvent.notify(); 247} 248 249void 250Process::throw_it(ExceptionWrapperBase &exc, bool inc_kids) 251{ 252 if (inc_kids) 253 forEachKid([&exc](Process *p) { p->throw_it(exc, true); }); 254 255 // Only inject an exception into threads that have started. 256 if (!_needsStart) 257 injectException(exc); 258} 259 260void 261Process::injectException(ExceptionWrapperBase &exc) 262{ 263 excWrapper = &exc; 264 scheduler.runNow(this); 265}; 266 267void 268Process::syncResetOn(bool inc_kids) 269{ 270 if (inc_kids) 271 forEachKid([](Process *p) { p->syncResetOn(true); }); 272 273 _syncReset = true; 274} 275 276void 277Process::syncResetOff(bool inc_kids) 278{ 279 if (inc_kids) 280 forEachKid([](Process *p) { p->syncResetOff(true); }); 281 282 _syncReset = false; 283} 284 285void 286Process::dontInitialize() 287{ 288 scheduler.dontInitialize(this); 289} 290 291void 292Process::finalize() 293{ 294 for (auto &s: pendingStaticSensitivities) { 295 s->finalize(staticSensitivities); 296 delete s; 297 s = nullptr; 298 } 299 pendingStaticSensitivities.clear(); 300}; 301 302void 303Process::run() 304{ 305 bool reset; 306 do { 307 reset = false; 308 try { 309 func->call(); 310 } catch(const ::sc_core::sc_unwind_exception &exc) { 311 reset = exc.is_reset(); 312 _isUnwinding = false; 313 } 314 } while (reset); 315} 316 317void 318Process::addStatic(PendingSensitivity *s) 319{ 320 pendingStaticSensitivities.push_back(s); 321} 322 323void 324Process::setDynamic(Sensitivity *s) 325{ 326 delete dynamicSensitivity; 327 dynamicSensitivity = s; 328} 329 330void 331Process::satisfySensitivity(Sensitivity *s) 332{ 333 // If there's a dynamic sensitivity and this wasn't it, ignore. 334 if (dynamicSensitivity && dynamicSensitivity != s) 335 return; 336 337 setDynamic(nullptr); 338 ready(); 339} 340 341void 342Process::ready() 343{ 344 if (disabled()) 345 return; 346 if (suspended()) 347 _suspendedReady = true; 348 else 349 scheduler.ready(this); 350} 351 352void 353Process::lastReport(::sc_core::sc_report *report) 354{ 355 if (report) { 356 _lastReport = std::unique_ptr<::sc_core::sc_report>( 357 new ::sc_core::sc_report(*report)); 358 } else { 359 _lastReport = nullptr; 360 } 361} 362 363::sc_core::sc_report *Process::lastReport() const { return _lastReport.get(); } 364 365Process::Process(const char *name, ProcessFuncWrapper *func, bool _dynamic) : 366 ::sc_core::sc_object(name), excWrapper(nullptr), func(func), 367 _needsStart(true), _dynamic(_dynamic), _isUnwinding(false), 368 _terminated(false), _suspended(false), _disabled(false), 369 _syncReset(false), refCount(0), stackSize(::Fiber::DefaultStackSize), 370 dynamicSensitivity(nullptr) 371{ 372 _newest = this; 373} 374 375void 376Process::terminate() 377{ 378 _terminated = true; 379 _suspendedReady = false; 380 _suspended = false; 381 _syncReset = false; 382 delete dynamicSensitivity; 383 dynamicSensitivity = nullptr; 384 for (auto s: staticSensitivities) 385 delete s; 386 staticSensitivities.clear(); 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 398