event.cc (13073:71fccb27bace) event.cc (13086:6a6fa249add7)
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/sc_main.hh"
41#include "systemc/ext/core/sc_module.hh"
40
41namespace sc_gem5
42{
43
42
43namespace sc_gem5
44{
45
44Event::Event(sc_core::sc_event *_sc_event) : Event(_sc_event, "") {}
46Event::Event(sc_core::sc_event *_sc_event) : Event(_sc_event, nullptr) {}
45
47
46Event::Event(sc_core::sc_event *_sc_event, const char *_basename) :
47 _sc_event(_sc_event), _basename(_basename),
48Event::Event(sc_core::sc_event *_sc_event, const char *_basename_cstr) :
49 _sc_event(_sc_event), _basename(_basename_cstr ? _basename_cstr : ""),
48 delayedNotify([this]() { this->notify(); })
49{
50 Module *p = currentModule();
51
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
52 if (p)
53 parent = p->obj()->sc_obj();
54 else if (scheduler.current())
55 parent = scheduler.current();
56 else
57 parent = nullptr;
58
59 if (parent) {
60 Object *obj = Object::getFromScObject(parent);
61 obj->addChildEvent(_sc_event);
62 } else {
63 topLevelEvents.emplace(topLevelEvents.end(), _sc_event);
64 }
65
66 if (parent)
67 _name = std::string(parent->name()) + "." + _basename;
68 else
69 _name = _basename;
70
71 allEvents.emplace(allEvents.end(), _sc_event);
72
73 // Determine if we're in the hierarchy (created once initialization starts
74 // means no).
75}
76
77Event::~Event()
78{
79 if (parent) {
80 Object *obj = Object::getFromScObject(parent);
81 obj->delChildEvent(_sc_event);
82 } else {
83 EventsIt it = find(topLevelEvents.begin(), topLevelEvents.end(),
84 _sc_event);
85 assert(it != topLevelEvents.end());
86 std::swap(*it, topLevelEvents.back());
87 topLevelEvents.pop_back();
88 }
89
90 EventsIt it = findEvent(_name);
91 std::swap(*it, allEvents.back());
92 allEvents.pop_back();
93
94 if (delayedNotify.scheduled())
95 scheduler.deschedule(&delayedNotify);
96}
97
98const std::string &
99Event::name() const
100{
101 return _name;
102}
103
104const std::string &
105Event::basename() const
106{
107 return _basename;
108}
109
110bool
111Event::inHierarchy() const
112{
113 return _name.length() != 0;
114}
115
116sc_core::sc_object *
117Event::getParentObject() const
118{
119 return parent;
120}
121
122void
123Event::notify()
124{
125 // An immediate notification overrides any pending delayed notification.
126 if (delayedNotify.scheduled())
127 scheduler.deschedule(&delayedNotify);
128
129 auto local_sensitivities = sensitivities;
130 for (auto s: local_sensitivities)
131 s->notify(this);
132}
133
134void
135Event::notify(const sc_core::sc_time &t)
136{
137 if (delayedNotify.scheduled()) {
138 if (scheduler.delayed(t) >= delayedNotify.when())
139 return;
140
141 scheduler.deschedule(&delayedNotify);
142 }
143 scheduler.schedule(&delayedNotify, t);
144}
145
146void
147Event::cancel()
148{
149 if (delayedNotify.scheduled())
150 scheduler.deschedule(&delayedNotify);
151}
152
153bool
154Event::triggered() const
155{
156 return false;
157}
158
159Events topLevelEvents;
160Events allEvents;
161
162EventsIt
163findEvent(const std::string &name)
164{
165 EventsIt it;
166 for (it = allEvents.begin(); it != allEvents.end(); it++)
167 if (!strcmp((*it)->name(), name.c_str()))
168 break;
169
170 return it;
171}
172
173} // namespace sc_gem5
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 if (parent) {
65 Object *obj = Object::getFromScObject(parent);
66 obj->addChildEvent(_sc_event);
67 } else {
68 topLevelEvents.emplace(topLevelEvents.end(), _sc_event);
69 }
70
71 if (parent)
72 _name = std::string(parent->name()) + "." + _basename;
73 else
74 _name = _basename;
75
76 allEvents.emplace(allEvents.end(), _sc_event);
77
78 // Determine if we're in the hierarchy (created once initialization starts
79 // means no).
80}
81
82Event::~Event()
83{
84 if (parent) {
85 Object *obj = Object::getFromScObject(parent);
86 obj->delChildEvent(_sc_event);
87 } else {
88 EventsIt it = find(topLevelEvents.begin(), topLevelEvents.end(),
89 _sc_event);
90 assert(it != topLevelEvents.end());
91 std::swap(*it, topLevelEvents.back());
92 topLevelEvents.pop_back();
93 }
94
95 EventsIt it = findEvent(_name);
96 std::swap(*it, allEvents.back());
97 allEvents.pop_back();
98
99 if (delayedNotify.scheduled())
100 scheduler.deschedule(&delayedNotify);
101}
102
103const std::string &
104Event::name() const
105{
106 return _name;
107}
108
109const std::string &
110Event::basename() const
111{
112 return _basename;
113}
114
115bool
116Event::inHierarchy() const
117{
118 return _name.length() != 0;
119}
120
121sc_core::sc_object *
122Event::getParentObject() const
123{
124 return parent;
125}
126
127void
128Event::notify()
129{
130 // An immediate notification overrides any pending delayed notification.
131 if (delayedNotify.scheduled())
132 scheduler.deschedule(&delayedNotify);
133
134 auto local_sensitivities = sensitivities;
135 for (auto s: local_sensitivities)
136 s->notify(this);
137}
138
139void
140Event::notify(const sc_core::sc_time &t)
141{
142 if (delayedNotify.scheduled()) {
143 if (scheduler.delayed(t) >= delayedNotify.when())
144 return;
145
146 scheduler.deschedule(&delayedNotify);
147 }
148 scheduler.schedule(&delayedNotify, t);
149}
150
151void
152Event::cancel()
153{
154 if (delayedNotify.scheduled())
155 scheduler.deschedule(&delayedNotify);
156}
157
158bool
159Event::triggered() const
160{
161 return false;
162}
163
164Events topLevelEvents;
165Events allEvents;
166
167EventsIt
168findEvent(const std::string &name)
169{
170 EventsIt it;
171 for (it = allEvents.begin(); it != allEvents.end(); it++)
172 if (!strcmp((*it)->name(), name.c_str()))
173 break;
174
175 return it;
176}
177
178} // namespace sc_gem5