peq_with_get.h revision 13513:bbf275465d3d
1/***************************************************************************** 2 3 Licensed to Accellera Systems Initiative Inc. (Accellera) under one or 4 more contributor license agreements. See the NOTICE file distributed 5 with this work for additional information regarding copyright ownership. 6 Accellera licenses this file to you under the Apache License, Version 2.0 7 (the "License"); you may not use this file except in compliance with the 8 License. You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 implied. See the License for the specific language governing 16 permissions and limitations under the License. 17 18 *****************************************************************************/ 19 20// 12-Jan-2009 John Aynsley Bug fix. sc_time argument to notify should be const 21// 20-Mar-2009 John Aynsley Add cancel_all() method 22 23 24#ifndef __SYSTEMC_EXT_TLM_UTILS_PEQ_WITH_GET_H__ 25#define __SYSTEMC_EXT_TLM_UTILS_PEQ_WITH_GET_H__ 26 27#include <map> 28#include <systemc> 29 30namespace tlm_utils 31{ 32 33template <class PAYLOAD> 34class peq_with_get : public sc_core::sc_object 35{ 36 public: 37 typedef PAYLOAD transaction_type; 38 typedef std::pair<const sc_core::sc_time, transaction_type *> pair_type; 39 40 public: 41 peq_with_get(const char *name) : sc_core::sc_object(name) {} 42 43 void 44 notify(transaction_type &trans, const sc_core::sc_time &t) 45 { 46 m_scheduled_events.insert(pair_type(t + sc_core::sc_time_stamp(), 47 &trans)); 48 m_event.notify(t); 49 } 50 51 void 52 notify(transaction_type &trans) 53 { 54 m_scheduled_events.insert(pair_type(sc_core::sc_time_stamp(), &trans)); 55 m_event.notify(); // Immediate notification. 56 } 57 58 // Needs to be called until it returns NULL 59 transaction_type * 60 get_next_transaction() 61 { 62 if (m_scheduled_events.empty()) { 63 return nullptr; 64 } 65 66 sc_core::sc_time now = sc_core::sc_time_stamp(); 67 if (m_scheduled_events.begin()->first <= now) { 68 transaction_type *trans = m_scheduled_events.begin()->second; 69 m_scheduled_events.erase(m_scheduled_events.begin()); 70 return trans; 71 } 72 73 m_event.notify(m_scheduled_events.begin()->first - now); 74 75 return nullptr; 76 } 77 78 sc_core::sc_event &get_event() { return m_event; } 79 80 // Cancel all events from the event queue. 81 void 82 cancel_all() 83 { 84 m_scheduled_events.clear(); 85 m_event.cancel(); 86 } 87 88 private: 89 std::multimap<const sc_core::sc_time, transaction_type *> 90 m_scheduled_events; 91 sc_core::sc_event m_event; 92}; 93 94} // namespace tlm_utils 95 96#endif /* __SYSTEMC_EXT_TLM_UTILS_PEQ_WITH_GET_H__ */ 97