SystemC 3.0.0
Accellera SystemC proof-of-concept library
sc_stage_callback_registry.h
Go to the documentation of this file.
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_stage_callback_registry.h -- Definition of the simulation stage callbacks
23
24 Original Author: Philipp A. Hartmann, OFFIS, 2013-02-15
25
26 CHANGE LOG AT END OF FILE
27 *****************************************************************************/
28
29#ifndef SC_STAGE_CALLBACK_REGISTRY_H_INCLUDED_
30#define SC_STAGE_CALLBACK_REGISTRY_H_INCLUDED_
31
35
36#include <vector>
37
38namespace sc_core {
39
41{
42public:
46
47 struct entry
48 {
51 };
52
53 friend class sc_simcontext;
54 friend class sc_object;
59
60
61private: // interface completely internal
62
63 explicit
65
67
68 // --- callback forwarders
69
70 void construction_done() const;
71 void elaboration_done() const;
72 void start_simulation() const;
73
74 void update_done() const;
75 void before_timestep() const;
76
77 void pre_suspend() const;
78 void post_suspend() const;
79
80 void simulation_paused() const;
81 void simulation_stopped() const;
82 void simulation_done() const;
83
84
85 // --- callback registration and implementation
86
87 void register_callback( cb_type&, mask_type mask );
88 void unregister_callback( cb_type&, mask_type mask );
89
90 // generic caller
91 void do_callback( sc_stage ) const;
92
93private:
94 typedef std::vector<entry> storage_type;
95 typedef std::vector<cb_type*> single_storage_type;
96
97 // set and restore simulation stage
98 struct scoped_stage
99 {
100 scoped_stage( sc_stage& ref, sc_stage s )
101 : ref_(ref), prev_(ref)
102 {
103 sc_scoped_lock lock(sc_get_curr_simcontext()->m_simulation_status_mutex); ref_ = s;
104 }
105 ~scoped_stage() {
106 sc_scoped_lock lock(sc_get_curr_simcontext()->m_simulation_status_mutex); ref_ = prev_;
107 }
108 private:
109 sc_stage& ref_;
110 sc_stage prev_;
111 }; // scoped_stage
112
113 mask_type validate_mask( cb_type&, mask_type, bool warn );
114
115private:
116
117 sc_simcontext* m_simc;
118 storage_type m_cb_vec; // all callbacks
119 single_storage_type m_cb_update_vec; // - update cb shortcut
120 single_storage_type m_cb_timestep_vec; // - timestep cb shortcut
121
122private:
123 // disabled
124 sc_stage_callback_registry( const this_type& );
125 this_type& operator=(const this_type&);
126
127}; // sc_stage_callback_registry
128
129
130// -------------------- callback implementations --------------------
131// (empty, if feature is disabled)
132
133inline void
134sc_stage_callback_registry::construction_done() const
135{
136 scoped_stage scope( m_simc->m_stage
139}
140
141inline void
142sc_stage_callback_registry::elaboration_done() const
143{
144 scoped_stage scope( m_simc->m_stage
146 do_callback( SC_POST_END_OF_ELABORATION );
147}
148
149inline void
150sc_stage_callback_registry::start_simulation() const
151{
152 scoped_stage scope( m_simc->m_stage
154 do_callback( SC_POST_START_OF_SIMULATION );
155}
156
157inline void
158sc_stage_callback_registry::pre_suspend() const
159{
160 scoped_stage scope( m_simc->m_stage
161 , SC_PRE_SUSPEND );
162 do_callback( SC_PRE_SUSPEND );
163}
164
165inline void
166sc_stage_callback_registry::post_suspend() const
167{
168 scoped_stage scope( m_simc->m_stage
169 , SC_POST_SUSPEND );
170 do_callback( SC_POST_SUSPEND );
171}
172
173inline void
174sc_stage_callback_registry::simulation_paused() const
175{
176 scoped_stage scope( m_simc->m_stage
177 , SC_PRE_PAUSE );
178 do_callback( SC_PRE_PAUSE );
179}
180
181inline void
182sc_stage_callback_registry::simulation_stopped() const
183{
184 scoped_stage scope( m_simc->m_stage
185 , SC_PRE_STOP );
186 do_callback( SC_PRE_STOP );
187}
188
189inline void
190sc_stage_callback_registry::simulation_done() const
191{
192 scoped_stage scope( m_simc->m_stage
194 do_callback( SC_POST_END_OF_SIMULATION );
195}
196
197// -------------- specialized callback implementations --------------
198
199inline void
200sc_stage_callback_registry::update_done() const
201{
202
203 if( SC_LIKELY_(!m_cb_update_vec.size()) ) return;
204 scoped_stage scope( m_simc->m_stage
205 , SC_POST_UPDATE );
206
207 typedef single_storage_type::const_iterator it_type;
208 single_storage_type const & vec = m_cb_update_vec;
209
210 for(it_type it = vec.begin(), end = vec.end(); it != end; ++it)
211 (*it)->stage_callback(SC_POST_UPDATE);
212}
213
214inline void
215sc_stage_callback_registry::before_timestep() const
216{
217 if( SC_LIKELY_(!m_cb_timestep_vec.size()) ) return;
218
219 scoped_stage scope( m_simc->m_stage
220 , SC_PRE_TIMESTEP );
221 typedef single_storage_type::const_iterator it_type;
222 single_storage_type const & vec = m_cb_timestep_vec;
223
224 for(it_type it = vec.begin(), end = vec.end(); it != end; ++it)
225 (*it)->stage_callback(SC_PRE_TIMESTEP);
226}
227
228// ------------------------------------------------------------------
229
230} // namespace sc_core
231
232/*****************************************************************************
233
234 MODIFICATION LOG - modifiers, enter your name, affiliation, date and
235 changes you are making here.
236
237 Name, Affiliation, Date:
238 Description of Modification:
239
240 *****************************************************************************/
241#endif /* SC_STAGE_CALLBACK_REGISTRY_H_INCLUDED_ */
242
243// Taf!
244
#define SC_LIKELY_(x)
Definition: sc_cmnhdr.h:86
#define SC_API
Definition: sc_cmnhdr.h:148
@ SC_POST_END_OF_ELABORATION
@ SC_POST_BEFORE_END_OF_ELABORATION
@ SC_POST_START_OF_SIMULATION
sc_simcontext * sc_get_curr_simcontext()
friend SC_API void sc_register_stage_callback(sc_stage_callback_if &cb, sc_stage_callback_if::stage_cb_mask mask)
friend SC_API void sc_unregister_stage_callback(sc_stage_callback_if &cb, sc_stage_callback_if::stage_cb_mask mask)