SystemC  2.3.2
Accellera SystemC proof-of-concept library
sc_spawn.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_spawn.h -- Process spawning support.
23 */
37 #if !defined(sc_spawn_h_INCLUDED)
38 #define sc_spawn_h_INCLUDED
39 
42 
43 namespace sc_core {
44 
45 class sc_event;
46 class sc_port_base;
47 class sc_interface;
48 class sc_event_finder;
49 class sc_process_b;
50 
51 //=============================================================================
52 // CLASS sc_spawn_object<T>
53 //
54 // This templated helper class allows an object to provide the execution
55 // semantics for a process via its () operator. An instance of the supplied
56 // execution object will be kept to provide the semantics when the process is
57 // scheduled for execution. The () operator does not return a value. An example
58 // of an object that might be used for this helper function would be void
59 // sc_bind bound function or method.
60 //
61 // This class is derived from sc_process_host and overloads
62 // sc_process_host::semantics to provide the actual semantic content.
63 //
64 // sc_spawn_object(T object, const char* name, const sc_spawn_options* opt_p)
65 // This is the object instance constructor for this class. It makes a
66 // copy of the supplied object. The tp_call constructor is called
67 // with an indication that this object instance should be reclaimed when
68 // execution completes.
69 // object = object whose () operator will be called to provide
70 // the process semantics.
71 // name_p = optional name for object instance, or zero.
72 // opt_p -> spawn options or zero.
73 //
74 // virtual void semantics()
75 // This virtual method provides the execution semantics for its process.
76 // It performs a () operation on m_object.
77 //=============================================================================
78 template<typename T>
80  public:
81  sc_spawn_object( T object) : m_object(object)
82  {
83  }
84 
85  virtual void semantics()
86  {
87  m_object();
88  }
89 
90  protected:
92 };
93 
94 
95 //------------------------------------------------------------------------------
96 //"sc_spawn - semantic object with no return value"
97 //
98 // This inline function spawns a process for execution. The execution semantics
99 // for the process being spawned will be provided by the supplied object
100 // instance via its () operator. (E.g., an sc_bind bound function)
101 // After creating the process it is registered with the simulator.
102 // object = object instance providing the execution semantics via its
103 // () operator.
104 // name_p = optional name for object instance, or zero.
105 // opt_p -> optional spawn options for process, or zero for the default.
106 //------------------------------------------------------------------------------
107 template <typename T>
109  T object,
110  const char* name_p = 0,
111  const sc_spawn_options* opt_p = 0)
112 {
113  sc_simcontext* context_p;
114  sc_spawn_object<T>* spawn_p;
115 
116  context_p = sc_get_curr_simcontext();
117  spawn_p = new sc_spawn_object<T>(object);
118  if ( !opt_p || !opt_p->is_method() )
119  {
120  sc_process_handle thread_handle = context_p->create_thread_process(
121  name_p, true,
123  spawn_p, opt_p
124  );
125  return thread_handle;
126  }
127  else
128  {
129  sc_process_handle method_handle = context_p->create_method_process(
130  name_p, true,
132  spawn_p, opt_p
133  );
134  return method_handle;
135  }
136 }
137 
138 //=============================================================================
139 // CLASS sc_spawn_object_v<T> for all compilers except HP aCC
140 // or
141 // CLASS sc_spawn_object_v<T, R> for HP aCC which tries to match this
142 // one template argument class when the sc_spawn() declared above is
143 // invoked with 3 arguments or 2 arguments, and generates compiler errors.
144 //
145 // This templated helper class allows an object to provide the execution
146 // semantics for a process via its () operator. An instance of the supplied
147 // object will be kept to provide the semantics when the process is scheduled
148 // for execution. The () operator returns a value, which will be stored at the
149 // location specified by the supplied pointer. An example of an object that
150 // might be used for this helper function would be valued sc_bind bound
151 // function or method.
152 //
153 // sc_spawn_object_v( typename F::result_type* r_p, T f, const char* name_p,
154 // const sc_spawn_options* opt_p )
155 // r_p -> where to place the result of the function invocation.
156 // f = information to be executed.
157 // name_p = optional name for object instance, or zero.
158 // opt_p -> optional spawn options for process, or zero for the default
159 // This is the object instance constructor for this class. It makes a
160 // copy of the supplied object. The tp_call constructor is called
161 // with an indication that this object instance should be reclaimed when
162 // execution completes.
163 // result_p -> where to place the value of the () operator.
164 // object = object whose () operator will be called to provide
165 // the process semantics.
166 //
167 // virtual void semantics()
168 // This virtual method provides the execution semantics for its process.
169 // It performs a () operation on m_object, placing the result at m_result_p.
170 //=============================================================================
171 
172 //------------------------------------------------------------------------------
173 //"sc_spawn_object_v - semantic object with return value"
174 //
175 // This inline function spawns a process for execution. The execution semantics
176 // for the process being spawned will be provided by the supplied object
177 // instance via its () operator. (E.g., an sc_bind bound function) That
178 // operator returns a value, which will be placed in the supplied return
179 // location.
180 // After creating the process it is registered with the simulator.
181 // object = object instance providing the execution semantics via its ()
182 // operator.
183 // r_p -> where to place the value of the () operator.
184 // name_p = optional name for object instance, or zero.
185 // opt_p -> optional spawn options for process, or zero for the default.
186 //------------------------------------------------------------------------------
187 
188 #if !defined (__HP_aCC)
189 
190 template<typename T>
192  public:
193  sc_spawn_object_v( typename T::result_type* r_p, T object ) :
194  m_object(object), m_result_p(r_p)
195  {
196  }
197 
198  virtual void semantics()
199  {
200  *m_result_p = m_object();
201  }
202 
203  protected:
205  typename T::result_type* m_result_p;
206 };
207 
208 template <typename T>
210  typename T::result_type* r_p,
211  T object,
212  const char* name_p = 0,
213  const sc_spawn_options* opt_p = 0)
214 {
215  sc_simcontext* context_p;
216  sc_spawn_object_v<T>* spawn_p;
217 
218  context_p = sc_get_curr_simcontext();
219 
220  spawn_p = new sc_spawn_object_v<T>(r_p, object);
221  if ( !opt_p || !opt_p->is_method() )
222  {
223  sc_process_handle thread_handle = context_p->create_thread_process(
224  name_p, true,
226  spawn_p, opt_p
227  );
228  return thread_handle;
229  }
230  else
231  {
232  sc_process_handle method_handle = context_p->create_method_process(
233  name_p, true,
235  spawn_p, opt_p
236  );
237  return method_handle;
238  }
239 }
240 
241 #else
242 // for HP aCC
243 template<typename T, typename R>
244 class sc_spawn_object_v : public sc_process_host {
245  public:
246  sc_spawn_object_v( R* r_p, T object) :
247  m_object(object), m_result_p(r_p)
248  {
249  }
250 
251  virtual void semantics()
252  {
253  *m_result_p = m_object();
254  }
255 
256  protected:
257  T m_object;
258  R* m_result_p;
259 };
260 
261 template <typename T, typename R>
263  R* r_p,
264  T object,
265  const char* name_p = 0,
266  const sc_spawn_options* opt_p = 0)
267 {
268  sc_simcontext* context_p;
269  sc_spawn_object_v<T,R>* spawn_p;
270 
271  context_p = sc_get_curr_simcontext();
272 
273  spawn_p = new sc_spawn_object_v<T,R>(r_p, object);
274  if ( !opt_p || !opt_p->is_method() )
275  {
276  sc_process_handle thread_handle = context_p->create_thread_process(
277  name_p, true,
278  static_cast<sc_core::SC_ENTRY_FUNC>(
280  spawn_p, opt_p
281  );
282  return thread_handle;
283  }
284  else
285  {
286  sc_process_handle method_handle = context_p->create_method_process(
287  name_p, true,
288  static_cast<sc_core::SC_ENTRY_FUNC>(
290  spawn_p, opt_p
291  );
292  return method_handle;
293  }
294 }
295 
296 #endif // HP
297 
298 } // namespace sc_core
299 
300 // $Log: sc_spawn.h,v $
301 // Revision 1.7 2011/08/26 20:46:11 acg
302 // Andy Goodrich: moved the modification log to the end of the file to
303 // eliminate source line number skew when check-ins are done.
304 //
305 // Revision 1.6 2011/02/18 20:27:14 acg
306 // Andy Goodrich: Updated Copyrights.
307 //
308 // Revision 1.5 2011/02/13 21:47:38 acg
309 // Andy Goodrich: update copyright notice.
310 //
311 // Revision 1.4 2011/02/01 21:14:02 acg
312 // Andy Goodrich: formatting.
313 //
314 // Revision 1.3 2009/07/28 01:10:53 acg
315 // Andy Goodrich: updates for 2.3 release candidate.
316 //
317 // Revision 1.2 2008/05/22 17:06:26 acg
318 // Andy Goodrich: updated copyright notice to include 2008.
319 //
320 // Revision 1.1.1.1 2006/12/15 20:20:05 acg
321 // SystemC 2.3
322 //
323 // Revision 1.6 2006/05/26 20:33:16 acg
324 // Andy Goodrich: changes required by additional platform compilers (i.e.,
325 // Microsoft VC++, Sun Forte, HP aCC).
326 //
327 // Revision 1.5 2006/05/08 18:01:44 acg
328 // Andy Goodrich: changed the HP-specific implementations of sc_spawn() to
329 // use a static_cast to create their entry functions rather than the
330 // SC_MAKE_FUNC_PTR macro. The HP preprocessor does not parse template
331 // arguments that contain a comma properly.
332 //
333 // Revision 1.4 2006/04/11 23:13:21 acg
334 // Andy Goodrich: Changes for reduced reset support that only includes
335 // sc_cthread, but has preliminary hooks for expanding to method and thread
336 // processes also.
337 //
338 // Revision 1.3 2006/01/13 18:44:30 acg
339 // Added $Log to record CVS changes into the source.
340 
341 #endif // !defined(sc_spawn_h_INCLUDED)
virtual void semantics()
Definition: sc_spawn.h:85
#define SC_MAKE_FUNC_PTR(callback_tag, func)
Definition: sc_process.h:154
Process access support.
sc_process_handle sc_spawn(T object, const char *name_p=0, const sc_spawn_options *opt_p=0)
Definition: sc_spawn.h:108
T::result_type * m_result_p
Definition: sc_spawn.h:205
class SC_API sc_event
Definition: sc_interface.h:40
The simulation context.
Process spawning options specification.
class SC_API sc_port_base
Definition: sc_interface.h:41
sc_process_handle create_thread_process(const char *name_p, bool free_host, SC_ENTRY_FUNC method_p, sc_process_host *host_p, const sc_spawn_options *opt_p)
sc_simcontext * sc_get_curr_simcontext()
virtual void semantics()
Definition: sc_spawn.h:198
sc_process_handle create_method_process(const char *name_p, bool free_host, SC_ENTRY_FUNC method_p, sc_process_host *host_p, const sc_spawn_options *opt_p)
sc_spawn_object(T object)
Definition: sc_spawn.h:81
sc_process_b sc_process_b
Definition: sc_process.h:458
sc_spawn_object_v(typename T::result_type *r_p, T object)
Definition: sc_spawn.h:193