sc_process_handle.cc revision 13254
112838Sgabeblack@google.com/*
212838Sgabeblack@google.com * Copyright 2018 Google, Inc.
312838Sgabeblack@google.com *
412838Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
512838Sgabeblack@google.com * modification, are permitted provided that the following conditions are
612838Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
712838Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
812838Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
912838Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1012838Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1112838Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1212838Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1312838Sgabeblack@google.com * this software without specific prior written permission.
1412838Sgabeblack@google.com *
1512838Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612838Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712838Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812838Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912838Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012838Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112838Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212838Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312838Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412838Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512838Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612838Sgabeblack@google.com *
2712838Sgabeblack@google.com * Authors: Gabe Black
2812838Sgabeblack@google.com */
2912838Sgabeblack@google.com
3012838Sgabeblack@google.com#include "base/logging.hh"
3112952Sgabeblack@google.com#include "systemc/core/process.hh"
3212953Sgabeblack@google.com#include "systemc/core/scheduler.hh"
3312960Sgabeblack@google.com#include "systemc/ext/core/sc_main.hh"
3412838Sgabeblack@google.com#include "systemc/ext/core/sc_process_handle.hh"
3513128Sgabeblack@google.com#include "systemc/ext/utils/sc_report_handler.hh"
3612838Sgabeblack@google.com
3712838Sgabeblack@google.comnamespace sc_core
3812838Sgabeblack@google.com{
3912838Sgabeblack@google.com
4012838Sgabeblack@google.comconst char *
4112838Sgabeblack@google.comsc_unwind_exception::what() const throw()
4212838Sgabeblack@google.com{
4312994Sgabeblack@google.com    return _isReset ? "RESET" : "KILL";
4412838Sgabeblack@google.com}
4512838Sgabeblack@google.com
4612838Sgabeblack@google.combool
4712838Sgabeblack@google.comsc_unwind_exception::is_reset() const
4812838Sgabeblack@google.com{
4912994Sgabeblack@google.com    return _isReset;
5012838Sgabeblack@google.com}
5112838Sgabeblack@google.com
5212994Sgabeblack@google.comsc_unwind_exception::sc_unwind_exception() : _isReset(false) {}
5312994Sgabeblack@google.comsc_unwind_exception::sc_unwind_exception(const sc_unwind_exception &e) :
5412994Sgabeblack@google.com    _isReset(e._isReset)
5512994Sgabeblack@google.com{}
5612952Sgabeblack@google.comsc_unwind_exception::~sc_unwind_exception() throw() {}
5712838Sgabeblack@google.com
5812838Sgabeblack@google.com
5913087Sgabeblack@google.comvoid
6013087Sgabeblack@google.comsc_set_location(const char *file, int lineno)
6112939Sgabeblack@google.com{
6213087Sgabeblack@google.com    sc_process_b *current = ::sc_gem5::scheduler.current();
6313087Sgabeblack@google.com    if (!current)
6413087Sgabeblack@google.com        return;
6513087Sgabeblack@google.com    current->file = file;
6613087Sgabeblack@google.com    current->lineno = lineno;
6712939Sgabeblack@google.com}
6812939Sgabeblack@google.com
6912939Sgabeblack@google.com
7012939Sgabeblack@google.comsc_process_b *
7112939Sgabeblack@google.comsc_get_curr_process_handle()
7212939Sgabeblack@google.com{
7313254Sgabeblack@google.com    return ::sc_gem5::scheduler.current();
7412939Sgabeblack@google.com}
7512939Sgabeblack@google.com
7612939Sgabeblack@google.com
7712952Sgabeblack@google.comsc_process_handle::sc_process_handle() : _gem5_process(nullptr) {}
7812952Sgabeblack@google.com
7912952Sgabeblack@google.comsc_process_handle::sc_process_handle(const sc_process_handle &handle) :
8012952Sgabeblack@google.com    _gem5_process(handle._gem5_process)
8112838Sgabeblack@google.com{
8212952Sgabeblack@google.com    if (_gem5_process)
8312952Sgabeblack@google.com        _gem5_process->incref();
8412838Sgabeblack@google.com}
8512838Sgabeblack@google.com
8612952Sgabeblack@google.comsc_process_handle::sc_process_handle(sc_object *obj) :
8712952Sgabeblack@google.com    _gem5_process(dynamic_cast<::sc_gem5::Process *>(obj))
8812838Sgabeblack@google.com{
8912952Sgabeblack@google.com    if (_gem5_process)
9012952Sgabeblack@google.com        _gem5_process->incref();
9112838Sgabeblack@google.com}
9212838Sgabeblack@google.com
9312838Sgabeblack@google.comsc_process_handle::~sc_process_handle()
9412838Sgabeblack@google.com{
9512952Sgabeblack@google.com    if (_gem5_process)
9612952Sgabeblack@google.com        _gem5_process->decref();
9712838Sgabeblack@google.com}
9812838Sgabeblack@google.com
9912838Sgabeblack@google.com
10012838Sgabeblack@google.combool
10112838Sgabeblack@google.comsc_process_handle::valid() const
10212838Sgabeblack@google.com{
10312952Sgabeblack@google.com    return _gem5_process != nullptr;
10412838Sgabeblack@google.com}
10512838Sgabeblack@google.com
10612838Sgabeblack@google.com
10712838Sgabeblack@google.comsc_process_handle &
10812952Sgabeblack@google.comsc_process_handle::operator = (const sc_process_handle &handle)
10912838Sgabeblack@google.com{
11012952Sgabeblack@google.com    if (_gem5_process)
11112952Sgabeblack@google.com        _gem5_process->decref();
11212952Sgabeblack@google.com    _gem5_process = handle._gem5_process;
11312952Sgabeblack@google.com    if (_gem5_process)
11412952Sgabeblack@google.com        _gem5_process->incref();
11512838Sgabeblack@google.com    return *this;
11612838Sgabeblack@google.com}
11712838Sgabeblack@google.com
11812838Sgabeblack@google.combool
11912952Sgabeblack@google.comsc_process_handle::operator == (const sc_process_handle &handle) const
12012838Sgabeblack@google.com{
12112952Sgabeblack@google.com    return _gem5_process && handle._gem5_process &&
12212952Sgabeblack@google.com        (_gem5_process == handle._gem5_process);
12312838Sgabeblack@google.com}
12412838Sgabeblack@google.com
12512838Sgabeblack@google.combool
12612952Sgabeblack@google.comsc_process_handle::operator != (const sc_process_handle &handle) const
12712838Sgabeblack@google.com{
12812952Sgabeblack@google.com    return !(handle == *this);
12912838Sgabeblack@google.com}
13012838Sgabeblack@google.com
13112838Sgabeblack@google.combool
13212952Sgabeblack@google.comsc_process_handle::operator < (const sc_process_handle &other) const
13312838Sgabeblack@google.com{
13412952Sgabeblack@google.com    return _gem5_process < other._gem5_process;
13512838Sgabeblack@google.com}
13612838Sgabeblack@google.com
13712952Sgabeblack@google.comvoid
13812952Sgabeblack@google.comsc_process_handle::swap(sc_process_handle &handle)
13912838Sgabeblack@google.com{
14012952Sgabeblack@google.com    ::sc_gem5::Process *temp = handle._gem5_process;
14112952Sgabeblack@google.com    handle._gem5_process = _gem5_process;
14212952Sgabeblack@google.com    _gem5_process = temp;
14312838Sgabeblack@google.com}
14412838Sgabeblack@google.com
14512838Sgabeblack@google.com
14612838Sgabeblack@google.comconst char *
14712838Sgabeblack@google.comsc_process_handle::name() const
14812838Sgabeblack@google.com{
14912952Sgabeblack@google.com    return _gem5_process ? _gem5_process->name() : "";
15012838Sgabeblack@google.com}
15112838Sgabeblack@google.com
15212838Sgabeblack@google.comsc_curr_proc_kind
15312838Sgabeblack@google.comsc_process_handle::proc_kind() const
15412838Sgabeblack@google.com{
15512952Sgabeblack@google.com    return _gem5_process ? _gem5_process->procKind() : SC_NO_PROC_;
15612838Sgabeblack@google.com}
15712838Sgabeblack@google.com
15812838Sgabeblack@google.comconst std::vector<sc_object *> &
15912838Sgabeblack@google.comsc_process_handle::get_child_objects() const
16012838Sgabeblack@google.com{
16112952Sgabeblack@google.com    static const std::vector<sc_object *> empty;
16212952Sgabeblack@google.com    return _gem5_process ? _gem5_process->get_child_objects() : empty;
16312838Sgabeblack@google.com}
16412838Sgabeblack@google.com
16512838Sgabeblack@google.comconst std::vector<sc_event *> &
16612838Sgabeblack@google.comsc_process_handle::get_child_events() const
16712838Sgabeblack@google.com{
16812952Sgabeblack@google.com    static const std::vector<sc_event *> empty;
16912952Sgabeblack@google.com    return _gem5_process ? _gem5_process->get_child_events() : empty;
17012838Sgabeblack@google.com}
17112838Sgabeblack@google.com
17212838Sgabeblack@google.comsc_object *
17312838Sgabeblack@google.comsc_process_handle::get_parent_object() const
17412838Sgabeblack@google.com{
17512952Sgabeblack@google.com    return _gem5_process ? _gem5_process->get_parent_object() : nullptr;
17612838Sgabeblack@google.com}
17712838Sgabeblack@google.com
17812838Sgabeblack@google.comsc_object *
17912838Sgabeblack@google.comsc_process_handle::get_process_object() const
18012838Sgabeblack@google.com{
18112952Sgabeblack@google.com    return _gem5_process;
18212838Sgabeblack@google.com}
18312838Sgabeblack@google.com
18412838Sgabeblack@google.combool
18512838Sgabeblack@google.comsc_process_handle::dynamic() const
18612838Sgabeblack@google.com{
18712952Sgabeblack@google.com    return _gem5_process ? _gem5_process->dynamic() : false;
18812838Sgabeblack@google.com}
18912838Sgabeblack@google.com
19012838Sgabeblack@google.combool
19112838Sgabeblack@google.comsc_process_handle::terminated() const
19212838Sgabeblack@google.com{
19312952Sgabeblack@google.com    return _gem5_process ? _gem5_process->terminated() : false;
19412838Sgabeblack@google.com}
19512838Sgabeblack@google.com
19612838Sgabeblack@google.comconst sc_event &
19712838Sgabeblack@google.comsc_process_handle::terminated_event() const
19812838Sgabeblack@google.com{
19913128Sgabeblack@google.com    if (!_gem5_process) {
20013128Sgabeblack@google.com        SC_REPORT_WARNING("(W570) attempt to use an empty "
20113128Sgabeblack@google.com                "process handle ignored", "terminated_event()");
20213128Sgabeblack@google.com        static sc_event non_event;
20313128Sgabeblack@google.com        return non_event;
20413128Sgabeblack@google.com    }
20513128Sgabeblack@google.com    return _gem5_process->terminatedEvent();
20612838Sgabeblack@google.com}
20712838Sgabeblack@google.com
20812838Sgabeblack@google.com
20912838Sgabeblack@google.comvoid
21012838Sgabeblack@google.comsc_process_handle::suspend(sc_descendent_inclusion_info include_descendants)
21112838Sgabeblack@google.com{
21213128Sgabeblack@google.com    if (!_gem5_process) {
21313128Sgabeblack@google.com        SC_REPORT_WARNING("(W570) attempt to use an empty "
21413128Sgabeblack@google.com                "process handle ignored", "suspend()");
21512952Sgabeblack@google.com        return;
21613128Sgabeblack@google.com    }
21712952Sgabeblack@google.com    _gem5_process->suspend(include_descendants == SC_INCLUDE_DESCENDANTS);
21812838Sgabeblack@google.com}
21912838Sgabeblack@google.com
22012838Sgabeblack@google.comvoid
22112838Sgabeblack@google.comsc_process_handle::resume(sc_descendent_inclusion_info include_descendants)
22212838Sgabeblack@google.com{
22313128Sgabeblack@google.com    if (!_gem5_process) {
22413128Sgabeblack@google.com        SC_REPORT_WARNING("(W570) attempt to use an empty "
22513128Sgabeblack@google.com                "process handle ignored", "resume()");
22612952Sgabeblack@google.com        return;
22713128Sgabeblack@google.com    }
22812952Sgabeblack@google.com    _gem5_process->resume(include_descendants == SC_INCLUDE_DESCENDANTS);
22912838Sgabeblack@google.com}
23012838Sgabeblack@google.com
23112838Sgabeblack@google.comvoid
23212838Sgabeblack@google.comsc_process_handle::disable(sc_descendent_inclusion_info include_descendants)
23312838Sgabeblack@google.com{
23413128Sgabeblack@google.com    if (!_gem5_process) {
23513128Sgabeblack@google.com        SC_REPORT_WARNING("(W570) attempt to use an empty "
23613128Sgabeblack@google.com                "process handle ignored", "disable()");
23712952Sgabeblack@google.com        return;
23813128Sgabeblack@google.com    }
23912952Sgabeblack@google.com    _gem5_process->disable(include_descendants == SC_INCLUDE_DESCENDANTS);
24012838Sgabeblack@google.com}
24112838Sgabeblack@google.com
24212838Sgabeblack@google.comvoid
24312838Sgabeblack@google.comsc_process_handle::enable(sc_descendent_inclusion_info include_descendants)
24412838Sgabeblack@google.com{
24513128Sgabeblack@google.com    if (!_gem5_process) {
24613128Sgabeblack@google.com        SC_REPORT_WARNING("(W570) attempt to use an empty "
24713128Sgabeblack@google.com                "process handle ignored", "enable()");
24812952Sgabeblack@google.com        return;
24913128Sgabeblack@google.com    }
25012952Sgabeblack@google.com    _gem5_process->enable(include_descendants == SC_INCLUDE_DESCENDANTS);
25112838Sgabeblack@google.com}
25212838Sgabeblack@google.com
25312838Sgabeblack@google.comvoid
25412838Sgabeblack@google.comsc_process_handle::kill(sc_descendent_inclusion_info include_descendants)
25512838Sgabeblack@google.com{
25613128Sgabeblack@google.com    if (!_gem5_process) {
25713128Sgabeblack@google.com        SC_REPORT_WARNING("(W570) attempt to use an empty "
25813128Sgabeblack@google.com                "process handle ignored", "kill()");
25912952Sgabeblack@google.com        return;
26013128Sgabeblack@google.com    }
26112952Sgabeblack@google.com    _gem5_process->kill(include_descendants == SC_INCLUDE_DESCENDANTS);
26212838Sgabeblack@google.com}
26312838Sgabeblack@google.com
26412838Sgabeblack@google.comvoid
26512838Sgabeblack@google.comsc_process_handle::reset(sc_descendent_inclusion_info include_descendants)
26612838Sgabeblack@google.com{
26713128Sgabeblack@google.com    if (!_gem5_process) {
26813128Sgabeblack@google.com        SC_REPORT_WARNING("(W570) attempt to use an empty "
26913128Sgabeblack@google.com                "process handle ignored", "reset()");
27012952Sgabeblack@google.com        return;
27113128Sgabeblack@google.com    }
27212952Sgabeblack@google.com    _gem5_process->reset(include_descendants == SC_INCLUDE_DESCENDANTS);
27312838Sgabeblack@google.com}
27412838Sgabeblack@google.com
27512838Sgabeblack@google.combool
27612838Sgabeblack@google.comsc_process_handle::is_unwinding()
27712838Sgabeblack@google.com{
27812952Sgabeblack@google.com    if (!_gem5_process) {
27913128Sgabeblack@google.com        SC_REPORT_WARNING("(W570) attempt to use an empty "
28013128Sgabeblack@google.com                "process handle ignored", "is_unwinding()");
28112952Sgabeblack@google.com        return false;
28212952Sgabeblack@google.com    }
28313128Sgabeblack@google.com    return _gem5_process->isUnwinding();
28412838Sgabeblack@google.com}
28512838Sgabeblack@google.com
28612838Sgabeblack@google.comconst sc_event &
28712838Sgabeblack@google.comsc_process_handle::reset_event() const
28812838Sgabeblack@google.com{
28913128Sgabeblack@google.com    if (!_gem5_process) {
29013128Sgabeblack@google.com        SC_REPORT_WARNING("(W570) attempt to use an empty "
29113128Sgabeblack@google.com                "process handle ignored", "reset()");
29213128Sgabeblack@google.com        static sc_event non_event;
29313128Sgabeblack@google.com        return non_event;
29413128Sgabeblack@google.com    }
29513128Sgabeblack@google.com    return _gem5_process->resetEvent();
29612838Sgabeblack@google.com}
29712838Sgabeblack@google.com
29812838Sgabeblack@google.com
29912838Sgabeblack@google.comvoid
30012838Sgabeblack@google.comsc_process_handle::sync_reset_on(
30112838Sgabeblack@google.com        sc_descendent_inclusion_info include_descendants)
30212838Sgabeblack@google.com{
30313128Sgabeblack@google.com    if (!_gem5_process) {
30413128Sgabeblack@google.com        SC_REPORT_WARNING("(W570) attempt to use an empty "
30513128Sgabeblack@google.com                "process handle ignored", "sync_reset_on()");
30612952Sgabeblack@google.com        return;
30713128Sgabeblack@google.com    }
30812952Sgabeblack@google.com    _gem5_process->syncResetOn(include_descendants == SC_INCLUDE_DESCENDANTS);
30912838Sgabeblack@google.com}
31012838Sgabeblack@google.com
31112838Sgabeblack@google.comvoid
31212838Sgabeblack@google.comsc_process_handle::sync_reset_off(
31312838Sgabeblack@google.com        sc_descendent_inclusion_info include_descendants)
31412838Sgabeblack@google.com{
31513128Sgabeblack@google.com    if (!_gem5_process) {
31613128Sgabeblack@google.com        SC_REPORT_WARNING("(W570) attempt to use an empty "
31713128Sgabeblack@google.com                "process handle ignored", "sync_reset_off()");
31812952Sgabeblack@google.com        return;
31913128Sgabeblack@google.com    }
32012952Sgabeblack@google.com    _gem5_process->syncResetOff(include_descendants == SC_INCLUDE_DESCENDANTS);
32112838Sgabeblack@google.com}
32212838Sgabeblack@google.com
32312838Sgabeblack@google.com
32412838Sgabeblack@google.comsc_process_handle
32512838Sgabeblack@google.comsc_get_current_process_handle()
32612838Sgabeblack@google.com{
32712960Sgabeblack@google.com    if (sc_is_running())
32812960Sgabeblack@google.com        return sc_process_handle(::sc_gem5::scheduler.current());
32912960Sgabeblack@google.com    else
33012960Sgabeblack@google.com        return sc_process_handle(::sc_gem5::Process::newest());
33112838Sgabeblack@google.com}
33212838Sgabeblack@google.com
33312838Sgabeblack@google.combool
33412838Sgabeblack@google.comsc_is_unwinding()
33512838Sgabeblack@google.com{
33612952Sgabeblack@google.com    return sc_get_current_process_handle().is_unwinding();
33712838Sgabeblack@google.com}
33812838Sgabeblack@google.com
33912899Sgabeblack@google.combool sc_allow_process_control_corners;
34012899Sgabeblack@google.com
34112838Sgabeblack@google.com} // namespace sc_core
342