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