process.cc revision 13206
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"
3513196Sgabeblack@google.com#include "systemc/ext/core/sc_join.hh"
3613102Sgabeblack@google.com#include "systemc/ext/core/sc_main.hh"
3712998Sgabeblack@google.com#include "systemc/ext/core/sc_process_handle.hh"
3812998Sgabeblack@google.com#include "systemc/ext/utils/sc_report_handler.hh"
3912952Sgabeblack@google.com
4012952Sgabeblack@google.comnamespace sc_gem5
4112952Sgabeblack@google.com{
4212952Sgabeblack@google.com
4312952Sgabeblack@google.comclass UnwindExceptionReset : public ::sc_core::sc_unwind_exception
4412952Sgabeblack@google.com{
4512952Sgabeblack@google.com  public:
4612995Sgabeblack@google.com    UnwindExceptionReset() { _isReset = true; }
4712952Sgabeblack@google.com};
4812952Sgabeblack@google.com
4912952Sgabeblack@google.comclass UnwindExceptionKill : public ::sc_core::sc_unwind_exception
5012952Sgabeblack@google.com{
5112952Sgabeblack@google.com  public:
5212995Sgabeblack@google.com    UnwindExceptionKill() {}
5312952Sgabeblack@google.com};
5412952Sgabeblack@google.com
5512952Sgabeblack@google.comtemplate <typename T>
5612952Sgabeblack@google.comstruct BuiltinExceptionWrapper : public ExceptionWrapperBase
5712952Sgabeblack@google.com{
5812952Sgabeblack@google.com  public:
5912952Sgabeblack@google.com    T t;
6012952Sgabeblack@google.com    void throw_it() override { throw t; }
6112952Sgabeblack@google.com};
6212952Sgabeblack@google.com
6312952Sgabeblack@google.comBuiltinExceptionWrapper<UnwindExceptionReset> resetException;
6412952Sgabeblack@google.comBuiltinExceptionWrapper<UnwindExceptionKill> killException;
6512952Sgabeblack@google.com
6612952Sgabeblack@google.com
6712952Sgabeblack@google.comvoid
6812952Sgabeblack@google.comProcess::forEachKid(const std::function<void(Process *)> &work)
6912952Sgabeblack@google.com{
7012952Sgabeblack@google.com    for (auto &kid: get_child_objects()) {
7112952Sgabeblack@google.com        Process *p_kid = dynamic_cast<Process *>(kid);
7212952Sgabeblack@google.com        if (p_kid)
7312952Sgabeblack@google.com            work(p_kid);
7412952Sgabeblack@google.com    }
7512952Sgabeblack@google.com}
7612952Sgabeblack@google.com
7712952Sgabeblack@google.comvoid
7812952Sgabeblack@google.comProcess::suspend(bool inc_kids)
7912952Sgabeblack@google.com{
8012952Sgabeblack@google.com    if (inc_kids)
8112952Sgabeblack@google.com        forEachKid([](Process *p) { p->suspend(true); });
8212952Sgabeblack@google.com
8312952Sgabeblack@google.com    if (!_suspended) {
8412952Sgabeblack@google.com        _suspended = true;
8513133Sgabeblack@google.com        _suspendedReady = scheduler.suspend(this);
8612952Sgabeblack@google.com
8713133Sgabeblack@google.com        if (procKind() != ::sc_core::SC_METHOD_PROC_ &&
8813133Sgabeblack@google.com                scheduler.current() == this) {
8913133Sgabeblack@google.com            // This isn't in the spec, but Accellera says that a thread that
9013133Sgabeblack@google.com            // self suspends should be marked ready immediately when it's
9113133Sgabeblack@google.com            // resumed.
9213133Sgabeblack@google.com            _suspendedReady = true;
9313133Sgabeblack@google.com            scheduler.yield();
9413133Sgabeblack@google.com        }
9512952Sgabeblack@google.com    }
9612952Sgabeblack@google.com}
9712952Sgabeblack@google.com
9812952Sgabeblack@google.comvoid
9912952Sgabeblack@google.comProcess::resume(bool inc_kids)
10012952Sgabeblack@google.com{
10112952Sgabeblack@google.com    if (inc_kids)
10212952Sgabeblack@google.com        forEachKid([](Process *p) { p->resume(true); });
10312952Sgabeblack@google.com
10412952Sgabeblack@google.com    if (_suspended) {
10512952Sgabeblack@google.com        _suspended = false;
10612959Sgabeblack@google.com        if (_suspendedReady)
10713133Sgabeblack@google.com            scheduler.resume(this);
10812959Sgabeblack@google.com        _suspendedReady = false;
10912952Sgabeblack@google.com    }
11012952Sgabeblack@google.com}
11112952Sgabeblack@google.com
11212952Sgabeblack@google.comvoid
11312952Sgabeblack@google.comProcess::disable(bool inc_kids)
11412952Sgabeblack@google.com{
11512952Sgabeblack@google.com    if (inc_kids)
11612952Sgabeblack@google.com        forEachKid([](Process *p) { p->disable(true); });
11712952Sgabeblack@google.com
11812999Sgabeblack@google.com    if (!::sc_core::sc_allow_process_control_corners &&
11913206Sgabeblack@google.com            timeoutEvent.scheduled()) {
12012999Sgabeblack@google.com        std::string message("attempt to disable a thread with timeout wait: ");
12112999Sgabeblack@google.com        message += name();
12212999Sgabeblack@google.com        SC_REPORT_ERROR("Undefined process control interaction",
12312999Sgabeblack@google.com                message.c_str());
12412999Sgabeblack@google.com    }
12512999Sgabeblack@google.com
12612952Sgabeblack@google.com    _disabled = true;
12712952Sgabeblack@google.com}
12812952Sgabeblack@google.com
12912952Sgabeblack@google.comvoid
13012952Sgabeblack@google.comProcess::enable(bool inc_kids)
13112952Sgabeblack@google.com{
13212952Sgabeblack@google.com
13312952Sgabeblack@google.com    if (inc_kids)
13412952Sgabeblack@google.com        forEachKid([](Process *p) { p->enable(true); });
13512952Sgabeblack@google.com
13612952Sgabeblack@google.com    _disabled = false;
13712952Sgabeblack@google.com}
13812952Sgabeblack@google.com
13912952Sgabeblack@google.comvoid
14012952Sgabeblack@google.comProcess::kill(bool inc_kids)
14112952Sgabeblack@google.com{
14213102Sgabeblack@google.com    if (::sc_core::sc_get_status() != ::sc_core::SC_RUNNING) {
14313102Sgabeblack@google.com        SC_REPORT_ERROR(
14413102Sgabeblack@google.com                "(E572) a process may not be killed before it is initialized",
14513102Sgabeblack@google.com                name());
14613102Sgabeblack@google.com    }
14713102Sgabeblack@google.com
14812952Sgabeblack@google.com    // Propogate the kill to our children no matter what happens to us.
14912952Sgabeblack@google.com    if (inc_kids)
15012952Sgabeblack@google.com        forEachKid([](Process *p) { p->kill(true); });
15112952Sgabeblack@google.com
15212952Sgabeblack@google.com    // If we're in the middle of unwinding, ignore the kill request.
15312952Sgabeblack@google.com    if (_isUnwinding)
15412952Sgabeblack@google.com        return;
15512952Sgabeblack@google.com
15612995Sgabeblack@google.com    // Update our state.
15712998Sgabeblack@google.com    terminate();
15812995Sgabeblack@google.com    _isUnwinding = true;
15912995Sgabeblack@google.com
16012998Sgabeblack@google.com    // Make sure this process isn't marked ready
16112998Sgabeblack@google.com    popListNode();
16212998Sgabeblack@google.com
16312998Sgabeblack@google.com    // Inject the kill exception into this process if it's started.
16412998Sgabeblack@google.com    if (!_needsStart)
16512998Sgabeblack@google.com        injectException(killException);
16612952Sgabeblack@google.com}
16712952Sgabeblack@google.com
16812952Sgabeblack@google.comvoid
16912952Sgabeblack@google.comProcess::reset(bool inc_kids)
17012952Sgabeblack@google.com{
17113102Sgabeblack@google.com    if (::sc_core::sc_get_status() != ::sc_core::SC_RUNNING) {
17213102Sgabeblack@google.com        SC_REPORT_ERROR(
17313102Sgabeblack@google.com                "(E573) a process may not be asynchronously reset while"
17413102Sgabeblack@google.com                "the simulation is not running", name());
17513102Sgabeblack@google.com    }
17613102Sgabeblack@google.com
17712952Sgabeblack@google.com    // Propogate the reset to our children no matter what happens to us.
17812952Sgabeblack@google.com    if (inc_kids)
17912952Sgabeblack@google.com        forEachKid([](Process *p) { p->reset(true); });
18012952Sgabeblack@google.com
18112952Sgabeblack@google.com    // If we're in the middle of unwinding, ignore the reset request.
18212952Sgabeblack@google.com    if (_isUnwinding)
18312952Sgabeblack@google.com        return;
18412952Sgabeblack@google.com
18512995Sgabeblack@google.com
18612998Sgabeblack@google.com    if (_needsStart) {
18712998Sgabeblack@google.com        scheduler.runNow(this);
18812998Sgabeblack@google.com    } else {
18912998Sgabeblack@google.com        _isUnwinding = true;
19012998Sgabeblack@google.com        injectException(resetException);
19112998Sgabeblack@google.com    }
19212952Sgabeblack@google.com
19312952Sgabeblack@google.com    _resetEvent.notify();
19412952Sgabeblack@google.com}
19512952Sgabeblack@google.com
19612952Sgabeblack@google.comvoid
19712952Sgabeblack@google.comProcess::throw_it(ExceptionWrapperBase &exc, bool inc_kids)
19812952Sgabeblack@google.com{
19913102Sgabeblack@google.com    if (::sc_core::sc_get_status() != ::sc_core::SC_RUNNING) {
20013102Sgabeblack@google.com        SC_REPORT_ERROR(
20113102Sgabeblack@google.com                "(E574) throw_it not allowed unless simulation is running ",
20213102Sgabeblack@google.com                name());
20313102Sgabeblack@google.com    }
20413102Sgabeblack@google.com
20512952Sgabeblack@google.com    if (inc_kids)
20612952Sgabeblack@google.com        forEachKid([&exc](Process *p) { p->throw_it(exc, true); });
20712998Sgabeblack@google.com
20812998Sgabeblack@google.com    // Only inject an exception into threads that have started.
20912998Sgabeblack@google.com    if (!_needsStart)
21012998Sgabeblack@google.com        injectException(exc);
21112952Sgabeblack@google.com}
21212952Sgabeblack@google.com
21312952Sgabeblack@google.comvoid
21412952Sgabeblack@google.comProcess::injectException(ExceptionWrapperBase &exc)
21512952Sgabeblack@google.com{
21612952Sgabeblack@google.com    excWrapper = &exc;
21712995Sgabeblack@google.com    scheduler.runNow(this);
21812952Sgabeblack@google.com};
21912952Sgabeblack@google.com
22012952Sgabeblack@google.comvoid
22112952Sgabeblack@google.comProcess::syncResetOn(bool inc_kids)
22212952Sgabeblack@google.com{
22312952Sgabeblack@google.com    if (inc_kids)
22412952Sgabeblack@google.com        forEachKid([](Process *p) { p->syncResetOn(true); });
22512952Sgabeblack@google.com
22612952Sgabeblack@google.com    _syncReset = true;
22712952Sgabeblack@google.com}
22812952Sgabeblack@google.com
22912952Sgabeblack@google.comvoid
23012952Sgabeblack@google.comProcess::syncResetOff(bool inc_kids)
23112952Sgabeblack@google.com{
23212952Sgabeblack@google.com    if (inc_kids)
23312952Sgabeblack@google.com        forEachKid([](Process *p) { p->syncResetOff(true); });
23412952Sgabeblack@google.com
23512952Sgabeblack@google.com    _syncReset = false;
23612952Sgabeblack@google.com}
23712952Sgabeblack@google.com
23812952Sgabeblack@google.comvoid
23912957Sgabeblack@google.comProcess::finalize()
24012957Sgabeblack@google.com{
24113206Sgabeblack@google.com    for (auto s: staticSensitivities)
24213206Sgabeblack@google.com        s->finalize();
24312957Sgabeblack@google.com};
24412957Sgabeblack@google.com
24512957Sgabeblack@google.comvoid
24612953Sgabeblack@google.comProcess::run()
24712952Sgabeblack@google.com{
24812953Sgabeblack@google.com    bool reset;
24912953Sgabeblack@google.com    do {
25012953Sgabeblack@google.com        reset = false;
25112953Sgabeblack@google.com        try {
25212953Sgabeblack@google.com            func->call();
25313175Sgabeblack@google.com        } catch(ScHalt) {
25413175Sgabeblack@google.com            std::cout << "Terminating process " << name() << std::endl;
25512995Sgabeblack@google.com        } catch(const ::sc_core::sc_unwind_exception &exc) {
25612953Sgabeblack@google.com            reset = exc.is_reset();
25712995Sgabeblack@google.com            _isUnwinding = false;
25813182Sgabeblack@google.com        } catch (...) {
25913182Sgabeblack@google.com            throw;
26012953Sgabeblack@google.com        }
26112953Sgabeblack@google.com    } while (reset);
26213093Sgabeblack@google.com    needsStart(true);
26312953Sgabeblack@google.com}
26412952Sgabeblack@google.com
26512957Sgabeblack@google.comvoid
26613206Sgabeblack@google.comProcess::addStatic(StaticSensitivity *s)
26712957Sgabeblack@google.com{
26813206Sgabeblack@google.com    staticSensitivities.push_back(s);
26912957Sgabeblack@google.com}
27012957Sgabeblack@google.com
27112957Sgabeblack@google.comvoid
27213206Sgabeblack@google.comProcess::setDynamic(DynamicSensitivity *s)
27312957Sgabeblack@google.com{
27413206Sgabeblack@google.com    if (dynamicSensitivity) {
27513206Sgabeblack@google.com        dynamicSensitivity->clear();
27613206Sgabeblack@google.com        delete dynamicSensitivity;
27713206Sgabeblack@google.com    }
27812957Sgabeblack@google.com    dynamicSensitivity = s;
27913206Sgabeblack@google.com    if (dynamicSensitivity)
28013206Sgabeblack@google.com        dynamicSensitivity->finalize();
28113206Sgabeblack@google.com}
28213206Sgabeblack@google.com
28313206Sgabeblack@google.comvoid
28413206Sgabeblack@google.comProcess::cancelTimeout()
28513206Sgabeblack@google.com{
28613206Sgabeblack@google.com    if (timeoutEvent.scheduled())
28713206Sgabeblack@google.com        scheduler.deschedule(&timeoutEvent);
28813206Sgabeblack@google.com}
28913206Sgabeblack@google.com
29013206Sgabeblack@google.comvoid
29113206Sgabeblack@google.comProcess::setTimeout(::sc_core::sc_time t)
29213206Sgabeblack@google.com{
29313206Sgabeblack@google.com    cancelTimeout();
29413206Sgabeblack@google.com    scheduler.schedule(&timeoutEvent, t);
29513206Sgabeblack@google.com}
29613206Sgabeblack@google.com
29713206Sgabeblack@google.comvoid
29813206Sgabeblack@google.comProcess::timeout()
29913206Sgabeblack@google.com{
30013206Sgabeblack@google.com    // A process is considered timed_out only if it was also waiting for an
30113206Sgabeblack@google.com    // event but got a timeout instead.
30213206Sgabeblack@google.com    _timedOut = (dynamicSensitivity != nullptr);
30313206Sgabeblack@google.com
30413206Sgabeblack@google.com    setDynamic(nullptr);
30513206Sgabeblack@google.com    if (disabled())
30613206Sgabeblack@google.com        return;
30713206Sgabeblack@google.com
30813206Sgabeblack@google.com    ready();
30912957Sgabeblack@google.com}
31012957Sgabeblack@google.com
31112959Sgabeblack@google.comvoid
31212959Sgabeblack@google.comProcess::satisfySensitivity(Sensitivity *s)
31312959Sgabeblack@google.com{
31412959Sgabeblack@google.com    // If there's a dynamic sensitivity and this wasn't it, ignore.
31513206Sgabeblack@google.com    if ((dynamicSensitivity || timeoutEvent.scheduled()) &&
31613206Sgabeblack@google.com            dynamicSensitivity != s) {
31712959Sgabeblack@google.com        return;
31813206Sgabeblack@google.com    }
31912959Sgabeblack@google.com
32013206Sgabeblack@google.com    _timedOut = false;
32113206Sgabeblack@google.com    // This sensitivity should already be cleared by this point, or the event
32213206Sgabeblack@google.com    // which triggered it will take care of it.
32313206Sgabeblack@google.com    delete dynamicSensitivity;
32413206Sgabeblack@google.com    dynamicSensitivity = nullptr;
32513206Sgabeblack@google.com    cancelTimeout();
32612959Sgabeblack@google.com    ready();
32712959Sgabeblack@google.com}
32812959Sgabeblack@google.com
32912959Sgabeblack@google.comvoid
33012959Sgabeblack@google.comProcess::ready()
33112959Sgabeblack@google.com{
33212996Sgabeblack@google.com    if (disabled())
33312996Sgabeblack@google.com        return;
33412959Sgabeblack@google.com    if (suspended())
33512959Sgabeblack@google.com        _suspendedReady = true;
33612959Sgabeblack@google.com    else
33712959Sgabeblack@google.com        scheduler.ready(this);
33812959Sgabeblack@google.com}
33912959Sgabeblack@google.com
34012997Sgabeblack@google.comvoid
34112997Sgabeblack@google.comProcess::lastReport(::sc_core::sc_report *report)
34212997Sgabeblack@google.com{
34312997Sgabeblack@google.com    if (report) {
34412997Sgabeblack@google.com        _lastReport = std::unique_ptr<::sc_core::sc_report>(
34512997Sgabeblack@google.com                new ::sc_core::sc_report(*report));
34612997Sgabeblack@google.com    } else {
34712997Sgabeblack@google.com        _lastReport = nullptr;
34812997Sgabeblack@google.com    }
34912997Sgabeblack@google.com}
35012997Sgabeblack@google.com
35112997Sgabeblack@google.com::sc_core::sc_report *Process::lastReport() const { return _lastReport.get(); }
35212997Sgabeblack@google.com
35313180Sgabeblack@google.comProcess::Process(const char *name, ProcessFuncWrapper *func, bool internal) :
35413206Sgabeblack@google.com    ::sc_core::sc_process_b(name), excWrapper(nullptr),
35513206Sgabeblack@google.com    timeoutEvent([this]() { this->timeout(); }),
35613206Sgabeblack@google.com    func(func), _internal(internal), _timedOut(false), _dontInitialize(false),
35713194Sgabeblack@google.com    _needsStart(true), _isUnwinding(false), _terminated(false),
35813194Sgabeblack@google.com    _suspended(false), _disabled(false), _syncReset(false), refCount(0),
35913189Sgabeblack@google.com    stackSize(::Fiber::DefaultStackSize), dynamicSensitivity(nullptr)
36012953Sgabeblack@google.com{
36113131Sgabeblack@google.com    _dynamic =
36213131Sgabeblack@google.com            (::sc_core::sc_get_status() >
36313131Sgabeblack@google.com             ::sc_core::SC_BEFORE_END_OF_ELABORATION);
36412953Sgabeblack@google.com    _newest = this;
36512953Sgabeblack@google.com}
36612952Sgabeblack@google.com
36712998Sgabeblack@google.comvoid
36812998Sgabeblack@google.comProcess::terminate()
36912998Sgabeblack@google.com{
37012998Sgabeblack@google.com    _terminated = true;
37112998Sgabeblack@google.com    _suspendedReady = false;
37212998Sgabeblack@google.com    _suspended = false;
37312998Sgabeblack@google.com    _syncReset = false;
37413206Sgabeblack@google.com    clearDynamic();
37513206Sgabeblack@google.com    cancelTimeout();
37613206Sgabeblack@google.com    for (auto s: staticSensitivities) {
37713206Sgabeblack@google.com        s->clear();
37812998Sgabeblack@google.com        delete s;
37913206Sgabeblack@google.com    }
38012998Sgabeblack@google.com    staticSensitivities.clear();
38113006Sgabeblack@google.com
38213006Sgabeblack@google.com    _terminatedEvent.notify();
38313196Sgabeblack@google.com
38413196Sgabeblack@google.com    for (auto jw: joinWaiters)
38513196Sgabeblack@google.com        jw->signal();
38613196Sgabeblack@google.com    joinWaiters.clear();
38712998Sgabeblack@google.com}
38812998Sgabeblack@google.com
38912953Sgabeblack@google.comProcess *Process::_newest;
39012952Sgabeblack@google.com
39112952Sgabeblack@google.comvoid
39212952Sgabeblack@google.comthrow_it_wrapper(Process *p, ExceptionWrapperBase &exc, bool inc_kids)
39312952Sgabeblack@google.com{
39412952Sgabeblack@google.com    p->throw_it(exc, inc_kids);
39512952Sgabeblack@google.com}
39612952Sgabeblack@google.com
39712952Sgabeblack@google.com} // namespace sc_gem5
398