112027Sjungma@eit.uni-kl.de/*****************************************************************************
212027Sjungma@eit.uni-kl.de
312027Sjungma@eit.uni-kl.de  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
412027Sjungma@eit.uni-kl.de  more contributor license agreements.  See the NOTICE file distributed
512027Sjungma@eit.uni-kl.de  with this work for additional information regarding copyright ownership.
612027Sjungma@eit.uni-kl.de  Accellera licenses this file to you under the Apache License, Version 2.0
712027Sjungma@eit.uni-kl.de  (the "License"); you may not use this file except in compliance with the
812027Sjungma@eit.uni-kl.de  License.  You may obtain a copy of the License at
912027Sjungma@eit.uni-kl.de
1012027Sjungma@eit.uni-kl.de    http://www.apache.org/licenses/LICENSE-2.0
1112027Sjungma@eit.uni-kl.de
1212027Sjungma@eit.uni-kl.de  Unless required by applicable law or agreed to in writing, software
1312027Sjungma@eit.uni-kl.de  distributed under the License is distributed on an "AS IS" BASIS,
1412027Sjungma@eit.uni-kl.de  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
1512027Sjungma@eit.uni-kl.de  implied.  See the License for the specific language governing
1612027Sjungma@eit.uni-kl.de  permissions and limitations under the License.
1712027Sjungma@eit.uni-kl.de
1812027Sjungma@eit.uni-kl.de *****************************************************************************/
1912027Sjungma@eit.uni-kl.de
2012027Sjungma@eit.uni-kl.de/*****************************************************************************
2112027Sjungma@eit.uni-kl.de
2212027Sjungma@eit.uni-kl.de  sc_semaphore.cpp -- The sc_semaphore primitive channel class.
2312027Sjungma@eit.uni-kl.de
2412027Sjungma@eit.uni-kl.de  Original Author: Martin Janssen, Synopsys, Inc., 2001-05-21
2512027Sjungma@eit.uni-kl.de
2612027Sjungma@eit.uni-kl.de  CHANGE LOG IS AT THE END OF THE FILE
2712027Sjungma@eit.uni-kl.de *****************************************************************************/
2812027Sjungma@eit.uni-kl.de
2912027Sjungma@eit.uni-kl.de#include "sysc/communication/sc_communication_ids.h"
3012027Sjungma@eit.uni-kl.de#include "sysc/communication/sc_semaphore.h"
3112027Sjungma@eit.uni-kl.de#include "sysc/kernel/sc_simcontext.h"
3212027Sjungma@eit.uni-kl.de#include "sysc/kernel/sc_wait.h"
3312027Sjungma@eit.uni-kl.de
3412027Sjungma@eit.uni-kl.denamespace sc_core {
3512027Sjungma@eit.uni-kl.de
3612027Sjungma@eit.uni-kl.de// ----------------------------------------------------------------------------
3712027Sjungma@eit.uni-kl.de//  CLASS : sc_semaphore
3812027Sjungma@eit.uni-kl.de//
3912027Sjungma@eit.uni-kl.de//  The sc_semaphore primitive channel class.
4012027Sjungma@eit.uni-kl.de// ----------------------------------------------------------------------------
4112027Sjungma@eit.uni-kl.de
4212027Sjungma@eit.uni-kl.de// error reporting
4312027Sjungma@eit.uni-kl.de
4412027Sjungma@eit.uni-kl.devoid
4512027Sjungma@eit.uni-kl.desc_semaphore::report_error( const char* id, const char* add_msg ) const
4612027Sjungma@eit.uni-kl.de{
4712027Sjungma@eit.uni-kl.de    char msg[BUFSIZ];
4812027Sjungma@eit.uni-kl.de    if( add_msg != 0 ) {
4912027Sjungma@eit.uni-kl.de	std::sprintf( msg, "%s: semaphore '%s'", add_msg, name() );
5012027Sjungma@eit.uni-kl.de    } else {
5112027Sjungma@eit.uni-kl.de	std::sprintf( msg, "semaphore '%s'", name() );
5212027Sjungma@eit.uni-kl.de    }
5312027Sjungma@eit.uni-kl.de    SC_REPORT_ERROR( id, msg );
5412027Sjungma@eit.uni-kl.de}
5512027Sjungma@eit.uni-kl.de
5612027Sjungma@eit.uni-kl.de
5712027Sjungma@eit.uni-kl.de// constructors
5812027Sjungma@eit.uni-kl.de
5912027Sjungma@eit.uni-kl.desc_semaphore::sc_semaphore( int init_value_ )
6012027Sjungma@eit.uni-kl.de: sc_object( sc_gen_unique_name( "semaphore" ) ),
6112027Sjungma@eit.uni-kl.de  m_free( (std::string(SC_KERNEL_EVENT_PREFIX)+"_free_event").c_str() ),
6212027Sjungma@eit.uni-kl.de  m_value( init_value_ )
6312027Sjungma@eit.uni-kl.de{
6412027Sjungma@eit.uni-kl.de    if( m_value < 0 ) {
6512027Sjungma@eit.uni-kl.de	report_error( SC_ID_INVALID_SEMAPHORE_VALUE_ );
6612027Sjungma@eit.uni-kl.de    }
6712027Sjungma@eit.uni-kl.de}
6812027Sjungma@eit.uni-kl.de
6912027Sjungma@eit.uni-kl.desc_semaphore::sc_semaphore( const char* name_, int init_value_ )
7012027Sjungma@eit.uni-kl.de: sc_object( name_ ),
7112027Sjungma@eit.uni-kl.de  m_free( (std::string(SC_KERNEL_EVENT_PREFIX)+"_free_event").c_str() ),
7212027Sjungma@eit.uni-kl.de  m_value( init_value_ )
7312027Sjungma@eit.uni-kl.de{
7412027Sjungma@eit.uni-kl.de    if( m_value < 0 ) {
7512027Sjungma@eit.uni-kl.de	report_error( SC_ID_INVALID_SEMAPHORE_VALUE_ );
7612027Sjungma@eit.uni-kl.de    }
7712027Sjungma@eit.uni-kl.de}
7812027Sjungma@eit.uni-kl.de
7912027Sjungma@eit.uni-kl.de
8012027Sjungma@eit.uni-kl.de// interface methods
8112027Sjungma@eit.uni-kl.de
8212027Sjungma@eit.uni-kl.de// lock (take) the semaphore, block if not available
8312027Sjungma@eit.uni-kl.de
8412027Sjungma@eit.uni-kl.deint
8512027Sjungma@eit.uni-kl.desc_semaphore::wait()
8612027Sjungma@eit.uni-kl.de{
8712027Sjungma@eit.uni-kl.de    while( in_use() ) {
8812027Sjungma@eit.uni-kl.de	sc_core::wait( m_free, sc_get_curr_simcontext() );
8912027Sjungma@eit.uni-kl.de    }
9012027Sjungma@eit.uni-kl.de    -- m_value;
9112027Sjungma@eit.uni-kl.de    return 0;
9212027Sjungma@eit.uni-kl.de}
9312027Sjungma@eit.uni-kl.de
9412027Sjungma@eit.uni-kl.de
9512027Sjungma@eit.uni-kl.de// lock (take) the semaphore, return -1 if not available
9612027Sjungma@eit.uni-kl.de
9712027Sjungma@eit.uni-kl.deint
9812027Sjungma@eit.uni-kl.desc_semaphore::trywait()
9912027Sjungma@eit.uni-kl.de{
10012027Sjungma@eit.uni-kl.de    if( in_use() ) {
10112027Sjungma@eit.uni-kl.de	return -1;
10212027Sjungma@eit.uni-kl.de    }
10312027Sjungma@eit.uni-kl.de    -- m_value;
10412027Sjungma@eit.uni-kl.de    return 0;
10512027Sjungma@eit.uni-kl.de}
10612027Sjungma@eit.uni-kl.de
10712027Sjungma@eit.uni-kl.de
10812027Sjungma@eit.uni-kl.de// unlock (give) the semaphore
10912027Sjungma@eit.uni-kl.de
11012027Sjungma@eit.uni-kl.deint
11112027Sjungma@eit.uni-kl.desc_semaphore::post()
11212027Sjungma@eit.uni-kl.de{
11312027Sjungma@eit.uni-kl.de    ++m_value;
11412027Sjungma@eit.uni-kl.de    m_free.notify();
11512027Sjungma@eit.uni-kl.de    return 0;
11612027Sjungma@eit.uni-kl.de}
11712027Sjungma@eit.uni-kl.de
11812027Sjungma@eit.uni-kl.de} // namespace sc_core
11912027Sjungma@eit.uni-kl.de
12012027Sjungma@eit.uni-kl.de// $Log: sc_semaphore.cpp,v $
12112027Sjungma@eit.uni-kl.de// Revision 1.5  2011/08/26 20:45:42  acg
12212027Sjungma@eit.uni-kl.de//  Andy Goodrich: moved the modification log to the end of the file to
12312027Sjungma@eit.uni-kl.de//  eliminate source line number skew when check-ins are done.
12412027Sjungma@eit.uni-kl.de//
12512027Sjungma@eit.uni-kl.de// Revision 1.4  2011/03/23 16:17:22  acg
12612027Sjungma@eit.uni-kl.de//  Andy Goodrich: hide the sc_events that are kernel related.
12712027Sjungma@eit.uni-kl.de//
12812027Sjungma@eit.uni-kl.de// Revision 1.3  2011/02/18 20:23:45  acg
12912027Sjungma@eit.uni-kl.de//  Andy Goodrich: Copyright update.
13012027Sjungma@eit.uni-kl.de//
13112027Sjungma@eit.uni-kl.de// Revision 1.2  2010/11/02 16:31:01  acg
13212027Sjungma@eit.uni-kl.de//  Andy Goodrich: changed object derivation to use sc_object rather than
13312027Sjungma@eit.uni-kl.de//  sc_prim_channel as the parent class.
13412027Sjungma@eit.uni-kl.de//
13512027Sjungma@eit.uni-kl.de// Revision 1.1.1.1  2006/12/15 20:20:04  acg
13612027Sjungma@eit.uni-kl.de// SystemC 2.3
13712027Sjungma@eit.uni-kl.de//
13812027Sjungma@eit.uni-kl.de// Revision 1.6  2006/11/28 20:30:49  acg
13912027Sjungma@eit.uni-kl.de//  Andy Goodrich: updated from 2.2 source. sc_event_queue constructors
14012027Sjungma@eit.uni-kl.de//  collapsed into a single constructor with an optional argument to get
14112027Sjungma@eit.uni-kl.de//  the sc_module_name stack done correctly. Class name prefixing added
14212027Sjungma@eit.uni-kl.de//  to sc_semaphore calls to wait() to keep gcc 4.x happy.
14312027Sjungma@eit.uni-kl.de//
14412027Sjungma@eit.uni-kl.de// Revision 1.3  2006/01/13 18:47:42  acg
14512027Sjungma@eit.uni-kl.de// Added $Log command so that CVS comments are reproduced in the source.
14612027Sjungma@eit.uni-kl.de//
14712027Sjungma@eit.uni-kl.de
14812027Sjungma@eit.uni-kl.de// Taf!
149