Consumer.hh revision 9465
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 296154Snate@binkert.org/* 306145Snate@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 */ 347039Snate@binkert.org 357039Snate@binkert.org#ifndef __MEM_RUBY_COMMON_CONSUMER_HH__ 367039Snate@binkert.org#define __MEM_RUBY_COMMON_CONSUMER_HH__ 377039Snate@binkert.org 387039Snate@binkert.org#include <iostream> 396145Snate@binkert.org#include <set> 406145Snate@binkert.org 417039Snate@binkert.org#include "mem/ruby/common/TypeDefines.hh" 427039Snate@binkert.org#include "sim/clocked_object.hh" 436145Snate@binkert.org 447039Snate@binkert.orgclass Consumer 457039Snate@binkert.org{ 467039Snate@binkert.org public: 477039Snate@binkert.org Consumer(ClockedObject *_em) 487039Snate@binkert.org : m_last_scheduled_wakeup(0), m_last_wakeup(0), em(_em) 496145Snate@binkert.org { 506145Snate@binkert.org } 516145Snate@binkert.org 526145Snate@binkert.org virtual 537039Snate@binkert.org ~Consumer() 546145Snate@binkert.org { } 556145Snate@binkert.org 567039Snate@binkert.org virtual void wakeup() = 0; 577039Snate@binkert.org virtual void print(std::ostream& out) const = 0; 586145Snate@binkert.org virtual void storeEventInfo(int info) {} 597039Snate@binkert.org 607039Snate@binkert.org const Tick& 617039Snate@binkert.org getLastScheduledWakeup() const 627039Snate@binkert.org { 637039Snate@binkert.org return m_last_scheduled_wakeup; 647039Snate@binkert.org } 656145Snate@binkert.org 666145Snate@binkert.org void 67 setLastScheduledWakeup(const Tick& time) 68 { 69 m_last_scheduled_wakeup = time; 70 } 71 72 bool 73 alreadyScheduled(Tick time) 74 { 75 return m_scheduled_wakeups.find(time) != m_scheduled_wakeups.end(); 76 } 77 78 void 79 insertScheduledWakeupTime(Tick time) 80 { 81 m_scheduled_wakeups.insert(time); 82 } 83 84 void 85 removeScheduledWakeupTime(Tick time) 86 { 87 assert(alreadyScheduled(time)); 88 m_scheduled_wakeups.erase(time); 89 } 90 91 void scheduleEvent(Time timeDelta); 92 void scheduleEventAbsolute(Time timeAbs); 93 94 private: 95 Tick m_last_scheduled_wakeup; 96 std::set<Tick> m_scheduled_wakeups; 97 Tick m_last_wakeup; 98 ClockedObject *em; 99 100 class ConsumerEvent : public Event 101 { 102 public: 103 ConsumerEvent(Consumer* _consumer) 104 : Event(Default_Pri, AutoDelete), m_consumer_ptr(_consumer) 105 { 106 } 107 108 void process() 109 { 110 m_consumer_ptr->wakeup(); 111 m_consumer_ptr->removeScheduledWakeupTime(when()); 112 } 113 114 private: 115 Consumer* m_consumer_ptr; 116 }; 117}; 118 119inline std::ostream& 120operator<<(std::ostream& out, const Consumer& obj) 121{ 122 obj.print(out); 123 out << std::flush; 124 return out; 125} 126 127#endif // __MEM_RUBY_COMMON_CONSUMER_HH__ 128