event.cc revision 13303
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, 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; 81 SC_REPORT_WARNING("(W505) object already exists", 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{ 162 if (scheduler.inUpdate()) { 163 SC_REPORT_ERROR("(E521) immediate notification is not allowed " 164 "during update phase or elaboration", ""); 165 } 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 } 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 238