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_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 // $Log: sc_cor_pthread.h,v $ 00037 // Revision 1.1.1.1 2006/12/15 20:31:37 acg 00038 // SystemC 2.2 00039 // 00040 // Revision 1.3 2006/01/13 18:44:29 acg 00041 // Added $Log to record CVS changes into the source. 00042 // 00043 00044 #ifndef SC_COR_PTHREAD_H 00045 #define SC_COR_PTHREAD_H 00046 00047 00048 #if defined(SC_USE_PTHREADS) 00049 00050 #include "sysc/kernel/sc_cor.h" 00051 #include "sysc/kernel/sc_cmnhdr.h" 00052 #include <pthread.h> 00053 00054 namespace sc_core { 00055 00056 class sc_cor_pkg_pthread; 00057 00058 00059 // ---------------------------------------------------------------------------- 00060 // CLASS : sc_cor_pthread 00061 // 00062 // Coroutine class implemented with Posix Threads. 00063 // 00064 // Notes: 00065 // (1) The thread creation mutex and the creation condition are used to 00066 // suspend the thread creating another one until the created thread 00067 // reaches its invoke_module_method. This allows us to get control of 00068 // thread scheduling away from the pthread package. 00069 // ---------------------------------------------------------------------------- 00070 00071 class sc_cor_pthread : public sc_cor 00072 { 00073 public: 00074 00075 // constructor 00076 sc_cor_pthread(); 00077 00078 // destructor 00079 virtual ~sc_cor_pthread(); 00080 00081 // module method invocator (starts thread execution) 00082 static void* invoke_module_method( void* context_p ); 00083 00084 public: 00085 static sc_cor_pthread* m_active_cor_p; // Active coroutine. 00086 00087 public: 00088 sc_cor_fn* m_cor_fn; // Core function. 00089 void* m_cor_fn_arg; // Core function argument. 00090 pthread_mutex_t m_mutex; // Mutex to suspend thread on. 00091 sc_cor_pkg_pthread* m_pkg_p; // the creating coroutine package 00092 pthread_cond_t m_pt_condition; // Condition waiting for. 00093 pthread_t m_thread; // Our pthread storage. 00094 00095 private: 00096 00097 // disabled 00098 sc_cor_pthread( const sc_cor_pthread& ); 00099 sc_cor_pthread& operator = ( const sc_cor_pthread& ); 00100 }; 00101 00102 00103 // ---------------------------------------------------------------------------- 00104 // CLASS : sc_cor_pkg_pthread 00105 // 00106 // Coroutine package class implemented with Posix Threads. 00107 // ---------------------------------------------------------------------------- 00108 00109 class sc_cor_pkg_pthread 00110 : public sc_cor_pkg 00111 { 00112 public: 00113 00114 // constructor 00115 sc_cor_pkg_pthread( sc_simcontext* simc ); 00116 00117 // destructor 00118 virtual ~sc_cor_pkg_pthread(); 00119 00120 // create a new coroutine 00121 virtual sc_cor* create( std::size_t stack_size, sc_cor_fn* fn, void* arg ); 00122 00123 // yield to the next coroutine 00124 virtual void yield( sc_cor* next_cor ); 00125 00126 // abort the current coroutine (and resume the next coroutine) 00127 virtual void abort( sc_cor* next_cor ); 00128 00129 // get the main coroutine 00130 virtual sc_cor* get_main(); 00131 00132 private: 00133 00134 static int instance_count; 00135 00136 private: 00137 00138 // disabled 00139 sc_cor_pkg_pthread(); 00140 sc_cor_pkg_pthread( const sc_cor_pkg_pthread& ); 00141 sc_cor_pkg_pthread& operator = ( const sc_cor_pkg_pthread& ); 00142 }; 00143 00144 } // namespace sc_core 00145 00146 #endif 00147 00148 00149 #endif // defined(SC_USE_PTHREADS) 00150 00151 // Taf!