event.cc revision 13303
112855Sgabeblack@google.com/*
212855Sgabeblack@google.com * Copyright 2018 Google, Inc.
312855Sgabeblack@google.com *
412855Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
512855Sgabeblack@google.com * modification, are permitted provided that the following conditions are
612855Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
712855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
812855Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
912855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1012855Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1112855Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1212855Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1312855Sgabeblack@google.com * this software without specific prior written permission.
1412855Sgabeblack@google.com *
1512855Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612855Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712855Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812855Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912855Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012855Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112855Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212855Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312855Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412855Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512855Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612855Sgabeblack@google.com *
2712855Sgabeblack@google.com * Authors: Gabe Black
2812855Sgabeblack@google.com */
2912855Sgabeblack@google.com
3012855Sgabeblack@google.com#include "systemc/core/event.hh"
3112855Sgabeblack@google.com
3212855Sgabeblack@google.com#include <algorithm>
3312855Sgabeblack@google.com#include <cstring>
3412855Sgabeblack@google.com#include <utility>
3512855Sgabeblack@google.com
3612855Sgabeblack@google.com#include "base/logging.hh"
3712855Sgabeblack@google.com#include "sim/core.hh"
3812855Sgabeblack@google.com#include "systemc/core/module.hh"
3912855Sgabeblack@google.com#include "systemc/core/scheduler.hh"
4012855Sgabeblack@google.com#include "systemc/ext/core/sc_main.hh"
4112855Sgabeblack@google.com#include "systemc/ext/core/sc_module.hh"
4212855Sgabeblack@google.com
4312855Sgabeblack@google.comnamespace sc_gem5
4412855Sgabeblack@google.com{
4512855Sgabeblack@google.com
4612855Sgabeblack@google.comEvent::Event(sc_core::sc_event *_sc_event, bool internal) :
4712855Sgabeblack@google.com    Event(_sc_event, nullptr, internal)
4812855Sgabeblack@google.com{}
4912855Sgabeblack@google.com
5012855Sgabeblack@google.comEvent::Event(sc_core::sc_event *_sc_event, const char *_basename_cstr,
5112855Sgabeblack@google.com        bool internal) :
5212855Sgabeblack@google.com    _sc_event(_sc_event), _basename(_basename_cstr ? _basename_cstr : ""),
5312855Sgabeblack@google.com    _inHierarchy(!internal), delayedNotify([this]() { this->notify(); }),
5412855Sgabeblack@google.com    _triggeredStamp(~0ULL)
5512855Sgabeblack@google.com{
5612855Sgabeblack@google.com    if (_basename == "" && ::sc_core::sc_is_running())
5712855Sgabeblack@google.com        _basename = ::sc_core::sc_gen_unique_name("event");
5812855Sgabeblack@google.com
5912855Sgabeblack@google.com    parent = internal ? nullptr : pickParentObj();
6012855Sgabeblack@google.com
6112855Sgabeblack@google.com    if (internal) {
6212855Sgabeblack@google.com        _basename = globalNameGen.gen(_basename);
6312855Sgabeblack@google.com        _name = _basename;
6412855Sgabeblack@google.com    } else {
6512855Sgabeblack@google.com        std::string original_name = _basename;
6612855Sgabeblack@google.com        _basename = pickUniqueName(parent, _basename);
6712855Sgabeblack@google.com
6812855Sgabeblack@google.com        if (parent) {
6912855Sgabeblack@google.com            Object *obj = Object::getFromScObject(parent);
7012855Sgabeblack@google.com            obj->addChildEvent(_sc_event);
7112855Sgabeblack@google.com        } else {
7212855Sgabeblack@google.com            topLevelEvents.emplace(topLevelEvents.end(), _sc_event);
7312855Sgabeblack@google.com        }
7412855Sgabeblack@google.com
7512855Sgabeblack@google.com        std::string path = parent ? (std::string(parent->name()) + ".") : "";
7612855Sgabeblack@google.com
7712855Sgabeblack@google.com        if (original_name != "" && _basename != original_name) {
7812855Sgabeblack@google.com            std::string message = path + original_name +
7912855Sgabeblack@google.com                ". Latter declaration will be renamed to " +
8012855Sgabeblack@google.com                path + _basename;
8112855Sgabeblack@google.com            SC_REPORT_WARNING("(W505) object already exists", message.c_str());
8212855Sgabeblack@google.com        }
8312855Sgabeblack@google.com
8412855Sgabeblack@google.com        _name = path + _basename;
8512855Sgabeblack@google.com    }
8612855Sgabeblack@google.com
8712855Sgabeblack@google.com    allEvents.emplace(allEvents.end(), _sc_event);
8812855Sgabeblack@google.com
8912855Sgabeblack@google.com    // Determine if we're in the hierarchy (created once initialization starts
9012855Sgabeblack@google.com    // means no).
9112855Sgabeblack@google.com}
9212855Sgabeblack@google.com
9312855Sgabeblack@google.comEvent::~Event()
9412855Sgabeblack@google.com{
9512855Sgabeblack@google.com    if (parent) {
9612855Sgabeblack@google.com        Object *obj = Object::getFromScObject(parent);
9712855Sgabeblack@google.com        obj->delChildEvent(_sc_event);
9812855Sgabeblack@google.com    } else if (inHierarchy()) {
9912855Sgabeblack@google.com        EventsIt it = find(topLevelEvents.begin(), topLevelEvents.end(),
10012855Sgabeblack@google.com                           _sc_event);
10112855Sgabeblack@google.com        assert(it != topLevelEvents.end());
10212855Sgabeblack@google.com        std::swap(*it, topLevelEvents.back());
10312855Sgabeblack@google.com        topLevelEvents.pop_back();
10412855Sgabeblack@google.com    }
10512855Sgabeblack@google.com
10612855Sgabeblack@google.com    EventsIt it = findEvent(_name);
10712855Sgabeblack@google.com    std::swap(*it, allEvents.back());
10812855Sgabeblack@google.com    allEvents.pop_back();
10912855Sgabeblack@google.com
11012855Sgabeblack@google.com    if (delayedNotify.scheduled())
11112855Sgabeblack@google.com        scheduler.deschedule(&delayedNotify);
11212855Sgabeblack@google.com}
11312855Sgabeblack@google.com
11412855Sgabeblack@google.comconst std::string &
11512855Sgabeblack@google.comEvent::name() const
11612855Sgabeblack@google.com{
11712855Sgabeblack@google.com    return _name;
11812855Sgabeblack@google.com}
11912855Sgabeblack@google.com
12012855Sgabeblack@google.comconst std::string &
12112855Sgabeblack@google.comEvent::basename() const
12212855Sgabeblack@google.com{
12312855Sgabeblack@google.com    return _basename;
12412855Sgabeblack@google.com}
12512855Sgabeblack@google.com
12612855Sgabeblack@google.combool
12712855Sgabeblack@google.comEvent::inHierarchy() const
12812855Sgabeblack@google.com{
12912855Sgabeblack@google.com    return _inHierarchy;
13012855Sgabeblack@google.com}
13112855Sgabeblack@google.com
13212855Sgabeblack@google.comsc_core::sc_object *
13312855Sgabeblack@google.comEvent::getParentObject() const
13412855Sgabeblack@google.com{
13512855Sgabeblack@google.com    return parent;
13612855Sgabeblack@google.com}
13712855Sgabeblack@google.com
13812855Sgabeblack@google.comvoid
13912855Sgabeblack@google.comEvent::notify(StaticSensitivities &senses)
14012855Sgabeblack@google.com{
14112855Sgabeblack@google.com    for (auto s: senses)
14212855Sgabeblack@google.com        s->notify(this);
14312855Sgabeblack@google.com}
14412855Sgabeblack@google.com
14512855Sgabeblack@google.comvoid
14612855Sgabeblack@google.comEvent::notify(DynamicSensitivities &senses)
14712855Sgabeblack@google.com{
14812855Sgabeblack@google.com    int size = senses.size();
14912855Sgabeblack@google.com    int pos = 0;
15012855Sgabeblack@google.com    while (pos < size) {
15112855Sgabeblack@google.com        if (senses[pos]->notify(this))
15212855Sgabeblack@google.com            senses[pos] = senses[--size];
15312855Sgabeblack@google.com        else
15412855Sgabeblack@google.com            pos++;
15512855Sgabeblack@google.com    }
15612855Sgabeblack@google.com    senses.resize(size);
15712855Sgabeblack@google.com}
15812855Sgabeblack@google.com
15912855Sgabeblack@google.comvoid
16012855Sgabeblack@google.comEvent::notify()
16112855Sgabeblack@google.com{
16212855Sgabeblack@google.com    if (scheduler.inUpdate()) {
16312855Sgabeblack@google.com        SC_REPORT_ERROR("(E521) immediate notification is not allowed "
16412855Sgabeblack@google.com                "during update phase or elaboration", "");
16512855Sgabeblack@google.com    }
16612855Sgabeblack@google.com
16712855Sgabeblack@google.com    // An immediate notification overrides any pending delayed notification.
16812855Sgabeblack@google.com    if (delayedNotify.scheduled())
16912855Sgabeblack@google.com        scheduler.deschedule(&delayedNotify);
17012855Sgabeblack@google.com
17112855Sgabeblack@google.com    _triggeredStamp = scheduler.changeStamp();
17212855Sgabeblack@google.com    notify(staticSenseMethod);
17312855Sgabeblack@google.com    notify(dynamicSenseMethod);
17412855Sgabeblack@google.com    notify(staticSenseThread);
17512855Sgabeblack@google.com    notify(dynamicSenseThread);
17612855Sgabeblack@google.com}
17712855Sgabeblack@google.com
17812855Sgabeblack@google.comvoid
17912855Sgabeblack@google.comEvent::notify(const sc_core::sc_time &t)
18012855Sgabeblack@google.com{
18112855Sgabeblack@google.com    if (delayedNotify.scheduled()) {
18212855Sgabeblack@google.com        if (scheduler.delayed(t) >= delayedNotify.when())
18312855Sgabeblack@google.com            return;
18412855Sgabeblack@google.com
18512855Sgabeblack@google.com        scheduler.deschedule(&delayedNotify);
18612855Sgabeblack@google.com    }
18712855Sgabeblack@google.com    scheduler.schedule(&delayedNotify, t);
18812855Sgabeblack@google.com}
18912855Sgabeblack@google.com
19012855Sgabeblack@google.comvoid
19112855Sgabeblack@google.comEvent::notifyDelayed(const sc_core::sc_time &t)
19212855Sgabeblack@google.com{
19312855Sgabeblack@google.com    if (delayedNotify.scheduled()) {
19412855Sgabeblack@google.com        SC_REPORT_ERROR("(E531) notify_delayed() cannot be called on events "
19512855Sgabeblack@google.com                "that have pending notifications", "");
19612855Sgabeblack@google.com    }
19712855Sgabeblack@google.com    notify(t);
19812855Sgabeblack@google.com}
19912855Sgabeblack@google.com
20012855Sgabeblack@google.comvoid
20112855Sgabeblack@google.comEvent::cancel()
20212855Sgabeblack@google.com{
20312855Sgabeblack@google.com    if (delayedNotify.scheduled())
20412855Sgabeblack@google.com        scheduler.deschedule(&delayedNotify);
20512855Sgabeblack@google.com}
20612855Sgabeblack@google.com
20712855Sgabeblack@google.combool
20812855Sgabeblack@google.comEvent::triggered() const
20912855Sgabeblack@google.com{
21012855Sgabeblack@google.com    return _triggeredStamp == scheduler.changeStamp();
21112855Sgabeblack@google.com}
21212855Sgabeblack@google.com
213void
214Event::clearParent()
215{
216    if (!parent)
217        return;
218    Object::getFromScObject(parent)->delChildEvent(sc_event());
219    parent = nullptr;
220    topLevelEvents.emplace(topLevelEvents.end(), sc_event());
221}
222
223Events topLevelEvents;
224Events allEvents;
225
226EventsIt
227findEvent(const std::string &name)
228{
229    EventsIt it;
230    for (it = allEvents.begin(); it != allEvents.end(); it++)
231        if (!strcmp((*it)->name(), name.c_str()))
232            break;
233
234    return it;
235}
236
237} // namespace sc_gem5
238