2 * Copyright (c) 2003 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * EventQueue interfaces
43 #include "sim/host.hh" // for Tick
45 #include "base/fast_alloc.hh"
46 #include "sim/serialize.hh"
47 #include "base/trace.hh"
49 class EventQueue; // forward declaration
52 * An item on an event queue. The action caused by a given
53 * event is specified by deriving a subclass and overriding the
54 * process() member function.
56 class Event : public Serializeable, public FastAlloc
58 friend class EventQueue;
61 /// queue to which this event belongs (though it may or may not be
62 /// scheduled on this queue yet)
67 Tick _when; //!< timestamp when event should be processed
68 int _priority; //!< event priority
79 bool getFlags(Flags f) const { return (_flags & f) == f; }
80 void setFlags(Flags f) { _flags |= f; }
81 void clearFlags(Flags f) { _flags &= ~f; }
84 EventQueue *theQueue() const { return queue; }
87 Tick when_created; //!< Keep track of creation time For debugging
88 Tick when_scheduled; //!< Keep track of creation time For debugging
90 virtual void trace(const char *action); //!< trace event activity
92 void trace(const char *) {}
95 unsigned annotated_value;
99 static const std::string defaultName;
103 * @param queue that the event gets scheduled on
105 Event(EventQueue *q, int p = 0)
106 : Serializeable(defaultName), queue(q), next(NULL),
107 _priority(p), _flags(None),
109 when_created(curTick), when_scheduled(0),
117 * @param queue that the event gets scheduled on
119 Event(EventQueue *q, std::string _name, int p = 0)
120 : Serializeable(_name), queue(q), next(NULL),
121 _priority(p), _flags(None),
123 when_created(curTick), when_scheduled(0),
131 /// Determine if the current event is scheduled
132 bool scheduled() const { return getFlags(Scheduled); }
134 /// Schedule the event with the current priority or default priority
135 void schedule(Tick t);
137 /// Schedule the event with a specific priority
138 void schedule(Tick t, int priority);
140 /// Reschedule the event with the current priority
141 void reschedule(Tick t);
143 /// Reschedule the event with a specific priority
144 void reschedule(Tick t, int priority);
146 /// Remove the event from the current schedule
149 /// Return a C string describing the event. This string should
150 /// *not* be dynamically allocated; just a const char array
151 /// describing the event class.
152 virtual const char *description();
154 /// Dump the current event data
158 * This member function is invoked when the event is processed
159 * (occurs). There is no default implementation; each subclass
160 * must provide its own implementation. The event is not
161 * automatically deleted after it is processed (to allow for
162 * statically allocated event objects).
164 * If the AutoDestroy flag is set, the object is deleted once it
167 virtual void process() = 0;
169 void annotate(unsigned value) { annotated_value = value; };
170 unsigned annotation() { return annotated_value; }
172 /// Squash the current event
173 void squash() { setFlags(Squashed); }
175 /// Check whether the event is squashed
176 bool squashed() { return getFlags(Squashed); }
178 /// Get the time that the event is scheduled
179 Tick when() const { return _when; }
181 /// Get the event priority
182 int priority() const { return _priority; }
184 struct priority_compare :
185 public std::binary_function<Event *, Event *, bool>
187 bool operator()(const Event *l, const Event *r) const {
188 return l->when() >= r->when() || l->priority() >= r->priority();
192 virtual void serialize(std::ostream &os);
193 virtual void unserialize(const IniFile *db, const std::string §ion);
196 template <class T, void (T::* F)()>
198 DelayFunction(Tick when, T *object)
200 class DelayEvent : public Event
206 DelayEvent(Tick when, T *o)
207 : Event(&mainEventQueue), object(o)
208 { setFlags(AutoDestroy); schedule(when); }
209 void process() { (object->*F)(); }
210 const char *description() { return "delay"; }
213 new DelayEvent(when, object);
217 * Queue of events sorted in time order
219 class EventQueue : public Serializeable
224 void insert(Event *event);
225 void remove(Event *event);
230 EventQueue(const std::string &n)
231 : Serializeable(n), head(NULL)
234 // schedule the given event on this queue
235 void schedule(Event *ev);
236 void deschedule(Event *ev);
237 void reschedule(Event *ev);
239 Tick nextTick() { return head->when(); }
242 // process all events up to the given timestamp. we inline a
243 // quick test to see if there are any events to process; if so,
244 // call the internal out-of-line version to process them all.
245 void serviceEvents(Tick when) {
247 if (nextTick() > when)
250 assert(head->when() >= when && "event scheduled in the past");
255 // default: process all events up to 'now' (curTick)
256 void serviceEvents() { serviceEvents(curTick); }
258 // return true if no events are queued
259 bool empty() { return head == NULL; }
263 Tick nextEventTime() { return empty() ? curTick : head->when(); }
265 virtual void nameChildren();
266 virtual void serialize(std::ostream &os);
270 //////////////////////
274 // can't put these inside declaration due to circular dependence
275 // between Event and EventQueue classes.
277 //////////////////////
279 // schedule at specified time (place on event queue specified via
282 Event::schedule(Tick t)
284 assert(!scheduled());
287 when_scheduled = curTick;
290 queue->schedule(this);
294 Event::schedule(Tick t, int p)
305 clearFlags(Squashed);
306 clearFlags(Scheduled);
307 queue->deschedule(this);
311 Event::reschedule(Tick t)
314 clearFlags(Squashed);
317 when_scheduled = curTick;
320 queue->reschedule(this);
324 Event::reschedule(Tick t, int p)
331 EventQueue::schedule(Event *event)
335 event->trace("scheduled");
339 EventQueue::deschedule(Event *event)
343 event->trace("descheduled");
347 EventQueue::reschedule(Event *event)
352 event->trace("rescheduled");
356 //////////////////////
360 // Events on this queue are processed at the *beginning* of each
361 // cycle, before the pipeline simulation is performed.
363 // defined in eventq.cc
365 //////////////////////
366 extern EventQueue mainEventQueue;
368 #endif // __EVENTQ_HH__