event.cc revision 13086
112950Sgabeblack@google.com/*
212950Sgabeblack@google.com * Copyright 2018 Google, Inc.
312950Sgabeblack@google.com *
412950Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
512950Sgabeblack@google.com * modification, are permitted provided that the following conditions are
612950Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
712950Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
812950Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
912950Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1012950Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1112950Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1212950Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1312950Sgabeblack@google.com * this software without specific prior written permission.
1412950Sgabeblack@google.com *
1512950Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612950Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712950Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812950Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912950Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012950Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112950Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212950Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312950Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412950Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512950Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612950Sgabeblack@google.com *
2712950Sgabeblack@google.com * Authors: Gabe Black
2812950Sgabeblack@google.com */
2912950Sgabeblack@google.com
3012950Sgabeblack@google.com#include "systemc/core/event.hh"
3112950Sgabeblack@google.com
3212988Sgabeblack@google.com#include <algorithm>
3312988Sgabeblack@google.com#include <cstring>
3412950Sgabeblack@google.com#include <utility>
3513127Sgabeblack@google.com
3612950Sgabeblack@google.com#include "base/logging.hh"
3712953Sgabeblack@google.com#include "sim/core.hh"
3812950Sgabeblack@google.com#include "systemc/core/module.hh"
3912950Sgabeblack@google.com#include "systemc/core/scheduler.hh"
4012950Sgabeblack@google.com#include "systemc/ext/core/sc_main.hh"
4112950Sgabeblack@google.com#include "systemc/ext/core/sc_module.hh"
4212950Sgabeblack@google.com
4312950Sgabeblack@google.comnamespace sc_gem5
4412950Sgabeblack@google.com{
4512950Sgabeblack@google.com
4612950Sgabeblack@google.comEvent::Event(sc_core::sc_event *_sc_event) : Event(_sc_event, nullptr) {}
4712950Sgabeblack@google.com
4812950Sgabeblack@google.comEvent::Event(sc_core::sc_event *_sc_event, const char *_basename_cstr) :
4912950Sgabeblack@google.com    _sc_event(_sc_event), _basename(_basename_cstr ? _basename_cstr : ""),
5012950Sgabeblack@google.com    delayedNotify([this]() { this->notify(); })
5112950Sgabeblack@google.com{
5212950Sgabeblack@google.com    Module *p = currentModule();
5312950Sgabeblack@google.com
5412950Sgabeblack@google.com    if (_basename == "" && ::sc_core::sc_is_running())
5512950Sgabeblack@google.com        _basename = ::sc_core::sc_gen_unique_name("event");
5612950Sgabeblack@google.com
5712950Sgabeblack@google.com    if (p)
5812950Sgabeblack@google.com        parent = p->obj()->sc_obj();
5912950Sgabeblack@google.com    else if (scheduler.current())
6012950Sgabeblack@google.com        parent = scheduler.current();
6112950Sgabeblack@google.com    else
6212950Sgabeblack@google.com        parent = nullptr;
6312950Sgabeblack@google.com
6412950Sgabeblack@google.com    if (parent) {
6512950Sgabeblack@google.com        Object *obj = Object::getFromScObject(parent);
6612950Sgabeblack@google.com        obj->addChildEvent(_sc_event);
6712950Sgabeblack@google.com    } else {
6812950Sgabeblack@google.com        topLevelEvents.emplace(topLevelEvents.end(), _sc_event);
6912950Sgabeblack@google.com    }
7012950Sgabeblack@google.com
7113127Sgabeblack@google.com    if (parent)
7213127Sgabeblack@google.com        _name = std::string(parent->name()) + "." + _basename;
7313127Sgabeblack@google.com    else
7413127Sgabeblack@google.com        _name = _basename;
7513127Sgabeblack@google.com
7613127Sgabeblack@google.com    allEvents.emplace(allEvents.end(), _sc_event);
7713127Sgabeblack@google.com
7813127Sgabeblack@google.com    // Determine if we're in the hierarchy (created once initialization starts
7913127Sgabeblack@google.com    // means no).
8013127Sgabeblack@google.com}
8113127Sgabeblack@google.com
8213127Sgabeblack@google.comEvent::~Event()
8312950Sgabeblack@google.com{
8412950Sgabeblack@google.com    if (parent) {
8512955Sgabeblack@google.com        Object *obj = Object::getFromScObject(parent);
8612950Sgabeblack@google.com        obj->delChildEvent(_sc_event);
8712955Sgabeblack@google.com    } else {
8813085Sgabeblack@google.com        EventsIt it = find(topLevelEvents.begin(), topLevelEvents.end(),
8912950Sgabeblack@google.com                           _sc_event);
9012950Sgabeblack@google.com        assert(it != topLevelEvents.end());
9112950Sgabeblack@google.com        std::swap(*it, topLevelEvents.back());
9212950Sgabeblack@google.com        topLevelEvents.pop_back();
9312950Sgabeblack@google.com    }
9413045Sgabeblack@google.com
9513045Sgabeblack@google.com    EventsIt it = findEvent(_name);
9612950Sgabeblack@google.com    std::swap(*it, allEvents.back());
9712950Sgabeblack@google.com    allEvents.pop_back();
9812950Sgabeblack@google.com
9912950Sgabeblack@google.com    if (delayedNotify.scheduled())
10012950Sgabeblack@google.com        scheduler.deschedule(&delayedNotify);
10112950Sgabeblack@google.com}
10212950Sgabeblack@google.com
10312950Sgabeblack@google.comconst std::string &
10412950Sgabeblack@google.comEvent::name() const
10513045Sgabeblack@google.com{
10612955Sgabeblack@google.com    return _name;
10712953Sgabeblack@google.com}
10812953Sgabeblack@google.com
10912953Sgabeblack@google.comconst std::string &
11013126Sgabeblack@google.comEvent::basename() const
11113127Sgabeblack@google.com{
11213127Sgabeblack@google.com    return _basename;
11313127Sgabeblack@google.com}
11413127Sgabeblack@google.com
11513126Sgabeblack@google.combool
11613127Sgabeblack@google.comEvent::inHierarchy() const
11712955Sgabeblack@google.com{
11812950Sgabeblack@google.com    return _name.length() != 0;
11912955Sgabeblack@google.com}
12012950Sgabeblack@google.com
12112950Sgabeblack@google.comsc_core::sc_object *
12212950Sgabeblack@google.comEvent::getParentObject() const
12312950Sgabeblack@google.com{
12412950Sgabeblack@google.com    return parent;
12512952Sgabeblack@google.com}
12612950Sgabeblack@google.com
12712950Sgabeblack@google.comvoid
12812950Sgabeblack@google.comEvent::notify()
12912955Sgabeblack@google.com{
13012955Sgabeblack@google.com    // An immediate notification overrides any pending delayed notification.
13112950Sgabeblack@google.com    if (delayedNotify.scheduled())
13212950Sgabeblack@google.com        scheduler.deschedule(&delayedNotify);
13312950Sgabeblack@google.com
13412950Sgabeblack@google.com    auto local_sensitivities = sensitivities;
13512950Sgabeblack@google.com    for (auto s: local_sensitivities)
13612950Sgabeblack@google.com        s->notify(this);
13712950Sgabeblack@google.com}
13812950Sgabeblack@google.com
13912950Sgabeblack@google.comvoid
14012950Sgabeblack@google.comEvent::notify(const sc_core::sc_time &t)
14112984Sgabeblack@google.com{
14212984Sgabeblack@google.com    if (delayedNotify.scheduled()) {
14312984Sgabeblack@google.com        if (scheduler.delayed(t) >= delayedNotify.when())
14412984Sgabeblack@google.com            return;
14512984Sgabeblack@google.com
14612984Sgabeblack@google.com        scheduler.deschedule(&delayedNotify);
14712950Sgabeblack@google.com    }
14812950Sgabeblack@google.com    scheduler.schedule(&delayedNotify, t);
14912950Sgabeblack@google.com}
15012950Sgabeblack@google.com
15112950Sgabeblack@google.comvoid
15212950Sgabeblack@google.comEvent::cancel()
15312950Sgabeblack@google.com{
15412950Sgabeblack@google.com    if (delayedNotify.scheduled())
15512950Sgabeblack@google.com        scheduler.deschedule(&delayedNotify);
15612950Sgabeblack@google.com}
15712950Sgabeblack@google.com
15812950Sgabeblack@google.combool
15912950Sgabeblack@google.comEvent::triggered() const
16012950Sgabeblack@google.com{
16112950Sgabeblack@google.com    return false;
16212950Sgabeblack@google.com}
16312950Sgabeblack@google.com
16412950Sgabeblack@google.comEvents topLevelEvents;
16512950Sgabeblack@google.comEvents allEvents;
16612950Sgabeblack@google.com
16712950Sgabeblack@google.comEventsIt
16812950Sgabeblack@google.comfindEvent(const std::string &name)
16912950Sgabeblack@google.com{
17012950Sgabeblack@google.com    EventsIt it;
17112950Sgabeblack@google.com    for (it = allEvents.begin(); it != allEvents.end(); it++)
17212950Sgabeblack@google.com        if (!strcmp((*it)->name(), name.c_str()))
17312950Sgabeblack@google.com            break;
17412950Sgabeblack@google.com
17512950Sgabeblack@google.com    return it;
17612950Sgabeblack@google.com}
17712955Sgabeblack@google.com
17812950Sgabeblack@google.com} // namespace sc_gem5
17912950Sgabeblack@google.com