process.cc revision 12962:004cc9133bd6
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 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 const char *what() const throw() override { return "RESET"; } 112 bool is_reset() const override { return true; } 113}; 114 115class UnwindExceptionKill : public ::sc_core::sc_unwind_exception 116{ 117 public: 118 const char *what() const throw() override { return "KILL"; } 119 bool is_reset() const override { return false; } 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 // Update our state. 198 _terminated = true; 199 _isUnwinding = true; 200 201 // Propogate the kill to our children no matter what happens to us. 202 if (inc_kids) 203 forEachKid([](Process *p) { p->kill(true); }); 204 205 // If we're in the middle of unwinding, ignore the kill request. 206 if (_isUnwinding) 207 return; 208 209 // Inject the kill exception into this process. 210 injectException(killException); 211 212 _terminatedEvent.notify(); 213} 214 215void 216Process::reset(bool inc_kids) 217{ 218 // Update our state. 219 _isUnwinding = true; 220 221 // Propogate the reset to our children no matter what happens to us. 222 if (inc_kids) 223 forEachKid([](Process *p) { p->reset(true); }); 224 225 // If we're in the middle of unwinding, ignore the reset request. 226 if (_isUnwinding) 227 return; 228 229 // Inject the reset exception into this process. 230 injectException(resetException); 231 232 _resetEvent.notify(); 233} 234 235void 236Process::throw_it(ExceptionWrapperBase &exc, bool inc_kids) 237{ 238 if (inc_kids) 239 forEachKid([&exc](Process *p) { p->throw_it(exc, true); }); 240} 241 242void 243Process::injectException(ExceptionWrapperBase &exc) 244{ 245 excWrapper = &exc; 246 // Let this process preempt us. 247}; 248 249void 250Process::syncResetOn(bool inc_kids) 251{ 252 if (inc_kids) 253 forEachKid([](Process *p) { p->syncResetOn(true); }); 254 255 _syncReset = true; 256} 257 258void 259Process::syncResetOff(bool inc_kids) 260{ 261 if (inc_kids) 262 forEachKid([](Process *p) { p->syncResetOff(true); }); 263 264 _syncReset = false; 265} 266 267void 268Process::dontInitialize() 269{ 270 scheduler.dontInitialize(this); 271} 272 273void 274Process::finalize() 275{ 276 for (auto &s: pendingStaticSensitivities) { 277 s->finalize(staticSensitivities); 278 delete s; 279 s = nullptr; 280 } 281 pendingStaticSensitivities.clear(); 282}; 283 284void 285Process::run() 286{ 287 bool reset; 288 do { 289 reset = false; 290 try { 291 func->call(); 292 } catch(::sc_core::sc_unwind_exception exc) { 293 reset = exc.is_reset(); 294 } 295 } while (reset); 296 _terminated = true; 297} 298 299void 300Process::addStatic(PendingSensitivity *s) 301{ 302 pendingStaticSensitivities.push_back(s); 303} 304 305void 306Process::setDynamic(Sensitivity *s) 307{ 308 delete dynamicSensitivity; 309 dynamicSensitivity = s; 310} 311 312void 313Process::satisfySensitivity(Sensitivity *s) 314{ 315 // If there's a dynamic sensitivity and this wasn't it, ignore. 316 if (dynamicSensitivity && dynamicSensitivity != s) 317 return; 318 319 setDynamic(nullptr); 320 ready(); 321} 322 323void 324Process::ready() 325{ 326 if (suspended()) 327 _suspendedReady = true; 328 else 329 scheduler.ready(this); 330} 331 332Process::Process(const char *name, ProcessFuncWrapper *func, 333 bool _dynamic, bool needs_start) : 334 ::sc_core::sc_object(name), excWrapper(nullptr), func(func), 335 _needsStart(needs_start), _dynamic(_dynamic), _isUnwinding(false), 336 _terminated(false), _suspended(false), _disabled(false), 337 _syncReset(false), refCount(0), stackSize(::Fiber::DefaultStackSize), 338 dynamicSensitivity(nullptr) 339{ 340 _newest = this; 341 if (_dynamic) 342 finalize(); 343 else 344 scheduler.reg(this); 345} 346 347Process *Process::_newest; 348 349void 350throw_it_wrapper(Process *p, ExceptionWrapperBase &exc, bool inc_kids) 351{ 352 p->throw_it(exc, inc_kids); 353} 354 355} // namespace sc_gem5 356