00001 /***************************************************************************** 00002 00003 The following code is derived, directly or indirectly, from the SystemC 00004 source code Copyright (c) 1996-2005 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_cor_pthread.h -- Coroutine implementation with pthreads. 00021 00022 Original Author: Andy Goodrich, Forte Design Systems, 2002-11-10 00023 00024 *****************************************************************************/ 00025 00026 /***************************************************************************** 00027 00028 MODIFICATION LOG - modifiers, enter your name, affiliation, date and 00029 changes you are making here. 00030 00031 Name, Affiliation, Date: 00032 Description of Modification: 00033 00034 *****************************************************************************/ 00035 00036 #ifndef SC_COR_PTHREAD_H 00037 #define SC_COR_PTHREAD_H 00038 00039 00040 #if defined(SC_USE_PTHREADS) 00041 00042 #include "sysc/kernel/sc_cor.h" 00043 #include "sysc/kernel/sc_cmnhdr.h" 00044 #include <pthread.h> 00045 00046 namespace sc_core { 00047 00048 class sc_cor_pkg_pthread; 00049 00050 00051 // ---------------------------------------------------------------------------- 00052 // CLASS : sc_cor_pthread 00053 // 00054 // Coroutine class implemented with Posix Threads. 00055 // 00056 // Notes: 00057 // (1) The thread creation mutex and the creation condition are used to 00058 // suspend the thread creating another one until the created thread 00059 // reaches its invoke_module_method. This allows us to get control of 00060 // thread scheduling away from the pthread package. 00061 // ---------------------------------------------------------------------------- 00062 00063 class sc_cor_pthread : public sc_cor 00064 { 00065 public: 00066 00067 // constructor 00068 sc_cor_pthread(); 00069 00070 // destructor 00071 virtual ~sc_cor_pthread(); 00072 00073 // module method invocator (starts thread execution) 00074 static void* invoke_module_method( void* context_p ); 00075 00076 public: 00077 static sc_cor_pthread* m_active_cor_p; // Active coroutine. 00078 static pthread_cond_t m_create_condition; // See note 1 above. 00079 static pthread_mutex_t m_create_mutex; // See note 1 above. 00080 00081 public: 00082 sc_cor_fn* m_cor_fn; // Core function. 00083 void* m_cor_fn_arg; // Core function argument. 00084 pthread_mutex_t m_mutex; // Mutex to suspend thread on. 00085 sc_cor_pkg_pthread* m_pkg_p; // the creating coroutine package 00086 pthread_cond_t m_pt_condition; // Condition waiting for. 00087 pthread_t m_thread; // Our pthread storage. 00088 00089 private: 00090 00091 // disabled 00092 sc_cor_pthread( const sc_cor_pthread& ); 00093 sc_cor_pthread& operator = ( const sc_cor_pthread& ); 00094 }; 00095 00096 00097 // ---------------------------------------------------------------------------- 00098 // CLASS : sc_cor_pkg_pthread 00099 // 00100 // Coroutine package class implemented with Posix Threads. 00101 // ---------------------------------------------------------------------------- 00102 00103 class sc_cor_pkg_pthread 00104 : public sc_cor_pkg 00105 { 00106 public: 00107 00108 // constructor 00109 sc_cor_pkg_pthread( sc_simcontext* simc ); 00110 00111 // destructor 00112 virtual ~sc_cor_pkg_pthread(); 00113 00114 // create a new coroutine 00115 virtual sc_cor* create( std::size_t stack_size, sc_cor_fn* fn, void* arg ); 00116 00117 // yield to the next coroutine 00118 virtual void yield( sc_cor* next_cor ); 00119 00120 // abort the current coroutine (and resume the next coroutine) 00121 virtual void abort( sc_cor* next_cor ); 00122 00123 // get the main coroutine 00124 virtual sc_cor* get_main(); 00125 00126 private: 00127 00128 static int instance_count; 00129 00130 private: 00131 00132 // disabled 00133 sc_cor_pkg_pthread(); 00134 sc_cor_pkg_pthread( const sc_cor_pkg_pthread& ); 00135 sc_cor_pkg_pthread& operator = ( const sc_cor_pkg_pthread& ); 00136 }; 00137 00138 } // namespace sc_core 00139 00140 #endif 00141 00142 00143 #endif // defined(SC_USE_PTHREADS) 00144 00145 // Taf!
1.5.1