event.cc revision 13179
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) : Event(_sc_event, nullptr) {} 47 48Event::Event(sc_core::sc_event *_sc_event, const char *_basename_cstr) : 49 _sc_event(_sc_event), _basename(_basename_cstr ? _basename_cstr : ""), 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 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 std::string original_name = _basename; 65 _basename = pickUniqueName(parent, _basename); 66 67 if (parent) { 68 Object *obj = Object::getFromScObject(parent); 69 obj->addChildEvent(_sc_event); 70 } else { 71 topLevelEvents.emplace(topLevelEvents.end(), _sc_event); 72 } 73 74 std::string path = parent ? (std::string(parent->name()) + ".") : ""; 75 76 if (original_name != "" && _basename != original_name) { 77 std::string message = path + original_name + 78 ". Latter declaration will be renamed to " + 79 path + _basename; 80 SC_REPORT_WARNING("(W505) object already exists", message.c_str()); 81 } 82 83 _name = path + _basename; 84 85 allEvents.emplace(allEvents.end(), _sc_event); 86 87 // Determine if we're in the hierarchy (created once initialization starts 88 // means no). 89} 90 91Event::~Event() 92{ 93 if (parent) { 94 Object *obj = Object::getFromScObject(parent); 95 obj->delChildEvent(_sc_event); 96 } else { 97 EventsIt it = find(topLevelEvents.begin(), topLevelEvents.end(), 98 _sc_event); 99 assert(it != topLevelEvents.end()); 100 std::swap(*it, topLevelEvents.back()); 101 topLevelEvents.pop_back(); 102 } 103 104 EventsIt it = findEvent(_name); 105 std::swap(*it, allEvents.back()); 106 allEvents.pop_back(); 107 108 if (delayedNotify.scheduled()) 109 scheduler.deschedule(&delayedNotify); 110} 111 112const std::string & 113Event::name() const 114{ 115 return _name; 116} 117 118const std::string & 119Event::basename() const 120{ 121 return _basename; 122} 123 124bool 125Event::inHierarchy() const 126{ 127 return _name.length() != 0; 128} 129 130sc_core::sc_object * 131Event::getParentObject() const 132{ 133 return parent; 134} 135 136void 137Event::notify() 138{ 139 // An immediate notification overrides any pending delayed notification. 140 if (delayedNotify.scheduled()) 141 scheduler.deschedule(&delayedNotify); 142 143 auto local_sensitivities = sensitivities; 144 for (auto s: local_sensitivities) 145 s->notify(this); 146} 147 148void 149Event::notify(const sc_core::sc_time &t) 150{ 151 if (delayedNotify.scheduled()) { 152 if (scheduler.delayed(t) >= delayedNotify.when()) 153 return; 154 155 scheduler.deschedule(&delayedNotify); 156 } 157 scheduler.schedule(&delayedNotify, t); 158} 159 160void 161Event::cancel() 162{ 163 if (delayedNotify.scheduled()) 164 scheduler.deschedule(&delayedNotify); 165} 166 167bool 168Event::triggered() const 169{ 170 return false; 171} 172 173Events topLevelEvents; 174Events allEvents; 175 176EventsIt 177findEvent(const std::string &name) 178{ 179 EventsIt it; 180 for (it = allEvents.begin(); it != allEvents.end(); it++) 181 if (!strcmp((*it)->name(), name.c_str())) 182 break; 183 184 return it; 185} 186 187} // namespace sc_gem5 188