process.cc revision 13087
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" 3512998Sgabeblack@google.com#include "systemc/ext/core/sc_process_handle.hh" 3612998Sgabeblack@google.com#include "systemc/ext/utils/sc_report_handler.hh" 3712952Sgabeblack@google.com 3812952Sgabeblack@google.comnamespace sc_gem5 3912952Sgabeblack@google.com{ 4012952Sgabeblack@google.com 4112957Sgabeblack@google.comSensitivityTimeout::SensitivityTimeout(Process *p, ::sc_core::sc_time t) : 4213063Sgabeblack@google.com Sensitivity(p), timeoutEvent([this]() { this->timeout(); }) 4312957Sgabeblack@google.com{ 4413063Sgabeblack@google.com scheduler.schedule(&timeoutEvent, t); 4512957Sgabeblack@google.com} 4612957Sgabeblack@google.com 4712957Sgabeblack@google.comSensitivityTimeout::~SensitivityTimeout() 4812957Sgabeblack@google.com{ 4912957Sgabeblack@google.com if (timeoutEvent.scheduled()) 5012962Sgabeblack@google.com scheduler.deschedule(&timeoutEvent); 5112962Sgabeblack@google.com} 5212962Sgabeblack@google.com 5312962Sgabeblack@google.comvoid 5412962Sgabeblack@google.comSensitivityTimeout::timeout() 5512962Sgabeblack@google.com{ 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 10713075Sgabeblack@google.comvoid 10813075Sgabeblack@google.comSensitivityTimeoutAndEventAndList::notifyWork(Event *e) 10913075Sgabeblack@google.com{ 11013075Sgabeblack@google.com if (e) { 11113075Sgabeblack@google.com // An event went off which must be part of the sc_event_and_list. 11213075Sgabeblack@google.com SensitivityEventAndList::notifyWork(e); 11313075Sgabeblack@google.com } else { 11413075Sgabeblack@google.com // There's no inciting event, so this must be a timeout. 11513075Sgabeblack@google.com SensitivityTimeout::notifyWork(e); 11613075Sgabeblack@google.com } 11713075Sgabeblack@google.com} 11813075Sgabeblack@google.com 11912957Sgabeblack@google.com 12012952Sgabeblack@google.comclass UnwindExceptionReset : public ::sc_core::sc_unwind_exception 12112952Sgabeblack@google.com{ 12212952Sgabeblack@google.com public: 12312995Sgabeblack@google.com UnwindExceptionReset() { _isReset = true; } 12412952Sgabeblack@google.com}; 12512952Sgabeblack@google.com 12612952Sgabeblack@google.comclass UnwindExceptionKill : public ::sc_core::sc_unwind_exception 12712952Sgabeblack@google.com{ 12812952Sgabeblack@google.com public: 12912995Sgabeblack@google.com UnwindExceptionKill() {} 13012952Sgabeblack@google.com}; 13112952Sgabeblack@google.com 13212952Sgabeblack@google.comtemplate <typename T> 13312952Sgabeblack@google.comstruct BuiltinExceptionWrapper : public ExceptionWrapperBase 13412952Sgabeblack@google.com{ 13512952Sgabeblack@google.com public: 13612952Sgabeblack@google.com T t; 13712952Sgabeblack@google.com void throw_it() override { throw t; } 13812952Sgabeblack@google.com}; 13912952Sgabeblack@google.com 14012952Sgabeblack@google.comBuiltinExceptionWrapper<UnwindExceptionReset> resetException; 14112952Sgabeblack@google.comBuiltinExceptionWrapper<UnwindExceptionKill> killException; 14212952Sgabeblack@google.com 14312952Sgabeblack@google.com 14412952Sgabeblack@google.comvoid 14512952Sgabeblack@google.comProcess::forEachKid(const std::function<void(Process *)> &work) 14612952Sgabeblack@google.com{ 14712952Sgabeblack@google.com for (auto &kid: get_child_objects()) { 14812952Sgabeblack@google.com Process *p_kid = dynamic_cast<Process *>(kid); 14912952Sgabeblack@google.com if (p_kid) 15012952Sgabeblack@google.com work(p_kid); 15112952Sgabeblack@google.com } 15212952Sgabeblack@google.com} 15312952Sgabeblack@google.com 15412952Sgabeblack@google.comvoid 15512952Sgabeblack@google.comProcess::suspend(bool inc_kids) 15612952Sgabeblack@google.com{ 15712952Sgabeblack@google.com if (inc_kids) 15812952Sgabeblack@google.com forEachKid([](Process *p) { p->suspend(true); }); 15912952Sgabeblack@google.com 16012952Sgabeblack@google.com if (!_suspended) { 16112952Sgabeblack@google.com _suspended = true; 16212959Sgabeblack@google.com _suspendedReady = false; 16312952Sgabeblack@google.com } 16412952Sgabeblack@google.com 16512953Sgabeblack@google.com if (procKind() != ::sc_core::SC_METHOD_PROC_ && 16612953Sgabeblack@google.com scheduler.current() == this) { 16712953Sgabeblack@google.com scheduler.yield(); 16812952Sgabeblack@google.com } 16912952Sgabeblack@google.com} 17012952Sgabeblack@google.com 17112952Sgabeblack@google.comvoid 17212952Sgabeblack@google.comProcess::resume(bool inc_kids) 17312952Sgabeblack@google.com{ 17412952Sgabeblack@google.com if (inc_kids) 17512952Sgabeblack@google.com forEachKid([](Process *p) { p->resume(true); }); 17612952Sgabeblack@google.com 17712952Sgabeblack@google.com if (_suspended) { 17812952Sgabeblack@google.com _suspended = false; 17912959Sgabeblack@google.com if (_suspendedReady) 18012959Sgabeblack@google.com ready(); 18112959Sgabeblack@google.com _suspendedReady = false; 18212952Sgabeblack@google.com } 18312952Sgabeblack@google.com} 18412952Sgabeblack@google.com 18512952Sgabeblack@google.comvoid 18612952Sgabeblack@google.comProcess::disable(bool inc_kids) 18712952Sgabeblack@google.com{ 18812952Sgabeblack@google.com if (inc_kids) 18912952Sgabeblack@google.com forEachKid([](Process *p) { p->disable(true); }); 19012952Sgabeblack@google.com 19112999Sgabeblack@google.com if (!::sc_core::sc_allow_process_control_corners && 19212999Sgabeblack@google.com dynamic_cast<SensitivityTimeout *>(dynamicSensitivity)) { 19312999Sgabeblack@google.com std::string message("attempt to disable a thread with timeout wait: "); 19412999Sgabeblack@google.com message += name(); 19512999Sgabeblack@google.com SC_REPORT_ERROR("Undefined process control interaction", 19612999Sgabeblack@google.com message.c_str()); 19712999Sgabeblack@google.com } 19812999Sgabeblack@google.com 19912952Sgabeblack@google.com _disabled = true; 20012952Sgabeblack@google.com} 20112952Sgabeblack@google.com 20212952Sgabeblack@google.comvoid 20312952Sgabeblack@google.comProcess::enable(bool inc_kids) 20412952Sgabeblack@google.com{ 20512952Sgabeblack@google.com 20612952Sgabeblack@google.com if (inc_kids) 20712952Sgabeblack@google.com forEachKid([](Process *p) { p->enable(true); }); 20812952Sgabeblack@google.com 20912952Sgabeblack@google.com _disabled = false; 21012952Sgabeblack@google.com} 21112952Sgabeblack@google.com 21212952Sgabeblack@google.comvoid 21312952Sgabeblack@google.comProcess::kill(bool inc_kids) 21412952Sgabeblack@google.com{ 21512952Sgabeblack@google.com // Propogate the kill to our children no matter what happens to us. 21612952Sgabeblack@google.com if (inc_kids) 21712952Sgabeblack@google.com forEachKid([](Process *p) { p->kill(true); }); 21812952Sgabeblack@google.com 21912952Sgabeblack@google.com // If we're in the middle of unwinding, ignore the kill request. 22012952Sgabeblack@google.com if (_isUnwinding) 22112952Sgabeblack@google.com return; 22212952Sgabeblack@google.com 22312995Sgabeblack@google.com // Update our state. 22412998Sgabeblack@google.com terminate(); 22512995Sgabeblack@google.com _isUnwinding = true; 22612995Sgabeblack@google.com 22712998Sgabeblack@google.com // Make sure this process isn't marked ready 22812998Sgabeblack@google.com popListNode(); 22912998Sgabeblack@google.com 23012998Sgabeblack@google.com // Inject the kill exception into this process if it's started. 23112998Sgabeblack@google.com if (!_needsStart) 23212998Sgabeblack@google.com injectException(killException); 23312952Sgabeblack@google.com} 23412952Sgabeblack@google.com 23512952Sgabeblack@google.comvoid 23612952Sgabeblack@google.comProcess::reset(bool inc_kids) 23712952Sgabeblack@google.com{ 23812952Sgabeblack@google.com // Propogate the reset to our children no matter what happens to us. 23912952Sgabeblack@google.com if (inc_kids) 24012952Sgabeblack@google.com forEachKid([](Process *p) { p->reset(true); }); 24112952Sgabeblack@google.com 24212952Sgabeblack@google.com // If we're in the middle of unwinding, ignore the reset request. 24312952Sgabeblack@google.com if (_isUnwinding) 24412952Sgabeblack@google.com return; 24512952Sgabeblack@google.com 24612995Sgabeblack@google.com 24712998Sgabeblack@google.com if (_needsStart) { 24812998Sgabeblack@google.com scheduler.runNow(this); 24912998Sgabeblack@google.com } else { 25012998Sgabeblack@google.com _isUnwinding = true; 25112998Sgabeblack@google.com injectException(resetException); 25212998Sgabeblack@google.com } 25312952Sgabeblack@google.com 25412952Sgabeblack@google.com _resetEvent.notify(); 25512952Sgabeblack@google.com} 25612952Sgabeblack@google.com 25712952Sgabeblack@google.comvoid 25812952Sgabeblack@google.comProcess::throw_it(ExceptionWrapperBase &exc, bool inc_kids) 25912952Sgabeblack@google.com{ 26012952Sgabeblack@google.com if (inc_kids) 26112952Sgabeblack@google.com forEachKid([&exc](Process *p) { p->throw_it(exc, true); }); 26212998Sgabeblack@google.com 26312998Sgabeblack@google.com // Only inject an exception into threads that have started. 26412998Sgabeblack@google.com if (!_needsStart) 26512998Sgabeblack@google.com injectException(exc); 26612952Sgabeblack@google.com} 26712952Sgabeblack@google.com 26812952Sgabeblack@google.comvoid 26912952Sgabeblack@google.comProcess::injectException(ExceptionWrapperBase &exc) 27012952Sgabeblack@google.com{ 27112952Sgabeblack@google.com excWrapper = &exc; 27212995Sgabeblack@google.com scheduler.runNow(this); 27312952Sgabeblack@google.com}; 27412952Sgabeblack@google.com 27512952Sgabeblack@google.comvoid 27612952Sgabeblack@google.comProcess::syncResetOn(bool inc_kids) 27712952Sgabeblack@google.com{ 27812952Sgabeblack@google.com if (inc_kids) 27912952Sgabeblack@google.com forEachKid([](Process *p) { p->syncResetOn(true); }); 28012952Sgabeblack@google.com 28112952Sgabeblack@google.com _syncReset = true; 28212952Sgabeblack@google.com} 28312952Sgabeblack@google.com 28412952Sgabeblack@google.comvoid 28512952Sgabeblack@google.comProcess::syncResetOff(bool inc_kids) 28612952Sgabeblack@google.com{ 28712952Sgabeblack@google.com if (inc_kids) 28812952Sgabeblack@google.com forEachKid([](Process *p) { p->syncResetOff(true); }); 28912952Sgabeblack@google.com 29012952Sgabeblack@google.com _syncReset = false; 29112952Sgabeblack@google.com} 29212952Sgabeblack@google.com 29312952Sgabeblack@google.comvoid 29412957Sgabeblack@google.comProcess::dontInitialize() 29512957Sgabeblack@google.com{ 29612957Sgabeblack@google.com scheduler.dontInitialize(this); 29712957Sgabeblack@google.com} 29812957Sgabeblack@google.com 29912957Sgabeblack@google.comvoid 30012957Sgabeblack@google.comProcess::finalize() 30112957Sgabeblack@google.com{ 30212957Sgabeblack@google.com for (auto &s: pendingStaticSensitivities) { 30312957Sgabeblack@google.com s->finalize(staticSensitivities); 30412957Sgabeblack@google.com delete s; 30512957Sgabeblack@google.com s = nullptr; 30612957Sgabeblack@google.com } 30712957Sgabeblack@google.com pendingStaticSensitivities.clear(); 30812957Sgabeblack@google.com}; 30912957Sgabeblack@google.com 31012957Sgabeblack@google.comvoid 31112953Sgabeblack@google.comProcess::run() 31212952Sgabeblack@google.com{ 31312953Sgabeblack@google.com bool reset; 31412953Sgabeblack@google.com do { 31512953Sgabeblack@google.com reset = false; 31612953Sgabeblack@google.com try { 31712953Sgabeblack@google.com func->call(); 31812995Sgabeblack@google.com } catch(const ::sc_core::sc_unwind_exception &exc) { 31912953Sgabeblack@google.com reset = exc.is_reset(); 32012995Sgabeblack@google.com _isUnwinding = false; 32112953Sgabeblack@google.com } 32212953Sgabeblack@google.com } while (reset); 32312953Sgabeblack@google.com} 32412952Sgabeblack@google.com 32512957Sgabeblack@google.comvoid 32612957Sgabeblack@google.comProcess::addStatic(PendingSensitivity *s) 32712957Sgabeblack@google.com{ 32812957Sgabeblack@google.com pendingStaticSensitivities.push_back(s); 32912957Sgabeblack@google.com} 33012957Sgabeblack@google.com 33112957Sgabeblack@google.comvoid 33212957Sgabeblack@google.comProcess::setDynamic(Sensitivity *s) 33312957Sgabeblack@google.com{ 33412957Sgabeblack@google.com delete dynamicSensitivity; 33512957Sgabeblack@google.com dynamicSensitivity = s; 33612957Sgabeblack@google.com} 33712957Sgabeblack@google.com 33812959Sgabeblack@google.comvoid 33912959Sgabeblack@google.comProcess::satisfySensitivity(Sensitivity *s) 34012959Sgabeblack@google.com{ 34112959Sgabeblack@google.com // If there's a dynamic sensitivity and this wasn't it, ignore. 34212959Sgabeblack@google.com if (dynamicSensitivity && dynamicSensitivity != s) 34312959Sgabeblack@google.com return; 34412959Sgabeblack@google.com 34512959Sgabeblack@google.com setDynamic(nullptr); 34612959Sgabeblack@google.com ready(); 34712959Sgabeblack@google.com} 34812959Sgabeblack@google.com 34912959Sgabeblack@google.comvoid 35012959Sgabeblack@google.comProcess::ready() 35112959Sgabeblack@google.com{ 35212996Sgabeblack@google.com if (disabled()) 35312996Sgabeblack@google.com return; 35412959Sgabeblack@google.com if (suspended()) 35512959Sgabeblack@google.com _suspendedReady = true; 35612959Sgabeblack@google.com else 35712959Sgabeblack@google.com scheduler.ready(this); 35812959Sgabeblack@google.com} 35912959Sgabeblack@google.com 36012997Sgabeblack@google.comvoid 36112997Sgabeblack@google.comProcess::lastReport(::sc_core::sc_report *report) 36212997Sgabeblack@google.com{ 36312997Sgabeblack@google.com if (report) { 36412997Sgabeblack@google.com _lastReport = std::unique_ptr<::sc_core::sc_report>( 36512997Sgabeblack@google.com new ::sc_core::sc_report(*report)); 36612997Sgabeblack@google.com } else { 36712997Sgabeblack@google.com _lastReport = nullptr; 36812997Sgabeblack@google.com } 36912997Sgabeblack@google.com} 37012997Sgabeblack@google.com 37112997Sgabeblack@google.com::sc_core::sc_report *Process::lastReport() const { return _lastReport.get(); } 37212997Sgabeblack@google.com 37312998Sgabeblack@google.comProcess::Process(const char *name, ProcessFuncWrapper *func, bool _dynamic) : 37413087Sgabeblack@google.com ::sc_core::sc_process_b(name), excWrapper(nullptr), func(func), 37512998Sgabeblack@google.com _needsStart(true), _dynamic(_dynamic), _isUnwinding(false), 37612953Sgabeblack@google.com _terminated(false), _suspended(false), _disabled(false), 37712957Sgabeblack@google.com _syncReset(false), refCount(0), stackSize(::Fiber::DefaultStackSize), 37812957Sgabeblack@google.com dynamicSensitivity(nullptr) 37912953Sgabeblack@google.com{ 38012953Sgabeblack@google.com _newest = this; 38112953Sgabeblack@google.com} 38212952Sgabeblack@google.com 38312998Sgabeblack@google.comvoid 38412998Sgabeblack@google.comProcess::terminate() 38512998Sgabeblack@google.com{ 38612998Sgabeblack@google.com _terminated = true; 38712998Sgabeblack@google.com _suspendedReady = false; 38812998Sgabeblack@google.com _suspended = false; 38912998Sgabeblack@google.com _syncReset = false; 39012998Sgabeblack@google.com delete dynamicSensitivity; 39112998Sgabeblack@google.com dynamicSensitivity = nullptr; 39212998Sgabeblack@google.com for (auto s: staticSensitivities) 39312998Sgabeblack@google.com delete s; 39412998Sgabeblack@google.com staticSensitivities.clear(); 39513006Sgabeblack@google.com 39613006Sgabeblack@google.com _terminatedEvent.notify(); 39712998Sgabeblack@google.com} 39812998Sgabeblack@google.com 39912953Sgabeblack@google.comProcess *Process::_newest; 40012952Sgabeblack@google.com 40112952Sgabeblack@google.comvoid 40212952Sgabeblack@google.comthrow_it_wrapper(Process *p, ExceptionWrapperBase &exc, bool inc_kids) 40312952Sgabeblack@google.com{ 40412952Sgabeblack@google.com p->throw_it(exc, inc_kids); 40512952Sgabeblack@google.com} 40612952Sgabeblack@google.com 40712952Sgabeblack@google.com} // namespace sc_gem5 408