event.cc revision 12957:e54f9890363d
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 <cstring>
33#include <utility>
34
35#include "base/logging.hh"
36#include "sim/core.hh"
37#include "systemc/core/module.hh"
38#include "systemc/core/scheduler.hh"
39
40namespace sc_gem5
41{
42
43Event::Event(sc_core::sc_event *_sc_event) : Event(_sc_event, "") {}
44
45Event::Event(sc_core::sc_event *_sc_event, const char *_basename) :
46    _sc_event(_sc_event), _basename(_basename), delayedNotify(this)
47{
48    Module *p = currentModule();
49
50    if (p)
51        parent = p->obj()->sc_obj();
52    else if (scheduler.current())
53        parent = scheduler.current();
54
55    if (parent) {
56        Object *obj = Object::getFromScObject(parent);
57        parentIt = obj->addChildEvent(_sc_event);
58    } else {
59        parentIt = topLevelEvents.emplace(topLevelEvents.end(), _sc_event);
60    }
61
62    if (parent)
63        _name = std::string(parent->name()) + "." + _basename;
64    else
65        _name = _basename;
66
67    allEvents.emplace(allEvents.end(), _sc_event);
68
69    // Determine if we're in the hierarchy (created once initialization starts
70    // means no).
71}
72
73Event::~Event()
74{
75    if (parent) {
76        Object *obj = Object::getFromScObject(parent);
77        obj->delChildEvent(parentIt);
78    } else {
79        std::swap(*parentIt, topLevelEvents.back());
80        topLevelEvents.pop_back();
81    }
82
83    EventsIt it = findEvent(_name);
84    std::swap(*it, allEvents.back());
85    allEvents.pop_back();
86
87    if (delayedNotifyEvent.scheduled())
88        scheduler.deschedule(&delayedNotifyEvent);
89}
90
91const std::string &
92Event::name() const
93{
94    return _name;
95}
96
97const std::string &
98Event::basename() const
99{
100    return _basename;
101}
102
103bool
104Event::inHierarchy() const
105{
106    return _name.length() != 0;
107}
108
109sc_core::sc_object *
110Event::getParentObject() const
111{
112    return parent;
113}
114
115void
116Event::notify()
117{
118    auto local_sensitivities = sensitivities;
119    for (auto s: local_sensitivities)
120        s->notify(this);
121}
122
123void
124Event::notify(const sc_core::sc_time &t)
125{
126    //XXX We're assuming the systemc time resolution is in ps.
127    Tick new_tick = t.value() * SimClock::Int::ps +
128        scheduler.eventQueue().getCurTick();
129    if (delayedNotify.scheduled()) {
130        Tick old_tick = delayedNotify.when();
131
132        if (new_tick >= old_tick)
133            return;
134
135        scheduler.eventQueue().deschedule(&delayedNotify);
136    }
137
138    scheduler.eventQueue().schedule(&delayedNotify, new_tick);
139}
140
141void
142Event::cancel()
143{
144    if (delayedNotify.scheduled())
145        scheduler.eventQueue().deschedule(&delayedNotify);
146}
147
148bool
149Event::triggered() const
150{
151    return false;
152}
153
154Events topLevelEvents;
155Events allEvents;
156
157EventsIt
158findEvent(const std::string &name)
159{
160    EventsIt it;
161    for (it = allEvents.begin(); it != allEvents.end(); it++)
162        if (!strcmp((*it)->name(), name.c_str()))
163            break;
164
165    return it;
166}
167
168} // namespace sc_gem5
169