process.hh revision 12959
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#ifndef __SYSTEMC_CORE_PROCESS_HH__
3112952Sgabeblack@google.com#define __SYSTEMC_CORE_PROCESS_HH__
3212952Sgabeblack@google.com
3312952Sgabeblack@google.com#include <functional>
3412957Sgabeblack@google.com#include <vector>
3512952Sgabeblack@google.com
3612952Sgabeblack@google.com#include "base/fiber.hh"
3712957Sgabeblack@google.com#include "sim/eventq.hh"
3812957Sgabeblack@google.com#include "systemc/core/bindinfo.hh"
3912953Sgabeblack@google.com#include "systemc/core/list.hh"
4012952Sgabeblack@google.com#include "systemc/core/object.hh"
4112952Sgabeblack@google.com#include "systemc/ext/core/sc_event.hh"
4212957Sgabeblack@google.com#include "systemc/ext/core/sc_interface.hh"
4312952Sgabeblack@google.com#include "systemc/ext/core/sc_module.hh"
4412957Sgabeblack@google.com#include "systemc/ext/core/sc_port.hh"
4512952Sgabeblack@google.com#include "systemc/ext/core/sc_process_handle.hh"
4612952Sgabeblack@google.com
4712952Sgabeblack@google.comnamespace sc_gem5
4812952Sgabeblack@google.com{
4912952Sgabeblack@google.com
5012957Sgabeblack@google.comclass Sensitivity
5112957Sgabeblack@google.com{
5212957Sgabeblack@google.com  protected:
5312957Sgabeblack@google.com    Process *process;
5412957Sgabeblack@google.com
5512957Sgabeblack@google.com  public:
5612957Sgabeblack@google.com    Sensitivity(Process *p) : process(p) {}
5712957Sgabeblack@google.com    virtual ~Sensitivity() {}
5812957Sgabeblack@google.com
5912959Sgabeblack@google.com    virtual void notifyWork(Event *e);
6012957Sgabeblack@google.com    void notify(Event *e);
6112957Sgabeblack@google.com    void notify() { notify(nullptr); }
6212957Sgabeblack@google.com
6312957Sgabeblack@google.com    const std::string name();
6412957Sgabeblack@google.com};
6512957Sgabeblack@google.com
6612957Sgabeblack@google.comclass SensitivityTimeout : virtual public Sensitivity
6712957Sgabeblack@google.com{
6812957Sgabeblack@google.com  private:
6912957Sgabeblack@google.com    EventWrapper<Sensitivity, &Sensitivity::notify> timeoutEvent;
7012957Sgabeblack@google.com    ::sc_core::sc_time timeout;
7112957Sgabeblack@google.com
7212957Sgabeblack@google.com  public:
7312957Sgabeblack@google.com    SensitivityTimeout(Process *p, ::sc_core::sc_time t);
7412957Sgabeblack@google.com    ~SensitivityTimeout();
7512957Sgabeblack@google.com};
7612957Sgabeblack@google.com
7712957Sgabeblack@google.comclass SensitivityEvent : virtual public Sensitivity
7812957Sgabeblack@google.com{
7912957Sgabeblack@google.com  private:
8012957Sgabeblack@google.com    const ::sc_core::sc_event *event;
8112957Sgabeblack@google.com
8212957Sgabeblack@google.com  public:
8312957Sgabeblack@google.com    SensitivityEvent(Process *p, const ::sc_core::sc_event *e);
8412957Sgabeblack@google.com    ~SensitivityEvent();
8512957Sgabeblack@google.com};
8612957Sgabeblack@google.com
8712957Sgabeblack@google.com//XXX This sensitivity can't be reused. To reset it, it has to be deleted and
8812957Sgabeblack@google.com//recreated. That works for dynamic sensitivities, but not for static.
8912957Sgabeblack@google.com//Fortunately processes can't be statically sensitive to sc_event_and_lists.
9012957Sgabeblack@google.comclass SensitivityEventAndList : virtual public Sensitivity
9112957Sgabeblack@google.com{
9212957Sgabeblack@google.com  private:
9312957Sgabeblack@google.com    const ::sc_core::sc_event_and_list *list;
9412957Sgabeblack@google.com    int count;
9512957Sgabeblack@google.com
9612957Sgabeblack@google.com  public:
9712957Sgabeblack@google.com    SensitivityEventAndList(
9812957Sgabeblack@google.com            Process *p, const ::sc_core::sc_event_and_list *list);
9912957Sgabeblack@google.com    ~SensitivityEventAndList();
10012957Sgabeblack@google.com
10112957Sgabeblack@google.com    virtual void notifyWork(Event *e) override;
10212957Sgabeblack@google.com};
10312957Sgabeblack@google.com
10412957Sgabeblack@google.comclass SensitivityEventOrList : virtual public Sensitivity
10512957Sgabeblack@google.com{
10612957Sgabeblack@google.com  private:
10712957Sgabeblack@google.com    const ::sc_core::sc_event_or_list *list;
10812957Sgabeblack@google.com
10912957Sgabeblack@google.com  public:
11012957Sgabeblack@google.com    SensitivityEventOrList(
11112957Sgabeblack@google.com            Process *p, const ::sc_core::sc_event_or_list *list);
11212957Sgabeblack@google.com    ~SensitivityEventOrList();
11312957Sgabeblack@google.com};
11412957Sgabeblack@google.com
11512957Sgabeblack@google.com// Combined sensitivities. These trigger when any of their parts do.
11612957Sgabeblack@google.com
11712957Sgabeblack@google.comclass SensitivityTimeoutAndEvent :
11812957Sgabeblack@google.com    public SensitivityTimeout, public SensitivityEvent
11912957Sgabeblack@google.com{
12012957Sgabeblack@google.com  public:
12112957Sgabeblack@google.com    SensitivityTimeoutAndEvent(
12212957Sgabeblack@google.com            Process *p, ::sc_core::sc_time t, const ::sc_core::sc_event *e) :
12312957Sgabeblack@google.com        Sensitivity(p), SensitivityTimeout(p, t), SensitivityEvent(p, e)
12412957Sgabeblack@google.com    {}
12512957Sgabeblack@google.com};
12612957Sgabeblack@google.com
12712957Sgabeblack@google.comclass SensitivityTimeoutAndEventAndList :
12812957Sgabeblack@google.com    public SensitivityTimeout, public SensitivityEventAndList
12912957Sgabeblack@google.com{
13012957Sgabeblack@google.com  public:
13112957Sgabeblack@google.com    SensitivityTimeoutAndEventAndList(
13212957Sgabeblack@google.com            Process *p, ::sc_core::sc_time t,
13312957Sgabeblack@google.com            const ::sc_core::sc_event_and_list *eal) :
13412957Sgabeblack@google.com        Sensitivity(p), SensitivityTimeout(p, t),
13512957Sgabeblack@google.com        SensitivityEventAndList(p, eal)
13612957Sgabeblack@google.com    {}
13712957Sgabeblack@google.com};
13812957Sgabeblack@google.com
13912957Sgabeblack@google.comclass SensitivityTimeoutAndEventOrList :
14012957Sgabeblack@google.com    public SensitivityTimeout, public SensitivityEventOrList
14112957Sgabeblack@google.com{
14212957Sgabeblack@google.com  public:
14312957Sgabeblack@google.com    SensitivityTimeoutAndEventOrList(
14412957Sgabeblack@google.com            Process *p, ::sc_core::sc_time t,
14512957Sgabeblack@google.com            const ::sc_core::sc_event_or_list *eol) :
14612957Sgabeblack@google.com        Sensitivity(p), SensitivityTimeout(p, t),
14712957Sgabeblack@google.com        SensitivityEventOrList(p, eol)
14812957Sgabeblack@google.com    {}
14912957Sgabeblack@google.com};
15012957Sgabeblack@google.com
15112957Sgabeblack@google.comtypedef std::vector<Sensitivity *> Sensitivities;
15212957Sgabeblack@google.com
15312957Sgabeblack@google.com
15412957Sgabeblack@google.com/*
15512957Sgabeblack@google.com * Pending sensitivities. These are records of sensitivities to install later,
15612957Sgabeblack@google.com * once all the information to configure them is available.
15712957Sgabeblack@google.com */
15812957Sgabeblack@google.com
15912957Sgabeblack@google.comclass PendingSensitivity
16012957Sgabeblack@google.com{
16112957Sgabeblack@google.com  protected:
16212957Sgabeblack@google.com    Process *process;
16312957Sgabeblack@google.com
16412957Sgabeblack@google.com  public:
16512957Sgabeblack@google.com    virtual void finalize(Sensitivities &s) = 0;
16612957Sgabeblack@google.com    PendingSensitivity(Process *p) : process(p) {}
16712957Sgabeblack@google.com    virtual ~PendingSensitivity() {}
16812957Sgabeblack@google.com};
16912957Sgabeblack@google.com
17012957Sgabeblack@google.comclass PendingSensitivityEvent : public PendingSensitivity
17112957Sgabeblack@google.com{
17212957Sgabeblack@google.com  private:
17312957Sgabeblack@google.com    const sc_core::sc_event *event;
17412957Sgabeblack@google.com
17512957Sgabeblack@google.com  public:
17612957Sgabeblack@google.com    PendingSensitivityEvent(Process *p, const sc_core::sc_event *e) :
17712957Sgabeblack@google.com        PendingSensitivity(p), event(e) {}
17812957Sgabeblack@google.com
17912957Sgabeblack@google.com    void
18012957Sgabeblack@google.com    finalize(Sensitivities &s) override
18112957Sgabeblack@google.com    {
18212957Sgabeblack@google.com        s.push_back(new SensitivityEvent(process, event));
18312957Sgabeblack@google.com    }
18412957Sgabeblack@google.com};
18512957Sgabeblack@google.com
18612957Sgabeblack@google.comclass PendingSensitivityInterface : public PendingSensitivity
18712957Sgabeblack@google.com{
18812957Sgabeblack@google.com  private:
18912957Sgabeblack@google.com    const sc_core::sc_interface *interface;
19012957Sgabeblack@google.com
19112957Sgabeblack@google.com  public:
19212957Sgabeblack@google.com    PendingSensitivityInterface(Process *p, const sc_core::sc_interface *i) :
19312957Sgabeblack@google.com        PendingSensitivity(p), interface(i)
19412957Sgabeblack@google.com    {}
19512957Sgabeblack@google.com
19612957Sgabeblack@google.com    void
19712957Sgabeblack@google.com    finalize(Sensitivities &s) override
19812957Sgabeblack@google.com    {
19912957Sgabeblack@google.com        s.push_back(new SensitivityEvent(process,
20012957Sgabeblack@google.com                                         &interface->default_event()));
20112957Sgabeblack@google.com    }
20212957Sgabeblack@google.com};
20312957Sgabeblack@google.com
20412957Sgabeblack@google.comclass PendingSensitivityPort : public PendingSensitivity
20512957Sgabeblack@google.com{
20612957Sgabeblack@google.com  private:
20712957Sgabeblack@google.com    const sc_core::sc_port_base *port;
20812957Sgabeblack@google.com
20912957Sgabeblack@google.com  public:
21012957Sgabeblack@google.com    PendingSensitivityPort(Process *p, const sc_core::sc_port_base *pb) :
21112957Sgabeblack@google.com        PendingSensitivity(p), port(pb)
21212957Sgabeblack@google.com    {}
21312957Sgabeblack@google.com
21412957Sgabeblack@google.com    void
21512957Sgabeblack@google.com    finalize(Sensitivities &s) override
21612957Sgabeblack@google.com    {
21712957Sgabeblack@google.com        for (int i = 0; i < port->size(); i++) {
21812957Sgabeblack@google.com            const ::sc_core::sc_event *e =
21912957Sgabeblack@google.com                &port->_gem5BindInfo[i]->interface->default_event();
22012957Sgabeblack@google.com            s.push_back(new SensitivityEvent(process, e));
22112957Sgabeblack@google.com        }
22212957Sgabeblack@google.com    }
22312957Sgabeblack@google.com};
22412957Sgabeblack@google.com
22512957Sgabeblack@google.comclass PendingSensitivityFinder : public PendingSensitivity
22612957Sgabeblack@google.com{
22712957Sgabeblack@google.com  private:
22812957Sgabeblack@google.com    const sc_core::sc_event_finder *finder;
22912957Sgabeblack@google.com
23012957Sgabeblack@google.com  public:
23112957Sgabeblack@google.com    PendingSensitivityFinder(Process *p, const sc_core::sc_event_finder *f) :
23212957Sgabeblack@google.com        PendingSensitivity(p), finder(f)
23312957Sgabeblack@google.com    {}
23412957Sgabeblack@google.com
23512957Sgabeblack@google.com    void
23612957Sgabeblack@google.com    finalize(Sensitivities &s) override
23712957Sgabeblack@google.com    {
23812957Sgabeblack@google.com        s.push_back(new SensitivityEvent(process, &finder->find_event()));
23912957Sgabeblack@google.com    }
24012957Sgabeblack@google.com};
24112957Sgabeblack@google.com
24212957Sgabeblack@google.comtypedef std::vector<PendingSensitivity *> PendingSensitivities;
24312957Sgabeblack@google.com
24412957Sgabeblack@google.com
24512953Sgabeblack@google.comclass Process : public ::sc_core::sc_object, public ListNode
24612952Sgabeblack@google.com{
24712952Sgabeblack@google.com  public:
24812952Sgabeblack@google.com    virtual ::sc_core::sc_curr_proc_kind procKind() const = 0;
24912953Sgabeblack@google.com    bool running() const { return _running; }
25012952Sgabeblack@google.com    bool dynamic() const { return _dynamic; }
25112952Sgabeblack@google.com    bool isUnwinding() const { return _isUnwinding; }
25212952Sgabeblack@google.com    bool terminated() const { return _terminated; }
25312952Sgabeblack@google.com
25412952Sgabeblack@google.com    void forEachKid(const std::function<void(Process *)> &work);
25512952Sgabeblack@google.com
25612952Sgabeblack@google.com    bool suspended() const { return _suspended; }
25712952Sgabeblack@google.com    bool disabled() const { return _disabled; }
25812952Sgabeblack@google.com
25912952Sgabeblack@google.com    void suspend(bool inc_kids);
26012952Sgabeblack@google.com    void resume(bool inc_kids);
26112952Sgabeblack@google.com    void disable(bool inc_kids);
26212952Sgabeblack@google.com    void enable(bool inc_kids);
26312952Sgabeblack@google.com
26412952Sgabeblack@google.com    void kill(bool inc_kids);
26512952Sgabeblack@google.com    void reset(bool inc_kids);
26612952Sgabeblack@google.com    virtual void throw_it(ExceptionWrapperBase &exc, bool inc_kids);
26712952Sgabeblack@google.com
26812952Sgabeblack@google.com    void injectException(ExceptionWrapperBase &exc);
26912952Sgabeblack@google.com    ExceptionWrapperBase *excWrapper;
27012952Sgabeblack@google.com
27112952Sgabeblack@google.com    void syncResetOn(bool inc_kids);
27212952Sgabeblack@google.com    void syncResetOff(bool inc_kids);
27312952Sgabeblack@google.com
27412952Sgabeblack@google.com    void incref() { refCount++; }
27512952Sgabeblack@google.com    void decref() { refCount--; }
27612952Sgabeblack@google.com
27712952Sgabeblack@google.com    const ::sc_core::sc_event &resetEvent() { return _resetEvent; }
27812952Sgabeblack@google.com    const ::sc_core::sc_event &terminatedEvent() { return _terminatedEvent; }
27912952Sgabeblack@google.com
28012953Sgabeblack@google.com    // This should only be called before initialization.
28112957Sgabeblack@google.com    void dontInitialize();
28212953Sgabeblack@google.com
28312953Sgabeblack@google.com    void setStackSize(size_t size) { stackSize = size; }
28412953Sgabeblack@google.com
28512957Sgabeblack@google.com    void finalize();
28612957Sgabeblack@google.com
28712953Sgabeblack@google.com    void run();
28812953Sgabeblack@google.com
28912957Sgabeblack@google.com    void addStatic(PendingSensitivity *);
29012957Sgabeblack@google.com    void setDynamic(Sensitivity *);
29112957Sgabeblack@google.com
29212959Sgabeblack@google.com    void satisfySensitivity(Sensitivity *);
29312959Sgabeblack@google.com
29412959Sgabeblack@google.com    void ready();
29512959Sgabeblack@google.com
29612953Sgabeblack@google.com    virtual Fiber *fiber() { return Fiber::primaryFiber(); }
29712953Sgabeblack@google.com
29812953Sgabeblack@google.com    static Process *newest() { return _newest; }
29912953Sgabeblack@google.com
30012952Sgabeblack@google.com  protected:
30112953Sgabeblack@google.com    Process(const char *name, ProcessFuncWrapper *func, bool _dynamic);
30212953Sgabeblack@google.com
30312953Sgabeblack@google.com    static Process *_newest;
30412952Sgabeblack@google.com
30512957Sgabeblack@google.com    virtual ~Process()
30612957Sgabeblack@google.com    {
30712957Sgabeblack@google.com        delete func;
30812957Sgabeblack@google.com        for (auto s: staticSensitivities)
30912957Sgabeblack@google.com            delete s;
31012957Sgabeblack@google.com    }
31112952Sgabeblack@google.com
31212952Sgabeblack@google.com    ::sc_core::sc_event _resetEvent;
31312952Sgabeblack@google.com    ::sc_core::sc_event _terminatedEvent;
31412952Sgabeblack@google.com
31512952Sgabeblack@google.com    ProcessFuncWrapper *func;
31612952Sgabeblack@google.com    sc_core::sc_curr_proc_kind _procKind;
31712953Sgabeblack@google.com    bool _running;
31812952Sgabeblack@google.com    bool _dynamic;
31912952Sgabeblack@google.com    bool _isUnwinding;
32012952Sgabeblack@google.com    bool _terminated;
32112952Sgabeblack@google.com
32212952Sgabeblack@google.com    bool _suspended;
32312959Sgabeblack@google.com    bool _suspendedReady;
32412952Sgabeblack@google.com    bool _disabled;
32512952Sgabeblack@google.com
32612952Sgabeblack@google.com    bool _syncReset;
32712952Sgabeblack@google.com
32812952Sgabeblack@google.com    int refCount;
32912952Sgabeblack@google.com
33012953Sgabeblack@google.com    size_t stackSize;
33112957Sgabeblack@google.com
33212957Sgabeblack@google.com    Sensitivities staticSensitivities;
33312957Sgabeblack@google.com    PendingSensitivities pendingStaticSensitivities;
33412957Sgabeblack@google.com
33512957Sgabeblack@google.com    Sensitivity *dynamicSensitivity;
33612952Sgabeblack@google.com};
33712952Sgabeblack@google.com
33812957Sgabeblack@google.cominline void
33912959Sgabeblack@google.comSensitivity::notifyWork(Event *e)
34012959Sgabeblack@google.com{
34112959Sgabeblack@google.com    process->satisfySensitivity(this);
34212959Sgabeblack@google.com}
34312959Sgabeblack@google.com
34412959Sgabeblack@google.cominline void
34512957Sgabeblack@google.comSensitivity::notify(Event *e)
34612957Sgabeblack@google.com{
34712957Sgabeblack@google.com    if (!process->disabled())
34812957Sgabeblack@google.com        notifyWork(e);
34912957Sgabeblack@google.com}
35012957Sgabeblack@google.com
35112957Sgabeblack@google.cominline const std::string
35212957Sgabeblack@google.comSensitivity::name()
35312957Sgabeblack@google.com{
35412957Sgabeblack@google.com    return std::string(process->name()) + ".timeout";
35512957Sgabeblack@google.com}
35612957Sgabeblack@google.com
35712952Sgabeblack@google.com} // namespace sc_gem5
35812952Sgabeblack@google.com
35912952Sgabeblack@google.com#endif  //__SYSTEMC_CORE_PROCESS_HH__
360