event.cc (13303:045f002c325c) event.cc (13317:36c574a4036e)
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"
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/messages.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, bool internal) :
47 Event(_sc_event, nullptr, internal)
48{}
49
50Event::Event(sc_core::sc_event *_sc_event, const char *_basename_cstr,
51 bool internal) :
52 _sc_event(_sc_event), _basename(_basename_cstr ? _basename_cstr : ""),
53 _inHierarchy(!internal), delayedNotify([this]() { this->notify(); }),
54 _triggeredStamp(~0ULL)
55{
56 if (_basename == "" && ::sc_core::sc_is_running())
57 _basename = ::sc_core::sc_gen_unique_name("event");
58
59 parent = internal ? nullptr : pickParentObj();
60
61 if (internal) {
62 _basename = globalNameGen.gen(_basename);
63 _name = _basename;
64 } else {
65 std::string original_name = _basename;
66 _basename = pickUniqueName(parent, _basename);
67
68 if (parent) {
69 Object *obj = Object::getFromScObject(parent);
70 obj->addChildEvent(_sc_event);
71 } else {
72 topLevelEvents.emplace(topLevelEvents.end(), _sc_event);
73 }
74
75 std::string path = parent ? (std::string(parent->name()) + ".") : "";
76
77 if (original_name != "" && _basename != original_name) {
78 std::string message = path + original_name +
79 ". Latter declaration will be renamed to " +
80 path + _basename;
41#include "systemc/ext/core/sc_main.hh"
42#include "systemc/ext/core/sc_module.hh"
43
44namespace sc_gem5
45{
46
47Event::Event(sc_core::sc_event *_sc_event, bool internal) :
48 Event(_sc_event, nullptr, internal)
49{}
50
51Event::Event(sc_core::sc_event *_sc_event, const char *_basename_cstr,
52 bool internal) :
53 _sc_event(_sc_event), _basename(_basename_cstr ? _basename_cstr : ""),
54 _inHierarchy(!internal), delayedNotify([this]() { this->notify(); }),
55 _triggeredStamp(~0ULL)
56{
57 if (_basename == "" && ::sc_core::sc_is_running())
58 _basename = ::sc_core::sc_gen_unique_name("event");
59
60 parent = internal ? nullptr : pickParentObj();
61
62 if (internal) {
63 _basename = globalNameGen.gen(_basename);
64 _name = _basename;
65 } else {
66 std::string original_name = _basename;
67 _basename = pickUniqueName(parent, _basename);
68
69 if (parent) {
70 Object *obj = Object::getFromScObject(parent);
71 obj->addChildEvent(_sc_event);
72 } else {
73 topLevelEvents.emplace(topLevelEvents.end(), _sc_event);
74 }
75
76 std::string path = parent ? (std::string(parent->name()) + ".") : "";
77
78 if (original_name != "" && _basename != original_name) {
79 std::string message = path + original_name +
80 ". Latter declaration will be renamed to " +
81 path + _basename;
81 SC_REPORT_WARNING("(W505) object already exists", message.c_str());
82 SC_REPORT_WARNING(sc_core::SC_ID_INSTANCE_EXISTS_,
83 message.c_str());
82 }
83
84 _name = path + _basename;
85 }
86
87 allEvents.emplace(allEvents.end(), _sc_event);
88
89 // Determine if we're in the hierarchy (created once initialization starts
90 // means no).
91}
92
93Event::~Event()
94{
95 if (parent) {
96 Object *obj = Object::getFromScObject(parent);
97 obj->delChildEvent(_sc_event);
98 } else if (inHierarchy()) {
99 EventsIt it = find(topLevelEvents.begin(), topLevelEvents.end(),
100 _sc_event);
101 assert(it != topLevelEvents.end());
102 std::swap(*it, topLevelEvents.back());
103 topLevelEvents.pop_back();
104 }
105
106 EventsIt it = findEvent(_name);
107 std::swap(*it, allEvents.back());
108 allEvents.pop_back();
109
110 if (delayedNotify.scheduled())
111 scheduler.deschedule(&delayedNotify);
112}
113
114const std::string &
115Event::name() const
116{
117 return _name;
118}
119
120const std::string &
121Event::basename() const
122{
123 return _basename;
124}
125
126bool
127Event::inHierarchy() const
128{
129 return _inHierarchy;
130}
131
132sc_core::sc_object *
133Event::getParentObject() const
134{
135 return parent;
136}
137
138void
139Event::notify(StaticSensitivities &senses)
140{
141 for (auto s: senses)
142 s->notify(this);
143}
144
145void
146Event::notify(DynamicSensitivities &senses)
147{
148 int size = senses.size();
149 int pos = 0;
150 while (pos < size) {
151 if (senses[pos]->notify(this))
152 senses[pos] = senses[--size];
153 else
154 pos++;
155 }
156 senses.resize(size);
157}
158
159void
160Event::notify()
161{
84 }
85
86 _name = path + _basename;
87 }
88
89 allEvents.emplace(allEvents.end(), _sc_event);
90
91 // Determine if we're in the hierarchy (created once initialization starts
92 // means no).
93}
94
95Event::~Event()
96{
97 if (parent) {
98 Object *obj = Object::getFromScObject(parent);
99 obj->delChildEvent(_sc_event);
100 } else if (inHierarchy()) {
101 EventsIt it = find(topLevelEvents.begin(), topLevelEvents.end(),
102 _sc_event);
103 assert(it != topLevelEvents.end());
104 std::swap(*it, topLevelEvents.back());
105 topLevelEvents.pop_back();
106 }
107
108 EventsIt it = findEvent(_name);
109 std::swap(*it, allEvents.back());
110 allEvents.pop_back();
111
112 if (delayedNotify.scheduled())
113 scheduler.deschedule(&delayedNotify);
114}
115
116const std::string &
117Event::name() const
118{
119 return _name;
120}
121
122const std::string &
123Event::basename() const
124{
125 return _basename;
126}
127
128bool
129Event::inHierarchy() const
130{
131 return _inHierarchy;
132}
133
134sc_core::sc_object *
135Event::getParentObject() const
136{
137 return parent;
138}
139
140void
141Event::notify(StaticSensitivities &senses)
142{
143 for (auto s: senses)
144 s->notify(this);
145}
146
147void
148Event::notify(DynamicSensitivities &senses)
149{
150 int size = senses.size();
151 int pos = 0;
152 while (pos < size) {
153 if (senses[pos]->notify(this))
154 senses[pos] = senses[--size];
155 else
156 pos++;
157 }
158 senses.resize(size);
159}
160
161void
162Event::notify()
163{
162 if (scheduler.inUpdate()) {
163 SC_REPORT_ERROR("(E521) immediate notification is not allowed "
164 "during update phase or elaboration", "");
165 }
164 if (scheduler.inUpdate())
165 SC_REPORT_ERROR(sc_core::SC_ID_IMMEDIATE_NOTIFICATION_, "");
166
167 // An immediate notification overrides any pending delayed notification.
168 if (delayedNotify.scheduled())
169 scheduler.deschedule(&delayedNotify);
170
171 _triggeredStamp = scheduler.changeStamp();
172 notify(staticSenseMethod);
173 notify(dynamicSenseMethod);
174 notify(staticSenseThread);
175 notify(dynamicSenseThread);
176}
177
178void
179Event::notify(const sc_core::sc_time &t)
180{
181 if (delayedNotify.scheduled()) {
182 if (scheduler.delayed(t) >= delayedNotify.when())
183 return;
184
185 scheduler.deschedule(&delayedNotify);
186 }
187 scheduler.schedule(&delayedNotify, t);
188}
189
190void
191Event::notifyDelayed(const sc_core::sc_time &t)
192{
166
167 // An immediate notification overrides any pending delayed notification.
168 if (delayedNotify.scheduled())
169 scheduler.deschedule(&delayedNotify);
170
171 _triggeredStamp = scheduler.changeStamp();
172 notify(staticSenseMethod);
173 notify(dynamicSenseMethod);
174 notify(staticSenseThread);
175 notify(dynamicSenseThread);
176}
177
178void
179Event::notify(const sc_core::sc_time &t)
180{
181 if (delayedNotify.scheduled()) {
182 if (scheduler.delayed(t) >= delayedNotify.when())
183 return;
184
185 scheduler.deschedule(&delayedNotify);
186 }
187 scheduler.schedule(&delayedNotify, t);
188}
189
190void
191Event::notifyDelayed(const sc_core::sc_time &t)
192{
193 if (delayedNotify.scheduled()) {
194 SC_REPORT_ERROR("(E531) notify_delayed() cannot be called on events "
195 "that have pending notifications", "");
196 }
193 if (delayedNotify.scheduled())
194 SC_REPORT_ERROR(sc_core::SC_ID_NOTIFY_DELAYED_, "");
197 notify(t);
198}
199
200void
201Event::cancel()
202{
203 if (delayedNotify.scheduled())
204 scheduler.deschedule(&delayedNotify);
205}
206
207bool
208Event::triggered() const
209{
210 return _triggeredStamp == scheduler.changeStamp();
211}
212
213void
214Event::clearParent()
215{
216 if (!parent)
217 return;
218 Object::getFromScObject(parent)->delChildEvent(sc_event());
219 parent = nullptr;
220 topLevelEvents.emplace(topLevelEvents.end(), sc_event());
221}
222
223Events topLevelEvents;
224Events allEvents;
225
226EventsIt
227findEvent(const std::string &name)
228{
229 EventsIt it;
230 for (it = allEvents.begin(); it != allEvents.end(); it++)
231 if (!strcmp((*it)->name(), name.c_str()))
232 break;
233
234 return it;
235}
236
237} // namespace sc_gem5
195 notify(t);
196}
197
198void
199Event::cancel()
200{
201 if (delayedNotify.scheduled())
202 scheduler.deschedule(&delayedNotify);
203}
204
205bool
206Event::triggered() const
207{
208 return _triggeredStamp == scheduler.changeStamp();
209}
210
211void
212Event::clearParent()
213{
214 if (!parent)
215 return;
216 Object::getFromScObject(parent)->delChildEvent(sc_event());
217 parent = nullptr;
218 topLevelEvents.emplace(topLevelEvents.end(), sc_event());
219}
220
221Events topLevelEvents;
222Events allEvents;
223
224EventsIt
225findEvent(const std::string &name)
226{
227 EventsIt it;
228 for (it = allEvents.begin(); it != allEvents.end(); it++)
229 if (!strcmp((*it)->name(), name.c_str()))
230 break;
231
232 return it;
233}
234
235} // namespace sc_gem5