process.cc revision 12995
112952Sgabeblack@google.com/* 212952Sgabeblack@google.com * Copyright 2018 Google, Inc. 312952Sgabeblack@google.com * 412952Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 512952Sgabeblack@google.com * modification, are permitted provided that the following conditions are 612952Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 712952Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 812952Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 912952Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 1012952Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 1112952Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 1212952Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 1312952Sgabeblack@google.com * this software without specific prior written permission. 1412952Sgabeblack@google.com * 1512952Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1612952Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1712952Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1812952Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1912952Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2012952Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2112952Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2212952Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2312952Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2412952Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2512952Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2612952Sgabeblack@google.com * 2712952Sgabeblack@google.com * Authors: Gabe Black 2812952Sgabeblack@google.com */ 2912952Sgabeblack@google.com 3012952Sgabeblack@google.com#include "systemc/core/process.hh" 3112957Sgabeblack@google.com 3212957Sgabeblack@google.com#include "base/logging.hh" 3312957Sgabeblack@google.com#include "systemc/core/event.hh" 3412953Sgabeblack@google.com#include "systemc/core/scheduler.hh" 3512952Sgabeblack@google.com 3612952Sgabeblack@google.comnamespace sc_gem5 3712952Sgabeblack@google.com{ 3812952Sgabeblack@google.com 3912957Sgabeblack@google.comSensitivityTimeout::SensitivityTimeout(Process *p, ::sc_core::sc_time t) : 4012962Sgabeblack@google.com Sensitivity(p), timeoutEvent(this), time(t) 4112957Sgabeblack@google.com{ 4212962Sgabeblack@google.com Tick when = scheduler.getCurTick() + time.value(); 4312962Sgabeblack@google.com scheduler.schedule(&timeoutEvent, when); 4412957Sgabeblack@google.com} 4512957Sgabeblack@google.com 4612957Sgabeblack@google.comSensitivityTimeout::~SensitivityTimeout() 4712957Sgabeblack@google.com{ 4812957Sgabeblack@google.com if (timeoutEvent.scheduled()) 4912962Sgabeblack@google.com scheduler.deschedule(&timeoutEvent); 5012962Sgabeblack@google.com} 5112962Sgabeblack@google.com 5212962Sgabeblack@google.comvoid 5312962Sgabeblack@google.comSensitivityTimeout::timeout() 5412962Sgabeblack@google.com{ 5512962Sgabeblack@google.com scheduler.eventHappened(); 5612962Sgabeblack@google.com notify(); 5712957Sgabeblack@google.com} 5812957Sgabeblack@google.com 5912957Sgabeblack@google.comSensitivityEvent::SensitivityEvent( 6012957Sgabeblack@google.com Process *p, const ::sc_core::sc_event *e) : Sensitivity(p), event(e) 6112957Sgabeblack@google.com{ 6212957Sgabeblack@google.com Event::getFromScEvent(event)->addSensitivity(this); 6312957Sgabeblack@google.com} 6412957Sgabeblack@google.com 6512957Sgabeblack@google.comSensitivityEvent::~SensitivityEvent() 6612957Sgabeblack@google.com{ 6712957Sgabeblack@google.com Event::getFromScEvent(event)->delSensitivity(this); 6812957Sgabeblack@google.com} 6912957Sgabeblack@google.com 7012957Sgabeblack@google.comSensitivityEventAndList::SensitivityEventAndList( 7112957Sgabeblack@google.com Process *p, const ::sc_core::sc_event_and_list *list) : 7212957Sgabeblack@google.com Sensitivity(p), list(list), count(0) 7312957Sgabeblack@google.com{ 7412957Sgabeblack@google.com for (auto e: list->events) 7512957Sgabeblack@google.com Event::getFromScEvent(e)->addSensitivity(this); 7612957Sgabeblack@google.com} 7712957Sgabeblack@google.com 7812957Sgabeblack@google.comSensitivityEventAndList::~SensitivityEventAndList() 7912957Sgabeblack@google.com{ 8012957Sgabeblack@google.com for (auto e: list->events) 8112957Sgabeblack@google.com Event::getFromScEvent(e)->delSensitivity(this); 8212957Sgabeblack@google.com} 8312957Sgabeblack@google.com 8412957Sgabeblack@google.comvoid 8512957Sgabeblack@google.comSensitivityEventAndList::notifyWork(Event *e) 8612957Sgabeblack@google.com{ 8712957Sgabeblack@google.com e->delSensitivity(this); 8812957Sgabeblack@google.com count++; 8912957Sgabeblack@google.com if (count == list->events.size()) 9012959Sgabeblack@google.com process->satisfySensitivity(this); 9112957Sgabeblack@google.com} 9212957Sgabeblack@google.com 9312957Sgabeblack@google.comSensitivityEventOrList::SensitivityEventOrList( 9412957Sgabeblack@google.com Process *p, const ::sc_core::sc_event_or_list *list) : 9512957Sgabeblack@google.com Sensitivity(p), list(list) 9612957Sgabeblack@google.com{ 9712957Sgabeblack@google.com for (auto e: list->events) 9812957Sgabeblack@google.com Event::getFromScEvent(e)->addSensitivity(this); 9912957Sgabeblack@google.com} 10012957Sgabeblack@google.com 10112957Sgabeblack@google.comSensitivityEventOrList::~SensitivityEventOrList() 10212957Sgabeblack@google.com{ 10312957Sgabeblack@google.com for (auto e: list->events) 10412957Sgabeblack@google.com Event::getFromScEvent(e)->delSensitivity(this); 10512957Sgabeblack@google.com} 10612957Sgabeblack@google.com 10712957Sgabeblack@google.com 10812952Sgabeblack@google.comclass UnwindExceptionReset : public ::sc_core::sc_unwind_exception 10912952Sgabeblack@google.com{ 11012952Sgabeblack@google.com public: 11112995Sgabeblack@google.com UnwindExceptionReset() { _isReset = true; } 11212952Sgabeblack@google.com}; 11312952Sgabeblack@google.com 11412952Sgabeblack@google.comclass UnwindExceptionKill : public ::sc_core::sc_unwind_exception 11512952Sgabeblack@google.com{ 11612952Sgabeblack@google.com public: 11712995Sgabeblack@google.com UnwindExceptionKill() {} 11812952Sgabeblack@google.com}; 11912952Sgabeblack@google.com 12012952Sgabeblack@google.comtemplate <typename T> 12112952Sgabeblack@google.comstruct BuiltinExceptionWrapper : public ExceptionWrapperBase 12212952Sgabeblack@google.com{ 12312952Sgabeblack@google.com public: 12412952Sgabeblack@google.com T t; 12512952Sgabeblack@google.com void throw_it() override { throw t; } 12612952Sgabeblack@google.com}; 12712952Sgabeblack@google.com 12812952Sgabeblack@google.comBuiltinExceptionWrapper<UnwindExceptionReset> resetException; 12912952Sgabeblack@google.comBuiltinExceptionWrapper<UnwindExceptionKill> killException; 13012952Sgabeblack@google.com 13112952Sgabeblack@google.com 13212952Sgabeblack@google.comvoid 13312952Sgabeblack@google.comProcess::forEachKid(const std::function<void(Process *)> &work) 13412952Sgabeblack@google.com{ 13512952Sgabeblack@google.com for (auto &kid: get_child_objects()) { 13612952Sgabeblack@google.com Process *p_kid = dynamic_cast<Process *>(kid); 13712952Sgabeblack@google.com if (p_kid) 13812952Sgabeblack@google.com work(p_kid); 13912952Sgabeblack@google.com } 14012952Sgabeblack@google.com} 14112952Sgabeblack@google.com 14212952Sgabeblack@google.comvoid 14312952Sgabeblack@google.comProcess::suspend(bool inc_kids) 14412952Sgabeblack@google.com{ 14512952Sgabeblack@google.com if (inc_kids) 14612952Sgabeblack@google.com forEachKid([](Process *p) { p->suspend(true); }); 14712952Sgabeblack@google.com 14812952Sgabeblack@google.com if (!_suspended) { 14912952Sgabeblack@google.com _suspended = true; 15012959Sgabeblack@google.com _suspendedReady = false; 15112952Sgabeblack@google.com } 15212952Sgabeblack@google.com 15312953Sgabeblack@google.com if (procKind() != ::sc_core::SC_METHOD_PROC_ && 15412953Sgabeblack@google.com scheduler.current() == this) { 15512953Sgabeblack@google.com scheduler.yield(); 15612952Sgabeblack@google.com } 15712952Sgabeblack@google.com} 15812952Sgabeblack@google.com 15912952Sgabeblack@google.comvoid 16012952Sgabeblack@google.comProcess::resume(bool inc_kids) 16112952Sgabeblack@google.com{ 16212952Sgabeblack@google.com if (inc_kids) 16312952Sgabeblack@google.com forEachKid([](Process *p) { p->resume(true); }); 16412952Sgabeblack@google.com 16512952Sgabeblack@google.com if (_suspended) { 16612952Sgabeblack@google.com _suspended = false; 16712959Sgabeblack@google.com if (_suspendedReady) 16812959Sgabeblack@google.com ready(); 16912959Sgabeblack@google.com _suspendedReady = false; 17012952Sgabeblack@google.com } 17112952Sgabeblack@google.com} 17212952Sgabeblack@google.com 17312952Sgabeblack@google.comvoid 17412952Sgabeblack@google.comProcess::disable(bool inc_kids) 17512952Sgabeblack@google.com{ 17612952Sgabeblack@google.com if (inc_kids) 17712952Sgabeblack@google.com forEachKid([](Process *p) { p->disable(true); }); 17812952Sgabeblack@google.com 17912952Sgabeblack@google.com _disabled = true; 18012952Sgabeblack@google.com} 18112952Sgabeblack@google.com 18212952Sgabeblack@google.comvoid 18312952Sgabeblack@google.comProcess::enable(bool inc_kids) 18412952Sgabeblack@google.com{ 18512952Sgabeblack@google.com 18612952Sgabeblack@google.com if (inc_kids) 18712952Sgabeblack@google.com forEachKid([](Process *p) { p->enable(true); }); 18812952Sgabeblack@google.com 18912952Sgabeblack@google.com _disabled = false; 19012952Sgabeblack@google.com} 19112952Sgabeblack@google.com 19212952Sgabeblack@google.comvoid 19312952Sgabeblack@google.comProcess::kill(bool inc_kids) 19412952Sgabeblack@google.com{ 19512952Sgabeblack@google.com // Propogate the kill to our children no matter what happens to us. 19612952Sgabeblack@google.com if (inc_kids) 19712952Sgabeblack@google.com forEachKid([](Process *p) { p->kill(true); }); 19812952Sgabeblack@google.com 19912952Sgabeblack@google.com // If we're in the middle of unwinding, ignore the kill request. 20012952Sgabeblack@google.com if (_isUnwinding) 20112952Sgabeblack@google.com return; 20212952Sgabeblack@google.com 20312995Sgabeblack@google.com // Update our state. 20412995Sgabeblack@google.com _terminated = true; 20512995Sgabeblack@google.com _isUnwinding = true; 20612995Sgabeblack@google.com _suspendedReady = false; 20712995Sgabeblack@google.com _suspended = false; 20812995Sgabeblack@google.com _syncReset = false; 20912995Sgabeblack@google.com 21012952Sgabeblack@google.com // Inject the kill exception into this process. 21112952Sgabeblack@google.com injectException(killException); 21212952Sgabeblack@google.com 21312952Sgabeblack@google.com _terminatedEvent.notify(); 21412952Sgabeblack@google.com} 21512952Sgabeblack@google.com 21612952Sgabeblack@google.comvoid 21712952Sgabeblack@google.comProcess::reset(bool inc_kids) 21812952Sgabeblack@google.com{ 21912952Sgabeblack@google.com // Propogate the reset to our children no matter what happens to us. 22012952Sgabeblack@google.com if (inc_kids) 22112952Sgabeblack@google.com forEachKid([](Process *p) { p->reset(true); }); 22212952Sgabeblack@google.com 22312952Sgabeblack@google.com // If we're in the middle of unwinding, ignore the reset request. 22412952Sgabeblack@google.com if (_isUnwinding) 22512952Sgabeblack@google.com return; 22612952Sgabeblack@google.com 22712995Sgabeblack@google.com // Update our state. 22812995Sgabeblack@google.com _isUnwinding = true; 22912995Sgabeblack@google.com 23012952Sgabeblack@google.com // Inject the reset exception into this process. 23112952Sgabeblack@google.com injectException(resetException); 23212952Sgabeblack@google.com 23312952Sgabeblack@google.com _resetEvent.notify(); 23412952Sgabeblack@google.com} 23512952Sgabeblack@google.com 23612952Sgabeblack@google.comvoid 23712952Sgabeblack@google.comProcess::throw_it(ExceptionWrapperBase &exc, bool inc_kids) 23812952Sgabeblack@google.com{ 23912952Sgabeblack@google.com if (inc_kids) 24012952Sgabeblack@google.com forEachKid([&exc](Process *p) { p->throw_it(exc, true); }); 24112952Sgabeblack@google.com} 24212952Sgabeblack@google.com 24312952Sgabeblack@google.comvoid 24412952Sgabeblack@google.comProcess::injectException(ExceptionWrapperBase &exc) 24512952Sgabeblack@google.com{ 24612952Sgabeblack@google.com excWrapper = &exc; 24712995Sgabeblack@google.com scheduler.runNow(this); 24812952Sgabeblack@google.com}; 24912952Sgabeblack@google.com 25012952Sgabeblack@google.comvoid 25112952Sgabeblack@google.comProcess::syncResetOn(bool inc_kids) 25212952Sgabeblack@google.com{ 25312952Sgabeblack@google.com if (inc_kids) 25412952Sgabeblack@google.com forEachKid([](Process *p) { p->syncResetOn(true); }); 25512952Sgabeblack@google.com 25612952Sgabeblack@google.com _syncReset = true; 25712952Sgabeblack@google.com} 25812952Sgabeblack@google.com 25912952Sgabeblack@google.comvoid 26012952Sgabeblack@google.comProcess::syncResetOff(bool inc_kids) 26112952Sgabeblack@google.com{ 26212952Sgabeblack@google.com if (inc_kids) 26312952Sgabeblack@google.com forEachKid([](Process *p) { p->syncResetOff(true); }); 26412952Sgabeblack@google.com 26512952Sgabeblack@google.com _syncReset = false; 26612952Sgabeblack@google.com} 26712952Sgabeblack@google.com 26812952Sgabeblack@google.comvoid 26912957Sgabeblack@google.comProcess::dontInitialize() 27012957Sgabeblack@google.com{ 27112957Sgabeblack@google.com scheduler.dontInitialize(this); 27212957Sgabeblack@google.com} 27312957Sgabeblack@google.com 27412957Sgabeblack@google.comvoid 27512957Sgabeblack@google.comProcess::finalize() 27612957Sgabeblack@google.com{ 27712957Sgabeblack@google.com for (auto &s: pendingStaticSensitivities) { 27812957Sgabeblack@google.com s->finalize(staticSensitivities); 27912957Sgabeblack@google.com delete s; 28012957Sgabeblack@google.com s = nullptr; 28112957Sgabeblack@google.com } 28212957Sgabeblack@google.com pendingStaticSensitivities.clear(); 28312957Sgabeblack@google.com}; 28412957Sgabeblack@google.com 28512957Sgabeblack@google.comvoid 28612953Sgabeblack@google.comProcess::run() 28712952Sgabeblack@google.com{ 28812953Sgabeblack@google.com bool reset; 28912953Sgabeblack@google.com do { 29012953Sgabeblack@google.com reset = false; 29112953Sgabeblack@google.com try { 29212953Sgabeblack@google.com func->call(); 29312995Sgabeblack@google.com } catch(const ::sc_core::sc_unwind_exception &exc) { 29412953Sgabeblack@google.com reset = exc.is_reset(); 29512995Sgabeblack@google.com _isUnwinding = false; 29612953Sgabeblack@google.com } 29712953Sgabeblack@google.com } while (reset); 29812961Sgabeblack@google.com _terminated = true; 29912953Sgabeblack@google.com} 30012952Sgabeblack@google.com 30112957Sgabeblack@google.comvoid 30212957Sgabeblack@google.comProcess::addStatic(PendingSensitivity *s) 30312957Sgabeblack@google.com{ 30412957Sgabeblack@google.com pendingStaticSensitivities.push_back(s); 30512957Sgabeblack@google.com} 30612957Sgabeblack@google.com 30712957Sgabeblack@google.comvoid 30812957Sgabeblack@google.comProcess::setDynamic(Sensitivity *s) 30912957Sgabeblack@google.com{ 31012957Sgabeblack@google.com delete dynamicSensitivity; 31112957Sgabeblack@google.com dynamicSensitivity = s; 31212957Sgabeblack@google.com} 31312957Sgabeblack@google.com 31412959Sgabeblack@google.comvoid 31512959Sgabeblack@google.comProcess::satisfySensitivity(Sensitivity *s) 31612959Sgabeblack@google.com{ 31712959Sgabeblack@google.com // If there's a dynamic sensitivity and this wasn't it, ignore. 31812959Sgabeblack@google.com if (dynamicSensitivity && dynamicSensitivity != s) 31912959Sgabeblack@google.com return; 32012959Sgabeblack@google.com 32112959Sgabeblack@google.com setDynamic(nullptr); 32212959Sgabeblack@google.com ready(); 32312959Sgabeblack@google.com} 32412959Sgabeblack@google.com 32512959Sgabeblack@google.comvoid 32612959Sgabeblack@google.comProcess::ready() 32712959Sgabeblack@google.com{ 32812959Sgabeblack@google.com if (suspended()) 32912959Sgabeblack@google.com _suspendedReady = true; 33012959Sgabeblack@google.com else 33112959Sgabeblack@google.com scheduler.ready(this); 33212959Sgabeblack@google.com} 33312959Sgabeblack@google.com 33412961Sgabeblack@google.comProcess::Process(const char *name, ProcessFuncWrapper *func, 33512961Sgabeblack@google.com bool _dynamic, bool needs_start) : 33612953Sgabeblack@google.com ::sc_core::sc_object(name), excWrapper(nullptr), func(func), 33712961Sgabeblack@google.com _needsStart(needs_start), _dynamic(_dynamic), _isUnwinding(false), 33812953Sgabeblack@google.com _terminated(false), _suspended(false), _disabled(false), 33912957Sgabeblack@google.com _syncReset(false), refCount(0), stackSize(::Fiber::DefaultStackSize), 34012957Sgabeblack@google.com dynamicSensitivity(nullptr) 34112953Sgabeblack@google.com{ 34212953Sgabeblack@google.com _newest = this; 34312953Sgabeblack@google.com} 34412952Sgabeblack@google.com 34512953Sgabeblack@google.comProcess *Process::_newest; 34612952Sgabeblack@google.com 34712952Sgabeblack@google.comvoid 34812952Sgabeblack@google.comthrow_it_wrapper(Process *p, ExceptionWrapperBase &exc, bool inc_kids) 34912952Sgabeblack@google.com{ 35012952Sgabeblack@google.com p->throw_it(exc, inc_kids); 35112952Sgabeblack@google.com} 35212952Sgabeblack@google.com 35312952Sgabeblack@google.com} // namespace sc_gem5 354