sc_mutex_if.h revision 12027
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_mutex_if.h -- The sc_mutex_if interface 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#ifndef SC_MUTEX_IF_H
3012027Sjungma@eit.uni-kl.de#define SC_MUTEX_IF_H
3112027Sjungma@eit.uni-kl.de
3212027Sjungma@eit.uni-kl.de#include "sysc/communication/sc_interface.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_mutex_if
3812027Sjungma@eit.uni-kl.de//
3912027Sjungma@eit.uni-kl.de//  The sc_mutex_if interface class.
4012027Sjungma@eit.uni-kl.de// ----------------------------------------------------------------------------
4112027Sjungma@eit.uni-kl.de
4212027Sjungma@eit.uni-kl.declass sc_mutex_if
4312027Sjungma@eit.uni-kl.de: virtual public sc_interface
4412027Sjungma@eit.uni-kl.de{
4512027Sjungma@eit.uni-kl.depublic:
4612027Sjungma@eit.uni-kl.de
4712027Sjungma@eit.uni-kl.de    // the classical operations: lock(), trylock(), and unlock()
4812027Sjungma@eit.uni-kl.de
4912027Sjungma@eit.uni-kl.de    // blocks until mutex could be locked
5012027Sjungma@eit.uni-kl.de    virtual int lock() = 0;
5112027Sjungma@eit.uni-kl.de
5212027Sjungma@eit.uni-kl.de    // returns -1 if mutex could not be locked
5312027Sjungma@eit.uni-kl.de    virtual int trylock() = 0;
5412027Sjungma@eit.uni-kl.de
5512027Sjungma@eit.uni-kl.de    // returns -1 if mutex was not locked by caller
5612027Sjungma@eit.uni-kl.de    virtual int unlock() = 0;
5712027Sjungma@eit.uni-kl.de
5812027Sjungma@eit.uni-kl.deprotected:
5912027Sjungma@eit.uni-kl.de
6012027Sjungma@eit.uni-kl.de    // constructor
6112027Sjungma@eit.uni-kl.de
6212027Sjungma@eit.uni-kl.de    sc_mutex_if()
6312027Sjungma@eit.uni-kl.de	{}
6412027Sjungma@eit.uni-kl.de
6512027Sjungma@eit.uni-kl.deprivate:
6612027Sjungma@eit.uni-kl.de
6712027Sjungma@eit.uni-kl.de    // disabled
6812027Sjungma@eit.uni-kl.de    sc_mutex_if( const sc_mutex_if& );
6912027Sjungma@eit.uni-kl.de    sc_mutex_if& operator = ( const sc_mutex_if& );
7012027Sjungma@eit.uni-kl.de};
7112027Sjungma@eit.uni-kl.de
7212027Sjungma@eit.uni-kl.de// ----------------------------------------------------------------------------
7312027Sjungma@eit.uni-kl.de//  CLASS : sc_scoped_lock
7412027Sjungma@eit.uni-kl.de//
7512027Sjungma@eit.uni-kl.de//  The sc_scoped_lock class to lock (and automatically release) a mutex.
7612027Sjungma@eit.uni-kl.de// ----------------------------------------------------------------------------
7712027Sjungma@eit.uni-kl.de
7812027Sjungma@eit.uni-kl.de//template< typename Lockable = sc_mutex_if >
7912027Sjungma@eit.uni-kl.declass sc_scoped_lock
8012027Sjungma@eit.uni-kl.de{
8112027Sjungma@eit.uni-kl.depublic:
8212027Sjungma@eit.uni-kl.de    //typedef Lockable lockable_type;
8312027Sjungma@eit.uni-kl.de    typedef sc_mutex_if lockable_type;
8412027Sjungma@eit.uni-kl.de
8512027Sjungma@eit.uni-kl.de    explicit
8612027Sjungma@eit.uni-kl.de    sc_scoped_lock( lockable_type& mtx )
8712027Sjungma@eit.uni-kl.de      : m_ref(mtx)
8812027Sjungma@eit.uni-kl.de      , m_active(true)
8912027Sjungma@eit.uni-kl.de    {
9012027Sjungma@eit.uni-kl.de        m_ref.lock();
9112027Sjungma@eit.uni-kl.de    }
9212027Sjungma@eit.uni-kl.de
9312027Sjungma@eit.uni-kl.de    bool release()
9412027Sjungma@eit.uni-kl.de    {
9512027Sjungma@eit.uni-kl.de        if( m_active )
9612027Sjungma@eit.uni-kl.de        {
9712027Sjungma@eit.uni-kl.de            m_ref.unlock();
9812027Sjungma@eit.uni-kl.de            m_active = false;
9912027Sjungma@eit.uni-kl.de            return true;
10012027Sjungma@eit.uni-kl.de        }
10112027Sjungma@eit.uni-kl.de        return false;
10212027Sjungma@eit.uni-kl.de    }
10312027Sjungma@eit.uni-kl.de
10412027Sjungma@eit.uni-kl.de    ~sc_scoped_lock()
10512027Sjungma@eit.uni-kl.de    {
10612027Sjungma@eit.uni-kl.de        release();
10712027Sjungma@eit.uni-kl.de    }
10812027Sjungma@eit.uni-kl.de
10912027Sjungma@eit.uni-kl.deprivate:
11012027Sjungma@eit.uni-kl.de    // disabled
11112027Sjungma@eit.uni-kl.de    sc_scoped_lock( const sc_scoped_lock& );
11212027Sjungma@eit.uni-kl.de    sc_scoped_lock& operator=( const sc_scoped_lock& );
11312027Sjungma@eit.uni-kl.de
11412027Sjungma@eit.uni-kl.de    lockable_type& m_ref;
11512027Sjungma@eit.uni-kl.de    bool           m_active;
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_mutex_if.h,v $
12112027Sjungma@eit.uni-kl.de//Revision 1.4  2011/08/26 20:45:41  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.3  2011/04/19 02:36:26  acg
12612027Sjungma@eit.uni-kl.de// Philipp A. Hartmann: new aysnc_update and mutex support.
12712027Sjungma@eit.uni-kl.de//
12812027Sjungma@eit.uni-kl.de//Revision 1.2  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.1.1.1  2006/12/15 20:20:04  acg
13212027Sjungma@eit.uni-kl.de//SystemC 2.3
13312027Sjungma@eit.uni-kl.de//
13412027Sjungma@eit.uni-kl.de//Revision 1.2  2006/01/03 23:18:26  acg
13512027Sjungma@eit.uni-kl.de//Changed copyright to include 2006.
13612027Sjungma@eit.uni-kl.de//
13712027Sjungma@eit.uni-kl.de//Revision 1.1.1.1  2005/12/19 23:16:43  acg
13812027Sjungma@eit.uni-kl.de//First check in of SystemC 2.1 into its own archive.
13912027Sjungma@eit.uni-kl.de//
14012027Sjungma@eit.uni-kl.de//Revision 1.8  2005/06/10 22:43:55  acg
14112027Sjungma@eit.uni-kl.de//Added CVS change log annotation.
14212027Sjungma@eit.uni-kl.de//
14312027Sjungma@eit.uni-kl.de
14412027Sjungma@eit.uni-kl.de#endif
14512027Sjungma@eit.uni-kl.de
14612027Sjungma@eit.uni-kl.de// Taf!
147