00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #if defined(SC_USE_PTHREADS)
00045
00046
00047
00048 using namespace std;
00049
00050 #include "sysc/kernel/sc_cor_pthread.h"
00051 #include "sysc/kernel/sc_simcontext.h"
00052
00053 namespace sc_core {
00054
00055
00056
00057 #if defined(__hpux)
00058 # define PTHREAD_NULL cma_c_null
00059 #else // !defined(__hpux)
00060 # define PTHREAD_NULL NULL
00061 #endif // !defined(__hpux)
00062
00063 #define DEBUGF \
00064 if (0) std::cout << "sc_cor_pthread.cpp(" << __LINE__ << ") "
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 static sc_cor_pthread* active_cor_p=0;
00076 static pthread_cond_t create_condition;
00077 static pthread_mutex_t create_mutex;
00078 static sc_cor_pthread main_cor;
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 sc_cor_pthread::sc_cor_pthread()
00090 : m_cor_fn_arg( 0 ), m_pkg_p( 0 )
00091 {
00092 DEBUGF << this << ": sc_cor_pthread::sc_cor_pthread()" << std::endl;
00093 pthread_cond_init( &m_pt_condition, PTHREAD_NULL );
00094 pthread_mutex_init( &m_mutex, PTHREAD_NULL );
00095 }
00096
00097
00098
00099
00100 sc_cor_pthread::~sc_cor_pthread()
00101 {
00102 DEBUGF << this << ": sc_cor_pthread::~sc_cor_pthread()" << std::endl;
00103 pthread_cond_destroy( &m_pt_condition);
00104 pthread_mutex_destroy( &m_mutex );
00105 }
00106
00107
00108
00109
00110
00111
00112
00113
00114 void* sc_cor_pthread::invoke_module_method(void* context_p)
00115 {
00116 sc_cor_pthread* p = (sc_cor_pthread*)context_p;
00117 DEBUGF << p << ": sc_cor_pthread::invoke_module_method()" << std::endl;
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 pthread_mutex_lock( &create_mutex );
00129 DEBUGF << p << ": child signalling main thread " << endl;
00130 pthread_cond_signal( &create_condition );
00131 pthread_mutex_lock( &p->m_mutex );
00132 pthread_mutex_unlock( &create_mutex );
00133 pthread_cond_wait( &p->m_pt_condition, &p->m_mutex );
00134 pthread_mutex_unlock( &p->m_mutex );
00135
00136
00137
00138
00139 active_cor_p = p;
00140 DEBUGF << p << ": about to invoke real method "
00141 << active_cor_p << std::endl;
00142 (p->m_cor_fn)(p->m_cor_fn_arg);
00143
00144 return 0;
00145 }
00146
00147
00148
00149
00150
00151
00152
00153
00154 int sc_cor_pkg_pthread::instance_count = 0;
00155
00156
00157
00158
00159 sc_cor_pkg_pthread::sc_cor_pkg_pthread( sc_simcontext* simc )
00160 : sc_cor_pkg( simc )
00161 {
00162
00163 if( ++ instance_count == 1 )
00164 {
00165 pthread_cond_init( &create_condition, PTHREAD_NULL );
00166 pthread_mutex_init( &create_mutex, PTHREAD_NULL );
00167 assert( active_cor_p == 0 );
00168 main_cor.m_pkg_p = this;
00169 DEBUGF << &main_cor << ": is main co-routine" << std::endl;
00170 active_cor_p = &main_cor;
00171 }
00172 }
00173
00174
00175
00176
00177 sc_cor_pkg_pthread::~sc_cor_pkg_pthread()
00178 {
00179 if( -- instance_count == 0 ) {
00180
00181 }
00182 }
00183
00184
00185
00186
00187 sc_cor*
00188 sc_cor_pkg_pthread::create( std::size_t stack_size, sc_cor_fn* fn, void* arg )
00189 {
00190 sc_cor_pthread* cor_p = new sc_cor_pthread;
00191 DEBUGF << &main_cor << ": sc_cor_pkg_pthread::create("
00192 << cor_p << ")" << std::endl;
00193
00194
00195
00196
00197 cor_p->m_pkg_p = this;
00198 cor_p->m_cor_fn = fn;
00199 cor_p->m_cor_fn_arg = arg;
00200
00201
00202
00203
00204
00205
00206
00207 pthread_attr_t attr;
00208 pthread_attr_init( &attr );
00209 if ( stack_size != 0 )
00210 {
00211 pthread_attr_setstacksize( &attr, stack_size );
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 pthread_mutex_lock( &create_mutex );
00228 DEBUGF << &main_cor << ": about to create actual thread "
00229 << cor_p << std::endl;
00230 if ( pthread_create( &cor_p->m_thread, &attr,
00231 &sc_cor_pthread::invoke_module_method, (void*)cor_p ) )
00232 {
00233 std::fprintf(stderr, "ERROR - could not create thread\n");
00234 }
00235
00236 DEBUGF << &main_cor << ": main thread waiting for signal from "
00237 << cor_p << std::endl;
00238 pthread_cond_wait( &create_condition, &create_mutex );
00239 DEBUGF << &main_cor << ": main thread signaled by "
00240 << cor_p << endl;
00241 pthread_attr_destroy( &attr );
00242 pthread_mutex_unlock( &create_mutex );
00243 DEBUGF << &main_cor << ": exiting sc_cor_pkg_pthread::create("
00244 << cor_p << ")" << std::endl;
00245
00246 return cor_p;
00247 }
00248
00249
00250
00251
00252
00253
00254
00255 void
00256 sc_cor_pkg_pthread::yield( sc_cor* next_cor_p )
00257 {
00258 sc_cor_pthread* from_p = active_cor_p;
00259 sc_cor_pthread* to_p = (sc_cor_pthread*)next_cor_p;
00260
00261 DEBUGF << from_p << ": switch to " << to_p << std::endl;
00262 if ( to_p != from_p )
00263 {
00264 pthread_mutex_lock( &to_p->m_mutex );
00265 pthread_cond_signal( &to_p->m_pt_condition );
00266 pthread_mutex_lock( &from_p->m_mutex );
00267 pthread_mutex_unlock( &to_p->m_mutex );
00268 pthread_cond_wait( &from_p->m_pt_condition, &from_p->m_mutex );
00269 pthread_mutex_unlock( &from_p->m_mutex );
00270 }
00271
00272 active_cor_p = from_p;
00273 DEBUGF << from_p << " restarting after yield to " << to_p << std::endl;
00274 }
00275
00276
00277
00278
00279 void
00280 sc_cor_pkg_pthread::abort( sc_cor* next_cor_p )
00281 {
00282 sc_cor_pthread* n_p = (sc_cor_pthread*)next_cor_p;
00283
00284 DEBUGF << active_cor_p << ": aborting, switching to " << n_p << std::endl;
00285 pthread_mutex_lock( &n_p->m_mutex );
00286 pthread_cond_signal( &n_p->m_pt_condition );
00287 pthread_mutex_unlock( &n_p->m_mutex );
00288 }
00289
00290
00291
00292
00293 sc_cor*
00294 sc_cor_pkg_pthread::get_main()
00295 {
00296 return &main_cor;
00297 }
00298
00299 }
00300
00301 #endif // defined(SC_USE_PTHREADS)
00302
00303
00304