SystemC  Recoding Infrastructure for SystemC v0.6.0 derived from Accellera 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;
44 
45 /**************************************************************************/
74 template<typename T>
76  public:
77  sc_spawn_object( T object) : m_object(object)
78  {
79  }
80 
81  virtual void semantics()
82  {
83  m_object();
84  }
85 
86  protected:
88 };
89 
90 
91 /**************************************************************************/
112 template <typename T>
114  T object,
115  const char* name_p = 0,
116  const sc_spawn_options* opt_p = 0)
117 {
118  sc_simcontext* context_p;
119  sc_spawn_object<T>* spawn_p;
120 
121  //assert( 0 ); // 08/20/2015 GL: to support sc_spawn in the future
122 
123  context_p = sc_get_curr_simcontext();
124  spawn_p = new sc_spawn_object<T>(object);
125  if ( !opt_p || !opt_p->is_method() )
126  {
127  // 05/25/2015 GL: sc_kernel_lock constructor acquires the kernel lock
128  sc_kernel_lock lock;
129 
130  // 05/27/2015 GL: we may or may not have acquired the kernel lock
131  // (static vs. dynamic spawn)
132 
133  sc_process_handle thread_handle = context_p->create_thread_process(
134  name_p, true,
136  spawn_p, opt_p,
137  -2, // 08/20/2015 GL: fake segment ID
138  -2 // 09/02/2015 GL: fake instance ID
139  );
140  return thread_handle;
141  // 05/25/2015 GL: sc_kernel_lock destructor releases the kernel lock
142  }
143  else
144  {
145  // 05/25/2015 GL: sc_kernel_lock constructor acquires the kernel lock
146  sc_kernel_lock lock;
147 
148  // 05/27/2015 GL: we may or may not have acquired the kernel lock
149  // (static vs. dynamic spawn)
150 
151  sc_process_handle method_handle = context_p->create_method_process(
152  name_p, true,
154  spawn_p, opt_p,
155  -2, // 08/20/2015 GL: fake segment ID
156  -2 // 09/02/2015 GL: fake instance ID
157  );
158  return method_handle;
159  // 05/25/2015 GL: sc_kernel_lock destructor releases the kernel lock
160  }
161 }
162 
163 //=============================================================================
164 // CLASS sc_spawn_object_v<T> for all compilers except HP aCC
165 // or
166 // CLASS sc_spawn_object_v<T, R> for HP aCC which tries to match this
167 // one template argument class when the sc_spawn() declared above is
168 // invoked with 3 arguments or 2 arguments, and generates compiler errors.
169 //
170 // This templated helper class allows an object to provide the execution
171 // semantics for a process via its () operator. An instance of the supplied
172 // object will be kept to provide the semantics when the process is scheduled
173 // for execution. The () operator returns a value, which will be stored at the
174 // location specified by the supplied pointer. An example of an object that
175 // might be used for this helper function would be valued SC_BOOST bound
176 // function or method.
177 //
178 // sc_spawn_object_v( typename F::result_type* r_p, T f, const char* name_p,
179 // const sc_spawn_options* opt_p )
180 // r_p -> where to place the result of the function invocation.
181 // f = information to be executed.
182 // name_p = optional name for object instance, or zero.
183 // opt_p -> optional spawn options for process, or zero for the default
184 // This is the object instance constructor for this class. It makes a
185 // copy of the supplied object. The tp_call constructor is called
186 // with an indication that this object instance should be reclaimed when
187 // execution completes.
188 // result_p -> where to place the value of the () operator.
189 // object = object whose () operator will be called to provide
190 // the process semantics.
191 //
192 // virtual void semantics()
193 // This virtual method provides the execution semantics for its process.
194 // It performs a () operation on m_object, placing the result at m_result_p.
195 //=============================================================================
196 
197 /**************************************************************************/
218 #if !defined (__HP_aCC)
219 
220 template<typename T>
222  public:
223  sc_spawn_object_v( typename T::result_type* r_p, T object ) :
224  m_object(object), m_result_p(r_p)
225  {
226  }
227 
228  virtual void semantics()
229  {
230  *m_result_p = m_object();
231  }
232 
233  protected:
235  typename T::result_type* m_result_p;
236 };
237 
242 // 08/20/2015 GL.
243 template <typename T>
245  typename T::result_type* r_p,
246  T object,
247  const char* name_p = 0,
248  const sc_spawn_options* opt_p = 0)
249 {
250  sc_simcontext* context_p;
251  sc_spawn_object_v<T>* spawn_p;
252 
253  assert( 0 ); // 08/20/2015 GL: to support sc_spawn in the future
254 
255  context_p = sc_get_curr_simcontext();
256 
257  spawn_p = new sc_spawn_object_v<T>(r_p, object);
258  if ( !opt_p || !opt_p->is_method() )
259  {
260  // 05/25/2015 GL: sc_kernel_lock constructor acquires the kernel lock
261  sc_kernel_lock lock;
262 
263  // 05/27/2015 GL: we may or may not have acquired the kernel lock
264  // (static vs. dynamic spawn)
265 
266  sc_process_handle thread_handle = context_p->create_thread_process(
267  name_p, true,
269  spawn_p, opt_p,
270  -2, // 08/20/2015 GL: fake segment ID
271  -2 // 09/02/2015 GL: fake instance ID
272  );
273  return thread_handle;
274  // 05/25/2015 GL: sc_kernel_lock destructor releases the kernel lock
275  }
276  else
277  {
278  // 05/25/2015 GL: sc_kernel_lock constructor acquires the kernel lock
279  sc_kernel_lock lock;
280 
281  // 05/27/2015 GL: we may or may not have acquired the kernel lock
282  // (static vs. dynamic spawn)
283 
284  sc_process_handle method_handle = context_p->create_method_process(
285  name_p, true,
287  spawn_p, opt_p,
288  -2, // 08/20/2015 GL: fake segment ID
289  -2 // 09/02/2015 GL: fake instance ID
290  );
291  return method_handle;
292  // 05/25/2015 GL: sc_kernel_lock destructor releases the kernel lock
293  }
294 }
295 
296 #else
297 // for HP aCC
298 template<typename T, typename R>
299 class sc_spawn_object_v : public sc_process_host {
300  public:
301  sc_spawn_object_v( R* r_p, T object) :
302  m_object(object), m_result_p(r_p)
303  {
304  }
305 
306  virtual void semantics()
307  {
308  *m_result_p = m_object();
309  }
310 
311  protected:
312  T m_object;
313  R* m_result_p;
314 };
315 
320 // 08/20/2015 GL.
321 template <typename T, typename R>
322 inline sc_process_handle sc_spawn(
323  R* r_p,
324  T object,
325  const char* name_p = 0,
326  const sc_spawn_options* opt_p = 0)
327 {
328  sc_simcontext* context_p;
329  sc_spawn_object_v<T,R>* spawn_p;
330 
331  assert( 0 ); // 08/20/2015 GL: to support sc_spawn in the future
332 
333  context_p = sc_get_curr_simcontext();
334 
335  spawn_p = new sc_spawn_object_v<T,R>(r_p, object);
336  if ( !opt_p || !opt_p->is_method() )
337  {
338  // 05/25/2015 GL: sc_kernel_lock constructor acquires the kernel lock
339  sc_kernel_lock lock;
340 
341  // 05/27/2015 GL: we may or may not have acquired the kernel lock
342  // (static vs. dynamic spawn)
343 
344  sc_process_handle thread_handle = context_p->create_thread_process(
345  name_p, true,
346  static_cast<sc_core::SC_ENTRY_FUNC>(
347  &sc_spawn_object_v<T,R>::semantics),
348  spawn_p, opt_p,
349  -2, // 08/20/2015 GL: fake segment ID
350  -2 // 09/02/2015 GL: fake instance ID
351  );
352  return thread_handle;
353  // 05/25/2015 GL: sc_kernel_lock destructor releases the kernel lock
354  }
355  else
356  {
357  // 05/25/2015 GL: sc_kernel_lock constructor acquires the kernel lock
358  sc_kernel_lock lock;
359 
360  // 05/27/2015 GL: we may or may not have acquired the kernel lock
361  // (static vs. dynamic spawn)
362 
363  sc_process_handle method_handle = context_p->create_method_process(
364  name_p, true,
365  static_cast<sc_core::SC_ENTRY_FUNC>(
366  &sc_spawn_object_v<T,R>::semantics),
367  spawn_p, opt_p,
368  -2, // 08/20/2015 GL: fake segment ID
369  -2 // 09/02/2015 GL: fake instance ID
370  );
371  return method_handle;
372  // 05/25/2015 GL: sc_kernel_lock destructor releases the kernel lock
373  }
374 }
375 
376 #endif // HP
377 
378 } // namespace sc_core
379 
380 // $Log: sc_spawn.h,v $
381 // Revision 1.7 2011/08/26 20:46:11 acg
382 // Andy Goodrich: moved the modification log to the end of the file to
383 // eliminate source line number skew when check-ins are done.
384 //
385 // Revision 1.6 2011/02/18 20:27:14 acg
386 // Andy Goodrich: Updated Copyrights.
387 //
388 // Revision 1.5 2011/02/13 21:47:38 acg
389 // Andy Goodrich: update copyright notice.
390 //
391 // Revision 1.4 2011/02/01 21:14:02 acg
392 // Andy Goodrich: formatting.
393 //
394 // Revision 1.3 2009/07/28 01:10:53 acg
395 // Andy Goodrich: updates for 2.3 release candidate.
396 //
397 // Revision 1.2 2008/05/22 17:06:26 acg
398 // Andy Goodrich: updated copyright notice to include 2008.
399 //
400 // Revision 1.1.1.1 2006/12/15 20:20:05 acg
401 // SystemC 2.3
402 //
403 // Revision 1.6 2006/05/26 20:33:16 acg
404 // Andy Goodrich: changes required by additional platform compilers (i.e.,
405 // Microsoft VC++, Sun Forte, HP aCC).
406 //
407 // Revision 1.5 2006/05/08 18:01:44 acg
408 // Andy Goodrich: changed the HP-specific implementations of sc_spawn() to
409 // use a static_cast to create their entry functions rather than the
410 // SC_MAKE_FUNC_PTR macro. The HP preprocessor does not parse template
411 // arguments that contain a comma properly.
412 //
413 // Revision 1.4 2006/04/11 23:13:21 acg
414 // Andy Goodrich: Changes for reduced reset support that only includes
415 // sc_cthread, but has preliminary hooks for expanding to method and thread
416 // processes also.
417 //
418 // Revision 1.3 2006/01/13 18:44:30 acg
419 // Added $Log to record CVS changes into the source.
420 
421 #endif // !defined(sc_spawn_h_INCLUDED)
virtual void semantics()
Definition: sc_spawn.h:81
T::result_type * m_result_p
Definition: sc_spawn.h:235
A scoped mutex for the kernel lock.
sc_spawn_object(T object)
Definition: sc_spawn.h:77
sc_process_b sc_process_b
Definition: sc_process.h:964
This class provides access to an sc_process_b object instance in a manner which allows some persisten...
sc_spawn_object_v(typename T::result_type *r_p, T object)
Definition: sc_spawn.h:223
sc_process_handle sc_spawn(T object, const char *name_p=0, const sc_spawn_options *opt_p=0)
Semantic object with no return value.
Definition: sc_spawn.h:113
The simulation context.
virtual void semantics()
Definition: sc_spawn.h:228
sc_simcontext * sc_get_curr_simcontext()
#define SC_MAKE_FUNC_PTR(callback_tag, func)
Definition: sc_process.h:213
This is the base class for objects which may have processes defined for their methods (e...
Definition: sc_process.h:148
Semantic object with return value.
Definition: sc_spawn.h:221
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, int seg_id, int inst_id)
Two new parameters segment ID and instance ID are added for the out-of-order simulation.
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, int seg_id, int inst_id)
Two new parameters segment ID and instance ID are added for the out-of-order simulation.