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/*****************************************************************************
21
22  sc_event_queue.cpp -- Event Queue Support
23
24  Original Author: Stuart Swan, Cadence Inc.
25
26  CHANGE LOG IS AT THE END OF THE FILE
27 *****************************************************************************/
28
29#include "sysc/communication/sc_event_queue.h"
30#include "sysc/kernel/sc_method_process.h"
31
32namespace sc_core {
33
34static int
35sc_time_compare( const void* p1, const void* p2 )
36{
37    const sc_time* t1 = static_cast<const sc_time*>( p1 );
38    const sc_time* t2 = static_cast<const sc_time*>( p2 );
39
40    if( *t1 < *t2 ) {
41	return 1;
42    } else if( *t1 > *t2 ) {
43	return -1;
44    } else {
45	return 0;
46    }
47}
48
49sc_event_queue::sc_event_queue( sc_module_name name_ )
50    : sc_module( name_ ),
51      m_ppq( 128, sc_time_compare ),
52      m_e( (std::string(SC_KERNEL_EVENT_PREFIX)+"_event").c_str() ),
53      m_change_stamp(0),
54      m_pending_delta(0)
55{
56    SC_METHOD( fire_event );
57    sensitive << m_e;
58    dont_initialize();
59}
60
61sc_event_queue::~sc_event_queue()
62{
63  while (m_ppq.size() > 0) {
64    delete m_ppq.extract_top();
65  }
66}
67
68void sc_event_queue::cancel_all()
69{
70    m_pending_delta = 0;
71    while( m_ppq.size() > 0 )
72	delete m_ppq.extract_top();
73    m_e.cancel();
74}
75
76void sc_event_queue::notify (const sc_time& when)
77{
78    m_change_stamp = simcontext()->change_stamp();
79    sc_time* t = new sc_time( when+sc_time_stamp() );
80    if ( m_ppq.size()==0 || *t < *m_ppq.top() ) {
81	m_e.notify( when );
82    }
83    m_ppq.insert( t );
84}
85
86void sc_event_queue::fire_event()
87{
88    if ( m_ppq.empty() ) { // event has been cancelled
89        return;
90    }
91    sc_time* t = m_ppq.extract_top();
92    assert( *t==sc_time_stamp() );
93    delete t;
94
95    if ( m_ppq.size() > 0 ) {
96	m_e.notify( *m_ppq.top() - sc_time_stamp() );
97    }
98}
99
100} // namespace sc_core
101
102// $Log: sc_event_queue.cpp,v $
103// Revision 1.9  2011/08/26 22:45:53  acg
104//  Torsten Maehne: remove redundant initialization assignment.
105//
106// Revision 1.8  2011/08/26 21:44:58  acg
107//  Andy Goodrich: fix internal event naming.
108//
109// Revision 1.7  2011/08/26 20:45:39  acg
110//  Andy Goodrich: moved the modification log to the end of the file to
111//  eliminate source line number skew when check-ins are done.
112//
113// Revision 1.6  2011/08/24 22:05:35  acg
114//  Torsten Maehne: initialization changes to remove warnings.
115//
116// Revision 1.5  2011/04/08 18:22:46  acg
117//  Philipp A. Hartmann: use the context of the primitive channel to get
118//  the change stamp value.
119//
120// Revision 1.4  2011/04/05 20:48:09  acg
121//  Andy Goodrich: changes to make sure that event(), posedge() and negedge()
122//  only return true if the clock has not moved.
123//
124// Revision 1.3  2011/02/18 20:23:45  acg
125//  Andy Goodrich: Copyright update.
126//
127// Revision 1.2  2010/07/22 20:02:30  acg
128//  Andy Goodrich: bug fixes.
129//
130// Revision 1.1.1.1  2006/12/15 20:20:04  acg
131// SystemC 2.3
132//
133// Revision 1.5  2006/11/28 20:30:48  acg
134//  Andy Goodrich: updated from 2.2 source. sc_event_queue constructors
135//  collapsed into a single constructor with an optional argument to get
136//  the sc_module_name stack done correctly. Class name prefixing added
137//  to sc_semaphore calls to wait() to keep gcc 4.x happy.
138//
139// Revision 1.4  2006/01/26 21:00:50  acg
140//  Andy Goodrich: conversion to use sc_event::notify(SC_ZERO_TIME) instead of
141//  sc_event::notify_delayed()
142//
143// Revision 1.3  2006/01/13 18:47:42  acg
144// Added $Log command so that CVS comments are reproduced in the source.
145
146// taf
147