eventq.hh revision 5769
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>
422SN/A#include <map>
432SN/A#include <string>
442SN/A#include <vector>
452SN/A
4656SN/A#include "base/fast_alloc.hh"
475769Snate@binkert.org#include "base/flags.hh"
482361SN/A#include "base/misc.hh"
491354SN/A#include "base/trace.hh"
5056SN/A#include "sim/serialize.hh"
515501Snate@binkert.org#include "sim/host.hh"
522SN/A
535543Ssaidi@eecs.umich.educlass EventQueue;       // forward declaration
542SN/A
551354SN/Aextern EventQueue mainEventQueue;
561354SN/A
572SN/A/*
582SN/A * An item on an event queue.  The action caused by a given
592SN/A * event is specified by deriving a subclass and overriding the
602SN/A * process() member function.
615501Snate@binkert.org *
625501Snate@binkert.org * Caution, the order of members is chosen to maximize data packing.
632SN/A */
64395SN/Aclass Event : public Serializable, public FastAlloc
652SN/A{
662SN/A    friend class EventQueue;
672SN/A
685769Snate@binkert.org  protected:
695769Snate@binkert.org    typedef short FlagsType;
705769Snate@binkert.org    typedef ::Flags<FlagsType> Flags;
715769Snate@binkert.org
725769Snate@binkert.org    static const FlagsType PublicRead    = 0x003f;
735769Snate@binkert.org    static const FlagsType PublicWrite   = 0x001d;
745769Snate@binkert.org    static const FlagsType Squashed      = 0x0001;
755769Snate@binkert.org    static const FlagsType Scheduled     = 0x0002;
765769Snate@binkert.org    static const FlagsType AutoDelete    = 0x0004;
775769Snate@binkert.org    static const FlagsType AutoSerialize = 0x0008;
785769Snate@binkert.org    static const FlagsType IsExitEvent   = 0x0010;
795769Snate@binkert.org    static const FlagsType IsMainQueue   = 0x0020;
805769Snate@binkert.org
812SN/A  private:
825502Snate@binkert.org    // The event queue is now a linked list of linked lists.  The
835502Snate@binkert.org    // 'nextBin' pointer is to find the bin, where a bin is defined as
845502Snate@binkert.org    // when+priority.  All events in the same bin will be stored in a
855503Snate@binkert.org    // second linked list (a stack) maintained by the 'nextInBin'
865503Snate@binkert.org    // pointer.  The list will be accessed in LIFO order.  The end
875502Snate@binkert.org    // result is that the insert/removal in 'nextBin' is
885502Snate@binkert.org    // linear/constant, and the lookup/removal in 'nextInBin' is
895502Snate@binkert.org    // constant/constant.  Hopefully this is a significant improvement
905502Snate@binkert.org    // over the current fully linear insertion.
915502Snate@binkert.org    Event *nextBin;
925502Snate@binkert.org    Event *nextInBin;
935502Snate@binkert.org
945602Snate@binkert.org    static Event *insertBefore(Event *event, Event *curr);
955602Snate@binkert.org    static Event *removeItem(Event *event, Event *last);
965501Snate@binkert.org
975543Ssaidi@eecs.umich.edu    Tick _when;         //!< timestamp when event should be processed
985543Ssaidi@eecs.umich.edu    short _priority;    //!< event priority
995769Snate@binkert.org    Flags flags;
1004016Sstever@eecs.umich.edu
1014016Sstever@eecs.umich.edu#ifndef NDEBUG
1024016Sstever@eecs.umich.edu    /// Global counter to generate unique IDs for Event instances
1034016Sstever@eecs.umich.edu    static Counter instanceCounter;
1044016Sstever@eecs.umich.edu
1054016Sstever@eecs.umich.edu    /// This event's unique ID.  We can also use pointer values for
1064016Sstever@eecs.umich.edu    /// this but they're not consistent across runs making debugging
1074016Sstever@eecs.umich.edu    /// more difficult.  Thus we use a global counter value when
1084016Sstever@eecs.umich.edu    /// debugging.
1095501Snate@binkert.org    Counter instance;
1105605Snate@binkert.org
1115605Snate@binkert.org    /// queue to which this event belongs (though it may or may not be
1125605Snate@binkert.org    /// scheduled on this queue yet)
1135605Snate@binkert.org    EventQueue *queue;
1145501Snate@binkert.org#endif
1154016Sstever@eecs.umich.edu
1165577SSteve.Reinhardt@amd.com#ifdef EVENTQ_DEBUG
1175501Snate@binkert.org    Tick whenCreated;   //!< time created
1185501Snate@binkert.org    Tick whenScheduled; //!< time scheduled
1195501Snate@binkert.org#endif
1205502Snate@binkert.org
1215502Snate@binkert.org    void
1225605Snate@binkert.org    setWhen(Tick when, EventQueue *q)
1235502Snate@binkert.org    {
1245502Snate@binkert.org        _when = when;
1255605Snate@binkert.org#ifndef NDEBUG
1265605Snate@binkert.org        queue = q;
1275605Snate@binkert.org#endif
1285577SSteve.Reinhardt@amd.com#ifdef EVENTQ_DEBUG
1295502Snate@binkert.org        whenScheduled = curTick;
1305502Snate@binkert.org#endif
1315502Snate@binkert.org    }
1325502Snate@binkert.org
1332SN/A  protected:
1345769Snate@binkert.org    /// Accessor for flags.
1355769Snate@binkert.org    Flags
1365769Snate@binkert.org    getFlags() const
1375769Snate@binkert.org    {
1385769Snate@binkert.org        return flags & PublicRead;
1395769Snate@binkert.org    }
1402SN/A
1415769Snate@binkert.org    Flags
1425769Snate@binkert.org    getFlags(Flags _flags) const
1435769Snate@binkert.org    {
1445769Snate@binkert.org        assert(flags.noneSet(~PublicRead));
1455769Snate@binkert.org        return flags.isSet(_flags);
1465769Snate@binkert.org    }
1472SN/A
1485769Snate@binkert.org    Flags
1495769Snate@binkert.org    allFlags(Flags _flags) const
1505769Snate@binkert.org    {
1515769Snate@binkert.org        assert(_flags.noneSet(~PublicRead));
1525769Snate@binkert.org        return flags.allSet(_flags);
1535769Snate@binkert.org    }
1545769Snate@binkert.org
1555769Snate@binkert.org    /// Accessor for flags.
1565769Snate@binkert.org    void
1575769Snate@binkert.org    setFlags(Flags _flags)
1585769Snate@binkert.org    {
1595769Snate@binkert.org        assert(_flags.noneSet(~PublicWrite));
1605769Snate@binkert.org        flags.set(_flags);
1615769Snate@binkert.org    }
1625769Snate@binkert.org
1635769Snate@binkert.org    void
1645769Snate@binkert.org    clearFlags(Flags _flags)
1655769Snate@binkert.org    {
1665769Snate@binkert.org        assert(_flags.noneSet(~PublicWrite));
1675769Snate@binkert.org        flags.clear(_flags);
1685769Snate@binkert.org    }
1695769Snate@binkert.org
1705769Snate@binkert.org    void
1715769Snate@binkert.org    clearFlags()
1725769Snate@binkert.org    {
1735769Snate@binkert.org        flags.clear(PublicWrite);
1745769Snate@binkert.org    }
1755769Snate@binkert.org
1765501Snate@binkert.org    // This function isn't really useful if TRACING_ON is not defined
1775543Ssaidi@eecs.umich.edu    virtual void trace(const char *action);     //!< trace event activity
1782SN/A
1792SN/A  public:
180396SN/A    /// Event priorities, to provide tie-breakers for events scheduled
181396SN/A    /// at the same cycle.  Most events are scheduled at the default
182396SN/A    /// priority; these values are used to control events that need to
183396SN/A    /// be ordered within a cycle.
184396SN/A    enum Priority {
1855501Snate@binkert.org        /// Minimum priority
1865543Ssaidi@eecs.umich.edu        Minimum_Pri             = SHRT_MIN,
1875501Snate@binkert.org
1883329Sstever@eecs.umich.edu        /// If we enable tracing on a particular cycle, do that as the
1893329Sstever@eecs.umich.edu        /// very first thing so we don't miss any of the events on
1903329Sstever@eecs.umich.edu        /// that cycle (even if we enter the debugger).
1913329Sstever@eecs.umich.edu        Trace_Enable_Pri        = -101,
1923329Sstever@eecs.umich.edu
1933329Sstever@eecs.umich.edu        /// Breakpoints should happen before anything else (except
1943329Sstever@eecs.umich.edu        /// enabling trace output), so we don't miss any action when
1953329Sstever@eecs.umich.edu        /// debugging.
1965543Ssaidi@eecs.umich.edu        Debug_Break_Pri         = -100,
197396SN/A
1983329Sstever@eecs.umich.edu        /// CPU switches schedule the new CPU's tick event for the
1993329Sstever@eecs.umich.edu        /// same cycle (after unscheduling the old CPU's tick event).
2003329Sstever@eecs.umich.edu        /// The switch needs to come before any tick events to make
2013329Sstever@eecs.umich.edu        /// sure we don't tick both CPUs in the same cycle.
2025543Ssaidi@eecs.umich.edu        CPU_Switch_Pri          =   -31,
2033329Sstever@eecs.umich.edu
204396SN/A        /// For some reason "delayed" inter-cluster writebacks are
205396SN/A        /// scheduled before regular writebacks (which have default
206396SN/A        /// priority).  Steve?
2075543Ssaidi@eecs.umich.edu        Delayed_Writeback_Pri   =   -1,
208396SN/A
209396SN/A        /// Default is zero for historical reasons.
2105543Ssaidi@eecs.umich.edu        Default_Pri             =    0,
211396SN/A
212396SN/A        /// Serailization needs to occur before tick events also, so
213396SN/A        /// that a serialize/unserialize is identical to an on-line
214396SN/A        /// CPU switch.
2155543Ssaidi@eecs.umich.edu        Serialize_Pri           =   32,
216396SN/A
217396SN/A        /// CPU ticks must come after other associated CPU events
218396SN/A        /// (such as writebacks).
2195543Ssaidi@eecs.umich.edu        CPU_Tick_Pri            =   50,
220396SN/A
221396SN/A        /// Statistics events (dump, reset, etc.) come after
222396SN/A        /// everything else, but before exit.
2235543Ssaidi@eecs.umich.edu        Stat_Event_Pri          =   90,
224396SN/A
2254075Sbinkertn@umich.edu        /// Progress events come at the end.
2264075Sbinkertn@umich.edu        Progress_Event_Pri      =   95,
2274075Sbinkertn@umich.edu
228396SN/A        /// If we want to exit on this cycle, it's the very last thing
229396SN/A        /// we do.
2305543Ssaidi@eecs.umich.edu        Sim_Exit_Pri            =  100,
2315501Snate@binkert.org
2325501Snate@binkert.org        /// Maximum priority
2335543Ssaidi@eecs.umich.edu        Maximum_Pri             = SHRT_MAX
234396SN/A    };
235396SN/A
2362SN/A    /*
2372SN/A     * Event constructor
2382SN/A     * @param queue that the event gets scheduled on
2392SN/A     */
2405605Snate@binkert.org    Event(Priority p = Default_Pri)
2415769Snate@binkert.org        : nextBin(NULL), nextInBin(NULL), _priority(p)
242224SN/A    {
2434016Sstever@eecs.umich.edu#ifndef NDEBUG
2445501Snate@binkert.org        instance = ++instanceCounter;
2455605Snate@binkert.org        queue = NULL;
2465501Snate@binkert.org#endif
2475501Snate@binkert.org#ifdef EVENTQ_DEBUG
2485501Snate@binkert.org        whenCreated = curTick;
2495501Snate@binkert.org        whenScheduled = 0;
2504016Sstever@eecs.umich.edu#endif
251224SN/A    }
252224SN/A
2535768Snate@binkert.org    virtual ~Event();
2545768Snate@binkert.org    virtual const std::string name() const;
255265SN/A
2565501Snate@binkert.org    /// Return a C string describing the event.  This string should
2575501Snate@binkert.org    /// *not* be dynamically allocated; just a const char array
2585501Snate@binkert.org    /// describing the event class.
2595501Snate@binkert.org    virtual const char *description() const;
2605501Snate@binkert.org
2615501Snate@binkert.org    /// Dump the current event data
2625501Snate@binkert.org    void dump() const;
2635501Snate@binkert.org
2645501Snate@binkert.org  public:
2655501Snate@binkert.org    /*
2665501Snate@binkert.org     * This member function is invoked when the event is processed
2675501Snate@binkert.org     * (occurs).  There is no default implementation; each subclass
2685501Snate@binkert.org     * must provide its own implementation.  The event is not
2695501Snate@binkert.org     * automatically deleted after it is processed (to allow for
2705501Snate@binkert.org     * statically allocated event objects).
2715501Snate@binkert.org     *
2725501Snate@binkert.org     * If the AutoDestroy flag is set, the object is deleted once it
2735501Snate@binkert.org     * is processed.
2745501Snate@binkert.org     */
2755501Snate@binkert.org    virtual void process() = 0;
2765501Snate@binkert.org
2772SN/A    /// Determine if the current event is scheduled
2785769Snate@binkert.org    bool scheduled() const { return flags.isSet(Scheduled); }
2792SN/A
2802SN/A    /// Squash the current event
2815769Snate@binkert.org    void squash() { flags.set(Squashed); }
2822SN/A
2832SN/A    /// Check whether the event is squashed
2845769Snate@binkert.org    bool squashed() const { return flags.isSet(Squashed); }
2852SN/A
2862667Sstever@eecs.umich.edu    /// See if this is a SimExitEvent (without resorting to RTTI)
2875769Snate@binkert.org    bool isExitEvent() const { return flags.isSet(IsExitEvent); }
2882667Sstever@eecs.umich.edu
2892SN/A    /// Get the time that the event is scheduled
2902SN/A    Tick when() const { return _when; }
2912SN/A
2922SN/A    /// Get the event priority
2932SN/A    int priority() const { return _priority; }
2942SN/A
2955605Snate@binkert.org#ifndef SWIG
2965501Snate@binkert.org    struct priority_compare
2975501Snate@binkert.org        : public std::binary_function<Event *, Event *, bool>
2982SN/A    {
2995501Snate@binkert.org        bool
3005501Snate@binkert.org        operator()(const Event *l, const Event *r) const
3015501Snate@binkert.org        {
3022SN/A            return l->when() >= r->when() || l->priority() >= r->priority();
3032SN/A        }
3042SN/A    };
305224SN/A
306224SN/A    virtual void serialize(std::ostream &os);
307237SN/A    virtual void unserialize(Checkpoint *cp, const std::string &section);
3085605Snate@binkert.org#endif
309571SN/A};
310571SN/A
3112SN/A/*
3122SN/A * Queue of events sorted in time order
3132SN/A */
314395SN/Aclass EventQueue : public Serializable
3152SN/A{
3165605Snate@binkert.org  private:
317265SN/A    std::string objName;
3182SN/A    Event *head;
3192SN/A
3202SN/A    void insert(Event *event);
3212SN/A    void remove(Event *event);
3222SN/A
3232SN/A  public:
3242SN/A    EventQueue(const std::string &n)
325265SN/A        : objName(n), head(NULL)
3262SN/A    {}
3272SN/A
328512SN/A    virtual const std::string name() const { return objName; }
329265SN/A
3302SN/A    // schedule the given event on this queue
3315738Snate@binkert.org    void schedule(Event *event, Tick when);
3325738Snate@binkert.org    void deschedule(Event *event);
3335738Snate@binkert.org    void reschedule(Event *event, Tick when, bool always = false);
3342SN/A
3355501Snate@binkert.org    Tick nextTick() const { return head->when(); }
3362667Sstever@eecs.umich.edu    Event *serviceOne();
3372SN/A
3382SN/A    // process all events up to the given timestamp.  we inline a
3392SN/A    // quick test to see if there are any events to process; if so,
3402SN/A    // call the internal out-of-line version to process them all.
3415501Snate@binkert.org    void
3425501Snate@binkert.org    serviceEvents(Tick when)
3435501Snate@binkert.org    {
3442SN/A        while (!empty()) {
3452SN/A            if (nextTick() > when)
3462SN/A                break;
3472SN/A
3481634SN/A            /**
3491634SN/A             * @todo this assert is a good bug catcher.  I need to
3501634SN/A             * make it true again.
3511634SN/A             */
3521634SN/A            //assert(head->when() >= when && "event scheduled in the past");
3532SN/A            serviceOne();
3542SN/A        }
3552SN/A    }
3562SN/A
3572SN/A    // default: process all events up to 'now' (curTick)
3582SN/A    void serviceEvents() { serviceEvents(curTick); }
3592SN/A
3602SN/A    // return true if no events are queued
3615501Snate@binkert.org    bool empty() const { return head == NULL; }
3622SN/A
3635501Snate@binkert.org    void dump() const;
3642SN/A
3652SN/A    Tick nextEventTime() { return empty() ? curTick : head->when(); }
3662SN/A
3675502Snate@binkert.org    bool debugVerify() const;
3685502Snate@binkert.org
3695605Snate@binkert.org#ifndef SWIG
370217SN/A    virtual void serialize(std::ostream &os);
371237SN/A    virtual void unserialize(Checkpoint *cp, const std::string &section);
3725605Snate@binkert.org#endif
3732SN/A};
3742SN/A
3755605Snate@binkert.org#ifndef SWIG
3765605Snate@binkert.orgclass EventManager
3775605Snate@binkert.org{
3785605Snate@binkert.org  protected:
3795605Snate@binkert.org    /** A pointer to this object's event queue */
3805605Snate@binkert.org    EventQueue *eventq;
3812SN/A
3825605Snate@binkert.org  public:
3835605Snate@binkert.org    EventManager(EventManager &em) : eventq(em.queue()) {}
3845605Snate@binkert.org    EventManager(EventManager *em) : eventq(em ? em->queue() : NULL) {}
3855605Snate@binkert.org    EventManager(EventQueue *eq) : eventq(eq) {}
3862SN/A
3875605Snate@binkert.org    EventQueue *
3885605Snate@binkert.org    queue() const
3895605Snate@binkert.org    {
3905605Snate@binkert.org        return eventq;
3915605Snate@binkert.org    }
3925605Snate@binkert.org
3935605Snate@binkert.org    void
3945605Snate@binkert.org    schedule(Event &event, Tick when)
3955605Snate@binkert.org    {
3965605Snate@binkert.org        eventq->schedule(&event, when);
3975605Snate@binkert.org    }
3985605Snate@binkert.org
3995605Snate@binkert.org    void
4005605Snate@binkert.org    deschedule(Event &event)
4015605Snate@binkert.org    {
4025605Snate@binkert.org        eventq->deschedule(&event);
4035605Snate@binkert.org    }
4045605Snate@binkert.org
4055605Snate@binkert.org    void
4065605Snate@binkert.org    reschedule(Event &event, Tick when, bool always = false)
4075605Snate@binkert.org    {
4085605Snate@binkert.org        eventq->reschedule(&event, when, always);
4095605Snate@binkert.org    }
4105605Snate@binkert.org
4115605Snate@binkert.org    void
4125605Snate@binkert.org    schedule(Event *event, Tick when)
4135605Snate@binkert.org    {
4145605Snate@binkert.org        eventq->schedule(event, when);
4155605Snate@binkert.org    }
4165605Snate@binkert.org
4175605Snate@binkert.org    void
4185605Snate@binkert.org    deschedule(Event *event)
4195605Snate@binkert.org    {
4205605Snate@binkert.org        eventq->deschedule(event);
4215605Snate@binkert.org    }
4225605Snate@binkert.org
4235605Snate@binkert.org    void
4245605Snate@binkert.org    reschedule(Event *event, Tick when, bool always = false)
4255605Snate@binkert.org    {
4265605Snate@binkert.org        eventq->reschedule(event, when, always);
4275605Snate@binkert.org    }
4285605Snate@binkert.org};
4295605Snate@binkert.org
4305605Snate@binkert.orgtemplate <class T, void (T::* F)()>
4315605Snate@binkert.orgvoid
4325605Snate@binkert.orgDelayFunction(EventQueue *eventq, Tick when, T *object)
4335605Snate@binkert.org{
4345605Snate@binkert.org    class DelayEvent : public Event
4355605Snate@binkert.org    {
4365605Snate@binkert.org      private:
4375605Snate@binkert.org        T *object;
4385605Snate@binkert.org
4395605Snate@binkert.org      public:
4405605Snate@binkert.org        DelayEvent(T *o)
4415605Snate@binkert.org            : object(o)
4425769Snate@binkert.org        { this->setFlags(AutoDelete); }
4435605Snate@binkert.org        void process() { (object->*F)(); }
4445605Snate@binkert.org        const char *description() const { return "delay"; }
4455605Snate@binkert.org    };
4465605Snate@binkert.org
4475605Snate@binkert.org    eventq->schedule(new DelayEvent(object), when);
4485605Snate@binkert.org}
4495605Snate@binkert.org
4505605Snate@binkert.orgtemplate <class T, void (T::* F)()>
4515605Snate@binkert.orgclass EventWrapper : public Event
4525605Snate@binkert.org{
4535605Snate@binkert.org  private:
4545605Snate@binkert.org    T *object;
4555605Snate@binkert.org
4565605Snate@binkert.org  public:
4575605Snate@binkert.org    EventWrapper(T *obj, bool del = false, Priority p = Default_Pri)
4585605Snate@binkert.org        : Event(p), object(obj)
4595605Snate@binkert.org    {
4605605Snate@binkert.org        if (del)
4615605Snate@binkert.org            setFlags(AutoDelete);
4625605Snate@binkert.org    }
4635605Snate@binkert.org
4645605Snate@binkert.org    void process() { (object->*F)(); }
4655605Snate@binkert.org};
4665605Snate@binkert.org
4672SN/Ainline void
4685605Snate@binkert.orgEventQueue::schedule(Event *event, Tick when)
4692SN/A{
4705605Snate@binkert.org    assert(when >= curTick);
4715605Snate@binkert.org    assert(!event->scheduled());
4725605Snate@binkert.org
4735605Snate@binkert.org    event->setWhen(when, this);
4745605Snate@binkert.org    insert(event);
4755769Snate@binkert.org    event->flags.set(Event::Scheduled);
4765605Snate@binkert.org    if (this == &mainEventQueue)
4775769Snate@binkert.org        event->flags.set(Event::IsMainQueue);
4785605Snate@binkert.org    else
4795769Snate@binkert.org        event->flags.clear(Event::IsMainQueue);
4805605Snate@binkert.org
4815605Snate@binkert.org    if (DTRACE(Event))
4825605Snate@binkert.org        event->trace("scheduled");
4832SN/A}
4842SN/A
4852SN/Ainline void
4865605Snate@binkert.orgEventQueue::deschedule(Event *event)
4872SN/A{
4885605Snate@binkert.org    assert(event->scheduled());
4895605Snate@binkert.org
4905605Snate@binkert.org    remove(event);
4915605Snate@binkert.org
4925769Snate@binkert.org    event->flags.clear(Event::Squashed);
4935769Snate@binkert.org    event->flags.clear(Event::Scheduled);
4945605Snate@binkert.org
4955769Snate@binkert.org    if (event->flags.isSet(Event::AutoDelete))
4965605Snate@binkert.org        delete event;
4975605Snate@binkert.org
4985605Snate@binkert.org    if (DTRACE(Event))
4995605Snate@binkert.org        event->trace("descheduled");
5002SN/A}
5012SN/A
5022SN/Ainline void
5035605Snate@binkert.orgEventQueue::reschedule(Event *event, Tick when, bool always)
5042SN/A{
5055605Snate@binkert.org    assert(when >= curTick);
5065605Snate@binkert.org    assert(always || event->scheduled());
5075605Snate@binkert.org
5085605Snate@binkert.org    if (event->scheduled())
5095605Snate@binkert.org        remove(event);
5105605Snate@binkert.org
5115605Snate@binkert.org    event->setWhen(when, this);
5125605Snate@binkert.org    insert(event);
5135769Snate@binkert.org    event->flags.clear(Event::Squashed);
5145769Snate@binkert.org    event->flags.set(Event::Scheduled);
5155605Snate@binkert.org    if (this == &mainEventQueue)
5165769Snate@binkert.org        event->flags.set(Event::IsMainQueue);
5175605Snate@binkert.org    else
5185769Snate@binkert.org        event->flags.clear(Event::IsMainQueue);
5195605Snate@binkert.org
5205605Snate@binkert.org    if (DTRACE(Event))
5215605Snate@binkert.org        event->trace("rescheduled");
5222SN/A}
5232SN/A
5245502Snate@binkert.orginline bool
5255502Snate@binkert.orgoperator<(const Event &l, const Event &r)
5265502Snate@binkert.org{
5275502Snate@binkert.org    return l.when() < r.when() ||
5285502Snate@binkert.org        (l.when() == r.when() && l.priority() < r.priority());
5295502Snate@binkert.org}
5305502Snate@binkert.org
5315502Snate@binkert.orginline bool
5325502Snate@binkert.orgoperator>(const Event &l, const Event &r)
5335502Snate@binkert.org{
5345502Snate@binkert.org    return l.when() > r.when() ||
5355502Snate@binkert.org        (l.when() == r.when() && l.priority() > r.priority());
5365502Snate@binkert.org}
5375502Snate@binkert.org
5385502Snate@binkert.orginline bool
5395502Snate@binkert.orgoperator<=(const Event &l, const Event &r)
5405502Snate@binkert.org{
5415502Snate@binkert.org    return l.when() < r.when() ||
5425502Snate@binkert.org        (l.when() == r.when() && l.priority() <= r.priority());
5435502Snate@binkert.org}
5445502Snate@binkert.orginline bool
5455502Snate@binkert.orgoperator>=(const Event &l, const Event &r)
5465502Snate@binkert.org{
5475502Snate@binkert.org    return l.when() > r.when() ||
5485502Snate@binkert.org        (l.when() == r.when() && l.priority() >= r.priority());
5495502Snate@binkert.org}
5505502Snate@binkert.org
5515502Snate@binkert.orginline bool
5525502Snate@binkert.orgoperator==(const Event &l, const Event &r)
5535502Snate@binkert.org{
5545502Snate@binkert.org    return l.when() == r.when() && l.priority() == r.priority();
5555502Snate@binkert.org}
5565502Snate@binkert.org
5575502Snate@binkert.orginline bool
5585502Snate@binkert.orgoperator!=(const Event &l, const Event &r)
5595502Snate@binkert.org{
5605502Snate@binkert.org    return l.when() != r.when() || l.priority() != r.priority();
5615502Snate@binkert.org}
5625605Snate@binkert.org#endif
5632SN/A
5641354SN/A#endif // __SIM_EVENTQ_HH__
565