eventq.hh revision 7005
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
4556SN/A#include "base/fast_alloc.hh"
465769Snate@binkert.org#include "base/flags.hh"
472361SN/A#include "base/misc.hh"
481354SN/A#include "base/trace.hh"
496216Snate@binkert.org#include "base/types.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 */
63395SN/Aclass Event : public Serializable, public FastAlloc
642SN/A{
652SN/A    friend class EventQueue;
662SN/A
675769Snate@binkert.org  protected:
685769Snate@binkert.org    typedef short FlagsType;
695769Snate@binkert.org    typedef ::Flags<FlagsType> Flags;
705769Snate@binkert.org
715769Snate@binkert.org    static const FlagsType PublicRead    = 0x003f;
725769Snate@binkert.org    static const FlagsType PublicWrite   = 0x001d;
735769Snate@binkert.org    static const FlagsType Squashed      = 0x0001;
745769Snate@binkert.org    static const FlagsType Scheduled     = 0x0002;
755769Snate@binkert.org    static const FlagsType AutoDelete    = 0x0004;
765769Snate@binkert.org    static const FlagsType AutoSerialize = 0x0008;
775769Snate@binkert.org    static const FlagsType IsExitEvent   = 0x0010;
785769Snate@binkert.org    static const FlagsType IsMainQueue   = 0x0020;
795774Snate@binkert.org#ifdef EVENTQ_DEBUG
805774Snate@binkert.org    static const FlagsType Initialized   = 0xf000;
815774Snate@binkert.org#endif
825769Snate@binkert.org
832SN/A  private:
845502Snate@binkert.org    // The event queue is now a linked list of linked lists.  The
855502Snate@binkert.org    // 'nextBin' pointer is to find the bin, where a bin is defined as
865502Snate@binkert.org    // when+priority.  All events in the same bin will be stored in a
875503Snate@binkert.org    // second linked list (a stack) maintained by the 'nextInBin'
885503Snate@binkert.org    // pointer.  The list will be accessed in LIFO order.  The end
895502Snate@binkert.org    // result is that the insert/removal in 'nextBin' is
905502Snate@binkert.org    // linear/constant, and the lookup/removal in 'nextInBin' is
915502Snate@binkert.org    // constant/constant.  Hopefully this is a significant improvement
925502Snate@binkert.org    // over the current fully linear insertion.
935502Snate@binkert.org    Event *nextBin;
945502Snate@binkert.org    Event *nextInBin;
955502Snate@binkert.org
965602Snate@binkert.org    static Event *insertBefore(Event *event, Event *curr);
975602Snate@binkert.org    static Event *removeItem(Event *event, Event *last);
985501Snate@binkert.org
995543Ssaidi@eecs.umich.edu    Tick _when;         //!< timestamp when event should be processed
1005543Ssaidi@eecs.umich.edu    short _priority;    //!< event priority
1015769Snate@binkert.org    Flags flags;
1024016Sstever@eecs.umich.edu
1034016Sstever@eecs.umich.edu#ifndef NDEBUG
1044016Sstever@eecs.umich.edu    /// Global counter to generate unique IDs for Event instances
1054016Sstever@eecs.umich.edu    static Counter instanceCounter;
1064016Sstever@eecs.umich.edu
1074016Sstever@eecs.umich.edu    /// This event's unique ID.  We can also use pointer values for
1084016Sstever@eecs.umich.edu    /// this but they're not consistent across runs making debugging
1094016Sstever@eecs.umich.edu    /// more difficult.  Thus we use a global counter value when
1104016Sstever@eecs.umich.edu    /// debugging.
1115501Snate@binkert.org    Counter instance;
1125605Snate@binkert.org
1135605Snate@binkert.org    /// queue to which this event belongs (though it may or may not be
1145605Snate@binkert.org    /// scheduled on this queue yet)
1155605Snate@binkert.org    EventQueue *queue;
1165501Snate@binkert.org#endif
1174016Sstever@eecs.umich.edu
1185577SSteve.Reinhardt@amd.com#ifdef EVENTQ_DEBUG
1195501Snate@binkert.org    Tick whenCreated;   //!< time created
1205501Snate@binkert.org    Tick whenScheduled; //!< time scheduled
1215501Snate@binkert.org#endif
1225502Snate@binkert.org
1235502Snate@binkert.org    void
1245605Snate@binkert.org    setWhen(Tick when, EventQueue *q)
1255502Snate@binkert.org    {
1265502Snate@binkert.org        _when = when;
1275605Snate@binkert.org#ifndef NDEBUG
1285605Snate@binkert.org        queue = q;
1295605Snate@binkert.org#endif
1305577SSteve.Reinhardt@amd.com#ifdef EVENTQ_DEBUG
1315502Snate@binkert.org        whenScheduled = curTick;
1325502Snate@binkert.org#endif
1335502Snate@binkert.org    }
1345502Snate@binkert.org
1352SN/A  protected:
1365769Snate@binkert.org    /// Accessor for flags.
1375769Snate@binkert.org    Flags
1385769Snate@binkert.org    getFlags() const
1395769Snate@binkert.org    {
1405769Snate@binkert.org        return flags & PublicRead;
1415769Snate@binkert.org    }
1422SN/A
1435769Snate@binkert.org    Flags
1445769Snate@binkert.org    getFlags(Flags _flags) const
1455769Snate@binkert.org    {
1465769Snate@binkert.org        assert(flags.noneSet(~PublicRead));
1475769Snate@binkert.org        return flags.isSet(_flags);
1485769Snate@binkert.org    }
1492SN/A
1505769Snate@binkert.org    Flags
1515769Snate@binkert.org    allFlags(Flags _flags) const
1525769Snate@binkert.org    {
1535769Snate@binkert.org        assert(_flags.noneSet(~PublicRead));
1545769Snate@binkert.org        return flags.allSet(_flags);
1555769Snate@binkert.org    }
1565769Snate@binkert.org
1575769Snate@binkert.org    /// Accessor for flags.
1585769Snate@binkert.org    void
1595769Snate@binkert.org    setFlags(Flags _flags)
1605769Snate@binkert.org    {
1615769Snate@binkert.org        assert(_flags.noneSet(~PublicWrite));
1625769Snate@binkert.org        flags.set(_flags);
1635769Snate@binkert.org    }
1645769Snate@binkert.org
1655769Snate@binkert.org    void
1665769Snate@binkert.org    clearFlags(Flags _flags)
1675769Snate@binkert.org    {
1685769Snate@binkert.org        assert(_flags.noneSet(~PublicWrite));
1695769Snate@binkert.org        flags.clear(_flags);
1705769Snate@binkert.org    }
1715769Snate@binkert.org
1725769Snate@binkert.org    void
1735769Snate@binkert.org    clearFlags()
1745769Snate@binkert.org    {
1755769Snate@binkert.org        flags.clear(PublicWrite);
1765769Snate@binkert.org    }
1775769Snate@binkert.org
1785501Snate@binkert.org    // This function isn't really useful if TRACING_ON is not defined
1795543Ssaidi@eecs.umich.edu    virtual void trace(const char *action);     //!< trace event activity
1802SN/A
1812SN/A  public:
182396SN/A    /// Event priorities, to provide tie-breakers for events scheduled
183396SN/A    /// at the same cycle.  Most events are scheduled at the default
184396SN/A    /// priority; these values are used to control events that need to
185396SN/A    /// be ordered within a cycle.
186396SN/A    enum Priority {
1875501Snate@binkert.org        /// Minimum priority
1885543Ssaidi@eecs.umich.edu        Minimum_Pri             = SHRT_MIN,
1895501Snate@binkert.org
1903329Sstever@eecs.umich.edu        /// If we enable tracing on a particular cycle, do that as the
1913329Sstever@eecs.umich.edu        /// very first thing so we don't miss any of the events on
1923329Sstever@eecs.umich.edu        /// that cycle (even if we enter the debugger).
1933329Sstever@eecs.umich.edu        Trace_Enable_Pri        = -101,
1943329Sstever@eecs.umich.edu
1953329Sstever@eecs.umich.edu        /// Breakpoints should happen before anything else (except
1963329Sstever@eecs.umich.edu        /// enabling trace output), so we don't miss any action when
1973329Sstever@eecs.umich.edu        /// debugging.
1985543Ssaidi@eecs.umich.edu        Debug_Break_Pri         = -100,
199396SN/A
2003329Sstever@eecs.umich.edu        /// CPU switches schedule the new CPU's tick event for the
2013329Sstever@eecs.umich.edu        /// same cycle (after unscheduling the old CPU's tick event).
2023329Sstever@eecs.umich.edu        /// The switch needs to come before any tick events to make
2033329Sstever@eecs.umich.edu        /// sure we don't tick both CPUs in the same cycle.
2045543Ssaidi@eecs.umich.edu        CPU_Switch_Pri          =   -31,
2053329Sstever@eecs.umich.edu
206396SN/A        /// For some reason "delayed" inter-cluster writebacks are
207396SN/A        /// scheduled before regular writebacks (which have default
208396SN/A        /// priority).  Steve?
2095543Ssaidi@eecs.umich.edu        Delayed_Writeback_Pri   =   -1,
210396SN/A
211396SN/A        /// Default is zero for historical reasons.
2125543Ssaidi@eecs.umich.edu        Default_Pri             =    0,
213396SN/A
214396SN/A        /// Serailization needs to occur before tick events also, so
215396SN/A        /// that a serialize/unserialize is identical to an on-line
216396SN/A        /// CPU switch.
2175543Ssaidi@eecs.umich.edu        Serialize_Pri           =   32,
218396SN/A
219396SN/A        /// CPU ticks must come after other associated CPU events
220396SN/A        /// (such as writebacks).
2215543Ssaidi@eecs.umich.edu        CPU_Tick_Pri            =   50,
222396SN/A
223396SN/A        /// Statistics events (dump, reset, etc.) come after
224396SN/A        /// everything else, but before exit.
2255543Ssaidi@eecs.umich.edu        Stat_Event_Pri          =   90,
226396SN/A
2274075Sbinkertn@umich.edu        /// Progress events come at the end.
2284075Sbinkertn@umich.edu        Progress_Event_Pri      =   95,
2294075Sbinkertn@umich.edu
230396SN/A        /// If we want to exit on this cycle, it's the very last thing
231396SN/A        /// we do.
2325543Ssaidi@eecs.umich.edu        Sim_Exit_Pri            =  100,
2335501Snate@binkert.org
2345501Snate@binkert.org        /// Maximum priority
2355543Ssaidi@eecs.umich.edu        Maximum_Pri             = SHRT_MAX
236396SN/A    };
237396SN/A
2382SN/A    /*
2392SN/A     * Event constructor
2402SN/A     * @param queue that the event gets scheduled on
2412SN/A     */
2425605Snate@binkert.org    Event(Priority p = Default_Pri)
2435769Snate@binkert.org        : nextBin(NULL), nextInBin(NULL), _priority(p)
244224SN/A    {
2454016Sstever@eecs.umich.edu#ifndef NDEBUG
2465501Snate@binkert.org        instance = ++instanceCounter;
2475605Snate@binkert.org        queue = NULL;
2485501Snate@binkert.org#endif
2495501Snate@binkert.org#ifdef EVENTQ_DEBUG
2505774Snate@binkert.org        flags.set(Initialized);
2515501Snate@binkert.org        whenCreated = curTick;
2525501Snate@binkert.org        whenScheduled = 0;
2534016Sstever@eecs.umich.edu#endif
254224SN/A    }
255224SN/A
2565768Snate@binkert.org    virtual ~Event();
2575768Snate@binkert.org    virtual const std::string name() const;
258265SN/A
2595501Snate@binkert.org    /// Return a C string describing the event.  This string should
2605501Snate@binkert.org    /// *not* be dynamically allocated; just a const char array
2615501Snate@binkert.org    /// describing the event class.
2625501Snate@binkert.org    virtual const char *description() const;
2635501Snate@binkert.org
2645501Snate@binkert.org    /// Dump the current event data
2655501Snate@binkert.org    void dump() const;
2665501Snate@binkert.org
2675501Snate@binkert.org  public:
2685501Snate@binkert.org    /*
2695501Snate@binkert.org     * This member function is invoked when the event is processed
2705501Snate@binkert.org     * (occurs).  There is no default implementation; each subclass
2715501Snate@binkert.org     * must provide its own implementation.  The event is not
2725501Snate@binkert.org     * automatically deleted after it is processed (to allow for
2735501Snate@binkert.org     * statically allocated event objects).
2745501Snate@binkert.org     *
2755501Snate@binkert.org     * If the AutoDestroy flag is set, the object is deleted once it
2765501Snate@binkert.org     * is processed.
2775501Snate@binkert.org     */
2785501Snate@binkert.org    virtual void process() = 0;
2795501Snate@binkert.org
2802SN/A    /// Determine if the current event is scheduled
2815769Snate@binkert.org    bool scheduled() const { return flags.isSet(Scheduled); }
2822SN/A
2832SN/A    /// Squash the current event
2845769Snate@binkert.org    void squash() { flags.set(Squashed); }
2852SN/A
2862SN/A    /// Check whether the event is squashed
2875769Snate@binkert.org    bool squashed() const { return flags.isSet(Squashed); }
2882SN/A
2892667Sstever@eecs.umich.edu    /// See if this is a SimExitEvent (without resorting to RTTI)
2905769Snate@binkert.org    bool isExitEvent() const { return flags.isSet(IsExitEvent); }
2912667Sstever@eecs.umich.edu
2922SN/A    /// Get the time that the event is scheduled
2932SN/A    Tick when() const { return _when; }
2942SN/A
2952SN/A    /// Get the event priority
2962SN/A    int priority() const { return _priority; }
2972SN/A
2985605Snate@binkert.org#ifndef SWIG
2995501Snate@binkert.org    struct priority_compare
3005501Snate@binkert.org        : public std::binary_function<Event *, Event *, bool>
3012SN/A    {
3025501Snate@binkert.org        bool
3035501Snate@binkert.org        operator()(const Event *l, const Event *r) const
3045501Snate@binkert.org        {
3052SN/A            return l->when() >= r->when() || l->priority() >= r->priority();
3062SN/A        }
3072SN/A    };
308224SN/A
309224SN/A    virtual void serialize(std::ostream &os);
310237SN/A    virtual void unserialize(Checkpoint *cp, const std::string &section);
3115605Snate@binkert.org#endif
312571SN/A};
313571SN/A
3147005Snate@binkert.org#ifndef SWIG
3157005Snate@binkert.orginline bool
3167005Snate@binkert.orgoperator<(const Event &l, const Event &r)
3177005Snate@binkert.org{
3187005Snate@binkert.org    return l.when() < r.when() ||
3197005Snate@binkert.org        (l.when() == r.when() && l.priority() < r.priority());
3207005Snate@binkert.org}
3217005Snate@binkert.org
3227005Snate@binkert.orginline bool
3237005Snate@binkert.orgoperator>(const Event &l, const Event &r)
3247005Snate@binkert.org{
3257005Snate@binkert.org    return l.when() > r.when() ||
3267005Snate@binkert.org        (l.when() == r.when() && l.priority() > r.priority());
3277005Snate@binkert.org}
3287005Snate@binkert.org
3297005Snate@binkert.orginline bool
3307005Snate@binkert.orgoperator<=(const Event &l, const Event &r)
3317005Snate@binkert.org{
3327005Snate@binkert.org    return l.when() < r.when() ||
3337005Snate@binkert.org        (l.when() == r.when() && l.priority() <= r.priority());
3347005Snate@binkert.org}
3357005Snate@binkert.orginline bool
3367005Snate@binkert.orgoperator>=(const Event &l, const Event &r)
3377005Snate@binkert.org{
3387005Snate@binkert.org    return l.when() > r.when() ||
3397005Snate@binkert.org        (l.when() == r.when() && l.priority() >= r.priority());
3407005Snate@binkert.org}
3417005Snate@binkert.org
3427005Snate@binkert.orginline bool
3437005Snate@binkert.orgoperator==(const Event &l, const Event &r)
3447005Snate@binkert.org{
3457005Snate@binkert.org    return l.when() == r.when() && l.priority() == r.priority();
3467005Snate@binkert.org}
3477005Snate@binkert.org
3487005Snate@binkert.orginline bool
3497005Snate@binkert.orgoperator!=(const Event &l, const Event &r)
3507005Snate@binkert.org{
3517005Snate@binkert.org    return l.when() != r.when() || l.priority() != r.priority();
3527005Snate@binkert.org}
3537005Snate@binkert.org#endif
3547005Snate@binkert.org
3552SN/A/*
3562SN/A * Queue of events sorted in time order
3572SN/A */
358395SN/Aclass EventQueue : public Serializable
3592SN/A{
3605605Snate@binkert.org  private:
361265SN/A    std::string objName;
3622SN/A    Event *head;
3632SN/A
3642SN/A    void insert(Event *event);
3652SN/A    void remove(Event *event);
3662SN/A
3672SN/A  public:
3682SN/A    EventQueue(const std::string &n)
369265SN/A        : objName(n), head(NULL)
3702SN/A    {}
3712SN/A
372512SN/A    virtual const std::string name() const { return objName; }
373265SN/A
3742SN/A    // schedule the given event on this queue
3755738Snate@binkert.org    void schedule(Event *event, Tick when);
3765738Snate@binkert.org    void deschedule(Event *event);
3775738Snate@binkert.org    void reschedule(Event *event, Tick when, bool always = false);
3782SN/A
3795501Snate@binkert.org    Tick nextTick() const { return head->when(); }
3802667Sstever@eecs.umich.edu    Event *serviceOne();
3812SN/A
3822SN/A    // process all events up to the given timestamp.  we inline a
3832SN/A    // quick test to see if there are any events to process; if so,
3842SN/A    // call the internal out-of-line version to process them all.
3855501Snate@binkert.org    void
3865501Snate@binkert.org    serviceEvents(Tick when)
3875501Snate@binkert.org    {
3882SN/A        while (!empty()) {
3892SN/A            if (nextTick() > when)
3902SN/A                break;
3912SN/A
3921634SN/A            /**
3931634SN/A             * @todo this assert is a good bug catcher.  I need to
3941634SN/A             * make it true again.
3951634SN/A             */
3961634SN/A            //assert(head->when() >= when && "event scheduled in the past");
3972SN/A            serviceOne();
3982SN/A        }
3992SN/A    }
4002SN/A
4012SN/A    // default: process all events up to 'now' (curTick)
4022SN/A    void serviceEvents() { serviceEvents(curTick); }
4032SN/A
4042SN/A    // return true if no events are queued
4055501Snate@binkert.org    bool empty() const { return head == NULL; }
4062SN/A
4075501Snate@binkert.org    void dump() const;
4082SN/A
4092SN/A    Tick nextEventTime() { return empty() ? curTick : head->when(); }
4102SN/A
4115502Snate@binkert.org    bool debugVerify() const;
4125502Snate@binkert.org
4135605Snate@binkert.org#ifndef SWIG
414217SN/A    virtual void serialize(std::ostream &os);
415237SN/A    virtual void unserialize(Checkpoint *cp, const std::string &section);
4165605Snate@binkert.org#endif
4172SN/A};
4182SN/A
4195605Snate@binkert.org#ifndef SWIG
4205605Snate@binkert.orgclass EventManager
4215605Snate@binkert.org{
4225605Snate@binkert.org  protected:
4235605Snate@binkert.org    /** A pointer to this object's event queue */
4245605Snate@binkert.org    EventQueue *eventq;
4252SN/A
4265605Snate@binkert.org  public:
4275605Snate@binkert.org    EventManager(EventManager &em) : eventq(em.queue()) {}
4285605Snate@binkert.org    EventManager(EventManager *em) : eventq(em ? em->queue() : NULL) {}
4295605Snate@binkert.org    EventManager(EventQueue *eq) : eventq(eq) {}
4302SN/A
4315605Snate@binkert.org    EventQueue *
4325605Snate@binkert.org    queue() const
4335605Snate@binkert.org    {
4345605Snate@binkert.org        return eventq;
4355605Snate@binkert.org    }
4365605Snate@binkert.org
4375605Snate@binkert.org    void
4385605Snate@binkert.org    schedule(Event &event, Tick when)
4395605Snate@binkert.org    {
4405605Snate@binkert.org        eventq->schedule(&event, when);
4415605Snate@binkert.org    }
4425605Snate@binkert.org
4435605Snate@binkert.org    void
4445605Snate@binkert.org    deschedule(Event &event)
4455605Snate@binkert.org    {
4465605Snate@binkert.org        eventq->deschedule(&event);
4475605Snate@binkert.org    }
4485605Snate@binkert.org
4495605Snate@binkert.org    void
4505605Snate@binkert.org    reschedule(Event &event, Tick when, bool always = false)
4515605Snate@binkert.org    {
4525605Snate@binkert.org        eventq->reschedule(&event, when, always);
4535605Snate@binkert.org    }
4545605Snate@binkert.org
4555605Snate@binkert.org    void
4565605Snate@binkert.org    schedule(Event *event, Tick when)
4575605Snate@binkert.org    {
4585605Snate@binkert.org        eventq->schedule(event, when);
4595605Snate@binkert.org    }
4605605Snate@binkert.org
4615605Snate@binkert.org    void
4625605Snate@binkert.org    deschedule(Event *event)
4635605Snate@binkert.org    {
4645605Snate@binkert.org        eventq->deschedule(event);
4655605Snate@binkert.org    }
4665605Snate@binkert.org
4675605Snate@binkert.org    void
4685605Snate@binkert.org    reschedule(Event *event, Tick when, bool always = false)
4695605Snate@binkert.org    {
4705605Snate@binkert.org        eventq->reschedule(event, when, always);
4715605Snate@binkert.org    }
4725605Snate@binkert.org};
4735605Snate@binkert.org
4742SN/Ainline void
4755605Snate@binkert.orgEventQueue::schedule(Event *event, Tick when)
4762SN/A{
4776712Snate@binkert.org    assert((UTick)when >= (UTick)curTick);
4785605Snate@binkert.org    assert(!event->scheduled());
4795774Snate@binkert.org#ifdef EVENTQ_DEBUG
4805774Snate@binkert.org    assert((event->flags & Event::Initialized) == Event::Initialized);
4815774Snate@binkert.org#endif
4825605Snate@binkert.org
4835605Snate@binkert.org    event->setWhen(when, this);
4845605Snate@binkert.org    insert(event);
4855769Snate@binkert.org    event->flags.set(Event::Scheduled);
4865605Snate@binkert.org    if (this == &mainEventQueue)
4875769Snate@binkert.org        event->flags.set(Event::IsMainQueue);
4885605Snate@binkert.org    else
4895769Snate@binkert.org        event->flags.clear(Event::IsMainQueue);
4905605Snate@binkert.org
4915605Snate@binkert.org    if (DTRACE(Event))
4925605Snate@binkert.org        event->trace("scheduled");
4932SN/A}
4942SN/A
4952SN/Ainline void
4965605Snate@binkert.orgEventQueue::deschedule(Event *event)
4972SN/A{
4985605Snate@binkert.org    assert(event->scheduled());
4995774Snate@binkert.org#ifdef EVENTQ_DEBUG
5005774Snate@binkert.org    assert((event->flags & Event::Initialized) == Event::Initialized);
5015774Snate@binkert.org#endif
5025605Snate@binkert.org
5035605Snate@binkert.org    remove(event);
5045605Snate@binkert.org
5055769Snate@binkert.org    event->flags.clear(Event::Squashed);
5065769Snate@binkert.org    event->flags.clear(Event::Scheduled);
5075605Snate@binkert.org
5085769Snate@binkert.org    if (event->flags.isSet(Event::AutoDelete))
5095605Snate@binkert.org        delete event;
5105605Snate@binkert.org
5115605Snate@binkert.org    if (DTRACE(Event))
5125605Snate@binkert.org        event->trace("descheduled");
5132SN/A}
5142SN/A
5152SN/Ainline void
5165605Snate@binkert.orgEventQueue::reschedule(Event *event, Tick when, bool always)
5172SN/A{
5185605Snate@binkert.org    assert(when >= curTick);
5195605Snate@binkert.org    assert(always || event->scheduled());
5205774Snate@binkert.org#ifdef EVENTQ_DEBUG
5215774Snate@binkert.org    assert((event->flags & Event::Initialized) == Event::Initialized);
5225774Snate@binkert.org#endif
5235605Snate@binkert.org
5245605Snate@binkert.org    if (event->scheduled())
5255605Snate@binkert.org        remove(event);
5265605Snate@binkert.org
5275605Snate@binkert.org    event->setWhen(when, this);
5285605Snate@binkert.org    insert(event);
5295769Snate@binkert.org    event->flags.clear(Event::Squashed);
5305769Snate@binkert.org    event->flags.set(Event::Scheduled);
5315605Snate@binkert.org    if (this == &mainEventQueue)
5325769Snate@binkert.org        event->flags.set(Event::IsMainQueue);
5335605Snate@binkert.org    else
5345769Snate@binkert.org        event->flags.clear(Event::IsMainQueue);
5355605Snate@binkert.org
5365605Snate@binkert.org    if (DTRACE(Event))
5375605Snate@binkert.org        event->trace("rescheduled");
5382SN/A}
5392SN/A
5407005Snate@binkert.orgtemplate <class T, void (T::* F)()>
5417005Snate@binkert.orgvoid
5427005Snate@binkert.orgDelayFunction(EventQueue *eventq, Tick when, T *object)
5435502Snate@binkert.org{
5447005Snate@binkert.org    class DelayEvent : public Event
5457005Snate@binkert.org    {
5467005Snate@binkert.org      private:
5477005Snate@binkert.org        T *object;
5487005Snate@binkert.org
5497005Snate@binkert.org      public:
5507005Snate@binkert.org        DelayEvent(T *o)
5517005Snate@binkert.org            : object(o)
5527005Snate@binkert.org        { this->setFlags(AutoDelete); }
5537005Snate@binkert.org        void process() { (object->*F)(); }
5547005Snate@binkert.org        const char *description() const { return "delay"; }
5557005Snate@binkert.org    };
5567005Snate@binkert.org
5577005Snate@binkert.org    eventq->schedule(new DelayEvent(object), when);
5585502Snate@binkert.org}
5595502Snate@binkert.org
5607005Snate@binkert.orgtemplate <class T, void (T::* F)()>
5617005Snate@binkert.orgclass EventWrapper : public Event
5625502Snate@binkert.org{
5637005Snate@binkert.org  private:
5647005Snate@binkert.org    T *object;
5655502Snate@binkert.org
5667005Snate@binkert.org  public:
5677005Snate@binkert.org    EventWrapper(T *obj, bool del = false, Priority p = Default_Pri)
5687005Snate@binkert.org        : Event(p), object(obj)
5697005Snate@binkert.org    {
5707005Snate@binkert.org        if (del)
5717005Snate@binkert.org            setFlags(AutoDelete);
5727005Snate@binkert.org    }
5735502Snate@binkert.org
5747005Snate@binkert.org    void process() { (object->*F)(); }
5755502Snate@binkert.org
5767005Snate@binkert.org    const std::string
5777005Snate@binkert.org    name() const
5787005Snate@binkert.org    {
5797005Snate@binkert.org        return object->name() + ".wrapped_event";
5807005Snate@binkert.org    }
5817005Snate@binkert.org
5827005Snate@binkert.org    const char *description() const { return "EventWrapped"; }
5837005Snate@binkert.org};
5845605Snate@binkert.org#endif
5852SN/A
5861354SN/A#endif // __SIM_EVENTQ_HH__
587