00001 /***************************************************************************** 00002 00003 The following code is derived, directly or indirectly, from the SystemC 00004 source code Copyright (c) 1996-2006 by all Contributors. 00005 All Rights reserved. 00006 00007 The contents of this file are subject to the restrictions and limitations 00008 set forth in the SystemC Open Source License Version 2.4 (the "License"); 00009 You may not use this file except in compliance with such restrictions and 00010 limitations. You may obtain instructions on how to receive a copy of the 00011 License at http://www.systemc.org/. Software distributed by Contributors 00012 under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF 00013 ANY KIND, either express or implied. See the License for the specific 00014 language governing rights and limitations under the License. 00015 00016 *****************************************************************************/ 00017 00018 /***************************************************************************** 00019 00020 sc_spawn.h -- Process spawning support. 00021 00022 Original Authors: Andy Goodrich, Forte Design Systems, 17 June 2003 00023 Stuart Swan, Cadence, 00024 Bishnupriya Bhattacharya, Cadence Design Systems, 00025 25 August, 2003 00026 00027 *****************************************************************************/ 00028 00029 /***************************************************************************** 00030 00031 MODIFICATION LOG - modifiers, enter your name, affiliation, date and 00032 changes you are making here. 00033 00034 Name, Affiliation, Date: 00035 Description of Modification: 00036 00037 *****************************************************************************/ 00038 00039 // $Log: sc_spawn.h,v $ 00040 // Revision 1.1.1.1 2006/12/15 20:31:37 acg 00041 // SystemC 2.2 00042 // 00043 // Revision 1.4 2006/04/11 23:13:21 acg 00044 // Andy Goodrich: Changes for reduced reset support that only includes 00045 // sc_cthread, but has preliminary hooks for expanding to method and thread 00046 // processes also. 00047 // 00048 // Revision 1.3 2006/01/13 18:44:30 acg 00049 // Added $Log to record CVS changes into the source. 00050 // 00051 00052 #if !defined(sc_spawn_h_INCLUDED) 00053 #define sc_spawn_h_INCLUDED 00054 00055 #include "sysc/kernel/sc_process_handle.h" 00056 #include "sysc/kernel/sc_spawn_options.h" 00057 00058 namespace sc_core { 00059 00060 class sc_event; 00061 class sc_port_base; 00062 class sc_interface; 00063 class sc_event_finder; 00064 class sc_process_b; 00065 00066 //============================================================================= 00067 // CLASS sc_spawn_object<T> 00068 // 00069 // This templated helper class allows an object to provide the execution 00070 // semantics for a process via its () operator. An instance of the supplied 00071 // execution object will be kept to provide the semantics when the process is 00072 // scheduled for execution. The () operator does not return a value. An example 00073 // of an object that might be used for this helper function would be void BOOST 00074 // bound function or method. 00075 // 00076 // This class is derived from sc_process_host and overloads 00077 // sc_process_host::semantics to provide the actual semantic content. 00078 // 00079 // sc_spawn_object(T object, const char* name, const sc_spawn_options* opt_p) 00080 // This is the object instance constructor for this class. It makes a 00081 // copy of the supplied object. The tp_call constructor is called 00082 // with an indication that this object instance should be reclaimed when 00083 // execution completes. 00084 // object = object whose () operator will be called to provide 00085 // the process semantics. 00086 // name_p = optional name for object instance, or zero. 00087 // opt_p -> spawn options or zero. 00088 // 00089 // virtual void semantics() 00090 // This virtual method provides the execution semantics for its process. 00091 // It performs a () operation on m_object. 00092 //============================================================================= 00093 template<typename T> 00094 class sc_spawn_object : public sc_process_host { 00095 public: 00096 sc_spawn_object( T object) : m_object(object) 00097 { 00098 } 00099 00100 virtual void semantics() 00101 { 00102 m_object(); 00103 } 00104 00105 protected: 00106 T m_object; 00107 }; 00108 00109 00110 //------------------------------------------------------------------------------ 00111 //"sc_spawn - semantic object with no return value" 00112 // 00113 // This inline function spawns a process for execution. The execution semantics 00114 // for the process being spawned will be provided by the supplied object 00115 // instance via its () operator. (E.g., a BOOST bound function) 00116 // After creating the process it is registered with the simulator. 00117 // object = object instance providing the execution semantics via its 00118 // () operator. 00119 // name_p = optional name for object instance, or zero. 00120 // opt_p -> optional spawn options for process, or zero for the default. 00121 //------------------------------------------------------------------------------ 00122 template <typename T> 00123 inline sc_process_handle sc_spawn( 00124 T object, 00125 const char* name_p = 0, 00126 const sc_spawn_options* opt_p = 0) 00127 { 00128 sc_simcontext* context_p; 00129 sc_spawn_object<T>* spawn_p; 00130 00131 context_p = sc_get_curr_simcontext(); 00132 spawn_p = new sc_spawn_object<T>(object); 00133 if ( !opt_p || !opt_p->is_method() ) 00134 { 00135 sc_process_handle thread_handle = context_p->create_thread_process( 00136 name_p, true, 00137 SC_MAKE_FUNC_PTR(sc_spawn_object<T>,semantics), 00138 spawn_p, opt_p 00139 ); 00140 return thread_handle; 00141 } 00142 else 00143 { 00144 sc_process_handle method_handle = context_p->create_method_process( 00145 name_p, true, 00146 SC_MAKE_FUNC_PTR(sc_spawn_object<T>,semantics), 00147 spawn_p, opt_p 00148 ); 00149 return method_handle; 00150 } 00151 } 00152 00153 //============================================================================= 00154 // CLASS sc_spawn_object_v<T> for all compilers except HP aCC 00155 // or 00156 // CLASS sc_spawn_object_v<T, R> for HP aCC which tries to match this 00157 // one template argument class when the sc_spawn() declared above is 00158 // invoked with 3 arguments or 2 arguments, and generates compiler errors. 00159 // 00160 // This templated helper class allows an object to provide the execution 00161 // semantics for a process via its () operator. An instance of the supplied 00162 // object will be kept to provide the semantics when the process is scheduled 00163 // for execution. The () operator returns a value, which will be stored at the 00164 // location specified by the supplied pointer. An example of an object that 00165 // might be used for this helper function would be valued BOOST bound function 00166 // or method. 00167 // 00168 // sc_spawn_object_v( typename F::result_type* r_p, T f, const char* name_p, 00169 // const sc_spawn_options* opt_p ) 00170 // r_p -> where to place the result of the function invocation. 00171 // f = information to be executed. 00172 // name_p = optional name for object instance, or zero. 00173 // opt_p -> optional spawn options for process, or zero for the default 00174 // This is the object instance constructor for this class. It makes a 00175 // copy of the supplied object. The tp_call constructor is called 00176 // with an indication that this object instance should be reclaimed when 00177 // execution completes. 00178 // result_p -> where to place the value of the () operator. 00179 // object = object whose () operator will be called to provide 00180 // the process semantics. 00181 // 00182 // virtual void semantics() 00183 // This virtual method provides the execution semantics for its process. 00184 // It performs a () operation on m_object, placing the result at m_result_p. 00185 //============================================================================= 00186 00187 //------------------------------------------------------------------------------ 00188 //"sc_spawn_object_v - semantic object with return value" 00189 // 00190 // This inline function spawns a process for execution. The execution semantics 00191 // for the process being spawned will be provided by the supplied object 00192 // instance via its () operator. (E.g., a BOOST bound function) That operator 00193 // returns a value, which will be placed in the supplied return location. 00194 // After creating the process it is registered with the simulator. 00195 // object = object instance providing the execution semantics via its () 00196 // operator. 00197 // r_p -> where to place the value of the () operator. 00198 // name_p = optional name for object instance, or zero. 00199 // opt_p -> optional spawn options for process, or zero for the default. 00200 //------------------------------------------------------------------------------ 00201 00202 #if !defined (__HP_aCC) 00203 00204 template<typename T> 00205 class sc_spawn_object_v : public sc_process_host { 00206 public: 00207 sc_spawn_object_v( typename T::result_type* r_p, T object ) : 00208 m_object(object), m_result_p(r_p) 00209 { 00210 } 00211 00212 virtual void semantics() 00213 { 00214 *m_result_p = m_object(); 00215 } 00216 00217 protected: 00218 T m_object; 00219 typename T::result_type* m_result_p; 00220 }; 00221 00222 template <typename T> 00223 inline sc_process_handle sc_spawn( 00224 typename T::result_type* r_p, 00225 T object, 00226 const char* name_p = 0, 00227 const sc_spawn_options* opt_p = 0) 00228 { 00229 sc_simcontext* context_p; 00230 sc_spawn_object_v<T>* spawn_p; 00231 00232 context_p = sc_get_curr_simcontext(); 00233 00234 spawn_p = new sc_spawn_object_v<T>(r_p, object); 00235 if ( !opt_p || !opt_p->is_method() ) 00236 { 00237 sc_process_handle thread_handle = context_p->create_thread_process( 00238 name_p, true, 00239 SC_MAKE_FUNC_PTR(sc_spawn_object_v<T>,semantics), 00240 spawn_p, opt_p 00241 ); 00242 return thread_handle; 00243 } 00244 else 00245 { 00246 sc_process_handle method_handle = context_p->create_method_process( 00247 name_p, true, 00248 SC_MAKE_FUNC_PTR(sc_spawn_object_v<T>,semantics), 00249 spawn_p, opt_p 00250 ); 00251 return method_handle; 00252 } 00253 } 00254 00255 #else 00256 // for HP aCC 00257 template<typename T, typename R> 00258 class sc_spawn_object_v : public sc_process_host { 00259 public: 00260 sc_spawn_object_v( R* r_p, T object) : 00261 m_object(object), m_result_p(r_p) 00262 { 00263 } 00264 00265 virtual void semantics() 00266 { 00267 *m_result_p = m_object(); 00268 } 00269 00270 protected: 00271 T m_object; 00272 R* m_result_p; 00273 }; 00274 00275 template <typename T, typename R> 00276 inline sc_process_handle sc_spawn( 00277 R* r_p, 00278 T object, 00279 const char* name_p = 0, 00280 const sc_spawn_options* opt_p = 0) 00281 { 00282 sc_simcontext* context_p; 00283 sc_spawn_object_v<T,R>* spawn_p; 00284 00285 context_p = sc_get_curr_simcontext(); 00286 00287 spawn_p = new sc_spawn_object_v<T,R>(r_p, object); 00288 if ( !opt_p || !opt_p->is_method() ) 00289 { 00290 sc_process_handle thread_handle = context_p->create_thread_process( 00291 name_p, true, 00292 static_cast<sc_core::SC_ENTRY_FUNC>( 00293 &sc_spawn_object_v<T,R>::semantics), 00294 spawn_p, opt_p 00295 ); 00296 return thread_handle; 00297 } 00298 else 00299 { 00300 sc_process_handle method_handle = context_p->create_method_process( 00301 name_p, true, 00302 static_cast<sc_core::SC_ENTRY_FUNC>( 00303 &sc_spawn_object_v<T,R>::semantics), 00304 spawn_p, opt_p 00305 ); 00306 return method_handle; 00307 } 00308 } 00309 00310 #endif // HP 00311 00312 } // namespace sc_core 00313 00314 #endif // !defined(sc_spawn_h_INCLUDED)