event.cc revision 13086:6a6fa249add7
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) : Event(_sc_event, nullptr) {}
4712855Sgabeblack@google.com
4812855Sgabeblack@google.comEvent::Event(sc_core::sc_event *_sc_event, const char *_basename_cstr) :
4912855Sgabeblack@google.com    _sc_event(_sc_event), _basename(_basename_cstr ? _basename_cstr : ""),
5012855Sgabeblack@google.com    delayedNotify([this]() { this->notify(); })
5112855Sgabeblack@google.com{
5212855Sgabeblack@google.com    Module *p = currentModule();
5312855Sgabeblack@google.com
5412855Sgabeblack@google.com    if (_basename == "" && ::sc_core::sc_is_running())
5512855Sgabeblack@google.com        _basename = ::sc_core::sc_gen_unique_name("event");
5612855Sgabeblack@google.com
5712855Sgabeblack@google.com    if (p)
5812855Sgabeblack@google.com        parent = p->obj()->sc_obj();
5912855Sgabeblack@google.com    else if (scheduler.current())
6012855Sgabeblack@google.com        parent = scheduler.current();
6112855Sgabeblack@google.com    else
6212855Sgabeblack@google.com        parent = nullptr;
6312855Sgabeblack@google.com
6412855Sgabeblack@google.com    if (parent) {
6512855Sgabeblack@google.com        Object *obj = Object::getFromScObject(parent);
6612855Sgabeblack@google.com        obj->addChildEvent(_sc_event);
6712855Sgabeblack@google.com    } else {
6812855Sgabeblack@google.com        topLevelEvents.emplace(topLevelEvents.end(), _sc_event);
6912855Sgabeblack@google.com    }
7012855Sgabeblack@google.com
7112855Sgabeblack@google.com    if (parent)
7212855Sgabeblack@google.com        _name = std::string(parent->name()) + "." + _basename;
7312855Sgabeblack@google.com    else
7412855Sgabeblack@google.com        _name = _basename;
7512855Sgabeblack@google.com
7612855Sgabeblack@google.com    allEvents.emplace(allEvents.end(), _sc_event);
7712855Sgabeblack@google.com
7812855Sgabeblack@google.com    // Determine if we're in the hierarchy (created once initialization starts
7912855Sgabeblack@google.com    // means no).
8012855Sgabeblack@google.com}
8112855Sgabeblack@google.com
8212855Sgabeblack@google.comEvent::~Event()
8312855Sgabeblack@google.com{
8412855Sgabeblack@google.com    if (parent) {
8512855Sgabeblack@google.com        Object *obj = Object::getFromScObject(parent);
8612855Sgabeblack@google.com        obj->delChildEvent(_sc_event);
8712855Sgabeblack@google.com    } else {
8812855Sgabeblack@google.com        EventsIt it = find(topLevelEvents.begin(), topLevelEvents.end(),
8912855Sgabeblack@google.com                           _sc_event);
9012855Sgabeblack@google.com        assert(it != topLevelEvents.end());
9112855Sgabeblack@google.com        std::swap(*it, topLevelEvents.back());
9212855Sgabeblack@google.com        topLevelEvents.pop_back();
9312855Sgabeblack@google.com    }
9412855Sgabeblack@google.com
9512855Sgabeblack@google.com    EventsIt it = findEvent(_name);
9612855Sgabeblack@google.com    std::swap(*it, allEvents.back());
9712855Sgabeblack@google.com    allEvents.pop_back();
9812855Sgabeblack@google.com
9912855Sgabeblack@google.com    if (delayedNotify.scheduled())
10012855Sgabeblack@google.com        scheduler.deschedule(&delayedNotify);
10112855Sgabeblack@google.com}
10212855Sgabeblack@google.com
10312855Sgabeblack@google.comconst std::string &
10412855Sgabeblack@google.comEvent::name() const
10512855Sgabeblack@google.com{
10612855Sgabeblack@google.com    return _name;
10712855Sgabeblack@google.com}
10812855Sgabeblack@google.com
10912855Sgabeblack@google.comconst std::string &
11012855Sgabeblack@google.comEvent::basename() const
11112855Sgabeblack@google.com{
11212855Sgabeblack@google.com    return _basename;
11312855Sgabeblack@google.com}
11412855Sgabeblack@google.com
11512855Sgabeblack@google.combool
11612855Sgabeblack@google.comEvent::inHierarchy() const
11712855Sgabeblack@google.com{
11812855Sgabeblack@google.com    return _name.length() != 0;
11912855Sgabeblack@google.com}
12012855Sgabeblack@google.com
12112855Sgabeblack@google.comsc_core::sc_object *
12212855Sgabeblack@google.comEvent::getParentObject() const
12312855Sgabeblack@google.com{
12412855Sgabeblack@google.com    return parent;
12512855Sgabeblack@google.com}
12612855Sgabeblack@google.com
12712855Sgabeblack@google.comvoid
12812855Sgabeblack@google.comEvent::notify()
12912855Sgabeblack@google.com{
13012855Sgabeblack@google.com    // An immediate notification overrides any pending delayed notification.
13112855Sgabeblack@google.com    if (delayedNotify.scheduled())
13212855Sgabeblack@google.com        scheduler.deschedule(&delayedNotify);
13312855Sgabeblack@google.com
13412855Sgabeblack@google.com    auto local_sensitivities = sensitivities;
13512855Sgabeblack@google.com    for (auto s: local_sensitivities)
13612855Sgabeblack@google.com        s->notify(this);
13712855Sgabeblack@google.com}
13812855Sgabeblack@google.com
13912855Sgabeblack@google.comvoid
14012855Sgabeblack@google.comEvent::notify(const sc_core::sc_time &t)
14112855Sgabeblack@google.com{
14212855Sgabeblack@google.com    if (delayedNotify.scheduled()) {
14312855Sgabeblack@google.com        if (scheduler.delayed(t) >= delayedNotify.when())
14412855Sgabeblack@google.com            return;
14512855Sgabeblack@google.com
14612855Sgabeblack@google.com        scheduler.deschedule(&delayedNotify);
14712855Sgabeblack@google.com    }
14812855Sgabeblack@google.com    scheduler.schedule(&delayedNotify, t);
14912855Sgabeblack@google.com}
15012855Sgabeblack@google.com
15112855Sgabeblack@google.comvoid
15212855Sgabeblack@google.comEvent::cancel()
15312855Sgabeblack@google.com{
15412855Sgabeblack@google.com    if (delayedNotify.scheduled())
15512855Sgabeblack@google.com        scheduler.deschedule(&delayedNotify);
15612855Sgabeblack@google.com}
15712855Sgabeblack@google.com
15812855Sgabeblack@google.combool
15912855Sgabeblack@google.comEvent::triggered() const
16012855Sgabeblack@google.com{
16112855Sgabeblack@google.com    return false;
16212855Sgabeblack@google.com}
16312855Sgabeblack@google.com
16412855Sgabeblack@google.comEvents topLevelEvents;
16512855Sgabeblack@google.comEvents allEvents;
16612855Sgabeblack@google.com
16712855Sgabeblack@google.comEventsIt
16812855Sgabeblack@google.comfindEvent(const std::string &name)
16912855Sgabeblack@google.com{
17012855Sgabeblack@google.com    EventsIt it;
17112855Sgabeblack@google.com    for (it = allEvents.begin(); it != allEvents.end(); it++)
17212855Sgabeblack@google.com        if (!strcmp((*it)->name(), name.c_str()))
17312855Sgabeblack@google.com            break;
17412855Sgabeblack@google.com
17512855Sgabeblack@google.com    return it;
17612855Sgabeblack@google.com}
17712855Sgabeblack@google.com
17812855Sgabeblack@google.com} // namespace sc_gem5
17912855Sgabeblack@google.com