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