eventq.hh revision 9099
12SN/A/*
21762SN/A * Copyright (c) 2000-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
292665Ssaidi@eecs.umich.edu *          Nathan Binkert
302SN/A */
312SN/A
322SN/A/* @file
332SN/A * EventQueue interfaces
342SN/A */
352SN/A
361354SN/A#ifndef __SIM_EVENTQ_HH__
371354SN/A#define __SIM_EVENTQ_HH__
382SN/A
392SN/A#include <algorithm>
405501Snate@binkert.org#include <cassert>
415546Snate@binkert.org#include <climits>
427004Snate@binkert.org#include <iosfwd>
432SN/A#include <string>
442SN/A
455769Snate@binkert.org#include "base/flags.hh"
462361SN/A#include "base/misc.hh"
471354SN/A#include "base/trace.hh"
486216Snate@binkert.org#include "base/types.hh"
498232Snate@binkert.org#include "debug/Event.hh"
5056SN/A#include "sim/serialize.hh"
512SN/A
525543Ssaidi@eecs.umich.educlass EventQueue;       // forward declaration
532SN/A
541354SN/Aextern EventQueue mainEventQueue;
551354SN/A
562SN/A/*
572SN/A * An item on an event queue.  The action caused by a given
582SN/A * event is specified by deriving a subclass and overriding the
592SN/A * process() member function.
605501Snate@binkert.org *
615501Snate@binkert.org * Caution, the order of members is chosen to maximize data packing.
622SN/A */
639044SAli.Saidi@ARM.comclass Event : public Serializable
642SN/A{
652SN/A    friend class EventQueue;
662SN/A
675769Snate@binkert.org  protected:
688902Sandreas.hansson@arm.com    typedef unsigned short FlagsType;
695769Snate@binkert.org    typedef ::Flags<FlagsType> Flags;
705769Snate@binkert.org
717059Snate@binkert.org    static const FlagsType PublicRead    = 0x003f; // public readable flags
727059Snate@binkert.org    static const FlagsType PublicWrite   = 0x001d; // public writable flags
737059Snate@binkert.org    static const FlagsType Squashed      = 0x0001; // has been squashed
747059Snate@binkert.org    static const FlagsType Scheduled     = 0x0002; // has been scheduled
757059Snate@binkert.org    static const FlagsType AutoDelete    = 0x0004; // delete after dispatch
767059Snate@binkert.org    static const FlagsType AutoSerialize = 0x0008; // must be serialized
777059Snate@binkert.org    static const FlagsType IsExitEvent   = 0x0010; // special exit event
787059Snate@binkert.org    static const FlagsType IsMainQueue   = 0x0020; // on main event queue
797059Snate@binkert.org    static const FlagsType Initialized   = 0x7a40; // somewhat random bits
807059Snate@binkert.org    static const FlagsType InitMask      = 0xffc0; // mask for init bits
817059Snate@binkert.org
827059Snate@binkert.org    bool
837059Snate@binkert.org    initialized() const
847059Snate@binkert.org    {
857059Snate@binkert.org        return this && (flags & InitMask) == Initialized;
867059Snate@binkert.org    }
875769Snate@binkert.org
887058Snate@binkert.org  public:
897058Snate@binkert.org    typedef int8_t Priority;
907058Snate@binkert.org
912SN/A  private:
925502Snate@binkert.org    // The event queue is now a linked list of linked lists.  The
935502Snate@binkert.org    // 'nextBin' pointer is to find the bin, where a bin is defined as
945502Snate@binkert.org    // when+priority.  All events in the same bin will be stored in a
955503Snate@binkert.org    // second linked list (a stack) maintained by the 'nextInBin'
965503Snate@binkert.org    // pointer.  The list will be accessed in LIFO order.  The end
975502Snate@binkert.org    // result is that the insert/removal in 'nextBin' is
985502Snate@binkert.org    // linear/constant, and the lookup/removal in 'nextInBin' is
995502Snate@binkert.org    // constant/constant.  Hopefully this is a significant improvement
1005502Snate@binkert.org    // over the current fully linear insertion.
1015502Snate@binkert.org    Event *nextBin;
1025502Snate@binkert.org    Event *nextInBin;
1035502Snate@binkert.org
1045602Snate@binkert.org    static Event *insertBefore(Event *event, Event *curr);
1055602Snate@binkert.org    static Event *removeItem(Event *event, Event *last);
1065501Snate@binkert.org
1075543Ssaidi@eecs.umich.edu    Tick _when;         //!< timestamp when event should be processed
1087058Snate@binkert.org    Priority _priority; //!< event priority
1095769Snate@binkert.org    Flags flags;
1104016Sstever@eecs.umich.edu
1114016Sstever@eecs.umich.edu#ifndef NDEBUG
1124016Sstever@eecs.umich.edu    /// Global counter to generate unique IDs for Event instances
1134016Sstever@eecs.umich.edu    static Counter instanceCounter;
1144016Sstever@eecs.umich.edu
1154016Sstever@eecs.umich.edu    /// This event's unique ID.  We can also use pointer values for
1164016Sstever@eecs.umich.edu    /// this but they're not consistent across runs making debugging
1174016Sstever@eecs.umich.edu    /// more difficult.  Thus we use a global counter value when
1184016Sstever@eecs.umich.edu    /// debugging.
1195501Snate@binkert.org    Counter instance;
1205605Snate@binkert.org
1215605Snate@binkert.org    /// queue to which this event belongs (though it may or may not be
1225605Snate@binkert.org    /// scheduled on this queue yet)
1235605Snate@binkert.org    EventQueue *queue;
1245501Snate@binkert.org#endif
1254016Sstever@eecs.umich.edu
1265577SSteve.Reinhardt@amd.com#ifdef EVENTQ_DEBUG
1275501Snate@binkert.org    Tick whenCreated;   //!< time created
1285501Snate@binkert.org    Tick whenScheduled; //!< time scheduled
1295501Snate@binkert.org#endif
1305502Snate@binkert.org
1315502Snate@binkert.org    void
1325605Snate@binkert.org    setWhen(Tick when, EventQueue *q)
1335502Snate@binkert.org    {
1345502Snate@binkert.org        _when = when;
1355605Snate@binkert.org#ifndef NDEBUG
1365605Snate@binkert.org        queue = q;
1375605Snate@binkert.org#endif
1385577SSteve.Reinhardt@amd.com#ifdef EVENTQ_DEBUG
1397823Ssteve.reinhardt@amd.com        whenScheduled = curTick();
1405502Snate@binkert.org#endif
1415502Snate@binkert.org    }
1425502Snate@binkert.org
1432SN/A  protected:
1445769Snate@binkert.org    /// Accessor for flags.
1455769Snate@binkert.org    Flags
1465769Snate@binkert.org    getFlags() const
1475769Snate@binkert.org    {
1485769Snate@binkert.org        return flags & PublicRead;
1495769Snate@binkert.org    }
1502SN/A
1518581Ssteve.reinhardt@amd.com    bool
1528581Ssteve.reinhardt@amd.com    isFlagSet(Flags _flags) const
1535769Snate@binkert.org    {
1547059Snate@binkert.org        assert(_flags.noneSet(~PublicRead));
1555769Snate@binkert.org        return flags.isSet(_flags);
1565769Snate@binkert.org    }
1572SN/A
1585769Snate@binkert.org    /// Accessor for flags.
1595769Snate@binkert.org    void
1605769Snate@binkert.org    setFlags(Flags _flags)
1615769Snate@binkert.org    {
1625769Snate@binkert.org        assert(_flags.noneSet(~PublicWrite));
1635769Snate@binkert.org        flags.set(_flags);
1645769Snate@binkert.org    }
1655769Snate@binkert.org
1665769Snate@binkert.org    void
1675769Snate@binkert.org    clearFlags(Flags _flags)
1685769Snate@binkert.org    {
1695769Snate@binkert.org        assert(_flags.noneSet(~PublicWrite));
1705769Snate@binkert.org        flags.clear(_flags);
1715769Snate@binkert.org    }
1725769Snate@binkert.org
1735769Snate@binkert.org    void
1745769Snate@binkert.org    clearFlags()
1755769Snate@binkert.org    {
1765769Snate@binkert.org        flags.clear(PublicWrite);
1775769Snate@binkert.org    }
1785769Snate@binkert.org
1795501Snate@binkert.org    // This function isn't really useful if TRACING_ON is not defined
1805543Ssaidi@eecs.umich.edu    virtual void trace(const char *action);     //!< trace event activity
1812SN/A
1822SN/A  public:
183396SN/A    /// Event priorities, to provide tie-breakers for events scheduled
184396SN/A    /// at the same cycle.  Most events are scheduled at the default
185396SN/A    /// priority; these values are used to control events that need to
186396SN/A    /// be ordered within a cycle.
1875501Snate@binkert.org
1887058Snate@binkert.org    /// Minimum priority
1897058Snate@binkert.org    static const Priority Minimum_Pri =          SCHAR_MIN;
1903329Sstever@eecs.umich.edu
1917058Snate@binkert.org    /// If we enable tracing on a particular cycle, do that as the
1927058Snate@binkert.org    /// very first thing so we don't miss any of the events on
1937058Snate@binkert.org    /// that cycle (even if we enter the debugger).
1947058Snate@binkert.org    static const Priority Trace_Enable_Pri =          -101;
195396SN/A
1967058Snate@binkert.org    /// Breakpoints should happen before anything else (except
1977058Snate@binkert.org    /// enabling trace output), so we don't miss any action when
1987058Snate@binkert.org    /// debugging.
1997058Snate@binkert.org    static const Priority Debug_Break_Pri =           -100;
2003329Sstever@eecs.umich.edu
2017058Snate@binkert.org    /// CPU switches schedule the new CPU's tick event for the
2027058Snate@binkert.org    /// same cycle (after unscheduling the old CPU's tick event).
2037058Snate@binkert.org    /// The switch needs to come before any tick events to make
2047058Snate@binkert.org    /// sure we don't tick both CPUs in the same cycle.
2057058Snate@binkert.org    static const Priority CPU_Switch_Pri =             -31;
206396SN/A
2077058Snate@binkert.org    /// For some reason "delayed" inter-cluster writebacks are
2087058Snate@binkert.org    /// scheduled before regular writebacks (which have default
2097058Snate@binkert.org    /// priority).  Steve?
2107058Snate@binkert.org    static const Priority Delayed_Writeback_Pri =       -1;
211396SN/A
2127058Snate@binkert.org    /// Default is zero for historical reasons.
2137058Snate@binkert.org    static const Priority Default_Pri =                  0;
214396SN/A
2157058Snate@binkert.org    /// Serailization needs to occur before tick events also, so
2167058Snate@binkert.org    /// that a serialize/unserialize is identical to an on-line
2177058Snate@binkert.org    /// CPU switch.
2187058Snate@binkert.org    static const Priority Serialize_Pri =               32;
219396SN/A
2207058Snate@binkert.org    /// CPU ticks must come after other associated CPU events
2217058Snate@binkert.org    /// (such as writebacks).
2227058Snate@binkert.org    static const Priority CPU_Tick_Pri =                50;
223396SN/A
2247058Snate@binkert.org    /// Statistics events (dump, reset, etc.) come after
2257058Snate@binkert.org    /// everything else, but before exit.
2267058Snate@binkert.org    static const Priority Stat_Event_Pri =              90;
2274075Sbinkertn@umich.edu
2287058Snate@binkert.org    /// Progress events come at the end.
2297058Snate@binkert.org    static const Priority Progress_Event_Pri =          95;
2305501Snate@binkert.org
2317058Snate@binkert.org    /// If we want to exit on this cycle, it's the very last thing
2327058Snate@binkert.org    /// we do.
2337058Snate@binkert.org    static const Priority Sim_Exit_Pri =               100;
2347058Snate@binkert.org
2357058Snate@binkert.org    /// Maximum priority
2367058Snate@binkert.org    static const Priority Maximum_Pri =          SCHAR_MAX;
237396SN/A
2382SN/A    /*
2392SN/A     * Event constructor
2402SN/A     * @param queue that the event gets scheduled on
2412SN/A     */
2428581Ssteve.reinhardt@amd.com    Event(Priority p = Default_Pri, Flags f = 0)
2438581Ssteve.reinhardt@amd.com        : nextBin(NULL), nextInBin(NULL), _priority(p),
2448581Ssteve.reinhardt@amd.com          flags(Initialized | f)
245224SN/A    {
2468581Ssteve.reinhardt@amd.com        assert(f.noneSet(~PublicWrite));
2474016Sstever@eecs.umich.edu#ifndef NDEBUG
2485501Snate@binkert.org        instance = ++instanceCounter;
2495605Snate@binkert.org        queue = NULL;
2505501Snate@binkert.org#endif
2515501Snate@binkert.org#ifdef EVENTQ_DEBUG
2527823Ssteve.reinhardt@amd.com        whenCreated = curTick();
2535501Snate@binkert.org        whenScheduled = 0;
2544016Sstever@eecs.umich.edu#endif
255224SN/A    }
256224SN/A
2575768Snate@binkert.org    virtual ~Event();
2585768Snate@binkert.org    virtual const std::string name() const;
259265SN/A
2605501Snate@binkert.org    /// Return a C string describing the event.  This string should
2615501Snate@binkert.org    /// *not* be dynamically allocated; just a const char array
2625501Snate@binkert.org    /// describing the event class.
2635501Snate@binkert.org    virtual const char *description() const;
2645501Snate@binkert.org
2655501Snate@binkert.org    /// Dump the current event data
2665501Snate@binkert.org    void dump() const;
2675501Snate@binkert.org
2685501Snate@binkert.org  public:
2695501Snate@binkert.org    /*
2705501Snate@binkert.org     * This member function is invoked when the event is processed
2715501Snate@binkert.org     * (occurs).  There is no default implementation; each subclass
2725501Snate@binkert.org     * must provide its own implementation.  The event is not
2735501Snate@binkert.org     * automatically deleted after it is processed (to allow for
2745501Snate@binkert.org     * statically allocated event objects).
2755501Snate@binkert.org     *
2765501Snate@binkert.org     * If the AutoDestroy flag is set, the object is deleted once it
2775501Snate@binkert.org     * is processed.
2785501Snate@binkert.org     */
2795501Snate@binkert.org    virtual void process() = 0;
2805501Snate@binkert.org
2812SN/A    /// Determine if the current event is scheduled
2825769Snate@binkert.org    bool scheduled() const { return flags.isSet(Scheduled); }
2832SN/A
2842SN/A    /// Squash the current event
2855769Snate@binkert.org    void squash() { flags.set(Squashed); }
2862SN/A
2872SN/A    /// Check whether the event is squashed
2885769Snate@binkert.org    bool squashed() const { return flags.isSet(Squashed); }
2892SN/A
2902667Sstever@eecs.umich.edu    /// See if this is a SimExitEvent (without resorting to RTTI)
2915769Snate@binkert.org    bool isExitEvent() const { return flags.isSet(IsExitEvent); }
2922667Sstever@eecs.umich.edu
2932SN/A    /// Get the time that the event is scheduled
2942SN/A    Tick when() const { return _when; }
2952SN/A
2962SN/A    /// Get the event priority
2977058Snate@binkert.org    Priority priority() const { return _priority; }
2982SN/A
2995605Snate@binkert.org#ifndef SWIG
3005501Snate@binkert.org    struct priority_compare
3015501Snate@binkert.org        : public std::binary_function<Event *, Event *, bool>
3022SN/A    {
3035501Snate@binkert.org        bool
3045501Snate@binkert.org        operator()(const Event *l, const Event *r) const
3055501Snate@binkert.org        {
3062SN/A            return l->when() >= r->when() || l->priority() >= r->priority();
3072SN/A        }
3082SN/A    };
309224SN/A
310224SN/A    virtual void serialize(std::ostream &os);
311237SN/A    virtual void unserialize(Checkpoint *cp, const std::string &section);
3125605Snate@binkert.org#endif
313571SN/A};
314571SN/A
3157005Snate@binkert.org#ifndef SWIG
3167005Snate@binkert.orginline bool
3177005Snate@binkert.orgoperator<(const Event &l, const Event &r)
3187005Snate@binkert.org{
3197005Snate@binkert.org    return l.when() < r.when() ||
3207005Snate@binkert.org        (l.when() == r.when() && l.priority() < r.priority());
3217005Snate@binkert.org}
3227005Snate@binkert.org
3237005Snate@binkert.orginline bool
3247005Snate@binkert.orgoperator>(const Event &l, const Event &r)
3257005Snate@binkert.org{
3267005Snate@binkert.org    return l.when() > r.when() ||
3277005Snate@binkert.org        (l.when() == r.when() && l.priority() > r.priority());
3287005Snate@binkert.org}
3297005Snate@binkert.org
3307005Snate@binkert.orginline bool
3317005Snate@binkert.orgoperator<=(const Event &l, const Event &r)
3327005Snate@binkert.org{
3337005Snate@binkert.org    return l.when() < r.when() ||
3347005Snate@binkert.org        (l.when() == r.when() && l.priority() <= r.priority());
3357005Snate@binkert.org}
3367005Snate@binkert.orginline bool
3377005Snate@binkert.orgoperator>=(const Event &l, const Event &r)
3387005Snate@binkert.org{
3397005Snate@binkert.org    return l.when() > r.when() ||
3407005Snate@binkert.org        (l.when() == r.when() && l.priority() >= r.priority());
3417005Snate@binkert.org}
3427005Snate@binkert.org
3437005Snate@binkert.orginline bool
3447005Snate@binkert.orgoperator==(const Event &l, const Event &r)
3457005Snate@binkert.org{
3467005Snate@binkert.org    return l.when() == r.when() && l.priority() == r.priority();
3477005Snate@binkert.org}
3487005Snate@binkert.org
3497005Snate@binkert.orginline bool
3507005Snate@binkert.orgoperator!=(const Event &l, const Event &r)
3517005Snate@binkert.org{
3527005Snate@binkert.org    return l.when() != r.when() || l.priority() != r.priority();
3537005Snate@binkert.org}
3547005Snate@binkert.org#endif
3557005Snate@binkert.org
3562SN/A/*
3572SN/A * Queue of events sorted in time order
3582SN/A */
359395SN/Aclass EventQueue : public Serializable
3602SN/A{
3615605Snate@binkert.org  private:
362265SN/A    std::string objName;
3632SN/A    Event *head;
3642SN/A
3652SN/A    void insert(Event *event);
3662SN/A    void remove(Event *event);
3672SN/A
3687063Snate@binkert.org    EventQueue(const EventQueue &);
3697063Snate@binkert.org    const EventQueue &operator=(const EventQueue &);
3707063Snate@binkert.org
3712SN/A  public:
3727063Snate@binkert.org    EventQueue(const std::string &n);
3732SN/A
374512SN/A    virtual const std::string name() const { return objName; }
375265SN/A
3762SN/A    // schedule the given event on this queue
3775738Snate@binkert.org    void schedule(Event *event, Tick when);
3785738Snate@binkert.org    void deschedule(Event *event);
3795738Snate@binkert.org    void reschedule(Event *event, Tick when, bool always = false);
3802SN/A
3815501Snate@binkert.org    Tick nextTick() const { return head->when(); }
3822667Sstever@eecs.umich.edu    Event *serviceOne();
3832SN/A
3842SN/A    // process all events up to the given timestamp.  we inline a
3852SN/A    // quick test to see if there are any events to process; if so,
3862SN/A    // call the internal out-of-line version to process them all.
3875501Snate@binkert.org    void
3885501Snate@binkert.org    serviceEvents(Tick when)
3895501Snate@binkert.org    {
3902SN/A        while (!empty()) {
3912SN/A            if (nextTick() > when)
3922SN/A                break;
3932SN/A
3941634SN/A            /**
3951634SN/A             * @todo this assert is a good bug catcher.  I need to
3961634SN/A             * make it true again.
3971634SN/A             */
3981634SN/A            //assert(head->when() >= when && "event scheduled in the past");
3992SN/A            serviceOne();
4002SN/A        }
4012SN/A    }
4022SN/A
4032SN/A    // return true if no events are queued
4045501Snate@binkert.org    bool empty() const { return head == NULL; }
4052SN/A
4065501Snate@binkert.org    void dump() const;
4072SN/A
4085502Snate@binkert.org    bool debugVerify() const;
4095502Snate@binkert.org
4108648Snilay@cs.wisc.edu    /**
4118648Snilay@cs.wisc.edu     *  function for replacing the head of the event queue, so that a
4128648Snilay@cs.wisc.edu     *  different set of events can run without disturbing events that have
4138648Snilay@cs.wisc.edu     *  already been scheduled. Already scheduled events can be processed
4148648Snilay@cs.wisc.edu     *  by replacing the original head back.
4158648Snilay@cs.wisc.edu     *  USING THIS FUNCTION CAN BE DANGEROUS TO THE HEALTH OF THE SIMULATOR.
4168648Snilay@cs.wisc.edu     *  NOT RECOMMENDED FOR USE.
4178648Snilay@cs.wisc.edu     */
4188648Snilay@cs.wisc.edu    Event* replaceHead(Event* s);
4198648Snilay@cs.wisc.edu
4205605Snate@binkert.org#ifndef SWIG
421217SN/A    virtual void serialize(std::ostream &os);
422237SN/A    virtual void unserialize(Checkpoint *cp, const std::string &section);
4235605Snate@binkert.org#endif
4242SN/A};
4252SN/A
4265605Snate@binkert.org#ifndef SWIG
4275605Snate@binkert.orgclass EventManager
4285605Snate@binkert.org{
4295605Snate@binkert.org  protected:
4305605Snate@binkert.org    /** A pointer to this object's event queue */
4315605Snate@binkert.org    EventQueue *eventq;
4322SN/A
4335605Snate@binkert.org  public:
4349099Sandreas.hansson@arm.com    EventManager(EventManager &em) : eventq(em.eventq) {}
4359099Sandreas.hansson@arm.com    EventManager(EventManager *em) : eventq(em ? em->eventq : NULL) {}
4365605Snate@binkert.org    EventManager(EventQueue *eq) : eventq(eq) {}
4372SN/A
4385605Snate@binkert.org    EventQueue *
4399099Sandreas.hansson@arm.com    eventQueue() const
4407060Snate@binkert.org    {
4417060Snate@binkert.org        return eventq;
4427060Snate@binkert.org    }
4437060Snate@binkert.org
4445605Snate@binkert.org    void
4455605Snate@binkert.org    schedule(Event &event, Tick when)
4465605Snate@binkert.org    {
4475605Snate@binkert.org        eventq->schedule(&event, when);
4485605Snate@binkert.org    }
4495605Snate@binkert.org
4505605Snate@binkert.org    void
4515605Snate@binkert.org    deschedule(Event &event)
4525605Snate@binkert.org    {
4535605Snate@binkert.org        eventq->deschedule(&event);
4545605Snate@binkert.org    }
4555605Snate@binkert.org
4565605Snate@binkert.org    void
4575605Snate@binkert.org    reschedule(Event &event, Tick when, bool always = false)
4585605Snate@binkert.org    {
4595605Snate@binkert.org        eventq->reschedule(&event, when, always);
4605605Snate@binkert.org    }
4615605Snate@binkert.org
4625605Snate@binkert.org    void
4635605Snate@binkert.org    schedule(Event *event, Tick when)
4645605Snate@binkert.org    {
4655605Snate@binkert.org        eventq->schedule(event, when);
4665605Snate@binkert.org    }
4675605Snate@binkert.org
4685605Snate@binkert.org    void
4695605Snate@binkert.org    deschedule(Event *event)
4705605Snate@binkert.org    {
4715605Snate@binkert.org        eventq->deschedule(event);
4725605Snate@binkert.org    }
4735605Snate@binkert.org
4745605Snate@binkert.org    void
4755605Snate@binkert.org    reschedule(Event *event, Tick when, bool always = false)
4765605Snate@binkert.org    {
4775605Snate@binkert.org        eventq->reschedule(event, when, always);
4785605Snate@binkert.org    }
4795605Snate@binkert.org};
4805605Snate@binkert.org
4812SN/Ainline void
4825605Snate@binkert.orgEventQueue::schedule(Event *event, Tick when)
4832SN/A{
4848186Sksewell@umich.edu    // Typecasting Tick->Utick here since gcc
4858186Sksewell@umich.edu    // complains about signed overflow
4867823Ssteve.reinhardt@amd.com    assert((UTick)when >= (UTick)curTick());
4875605Snate@binkert.org    assert(!event->scheduled());
4887059Snate@binkert.org    assert(event->initialized());
4895605Snate@binkert.org
4905605Snate@binkert.org    event->setWhen(when, this);
4915605Snate@binkert.org    insert(event);
4925769Snate@binkert.org    event->flags.set(Event::Scheduled);
4935605Snate@binkert.org    if (this == &mainEventQueue)
4945769Snate@binkert.org        event->flags.set(Event::IsMainQueue);
4955605Snate@binkert.org    else
4965769Snate@binkert.org        event->flags.clear(Event::IsMainQueue);
4975605Snate@binkert.org
4985605Snate@binkert.org    if (DTRACE(Event))
4995605Snate@binkert.org        event->trace("scheduled");
5002SN/A}
5012SN/A
5022SN/Ainline void
5035605Snate@binkert.orgEventQueue::deschedule(Event *event)
5042SN/A{
5055605Snate@binkert.org    assert(event->scheduled());
5067059Snate@binkert.org    assert(event->initialized());
5075605Snate@binkert.org
5085605Snate@binkert.org    remove(event);
5095605Snate@binkert.org
5105769Snate@binkert.org    event->flags.clear(Event::Squashed);
5115769Snate@binkert.org    event->flags.clear(Event::Scheduled);
5125605Snate@binkert.org
5138990SAli.Saidi@ARM.com    if (DTRACE(Event))
5148990SAli.Saidi@ARM.com        event->trace("descheduled");
5158990SAli.Saidi@ARM.com
5165769Snate@binkert.org    if (event->flags.isSet(Event::AutoDelete))
5175605Snate@binkert.org        delete event;
5182SN/A}
5192SN/A
5202SN/Ainline void
5215605Snate@binkert.orgEventQueue::reschedule(Event *event, Tick when, bool always)
5222SN/A{
5238186Sksewell@umich.edu    // Typecasting Tick->Utick here since gcc
5248186Sksewell@umich.edu    // complains about signed overflow
5258186Sksewell@umich.edu    assert((UTick)when >= (UTick)curTick());
5265605Snate@binkert.org    assert(always || event->scheduled());
5277059Snate@binkert.org    assert(event->initialized());
5285605Snate@binkert.org
5295605Snate@binkert.org    if (event->scheduled())
5305605Snate@binkert.org        remove(event);
5315605Snate@binkert.org
5325605Snate@binkert.org    event->setWhen(when, this);
5335605Snate@binkert.org    insert(event);
5345769Snate@binkert.org    event->flags.clear(Event::Squashed);
5355769Snate@binkert.org    event->flags.set(Event::Scheduled);
5365605Snate@binkert.org    if (this == &mainEventQueue)
5375769Snate@binkert.org        event->flags.set(Event::IsMainQueue);
5385605Snate@binkert.org    else
5395769Snate@binkert.org        event->flags.clear(Event::IsMainQueue);
5405605Snate@binkert.org
5415605Snate@binkert.org    if (DTRACE(Event))
5425605Snate@binkert.org        event->trace("rescheduled");
5432SN/A}
5442SN/A
5457005Snate@binkert.orgtemplate <class T, void (T::* F)()>
5467005Snate@binkert.orgvoid
5477005Snate@binkert.orgDelayFunction(EventQueue *eventq, Tick when, T *object)
5485502Snate@binkert.org{
5497005Snate@binkert.org    class DelayEvent : public Event
5507005Snate@binkert.org    {
5517005Snate@binkert.org      private:
5527005Snate@binkert.org        T *object;
5537005Snate@binkert.org
5547005Snate@binkert.org      public:
5557005Snate@binkert.org        DelayEvent(T *o)
5568581Ssteve.reinhardt@amd.com            : Event(Default_Pri, AutoDelete), object(o)
5578581Ssteve.reinhardt@amd.com        { }
5587005Snate@binkert.org        void process() { (object->*F)(); }
5597005Snate@binkert.org        const char *description() const { return "delay"; }
5607005Snate@binkert.org    };
5617005Snate@binkert.org
5627005Snate@binkert.org    eventq->schedule(new DelayEvent(object), when);
5635502Snate@binkert.org}
5645502Snate@binkert.org
5657005Snate@binkert.orgtemplate <class T, void (T::* F)()>
5667005Snate@binkert.orgclass EventWrapper : public Event
5675502Snate@binkert.org{
5687005Snate@binkert.org  private:
5697005Snate@binkert.org    T *object;
5705502Snate@binkert.org
5717005Snate@binkert.org  public:
5727005Snate@binkert.org    EventWrapper(T *obj, bool del = false, Priority p = Default_Pri)
5737005Snate@binkert.org        : Event(p), object(obj)
5747005Snate@binkert.org    {
5757005Snate@binkert.org        if (del)
5767005Snate@binkert.org            setFlags(AutoDelete);
5777005Snate@binkert.org    }
5785502Snate@binkert.org
5797066Snate@binkert.org    EventWrapper(T &obj, bool del = false, Priority p = Default_Pri)
5807066Snate@binkert.org        : Event(p), object(&obj)
5817066Snate@binkert.org    {
5827066Snate@binkert.org        if (del)
5837066Snate@binkert.org            setFlags(AutoDelete);
5847066Snate@binkert.org    }
5857066Snate@binkert.org
5867005Snate@binkert.org    void process() { (object->*F)(); }
5875502Snate@binkert.org
5887005Snate@binkert.org    const std::string
5897005Snate@binkert.org    name() const
5907005Snate@binkert.org    {
5917005Snate@binkert.org        return object->name() + ".wrapped_event";
5927005Snate@binkert.org    }
5937005Snate@binkert.org
5947005Snate@binkert.org    const char *description() const { return "EventWrapped"; }
5957005Snate@binkert.org};
5965605Snate@binkert.org#endif
5972SN/A
5981354SN/A#endif // __SIM_EVENTQ_HH__
599