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