Consumer.hh revision 9499:b03b556a8fbb
17584SN/A/*
28869SAli.Saidi@ARM.com * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
37584SN/A * All rights reserved.
47584SN/A *
57584SN/A * Redistribution and use in source and binary forms, with or without
67584SN/A * modification, are permitted provided that the following conditions are
77584SN/A * met: redistributions of source code must retain the above copyright
87584SN/A * notice, this list of conditions and the following disclaimer;
97584SN/A * redistributions in binary form must reproduce the above copyright
107584SN/A * notice, this list of conditions and the following disclaimer in the
117584SN/A * documentation and/or other materials provided with the distribution;
127584SN/A * neither the name of the copyright holders nor the names of its
137584SN/A * contributors may be used to endorse or promote products derived from
147584SN/A * this software without specific prior written permission.
157584SN/A *
167584SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
177584SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
187584SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
197584SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
207584SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
217584SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
227584SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237584SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247584SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257584SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
267584SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
277584SN/A */
287584SN/A
297584SN/A/*
307584SN/A * This is the virtual base class of all classes that can be the
317584SN/A * targets of wakeup events.  There is only two methods, wakeup() and
327584SN/A * print() and no data members.
337584SN/A */
347584SN/A
357584SN/A#ifndef __MEM_RUBY_COMMON_CONSUMER_HH__
367584SN/A#define __MEM_RUBY_COMMON_CONSUMER_HH__
377584SN/A
387584SN/A#include <iostream>
397584SN/A#include <set>
4011793Sbrandon.potter@amd.com
4111793Sbrandon.potter@amd.com#include "sim/clocked_object.hh"
427584SN/A
438869SAli.Saidi@ARM.comclass Consumer
447584SN/A{
458245SN/A  public:
468245SN/A    Consumer(ClockedObject *_em)
478869SAli.Saidi@ARM.com        : m_last_scheduled_wakeup(0), m_last_wakeup(0), em(_em)
487584SN/A    {
497584SN/A    }
507584SN/A
518869SAli.Saidi@ARM.com    virtual
5212772Snikos.nikoleris@arm.com    ~Consumer()
539808Sstever@gmail.com    { }
5412086Sspwilson2@wisc.edu
5512086Sspwilson2@wisc.edu    virtual void wakeup() = 0;
567584SN/A    virtual void print(std::ostream& out) const = 0;
577584SN/A    virtual void storeEventInfo(int info) {}
587584SN/A
597584SN/A    const Tick&
607584SN/A    getLastScheduledWakeup() const
618869SAli.Saidi@ARM.com    {
627584SN/A        return m_last_scheduled_wakeup;
637584SN/A    }
647584SN/A
657584SN/A    void
668869SAli.Saidi@ARM.com    setLastScheduledWakeup(const Tick& time)
677584SN/A    {
688869SAli.Saidi@ARM.com        m_last_scheduled_wakeup = time;
698869SAli.Saidi@ARM.com    }
708869SAli.Saidi@ARM.com
718869SAli.Saidi@ARM.com    bool
728869SAli.Saidi@ARM.com    alreadyScheduled(Tick time)
738869SAli.Saidi@ARM.com    {
748869SAli.Saidi@ARM.com        return m_scheduled_wakeups.find(time) != m_scheduled_wakeups.end();
758869SAli.Saidi@ARM.com    }
768869SAli.Saidi@ARM.com
778869SAli.Saidi@ARM.com    void
788869SAli.Saidi@ARM.com    insertScheduledWakeupTime(Tick time)
798869SAli.Saidi@ARM.com    {
808869SAli.Saidi@ARM.com        m_scheduled_wakeups.insert(time);
818869SAli.Saidi@ARM.com    }
828869SAli.Saidi@ARM.com
838869SAli.Saidi@ARM.com    void
848869SAli.Saidi@ARM.com    removeScheduledWakeupTime(Tick time)
858869SAli.Saidi@ARM.com    {
868869SAli.Saidi@ARM.com        assert(alreadyScheduled(time));
878869SAli.Saidi@ARM.com        m_scheduled_wakeups.erase(time);
888869SAli.Saidi@ARM.com    }
898869SAli.Saidi@ARM.com
908869SAli.Saidi@ARM.com    void scheduleEvent(Cycles timeDelta);
918869SAli.Saidi@ARM.com    void scheduleEventAbsolute(Cycles timeAbs);
928869SAli.Saidi@ARM.com
939806Sstever@gmail.com  private:
948869SAli.Saidi@ARM.com    Tick m_last_scheduled_wakeup;
9513230Sgabeblack@google.com    std::set<Tick> m_scheduled_wakeups;
968869SAli.Saidi@ARM.com    Tick m_last_wakeup;
978869SAli.Saidi@ARM.com    ClockedObject *em;
988869SAli.Saidi@ARM.com
998869SAli.Saidi@ARM.com    class ConsumerEvent : public Event
1008869SAli.Saidi@ARM.com    {
1018869SAli.Saidi@ARM.com      public:
1028869SAli.Saidi@ARM.com          ConsumerEvent(Consumer* _consumer)
1038869SAli.Saidi@ARM.com              : Event(Default_Pri, AutoDelete), m_consumer_ptr(_consumer)
10413230Sgabeblack@google.com          {
1058869SAli.Saidi@ARM.com          }
1068869SAli.Saidi@ARM.com
10713230Sgabeblack@google.com          void process()
1088869SAli.Saidi@ARM.com          {
1098869SAli.Saidi@ARM.com              m_consumer_ptr->wakeup();
11013230Sgabeblack@google.com              m_consumer_ptr->removeScheduledWakeupTime(when());
1118869SAli.Saidi@ARM.com          }
1128869SAli.Saidi@ARM.com
1138869SAli.Saidi@ARM.com      private:
1148869SAli.Saidi@ARM.com          Consumer* m_consumer_ptr;
1158869SAli.Saidi@ARM.com    };
1168869SAli.Saidi@ARM.com};
1178869SAli.Saidi@ARM.com
1187584SN/Ainline std::ostream&
1197584SN/Aoperator<<(std::ostream& out, const Consumer& obj)
1207584SN/A{
1217584SN/A    obj.print(out);
1227584SN/A    out << std::flush;
1238869SAli.Saidi@ARM.com    return out;
1247584SN/A}
1257584SN/A
1267584SN/A#endif // __MEM_RUBY_COMMON_CONSUMER_HH__
1277584SN/A