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_fiber.cpp -- Coroutine implementation with fibers. 00021 00022 Original Author: Martin Janssen, Synopsys, Inc., 2001-12-18 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 00037 // $Log: sc_cor_fiber.cpp,v $ 00038 // Revision 1.1.1.1 2006/12/15 20:31:36 acg 00039 // SystemC 2.2 00040 // 00041 // Revision 1.3 2006/01/13 18:44:29 acg 00042 // Added $Log to record CVS changes into the source. 00043 // 00044 00045 #ifdef WIN32 00046 00047 00048 #include "sysc/kernel/sc_cor_fiber.h" 00049 #include "sysc/kernel/sc_simcontext.h" 00050 00051 #if( defined(_MSC_VER) && _MSC_VER >= 1300 ) 00052 00053 using std::size_t; 00054 00055 #endif 00056 00057 00058 namespace sc_core { 00059 00060 // ---------------------------------------------------------------------------- 00061 // File static variables. 00062 // ---------------------------------------------------------------------------- 00063 00064 // main coroutine 00065 00066 static sc_cor_fiber main_cor; 00067 00068 00069 // ---------------------------------------------------------------------------- 00070 // CLASS : sc_cor_fiber 00071 // 00072 // Coroutine class implemented with QuickThreads. 00073 // ---------------------------------------------------------------------------- 00074 00075 // destructor 00076 00077 sc_cor_fiber::~sc_cor_fiber() 00078 { 00079 if( m_fiber != 0 ) { 00080 # ifdef WIN32 00081 PVOID cur_fiber = GetCurrentFiber(); 00082 if (m_fiber != cur_fiber) 00083 # endif 00084 DeleteFiber( m_fiber ); 00085 } 00086 } 00087 00088 00089 // ---------------------------------------------------------------------------- 00090 // CLASS : sc_cor_pkg_fiber 00091 // 00092 // Coroutine package class implemented with QuickThreads. 00093 // ---------------------------------------------------------------------------- 00094 00095 int sc_cor_pkg_fiber::instance_count = 0; 00096 00097 00098 // constructor 00099 00100 sc_cor_pkg_fiber::sc_cor_pkg_fiber( sc_simcontext* simc ) 00101 : sc_cor_pkg( simc ) 00102 { 00103 if( ++ instance_count == 1 ) { 00104 // initialize the main coroutine 00105 assert( main_cor.m_fiber == 0 ); 00106 main_cor.m_fiber = ConvertThreadToFiber( 0 ); 00107 } 00108 } 00109 00110 00111 // destructor 00112 00113 sc_cor_pkg_fiber::~sc_cor_pkg_fiber() 00114 { 00115 if( -- instance_count == 0 ) { 00116 // cleanup the main coroutine 00117 main_cor.m_fiber = 0; 00118 } 00119 } 00120 00121 00122 // create a new coroutine 00123 00124 sc_cor* 00125 sc_cor_pkg_fiber::create( std::size_t stack_size, sc_cor_fn* fn, void* arg ) 00126 { 00127 sc_cor_fiber* cor = new sc_cor_fiber; 00128 cor->m_pkg = this; 00129 cor->m_stack_size = stack_size; 00130 cor->m_fiber = CreateFiber( cor->m_stack_size, 00131 (LPFIBER_START_ROUTINE) fn, arg ); 00132 return cor; 00133 } 00134 00135 00136 // yield to the next coroutine 00137 00138 void 00139 sc_cor_pkg_fiber::yield( sc_cor* next_cor ) 00140 { 00141 sc_cor_fiber* new_cor = SCAST<sc_cor_fiber*>( next_cor ); 00142 SwitchToFiber( new_cor->m_fiber ); 00143 } 00144 00145 00146 // abort the current coroutine (and resume the next coroutine) 00147 00148 void 00149 sc_cor_pkg_fiber::abort( sc_cor* next_cor ) 00150 { 00151 sc_cor_fiber* new_cor = SCAST<sc_cor_fiber*>( next_cor ); 00152 SwitchToFiber( new_cor->m_fiber ); 00153 } 00154 00155 00156 // get the main coroutine 00157 00158 sc_cor* 00159 sc_cor_pkg_fiber::get_main() 00160 { 00161 return &main_cor; 00162 } 00163 00164 } // namespace sc_core 00165 00166 #endif 00167 00168 // Taf!