ruby: Migrate all of ruby and slicc to SCons.
[gem5.git] / src / mem / ruby / eventqueue / RubyEventQueue.hh
1
2 /*
3 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * $Id$
32 *
33 * Description: The EventQueue class implements an event queue which
34 * can be trigger events, allowing our simulation to be event driven.
35 *
36 * Currently, the only event we support is a Consumer being signaled
37 * by calling the consumer's wakeup() routine. Adding the event to
38 * the queue does not require a virtual function call, though calling
39 * wakeup() is a virtual function call.
40 *
41 * The method triggerEvents() is called with a global time. All
42 * events which are before or at this time are triggered in timestamp
43 * order. No ordering is enforced for events scheduled to occur at
44 * the same time. Events scheduled to wakeup the same consumer at the
45 * same time are combined into a single event.
46 *
47 * The method scheduleConsumerWakeup() is called with a global time
48 * and a consumer pointer. The event queue will call the wakeup()
49 * method of the consumer at the appropriate time.
50 *
51 * This implementation of EventQueue uses a dynamically sized array
52 * managed as a heap. The algorithms used has O(lg n) for insert and
53 * O(lg n) for extract minimum element. (Based on chapter 7 of Cormen,
54 * Leiserson, and Rivest.) The array is dynamically sized and is
55 * automatically doubled in size when necessary.
56 *
57 */
58
59 #ifndef RUBYEVENTQUEUE_H
60 #define RUBYEVENTQUEUE_H
61
62 #include "config/no_vector_bounds_checks.hh"
63 #include "mem/ruby/common/Global.hh"
64 #include "mem/gems_common/Vector.hh"
65
66 class Consumer;
67 template <class TYPE> class PrioHeap;
68 class RubyEventQueueNode;
69
70 class RubyEventQueue {
71 public:
72 // Constructors
73 RubyEventQueue();
74
75 // Destructor
76 ~RubyEventQueue();
77
78 // Public Methods
79
80 Time getTime() const { return m_globalTime; }
81 void scheduleEvent(Consumer* consumer, Time timeDelta) { scheduleEventAbsolute(consumer, timeDelta + m_globalTime); }
82 void scheduleEventAbsolute(Consumer* consumer, Time timeAbs);
83 void triggerEvents(Time t); // called to handle all events <= time t
84 void triggerAllEvents();
85 void print(ostream& out) const;
86 bool isEmpty() const;
87
88 Time getTimeOfLastRecovery() {return m_timeOfLastRecovery;}
89 void setTimeOfLastRecovery(Time t) {m_timeOfLastRecovery = t;}
90
91 // Private Methods
92 private:
93 // Private copy constructor and assignment operator
94 void init();
95 RubyEventQueue(const RubyEventQueue& obj);
96 RubyEventQueue& operator=(const RubyEventQueue& obj);
97
98 // Data Members (m_ prefix)
99 PrioHeap<RubyEventQueueNode>* m_prio_heap_ptr;
100 Time m_globalTime;
101 Time m_timeOfLastRecovery;
102 };
103
104 // Output operator declaration
105 inline extern
106 ostream& operator<<(ostream& out, const RubyEventQueue& obj);
107
108 // ******************* Definitions *******************
109
110 // Output operator definition
111 inline extern
112 ostream& operator<<(ostream& out, const RubyEventQueue& obj)
113 {
114 obj.print(out);
115 out << flush;
116 return out;
117 }
118
119 #endif //EVENTQUEUE_H