event.cc revision 13127:d3318222cf09
1/*
2 * Copyright 2018 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: Gabe Black
28 */
29
30#include "systemc/core/event.hh"
31
32#include <algorithm>
33#include <cstring>
34#include <utility>
35
36#include "base/logging.hh"
37#include "sim/core.hh"
38#include "systemc/core/module.hh"
39#include "systemc/core/scheduler.hh"
40#include "systemc/ext/core/sc_main.hh"
41#include "systemc/ext/core/sc_module.hh"
42
43namespace sc_gem5
44{
45
46Event::Event(sc_core::sc_event *_sc_event) : Event(_sc_event, nullptr) {}
47
48Event::Event(sc_core::sc_event *_sc_event, const char *_basename_cstr) :
49    _sc_event(_sc_event), _basename(_basename_cstr ? _basename_cstr : ""),
50    delayedNotify([this]() { this->notify(); })
51{
52    Module *p = currentModule();
53
54    if (_basename == "" && ::sc_core::sc_is_running())
55        _basename = ::sc_core::sc_gen_unique_name("event");
56
57    if (p)
58        parent = p->obj()->sc_obj();
59    else if (scheduler.current())
60        parent = scheduler.current();
61    else
62        parent = nullptr;
63
64    pickUniqueName(parent, _basename);
65
66    if (parent) {
67        Object *obj = Object::getFromScObject(parent);
68        obj->addChildEvent(_sc_event);
69    } else {
70        topLevelEvents.emplace(topLevelEvents.end(), _sc_event);
71    }
72
73    if (parent)
74        _name = std::string(parent->name()) + "." + _basename;
75    else
76        _name = _basename;
77
78    allEvents.emplace(allEvents.end(), _sc_event);
79
80    // Determine if we're in the hierarchy (created once initialization starts
81    // means no).
82}
83
84Event::~Event()
85{
86    if (parent) {
87        Object *obj = Object::getFromScObject(parent);
88        obj->delChildEvent(_sc_event);
89    } else {
90        EventsIt it = find(topLevelEvents.begin(), topLevelEvents.end(),
91                           _sc_event);
92        assert(it != topLevelEvents.end());
93        std::swap(*it, topLevelEvents.back());
94        topLevelEvents.pop_back();
95    }
96
97    EventsIt it = findEvent(_name);
98    std::swap(*it, allEvents.back());
99    allEvents.pop_back();
100
101    if (delayedNotify.scheduled())
102        scheduler.deschedule(&delayedNotify);
103}
104
105const std::string &
106Event::name() const
107{
108    return _name;
109}
110
111const std::string &
112Event::basename() const
113{
114    return _basename;
115}
116
117bool
118Event::inHierarchy() const
119{
120    return _name.length() != 0;
121}
122
123sc_core::sc_object *
124Event::getParentObject() const
125{
126    return parent;
127}
128
129void
130Event::notify()
131{
132    // An immediate notification overrides any pending delayed notification.
133    if (delayedNotify.scheduled())
134        scheduler.deschedule(&delayedNotify);
135
136    auto local_sensitivities = sensitivities;
137    for (auto s: local_sensitivities)
138        s->notify(this);
139}
140
141void
142Event::notify(const sc_core::sc_time &t)
143{
144    if (delayedNotify.scheduled()) {
145        if (scheduler.delayed(t) >= delayedNotify.when())
146            return;
147
148        scheduler.deschedule(&delayedNotify);
149    }
150    scheduler.schedule(&delayedNotify, t);
151}
152
153void
154Event::cancel()
155{
156    if (delayedNotify.scheduled())
157        scheduler.deschedule(&delayedNotify);
158}
159
160bool
161Event::triggered() const
162{
163    return false;
164}
165
166Events topLevelEvents;
167Events allEvents;
168
169EventsIt
170findEvent(const std::string &name)
171{
172    EventsIt it;
173    for (it = allEvents.begin(); it != allEvents.end(); it++)
174        if (!strcmp((*it)->name(), name.c_str()))
175            break;
176
177    return it;
178}
179
180} // namespace sc_gem5
181