MessageBuffer.hh revision 6145
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: Unordered buffer of messages that can be inserted such 34 * that they can be dequeued after a given delta time has expired. 35 * 36 */ 37 38#ifndef MESSAGEBUFFER_H 39#define MESSAGEBUFFER_H 40 41#include "Global.hh" 42#include "MessageBufferNode.hh" 43#include "Consumer.hh" 44#include "EventQueue.hh" 45#include "Message.hh" 46#include "PrioHeap.hh" 47#include "util.hh" 48 49class Chip; 50 51class MessageBuffer { 52public: 53 // Constructors 54 MessageBuffer(); 55 MessageBuffer(const Chip* chip_ptr); // The chip_ptr is ignored, but could be used for extra debugging 56 57 // Use Default Destructor 58 // ~MessageBuffer() 59 60 // Public Methods 61 62 static void printConfig(ostream& out) {} 63 64 // TRUE if head of queue timestamp <= SystemTime 65 bool isReady() const { 66 return ((m_prio_heap.size() > 0) && 67 (m_prio_heap.peekMin().m_time <= g_eventQueue_ptr->getTime())); 68 } 69 70 bool areNSlotsAvailable(int n); 71 int getPriority() { return m_priority_rank; } 72 void setPriority(int rank) { m_priority_rank = rank; } 73 void setConsumer(Consumer* consumer_ptr) { ASSERT(m_consumer_ptr==NULL); m_consumer_ptr = consumer_ptr; } 74 void setDescription(const string& name) { m_name = name; } 75 string getDescription() { return m_name;} 76 77 Consumer* getConsumer() { return m_consumer_ptr; } 78 79 const Message* peekAtHeadOfQueue() const; 80 const Message* peek() const { return peekAtHeadOfQueue(); } 81 const MsgPtr getMsgPtrCopy() const; 82 const MsgPtr& peekMsgPtr() const { assert(isReady()); return m_prio_heap.peekMin().m_msgptr; } 83 const MsgPtr& peekMsgPtrEvenIfNotReady() const {return m_prio_heap.peekMin().m_msgptr; } 84 85 void enqueue(const MsgPtr& message) { enqueue(message, 1); } 86 void enqueue(const MsgPtr& message, Time delta); 87 // void enqueueAbsolute(const MsgPtr& message, Time absolute_time); 88 int dequeue_getDelayCycles(MsgPtr& message); // returns delay cycles of the message 89 void dequeue(MsgPtr& message); 90 int dequeue_getDelayCycles(); // returns delay cycles of the message 91 void dequeue() { pop(); } 92 void pop(); 93 void recycle(); 94 bool isEmpty() const { return m_prio_heap.size() == 0; } 95 96 void setOrdering(bool order) { m_strict_fifo = order; m_ordering_set = true; } 97 void setSize(int size) {m_max_size = size;} 98 int getSize(); 99 void setRandomization(bool random_flag) { m_randomization = random_flag; } 100 101 void clear(); 102 103 void print(ostream& out) const; 104 void printStats(ostream& out); 105 void clearStats() { m_not_avail_count = 0; m_msg_counter = 0; } 106 107private: 108 // Private Methods 109 int setAndReturnDelayCycles(MsgPtr& message); 110 111 // Private copy constructor and assignment operator 112 MessageBuffer(const MessageBuffer& obj); 113 MessageBuffer& operator=(const MessageBuffer& obj); 114 115 // Data Members (m_ prefix) 116 Consumer* m_consumer_ptr; // Consumer to signal a wakeup(), can be NULL 117 PrioHeap<MessageBufferNode> m_prio_heap; 118 string m_name; 119 120 int m_max_size; 121 int m_size; 122 123 Time m_time_last_time_size_checked; 124 int m_size_last_time_size_checked; 125 126 // variables used so enqueues appear to happen imediately, while pop happen the next cycle 127 Time m_time_last_time_enqueue; 128 Time m_time_last_time_pop; 129 int m_size_at_cycle_start; 130 int m_msgs_this_cycle; 131 132 int m_not_avail_count; // count the # of times I didn't have N slots available 133 int m_msg_counter; 134 int m_priority_rank; 135 bool m_strict_fifo; 136 bool m_ordering_set; 137 bool m_randomization; 138 Time m_last_arrival_time; 139}; 140 141// Output operator declaration 142//template <class TYPE> 143ostream& operator<<(ostream& out, const MessageBuffer& obj); 144 145// ******************* Definitions ******************* 146 147// Output operator definition 148extern inline 149ostream& operator<<(ostream& out, const MessageBuffer& obj) 150{ 151 obj.print(out); 152 out << flush; 153 return out; 154} 155 156#endif //MESSAGEBUFFER_H 157