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_semaphore.cpp -- The sc_semaphore 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_semaphore.cpp,v $ 00038 // Revision 1.1.1.1 2006/12/15 20:31:35 acg 00039 // SystemC 2.2 00040 // 00041 // Revision 1.3 2006/01/13 18:47:42 acg 00042 // Added $Log command so that CVS comments are reproduced in the source. 00043 // 00044 00045 #include "sysc/communication/sc_communication_ids.h" 00046 #include "sysc/communication/sc_semaphore.h" 00047 #include "sysc/kernel/sc_simcontext.h" 00048 00049 namespace sc_core { 00050 00051 // ---------------------------------------------------------------------------- 00052 // CLASS : sc_semaphore 00053 // 00054 // The sc_semaphore primitive channel class. 00055 // ---------------------------------------------------------------------------- 00056 00057 // error reporting 00058 00059 void 00060 sc_semaphore::report_error( const char* id, const char* add_msg ) const 00061 { 00062 char msg[BUFSIZ]; 00063 if( add_msg != 0 ) { 00064 std::sprintf( msg, "%s: semaphore '%s'", add_msg, name() ); 00065 } else { 00066 std::sprintf( msg, "semaphore '%s'", name() ); 00067 } 00068 SC_REPORT_ERROR( id, msg ); 00069 } 00070 00071 00072 // constructors 00073 00074 sc_semaphore::sc_semaphore( int init_value_ ) 00075 : sc_prim_channel( sc_gen_unique_name( "semaphore" ) ), 00076 m_value( init_value_ ) 00077 { 00078 if( m_value < 0 ) { 00079 report_error( SC_ID_INVALID_SEMAPHORE_VALUE_ ); 00080 } 00081 } 00082 00083 sc_semaphore::sc_semaphore( const char* name_, int init_value_ ) 00084 : sc_prim_channel( name_ ), m_value( init_value_ ) 00085 { 00086 if( m_value < 0 ) { 00087 report_error( SC_ID_INVALID_SEMAPHORE_VALUE_ ); 00088 } 00089 } 00090 00091 00092 // interface methods 00093 00094 // lock (take) the semaphore, block if not available 00095 00096 int 00097 sc_semaphore::wait() 00098 { 00099 while( in_use() ) { 00100 sc_prim_channel::wait( m_free ); 00101 } 00102 -- m_value; 00103 return 0; 00104 } 00105 00106 00107 // lock (take) the semaphore, return -1 if not available 00108 00109 int 00110 sc_semaphore::trywait() 00111 { 00112 if( in_use() ) { 00113 return -1; 00114 } 00115 -- m_value; 00116 return 0; 00117 } 00118 00119 00120 // unlock (give) the semaphore 00121 00122 int 00123 sc_semaphore::post() 00124 { 00125 ++m_value; 00126 m_free.notify(); 00127 return 0; 00128 } 00129 00130 } // namespace sc_core 00131 00132 // Taf!