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_mutex.cpp -- The sc_mutex primitive channel class. 00021 00022 Original Author: Martin Janssen, Synopsys, Inc., 2001-05-21 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_mutex.cpp,v $ 00038 // Revision 1.1.1.1 2006/12/15 20:31:35 acg 00039 // SystemC 2.2 00040 // 00041 // Revision 1.4 2006/03/21 00:00:27 acg 00042 // Andy Goodrich: changed name of sc_get_current_process_base() to be 00043 // sc_get_current_process_b() since its returning an sc_process_b instance. 00044 // 00045 // Revision 1.3 2006/01/13 18:47:42 acg 00046 // Added $Log command so that CVS comments are reproduced in the source. 00047 // 00048 00049 #include "sysc/communication/sc_mutex.h" 00050 #include "sysc/kernel/sc_simcontext.h" 00051 00052 namespace sc_core { 00053 00054 // ---------------------------------------------------------------------------- 00055 // CLASS : sc_mutex 00056 // 00057 // The sc_mutex primitive channel class. 00058 // ---------------------------------------------------------------------------- 00059 00060 // constructors 00061 00062 sc_mutex::sc_mutex() 00063 : sc_prim_channel( sc_gen_unique_name( "mutex" ) ), 00064 m_owner( 0 ) 00065 {} 00066 00067 sc_mutex::sc_mutex( const char* name_ ) 00068 : sc_prim_channel( name_ ), 00069 m_owner( 0 ) 00070 {} 00071 00072 00073 // destructor 00074 00075 sc_mutex::~sc_mutex() 00076 {} 00077 00078 // interface methods 00079 00080 // blocks until mutex could be locked 00081 00082 int 00083 sc_mutex::lock() 00084 { 00085 if (m_owner == sc_get_current_process_b()) return 0; 00086 while( in_use() ) { 00087 wait( m_free ); 00088 } 00089 m_owner = sc_get_current_process_b(); 00090 return 0; 00091 } 00092 00093 00094 // returns -1 if mutex could not be locked 00095 00096 int 00097 sc_mutex::trylock() 00098 { 00099 if (m_owner == sc_get_current_process_b()) return 0; 00100 if( in_use() ) { 00101 return -1; 00102 } 00103 m_owner = sc_get_current_process_b(); 00104 return 0; 00105 } 00106 00107 00108 // returns -1 if mutex was not locked by caller 00109 00110 int 00111 sc_mutex::unlock() 00112 { 00113 if( m_owner != sc_get_current_process_b() ) { 00114 return -1; 00115 } 00116 m_owner = 0; 00117 m_free.notify(); 00118 return 0; 00119 } 00120 00121 } // namespace sc_core 00122 00123 // Taf!