event.cc revision 13127
112955Sgabeblack@google.com/*
212955Sgabeblack@google.com * Copyright 2018 Google, Inc.
312955Sgabeblack@google.com *
412955Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
512955Sgabeblack@google.com * modification, are permitted provided that the following conditions are
612955Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
712955Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
812955Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
912955Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1012955Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1112955Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1212955Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1312955Sgabeblack@google.com * this software without specific prior written permission.
1412955Sgabeblack@google.com *
1512955Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612955Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712955Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812955Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912955Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012955Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112955Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212955Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312955Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412955Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512955Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612955Sgabeblack@google.com *
2712955Sgabeblack@google.com * Authors: Gabe Black
2812955Sgabeblack@google.com */
2912955Sgabeblack@google.com
3012955Sgabeblack@google.com#include "systemc/core/event.hh"
3112955Sgabeblack@google.com
3212988Sgabeblack@google.com#include <algorithm>
3312955Sgabeblack@google.com#include <cstring>
3412955Sgabeblack@google.com#include <utility>
3512955Sgabeblack@google.com
3612957Sgabeblack@google.com#include "base/logging.hh"
3712957Sgabeblack@google.com#include "sim/core.hh"
3812955Sgabeblack@google.com#include "systemc/core/module.hh"
3912955Sgabeblack@google.com#include "systemc/core/scheduler.hh"
4013086Sgabeblack@google.com#include "systemc/ext/core/sc_main.hh"
4113086Sgabeblack@google.com#include "systemc/ext/core/sc_module.hh"
4212955Sgabeblack@google.com
4312955Sgabeblack@google.comnamespace sc_gem5
4412955Sgabeblack@google.com{
4512955Sgabeblack@google.com
4613086Sgabeblack@google.comEvent::Event(sc_core::sc_event *_sc_event) : Event(_sc_event, nullptr) {}
4712955Sgabeblack@google.com
4813086Sgabeblack@google.comEvent::Event(sc_core::sc_event *_sc_event, const char *_basename_cstr) :
4913086Sgabeblack@google.com    _sc_event(_sc_event), _basename(_basename_cstr ? _basename_cstr : ""),
5013063Sgabeblack@google.com    delayedNotify([this]() { this->notify(); })
5112955Sgabeblack@google.com{
5212955Sgabeblack@google.com    Module *p = currentModule();
5312955Sgabeblack@google.com
5413086Sgabeblack@google.com    if (_basename == "" && ::sc_core::sc_is_running())
5513086Sgabeblack@google.com        _basename = ::sc_core::sc_gen_unique_name("event");
5613086Sgabeblack@google.com
5712955Sgabeblack@google.com    if (p)
5812955Sgabeblack@google.com        parent = p->obj()->sc_obj();
5912955Sgabeblack@google.com    else if (scheduler.current())
6012955Sgabeblack@google.com        parent = scheduler.current();
6112988Sgabeblack@google.com    else
6212988Sgabeblack@google.com        parent = nullptr;
6312955Sgabeblack@google.com
6413127Sgabeblack@google.com    pickUniqueName(parent, _basename);
6513127Sgabeblack@google.com
6612955Sgabeblack@google.com    if (parent) {
6712955Sgabeblack@google.com        Object *obj = Object::getFromScObject(parent);
6812988Sgabeblack@google.com        obj->addChildEvent(_sc_event);
6912955Sgabeblack@google.com    } else {
7012988Sgabeblack@google.com        topLevelEvents.emplace(topLevelEvents.end(), _sc_event);
7112955Sgabeblack@google.com    }
7212955Sgabeblack@google.com
7312955Sgabeblack@google.com    if (parent)
7412955Sgabeblack@google.com        _name = std::string(parent->name()) + "." + _basename;
7512955Sgabeblack@google.com    else
7612955Sgabeblack@google.com        _name = _basename;
7712955Sgabeblack@google.com
7812955Sgabeblack@google.com    allEvents.emplace(allEvents.end(), _sc_event);
7912955Sgabeblack@google.com
8012955Sgabeblack@google.com    // Determine if we're in the hierarchy (created once initialization starts
8112955Sgabeblack@google.com    // means no).
8212955Sgabeblack@google.com}
8312955Sgabeblack@google.com
8412955Sgabeblack@google.comEvent::~Event()
8512955Sgabeblack@google.com{
8612955Sgabeblack@google.com    if (parent) {
8712955Sgabeblack@google.com        Object *obj = Object::getFromScObject(parent);
8812988Sgabeblack@google.com        obj->delChildEvent(_sc_event);
8912955Sgabeblack@google.com    } else {
9012988Sgabeblack@google.com        EventsIt it = find(topLevelEvents.begin(), topLevelEvents.end(),
9112988Sgabeblack@google.com                           _sc_event);
9212988Sgabeblack@google.com        assert(it != topLevelEvents.end());
9312988Sgabeblack@google.com        std::swap(*it, topLevelEvents.back());
9412955Sgabeblack@google.com        topLevelEvents.pop_back();
9512955Sgabeblack@google.com    }
9612955Sgabeblack@google.com
9712955Sgabeblack@google.com    EventsIt it = findEvent(_name);
9812955Sgabeblack@google.com    std::swap(*it, allEvents.back());
9912955Sgabeblack@google.com    allEvents.pop_back();
10012957Sgabeblack@google.com
10113063Sgabeblack@google.com    if (delayedNotify.scheduled())
10213063Sgabeblack@google.com        scheduler.deschedule(&delayedNotify);
10312955Sgabeblack@google.com}
10412955Sgabeblack@google.com
10512955Sgabeblack@google.comconst std::string &
10612955Sgabeblack@google.comEvent::name() const
10712955Sgabeblack@google.com{
10812955Sgabeblack@google.com    return _name;
10912955Sgabeblack@google.com}
11012955Sgabeblack@google.com
11112955Sgabeblack@google.comconst std::string &
11212955Sgabeblack@google.comEvent::basename() const
11312955Sgabeblack@google.com{
11412955Sgabeblack@google.com    return _basename;
11512955Sgabeblack@google.com}
11612955Sgabeblack@google.com
11712955Sgabeblack@google.combool
11812955Sgabeblack@google.comEvent::inHierarchy() const
11912955Sgabeblack@google.com{
12012955Sgabeblack@google.com    return _name.length() != 0;
12112955Sgabeblack@google.com}
12212955Sgabeblack@google.com
12312955Sgabeblack@google.comsc_core::sc_object *
12412955Sgabeblack@google.comEvent::getParentObject() const
12512955Sgabeblack@google.com{
12612955Sgabeblack@google.com    return parent;
12712955Sgabeblack@google.com}
12812955Sgabeblack@google.com
12912955Sgabeblack@google.comvoid
13012955Sgabeblack@google.comEvent::notify()
13112955Sgabeblack@google.com{
13213073Sgabeblack@google.com    // An immediate notification overrides any pending delayed notification.
13313073Sgabeblack@google.com    if (delayedNotify.scheduled())
13413073Sgabeblack@google.com        scheduler.deschedule(&delayedNotify);
13513073Sgabeblack@google.com
13612957Sgabeblack@google.com    auto local_sensitivities = sensitivities;
13712957Sgabeblack@google.com    for (auto s: local_sensitivities)
13812957Sgabeblack@google.com        s->notify(this);
13912955Sgabeblack@google.com}
14012955Sgabeblack@google.com
14112955Sgabeblack@google.comvoid
14212955Sgabeblack@google.comEvent::notify(const sc_core::sc_time &t)
14312955Sgabeblack@google.com{
14413063Sgabeblack@google.com    if (delayedNotify.scheduled()) {
14513063Sgabeblack@google.com        if (scheduler.delayed(t) >= delayedNotify.when())
14612957Sgabeblack@google.com            return;
14712957Sgabeblack@google.com
14813063Sgabeblack@google.com        scheduler.deschedule(&delayedNotify);
14912957Sgabeblack@google.com    }
15013063Sgabeblack@google.com    scheduler.schedule(&delayedNotify, t);
15112955Sgabeblack@google.com}
15212955Sgabeblack@google.com
15312955Sgabeblack@google.comvoid
15412955Sgabeblack@google.comEvent::cancel()
15512955Sgabeblack@google.com{
15613063Sgabeblack@google.com    if (delayedNotify.scheduled())
15713063Sgabeblack@google.com        scheduler.deschedule(&delayedNotify);
15812955Sgabeblack@google.com}
15912955Sgabeblack@google.com
16012955Sgabeblack@google.combool
16112955Sgabeblack@google.comEvent::triggered() const
16212955Sgabeblack@google.com{
16312955Sgabeblack@google.com    return false;
16412955Sgabeblack@google.com}
16512955Sgabeblack@google.com
16612955Sgabeblack@google.comEvents topLevelEvents;
16712955Sgabeblack@google.comEvents allEvents;
16812955Sgabeblack@google.com
16912955Sgabeblack@google.comEventsIt
17012955Sgabeblack@google.comfindEvent(const std::string &name)
17112955Sgabeblack@google.com{
17212955Sgabeblack@google.com    EventsIt it;
17312955Sgabeblack@google.com    for (it = allEvents.begin(); it != allEvents.end(); it++)
17412955Sgabeblack@google.com        if (!strcmp((*it)->name(), name.c_str()))
17512955Sgabeblack@google.com            break;
17612955Sgabeblack@google.com
17712955Sgabeblack@google.com    return it;
17812955Sgabeblack@google.com}
17912955Sgabeblack@google.com
18012955Sgabeblack@google.com} // namespace sc_gem5
181