gem5 [DEVELOP-FOR-25.1]
Loading...
Searching...
No Matches
event.cc
Go to the documentation of this file.
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
28#include "systemc/core/event.hh"
29
30#include <algorithm>
31#include <cstring>
32#include <mutex>
33#include <shared_mutex>
34#include <utility>
35
41
42namespace sc_gem5
43{
44
45namespace
46{
47
49findEventIn(Events &events, const std::string &name)
50{
51 EventsIt it;
52 for (it = events.begin(); it != events.end(); it++)
53 if (!strcmp((*it)->name(), name.c_str()))
54 break;
55
56 return it;
57}
58
59void
60addEvent(Events *events, sc_core::sc_event *event)
61{
62 events->emplace(events->end(), event);
63}
64
65void
66popEvent(Events* events, const std::string &name)
67{
68 EventsIt it = findEventIn(*events, name);
69 assert(it != events->end());
70 std::swap(events->back(), *it);
71 events->pop_back();
72}
73
74std::shared_mutex globalEventLock;
75
76} // anonymous namespace
77
80
82 Event(_sc_event, nullptr, internal)
83{}
84
85Event::Event(sc_core::sc_event *_sc_event, const char *_basename_cstr,
86 bool internal) :
87 _sc_event(_sc_event), _basename(_basename_cstr ? _basename_cstr : ""),
88 _inHierarchy(!internal), delayedNotify([this]() { this->notify(); }),
89 _triggeredStamp(~0ULL)
90{
91 [[maybe_unused]] std::unique_lock lock(globalEventLock);
92
93 if (_basename == "" && ::sc_core::sc_is_running())
94 _basename = ::sc_core::sc_gen_unique_name("event");
95
96 parent = internal ? nullptr : pickParentObj();
97
98 if (internal) {
99 _basename = globalNameGen.gen(_basename);
100 _name = _basename;
101 } else {
102 std::string original_name = _basename;
103 _basename = pickUniqueName(parent, _basename);
104
105 if (parent) {
106 Object *obj = Object::getFromScObject(parent);
107 obj->addChildEvent(_sc_event);
108 } else {
109 addEvent(&topLevelEvents, _sc_event);
110 }
111
112 std::string path = parent ? (std::string(parent->name()) + ".") : "";
113
114 if (original_name != "" && _basename != original_name) {
115 std::string message = path + original_name +
116 ". Latter declaration will be renamed to " +
117 path + _basename;
119 message.c_str());
120 }
121
122 _name = path + _basename;
123 }
124
125 addEvent(&allEvents, _sc_event);
126
127 // Determine if we're in the hierarchy (created once initialization starts
128 // means no).
129}
130
132{
133 [[maybe_unused]] std::unique_lock lock(globalEventLock);
134
135 if (parent) {
138 } else if (inHierarchy()) {
139 popEvent(&topLevelEvents, _name);
140 }
141
142 popEvent(&allEvents, _name);
143
144 if (delayedNotify.scheduled())
145 scheduler.deschedule(&delayedNotify);
146}
147
148const std::string &
150{
151 return _name;
152}
153
154const std::string &
156{
157 return _basename;
158}
159
160bool
162{
163 return _inHierarchy;
164}
165
168{
169 return parent;
170}
171
172void
174{
175 for (auto s: senses)
176 s->notify(this);
177}
178
179void
181{
182 int size = senses.size();
183 int pos = 0;
184 while (pos < size) {
185 if (senses[pos]->notify(this))
186 senses[pos] = senses[--size];
187 else
188 pos++;
189 }
190 senses.resize(size);
191}
192
193void
195{
196 if (scheduler.inUpdate())
198
199 // An immediate notification overrides any pending delayed notification.
200 if (delayedNotify.scheduled())
201 scheduler.deschedule(&delayedNotify);
202
203 _triggeredStamp = scheduler.changeStamp();
208}
209
210void
212{
213 if (delayedNotify.scheduled()) {
214 if (scheduler.delayed(t) >= delayedNotify.when())
215 return;
216
217 scheduler.deschedule(&delayedNotify);
218 }
219 scheduler.schedule(&delayedNotify, t);
220}
221
222void
229
230void
232{
233 if (delayedNotify.scheduled())
234 scheduler.deschedule(&delayedNotify);
235}
236
237bool
239{
240 return _triggeredStamp == scheduler.changeStamp();
241}
242
243void
245{
246 [[maybe_unused]] std::unique_lock lock(globalEventLock);
247
248 if (!parent)
249 return;
251 parent = nullptr;
252 addEvent(&topLevelEvents, _sc_event);
253}
254
256findEvent(const char *name)
257{
258 [[maybe_unused]] std::shared_lock lock(globalEventLock);
259
260 EventsIt it = findEventIn(allEvents, name);
261 return it == allEvents.end() ? nullptr : *it;
262}
263
264} // namespace sc_gem5
void notify()
Definition event.cc:194
StaticSensitivities staticSenseMethod
Definition event.hh:155
bool triggered() const
Definition event.cc:238
DynamicSensitivities dynamicSenseMethod
Definition event.hh:157
sc_core::sc_object * getParentObject() const
Definition event.cc:167
StaticSensitivities staticSenseThread
Definition event.hh:156
const std::string & basename() const
Definition event.cc:155
DynamicSensitivities dynamicSenseThread
Definition event.hh:158
void clearParent()
Definition event.cc:244
bool _inHierarchy
Definition event.hh:148
Event(sc_core::sc_event *_sc_event, bool internal=false)
Definition event.cc:81
bool inHierarchy() const
Definition event.cc:161
std::string _basename
Definition event.hh:146
void notifyDelayed(const sc_core::sc_time &t)
Definition event.cc:223
sc_core::sc_event * _sc_event
Definition event.hh:144
const std::string & name() const
Definition event.cc:149
ScEvent delayedNotify
Definition event.hh:152
std::string _name
Definition event.hh:147
sc_core::sc_object * parent
Definition event.hh:150
void cancel()
Definition event.cc:231
uint64_t _triggeredStamp
Definition event.hh:153
void delChildEvent(sc_core::sc_event *e)
Definition object.cc:268
static Object * getFromScObject(sc_core::sc_object *sc_obj)
Definition object.hh:82
const char SC_ID_INSTANCE_EXISTS_[]
Definition messages.cc:39
const char SC_ID_IMMEDIATE_NOTIFICATION_[]
Definition messages.cc:66
bool sc_is_running()
Definition sc_main.cc:141
const char SC_ID_NOTIFY_DELAYED_[]
Definition messages.cc:79
const char * sc_gen_unique_name(const char *seed)
Definition sc_module.cc:820
sc_core::sc_event * findEvent(const char *name)
Definition event.cc:256
UniqueNameGen globalNameGen
Definition module.cc:49
std::vector< sc_core::sc_event * > Events
Definition event.hh:54
std::string pickUniqueName(::sc_core::sc_object *parent, std::string base)
Definition object.cc:290
Events::iterator EventsIt
Definition object.hh:46
Events topLevelEvents
Definition event.cc:78
std::vector< StaticSensitivity * > StaticSensitivities
sc_core::sc_object * pickParentObj()
Definition object.cc:318
std::vector< DynamicSensitivity * > DynamicSensitivities
Scheduler scheduler
Definition scheduler.cc:494
Events allEvents
Definition event.cc:79
#define SC_REPORT_WARNING(msg_type, msg)
#define SC_REPORT_ERROR(msg_type, msg)
const std::string & name()
Definition trace.cc:48

Generated on Mon Oct 27 2025 04:13:04 for gem5 by doxygen 1.14.0