SystemC 3.0.0
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
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#ifndef SC_SPAWN_H_INCLUDED_
34#define SC_SPAWN_H_INCLUDED_
35
36#include <type_traits>
37
40
41namespace sc_core {
42
43class sc_event;
44class sc_port_base;
45class sc_interface;
46class sc_event_finder;
47class sc_process_b;
48
49//=============================================================================
50// CLASS sc_spawn_object<T>
51//
52// This templated helper class allows an object to provide the execution
53// semantics for a process via its () operator. An instance of the supplied
54// execution object will be kept to provide the semantics when the process is
55// scheduled for execution. The () operator does not return a value. An example
56// of an object that might be used for this helper function would be void
57// sc_bind bound function or method.
58//
59// This class is derived from sc_process_host and overloads
60// sc_process_host::semantics to provide the actual semantic content.
61//
62// sc_spawn_object(T object, const char* name, const sc_spawn_options* opt_p)
63// This is the object instance constructor for this class. It makes a
64// copy of the supplied object. The tp_call constructor is called
65// with an indication that this object instance should be reclaimed when
66// execution completes.
67// object = object whose () operator will be called to provide
68// the process semantics.
69// name_p = optional name for object instance, or zero.
70// opt_p -> spawn options or zero.
71//
72// virtual void semantics()
73// This virtual method provides the execution semantics for its process.
74// It performs a () operation on m_object.
75//=============================================================================
76template<typename T>
78{
79public:
80 explicit sc_spawn_object( T object )
81 : m_object(object)
82 {}
83
84 virtual void semantics()
85 {
86 m_object();
87 }
88
89protected:
91};
92
93
94//------------------------------------------------------------------------------
95//"sc_spawn - semantic object with no return value"
96//
97// This inline function spawns a process for execution. The execution semantics
98// for the process being spawned will be provided by the supplied object
99// instance via its () operator. (E.g., an sc_bind bound function)
100// After creating the process it is registered with the simulator.
101// object = object instance providing the execution semantics via its
102// () operator.
103// name_p = optional name for object instance, or zero.
104// opt_p -> optional spawn options for process, or zero for the default.
105//------------------------------------------------------------------------------
106template< typename T
107 , typename std::enable_if<std::is_invocable_v<T>,bool>::type = true
108>
110sc_spawn( T object, const char* name_p = 0, const sc_spawn_options* opt_p = 0 )
111{
112 auto * context_p = sc_get_curr_simcontext();
113 auto * 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,
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,
128 spawn_p, opt_p
129 );
130 return method_handle;
131 }
132}
133
134//=============================================================================
135// CLASS sc_spawn_object_v<T, R>
136//
137// This templated helper class allows an object to provide the execution
138// semantics for a process via its () operator. An instance of the supplied
139// object will be kept to provide the semantics when the process is scheduled
140// for execution. The () operator returns a value, which will be stored at the
141// location specified by the supplied pointer. An example of an object that
142// might be used for this helper function would be valued sc_bind bound
143// function or method.
144//
145// sc_spawn_object_v( R* r_p, T f, const char* name_p,
146// const sc_spawn_options* opt_p )
147// r_p -> where to place the result of the function invocation.
148// f = information to be executed.
149// name_p = optional name for object instance, or zero.
150// opt_p -> optional spawn options for process, or zero for the default
151// This is the object instance constructor for this class. It makes a
152// copy of the supplied object. The tp_call constructor is called
153// with an indication that this object instance should be reclaimed when
154// execution completes.
155// result_p -> where to place the value of the () operator.
156// object = object whose () operator will be called to provide
157// the process semantics.
158//
159// virtual void semantics()
160// This virtual method provides the execution semantics for its process.
161// It performs a () operation on m_object, placing the result at m_result_p.
162//=============================================================================
163
164//------------------------------------------------------------------------------
165//"sc_spawn_object_v - semantic object with return value"
166//
167// This inline function spawns a process for execution. The execution semantics
168// for the process being spawned will be provided by the supplied object
169// instance via its () operator. (E.g., an sc_bind bound function) That
170// operator returns a value, which will be placed in the supplied return
171// location.
172// After creating the process it is registered with the simulator.
173// object = object instance providing the execution semantics via its ()
174// operator.
175// r_p -> where to place the value of the () operator.
176// name_p = optional name for object instance, or zero.
177// opt_p -> optional spawn options for process, or zero for the default.
178//------------------------------------------------------------------------------
179
180template<typename T, typename R>
182{
183public:
184 sc_spawn_object_v( R* r_p, T object )
185 : m_object(object)
186 , m_result_p(r_p)
187 {}
188
189 virtual void semantics()
190 {
191 *m_result_p = m_object();
192 }
193
194protected:
197};
198
199template< typename T, typename R
200 , typename std::enable_if_t<std::is_invocable_r_v<R,T>, bool> = true
201>
203sc_spawn( R* r_p, T object, const char* name_p = 0, const sc_spawn_options* opt_p = 0 )
204{
205 using spawn_object_t = sc_spawn_object_v<T,R>;
206 auto* context_p = sc_get_curr_simcontext();
207 auto* spawn_p = new spawn_object_t(r_p, object);
208
209 if ( !opt_p || !opt_p->is_method() )
210 {
211 sc_process_handle thread_handle = context_p->create_thread_process(
212 name_p, true,
213 SC_MAKE_FUNC_PTR(spawn_object_t,semantics),
214 spawn_p, opt_p
215 );
216 return thread_handle;
217 }
218 else
219 {
220 sc_process_handle method_handle = context_p->create_method_process(
221 name_p, true,
222 SC_MAKE_FUNC_PTR(spawn_object_t,semantics),
223 spawn_p, opt_p
224 );
225 return method_handle;
226 }
227}
228
229} // namespace sc_core
230
231// $Log: sc_spawn.h,v $
232// Revision 1.7 2011/08/26 20:46:11 acg
233// Andy Goodrich: moved the modification log to the end of the file to
234// eliminate source line number skew when check-ins are done.
235//
236// Revision 1.6 2011/02/18 20:27:14 acg
237// Andy Goodrich: Updated Copyrights.
238//
239// Revision 1.5 2011/02/13 21:47:38 acg
240// Andy Goodrich: update copyright notice.
241//
242// Revision 1.4 2011/02/01 21:14:02 acg
243// Andy Goodrich: formatting.
244//
245// Revision 1.3 2009/07/28 01:10:53 acg
246// Andy Goodrich: updates for 2.3 release candidate.
247//
248// Revision 1.2 2008/05/22 17:06:26 acg
249// Andy Goodrich: updated copyright notice to include 2008.
250//
251// Revision 1.1.1.1 2006/12/15 20:20:05 acg
252// SystemC 2.3
253//
254// Revision 1.6 2006/05/26 20:33:16 acg
255// Andy Goodrich: changes required by additional platform compilers (i.e.,
256// Microsoft VC++, Sun Forte, HP aCC).
257//
258// Revision 1.5 2006/05/08 18:01:44 acg
259// Andy Goodrich: changed the HP-specific implementations of sc_spawn() to
260// use a static_cast to create their entry functions rather than the
261// SC_MAKE_FUNC_PTR macro. The HP preprocessor does not parse template
262// arguments that contain a comma properly.
263//
264// Revision 1.4 2006/04/11 23:13:21 acg
265// Andy Goodrich: Changes for reduced reset support that only includes
266// sc_cthread, but has preliminary hooks for expanding to method and thread
267// processes also.
268//
269// Revision 1.3 2006/01/13 18:44:30 acg
270// Added $Log to record CVS changes into the source.
271
272#endif // SC_SPAWN_H_INCLUDED_
#define SC_MAKE_FUNC_PTR(callback_tag, func)
Definition: sc_process.h:133
class SC_API sc_port_base
Definition: sc_interface.h:37
class SC_API sc_event
Definition: sc_interface.h:36
sc_simcontext * sc_get_curr_simcontext()
sc_process_handle sc_spawn(T object, const char *name_p=0, const sc_spawn_options *opt_p=0)
Definition: sc_spawn.h:110
virtual void semantics()
Definition: sc_spawn.h:84
sc_spawn_object(T object)
Definition: sc_spawn.h:80
virtual void semantics()
Definition: sc_spawn.h:189
sc_spawn_object_v(R *r_p, T object)
Definition: sc_spawn.h:184