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
29#include "../core/sc_event.hh"
30#include "../core/sc_main.hh"
31#include "../core/sc_object.hh"
32#include "../core/sc_time.hh"
33
34namespace tlm_utils
35{
36
37template <class PAYLOAD>
38class peq_with_get : public sc_core::sc_object
39{
40  public:
41    typedef PAYLOAD transaction_type;
42    typedef std::pair<const sc_core::sc_time, transaction_type *> pair_type;
43
44  public:
45    peq_with_get(const char *name) : sc_core::sc_object(name) {}
46
47    void
48    notify(transaction_type &trans, const sc_core::sc_time &t)
49    {
50        m_scheduled_events.insert(pair_type(t + sc_core::sc_time_stamp(),
51                    &trans));
52        m_event.notify(t);
53    }
54
55    void
56    notify(transaction_type &trans)
57    {
58        m_scheduled_events.insert(pair_type(sc_core::sc_time_stamp(), &trans));
59        m_event.notify(); // Immediate notification.
60    }
61
62    // Needs to be called until it returns NULL
63    transaction_type *
64    get_next_transaction()
65    {
66        if (m_scheduled_events.empty()) {
67            return nullptr;
68        }
69
70        sc_core::sc_time now = sc_core::sc_time_stamp();
71        if (m_scheduled_events.begin()->first <= now) {
72            transaction_type *trans = m_scheduled_events.begin()->second;
73            m_scheduled_events.erase(m_scheduled_events.begin());
74            return trans;
75        }
76
77        m_event.notify(m_scheduled_events.begin()->first - now);
78
79        return nullptr;
80    }
81
82    sc_core::sc_event &get_event() { return m_event; }
83
84    // Cancel all events from the event queue.
85    void
86    cancel_all()
87    {
88        m_scheduled_events.clear();
89        m_event.cancel();
90    }
91
92  private:
93    std::multimap<const sc_core::sc_time, transaction_type *>
94        m_scheduled_events;
95    sc_core::sc_event m_event;
96};
97
98} // namespace tlm_utils
99
100#endif /* __SYSTEMC_EXT_TLM_UTILS_PEQ_WITH_GET_H__ */
101