Consumer.hh revision 9230
16145Snate@binkert.org/* 26145Snate@binkert.org * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 36145Snate@binkert.org * All rights reserved. 46145Snate@binkert.org * 56145Snate@binkert.org * Redistribution and use in source and binary forms, with or without 66145Snate@binkert.org * modification, are permitted provided that the following conditions are 76145Snate@binkert.org * met: redistributions of source code must retain the above copyright 86145Snate@binkert.org * notice, this list of conditions and the following disclaimer; 96145Snate@binkert.org * redistributions in binary form must reproduce the above copyright 106145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the 116145Snate@binkert.org * documentation and/or other materials provided with the distribution; 126145Snate@binkert.org * neither the name of the copyright holders nor the names of its 136145Snate@binkert.org * contributors may be used to endorse or promote products derived from 146145Snate@binkert.org * this software without specific prior written permission. 156145Snate@binkert.org * 166145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 176145Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 186145Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 196145Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 206145Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 216145Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 226145Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 236145Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 246145Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 256145Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 266145Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 276145Snate@binkert.org */ 286145Snate@binkert.org 296145Snate@binkert.org/* 307039Snate@binkert.org * This is the virtual base class of all classes that can be the 317039Snate@binkert.org * targets of wakeup events. There is only two methods, wakeup() and 327039Snate@binkert.org * print() and no data members. 336145Snate@binkert.org */ 346145Snate@binkert.org 357039Snate@binkert.org#ifndef __MEM_RUBY_COMMON_CONSUMER_HH__ 367039Snate@binkert.org#define __MEM_RUBY_COMMON_CONSUMER_HH__ 376145Snate@binkert.org 387002Snate@binkert.org#include <iostream> 397021Stushar@csail.mit.edu#include <set> 407002Snate@binkert.org 419171Snilay@cs.wisc.edu#include "mem/ruby/common/TypeDefines.hh" 429171Snilay@cs.wisc.edu#include "sim/eventq.hh" 436145Snate@binkert.org 447039Snate@binkert.orgclass Consumer 457039Snate@binkert.org{ 467039Snate@binkert.org public: 479230Snilay@cs.wisc.edu Consumer(EventManager *_em) 489230Snilay@cs.wisc.edu : m_last_scheduled_wakeup(0), m_last_wakeup(0), em(_em) 497039Snate@binkert.org { 507039Snate@binkert.org } 516145Snate@binkert.org 527039Snate@binkert.org virtual 537039Snate@binkert.org ~Consumer() 547039Snate@binkert.org { } 556145Snate@binkert.org 567039Snate@binkert.org virtual void wakeup() = 0; 577039Snate@binkert.org virtual void print(std::ostream& out) const = 0; 587973Snilay@cs.wisc.edu virtual void storeEventInfo(int info) {} 596145Snate@binkert.org 609171Snilay@cs.wisc.edu const Tick& 617039Snate@binkert.org getLastScheduledWakeup() const 627039Snate@binkert.org { 637039Snate@binkert.org return m_last_scheduled_wakeup; 647039Snate@binkert.org } 657039Snate@binkert.org 667039Snate@binkert.org void 679171Snilay@cs.wisc.edu setLastScheduledWakeup(const Tick& time) 687039Snate@binkert.org { 697039Snate@binkert.org m_last_scheduled_wakeup = time; 707039Snate@binkert.org } 717039Snate@binkert.org 727039Snate@binkert.org bool 739171Snilay@cs.wisc.edu alreadyScheduled(Tick time) 747039Snate@binkert.org { 757039Snate@binkert.org return m_scheduled_wakeups.find(time) != m_scheduled_wakeups.end(); 767039Snate@binkert.org } 777039Snate@binkert.org 787039Snate@binkert.org void 799171Snilay@cs.wisc.edu insertScheduledWakeupTime(Tick time) 807039Snate@binkert.org { 817039Snate@binkert.org m_scheduled_wakeups.insert(time); 827039Snate@binkert.org } 837039Snate@binkert.org 847039Snate@binkert.org void 859171Snilay@cs.wisc.edu removeScheduledWakeupTime(Tick time) 867039Snate@binkert.org { 877039Snate@binkert.org assert(alreadyScheduled(time)); 887039Snate@binkert.org m_scheduled_wakeups.erase(time); 897039Snate@binkert.org } 907039Snate@binkert.org 919171Snilay@cs.wisc.edu void scheduleEvent(Time timeDelta); 929171Snilay@cs.wisc.edu void scheduleEventAbsolute(Time timeAbs); 939171Snilay@cs.wisc.edu 947039Snate@binkert.org private: 959171Snilay@cs.wisc.edu Tick m_last_scheduled_wakeup; 969171Snilay@cs.wisc.edu std::set<Tick> m_scheduled_wakeups; 979171Snilay@cs.wisc.edu Tick m_last_wakeup; 989230Snilay@cs.wisc.edu EventManager *em; 999171Snilay@cs.wisc.edu 1009171Snilay@cs.wisc.edu class ConsumerEvent : public Event 1019171Snilay@cs.wisc.edu { 1029171Snilay@cs.wisc.edu public: 1039171Snilay@cs.wisc.edu ConsumerEvent(Consumer* _consumer) 1049171Snilay@cs.wisc.edu : Event(Default_Pri, AutoDelete), m_consumer_ptr(_consumer) 1059171Snilay@cs.wisc.edu { 1069171Snilay@cs.wisc.edu } 1079171Snilay@cs.wisc.edu 1089171Snilay@cs.wisc.edu void process() 1099171Snilay@cs.wisc.edu { 1109171Snilay@cs.wisc.edu m_consumer_ptr->wakeup(); 1119171Snilay@cs.wisc.edu m_consumer_ptr->removeScheduledWakeupTime(when()); 1129171Snilay@cs.wisc.edu } 1139171Snilay@cs.wisc.edu 1149171Snilay@cs.wisc.edu private: 1159171Snilay@cs.wisc.edu Consumer* m_consumer_ptr; 1169171Snilay@cs.wisc.edu }; 1176145Snate@binkert.org}; 1186145Snate@binkert.org 1197039Snate@binkert.orginline std::ostream& 1207039Snate@binkert.orgoperator<<(std::ostream& out, const Consumer& obj) 1216145Snate@binkert.org{ 1227039Snate@binkert.org obj.print(out); 1237039Snate@binkert.org out << std::flush; 1247039Snate@binkert.org return out; 1256145Snate@binkert.org} 1266145Snate@binkert.org 1277039Snate@binkert.org#endif // __MEM_RUBY_COMMON_CONSUMER_HH__ 128