process.cc revision 12957:e54f9890363d
112841Sgabeblack@google.com/* 212841Sgabeblack@google.com * Copyright 2018 Google, Inc. 312841Sgabeblack@google.com * 412841Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 512841Sgabeblack@google.com * modification, are permitted provided that the following conditions are 612841Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 712841Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 812841Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 912841Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 1012841Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 1112841Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 1212841Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 1312841Sgabeblack@google.com * this software without specific prior written permission. 1412841Sgabeblack@google.com * 1512841Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1612841Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1712841Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1812841Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1912841Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2012841Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2112841Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2212841Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2312841Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2412841Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2512841Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2612841Sgabeblack@google.com * 2712841Sgabeblack@google.com * Authors: Gabe Black 2812841Sgabeblack@google.com */ 2912841Sgabeblack@google.com 3012841Sgabeblack@google.com#include "systemc/core/process.hh" 3112841Sgabeblack@google.com 3212841Sgabeblack@google.com#include "base/logging.hh" 3312841Sgabeblack@google.com#include "systemc/core/event.hh" 3412841Sgabeblack@google.com#include "systemc/core/scheduler.hh" 3512841Sgabeblack@google.com 3612841Sgabeblack@google.comnamespace sc_gem5 3712841Sgabeblack@google.com{ 3812841Sgabeblack@google.com 3912841Sgabeblack@google.comvoid 4012841Sgabeblack@google.comSensitivity::satisfy() 4112841Sgabeblack@google.com{ 4212841Sgabeblack@google.com warn_once("Ignoring suspended status for now.\n"); 4312841Sgabeblack@google.com process->setDynamic(nullptr); 4412841Sgabeblack@google.com scheduler.ready(process); 4512841Sgabeblack@google.com} 4612841Sgabeblack@google.com 4712841Sgabeblack@google.comSensitivityTimeout::SensitivityTimeout(Process *p, ::sc_core::sc_time t) : 4812841Sgabeblack@google.com Sensitivity(p), timeoutEvent(this), timeout(t) 4912841Sgabeblack@google.com{ 5012841Sgabeblack@google.com Tick when = scheduler.eventQueue().getCurTick() + timeout.value(); 5112841Sgabeblack@google.com scheduler.eventQueue().schedule(&timeoutEvent, when); 5212841Sgabeblack@google.com} 5312841Sgabeblack@google.com 5412841Sgabeblack@google.comSensitivityTimeout::~SensitivityTimeout() 5512841Sgabeblack@google.com{ 5612841Sgabeblack@google.com if (timeoutEvent.scheduled()) 5712841Sgabeblack@google.com scheduler.eventQueue().deschedule(&timeoutEvent); 5812841Sgabeblack@google.com} 5913214Sgabeblack@google.com 6012841Sgabeblack@google.comSensitivityEvent::SensitivityEvent( 6113214Sgabeblack@google.com Process *p, const ::sc_core::sc_event *e) : Sensitivity(p), event(e) 6212841Sgabeblack@google.com{ 6312841Sgabeblack@google.com Event::getFromScEvent(event)->addSensitivity(this); 6412841Sgabeblack@google.com} 6513214Sgabeblack@google.com 6612841Sgabeblack@google.comSensitivityEvent::~SensitivityEvent() 6713214Sgabeblack@google.com{ 6812841Sgabeblack@google.com Event::getFromScEvent(event)->delSensitivity(this); 6912841Sgabeblack@google.com} 7012841Sgabeblack@google.com 7113214Sgabeblack@google.comSensitivityEventAndList::SensitivityEventAndList( 7212841Sgabeblack@google.com Process *p, const ::sc_core::sc_event_and_list *list) : 7313214Sgabeblack@google.com Sensitivity(p), list(list), count(0) 7412841Sgabeblack@google.com{ 7512841Sgabeblack@google.com for (auto e: list->events) 7612841Sgabeblack@google.com Event::getFromScEvent(e)->addSensitivity(this); 7713214Sgabeblack@google.com} 7812841Sgabeblack@google.com 7913214Sgabeblack@google.comSensitivityEventAndList::~SensitivityEventAndList() 8012841Sgabeblack@google.com{ 8112841Sgabeblack@google.com for (auto e: list->events) 8212841Sgabeblack@google.com Event::getFromScEvent(e)->delSensitivity(this); 8313214Sgabeblack@google.com} 8412841Sgabeblack@google.com 8513214Sgabeblack@google.comvoid 8612841Sgabeblack@google.comSensitivityEventAndList::notifyWork(Event *e) 8712841Sgabeblack@google.com{ 8812841Sgabeblack@google.com e->delSensitivity(this); 8912841Sgabeblack@google.com count++; 9012841Sgabeblack@google.com if (count == list->events.size()) 9112841Sgabeblack@google.com satisfy(); 9212841Sgabeblack@google.com} 9312841Sgabeblack@google.com 9412841Sgabeblack@google.comSensitivityEventOrList::SensitivityEventOrList( 9512841Sgabeblack@google.com Process *p, const ::sc_core::sc_event_or_list *list) : 9612841Sgabeblack@google.com Sensitivity(p), list(list) 9712841Sgabeblack@google.com{ 9812841Sgabeblack@google.com for (auto e: list->events) 99 Event::getFromScEvent(e)->addSensitivity(this); 100} 101 102SensitivityEventOrList::~SensitivityEventOrList() 103{ 104 for (auto e: list->events) 105 Event::getFromScEvent(e)->delSensitivity(this); 106} 107 108 109class UnwindExceptionReset : public ::sc_core::sc_unwind_exception 110{ 111 public: 112 const char *what() const throw() override { return "RESET"; } 113 bool is_reset() const override { return true; } 114}; 115 116class UnwindExceptionKill : public ::sc_core::sc_unwind_exception 117{ 118 public: 119 const char *what() const throw() override { return "KILL"; } 120 bool is_reset() const override { return false; } 121}; 122 123template <typename T> 124struct BuiltinExceptionWrapper : public ExceptionWrapperBase 125{ 126 public: 127 T t; 128 void throw_it() override { throw t; } 129}; 130 131BuiltinExceptionWrapper<UnwindExceptionReset> resetException; 132BuiltinExceptionWrapper<UnwindExceptionKill> killException; 133 134 135void 136Process::forEachKid(const std::function<void(Process *)> &work) 137{ 138 for (auto &kid: get_child_objects()) { 139 Process *p_kid = dynamic_cast<Process *>(kid); 140 if (p_kid) 141 work(p_kid); 142 } 143} 144 145void 146Process::suspend(bool inc_kids) 147{ 148 if (inc_kids) 149 forEachKid([](Process *p) { p->suspend(true); }); 150 151 if (!_suspended) { 152 _suspended = true; 153 //TODO Suspend this process. 154 } 155 156 if (procKind() != ::sc_core::SC_METHOD_PROC_ && 157 scheduler.current() == this) { 158 scheduler.yield(); 159 } 160} 161 162void 163Process::resume(bool inc_kids) 164{ 165 if (inc_kids) 166 forEachKid([](Process *p) { p->resume(true); }); 167 168 if (_suspended) { 169 _suspended = false; 170 //TODO Resume this process. 171 } 172} 173 174void 175Process::disable(bool inc_kids) 176{ 177 if (inc_kids) 178 forEachKid([](Process *p) { p->disable(true); }); 179 180 _disabled = true; 181} 182 183void 184Process::enable(bool inc_kids) 185{ 186 187 if (inc_kids) 188 forEachKid([](Process *p) { p->enable(true); }); 189 190 _disabled = false; 191} 192 193void 194Process::kill(bool inc_kids) 195{ 196 // Update our state. 197 _terminated = true; 198 _isUnwinding = true; 199 200 // Propogate the kill to our children no matter what happens to us. 201 if (inc_kids) 202 forEachKid([](Process *p) { p->kill(true); }); 203 204 // If we're in the middle of unwinding, ignore the kill request. 205 if (_isUnwinding) 206 return; 207 208 // Inject the kill exception into this process. 209 injectException(killException); 210 211 _terminatedEvent.notify(); 212} 213 214void 215Process::reset(bool inc_kids) 216{ 217 // Update our state. 218 _isUnwinding = true; 219 220 // Propogate the reset to our children no matter what happens to us. 221 if (inc_kids) 222 forEachKid([](Process *p) { p->reset(true); }); 223 224 // If we're in the middle of unwinding, ignore the reset request. 225 if (_isUnwinding) 226 return; 227 228 // Inject the reset exception into this process. 229 injectException(resetException); 230 231 _resetEvent.notify(); 232} 233 234void 235Process::throw_it(ExceptionWrapperBase &exc, bool inc_kids) 236{ 237 if (inc_kids) 238 forEachKid([&exc](Process *p) { p->throw_it(exc, true); }); 239} 240 241void 242Process::injectException(ExceptionWrapperBase &exc) 243{ 244 excWrapper = &exc; 245 // Let this process preempt us. 246}; 247 248void 249Process::syncResetOn(bool inc_kids) 250{ 251 if (inc_kids) 252 forEachKid([](Process *p) { p->syncResetOn(true); }); 253 254 _syncReset = true; 255} 256 257void 258Process::syncResetOff(bool inc_kids) 259{ 260 if (inc_kids) 261 forEachKid([](Process *p) { p->syncResetOff(true); }); 262 263 _syncReset = false; 264} 265 266void 267Process::dontInitialize() 268{ 269 scheduler.dontInitialize(this); 270} 271 272void 273Process::finalize() 274{ 275 for (auto &s: pendingStaticSensitivities) { 276 s->finalize(staticSensitivities); 277 delete s; 278 s = nullptr; 279 } 280 pendingStaticSensitivities.clear(); 281}; 282 283void 284Process::run() 285{ 286 _running = true; 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 _running = false; 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 312Process::Process(const char *name, ProcessFuncWrapper *func, bool _dynamic) : 313 ::sc_core::sc_object(name), excWrapper(nullptr), func(func), 314 _running(false), _dynamic(_dynamic), _isUnwinding(false), 315 _terminated(false), _suspended(false), _disabled(false), 316 _syncReset(false), refCount(0), stackSize(::Fiber::DefaultStackSize), 317 dynamicSensitivity(nullptr) 318{ 319 _newest = this; 320 if (_dynamic) 321 finalize(); 322 else 323 scheduler.reg(this); 324} 325 326Process *Process::_newest; 327 328void 329throw_it_wrapper(Process *p, ExceptionWrapperBase &exc, bool inc_kids) 330{ 331 p->throw_it(exc, inc_kids); 332} 333 334} // namespace sc_gem5 335