1/*****************************************************************************
2
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements.  See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License.  You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied.  See the License for the specific language governing
16  permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22  sc_mutex_if.h -- The sc_mutex_if interface class.
23
24  Original Author: Martin Janssen, Synopsys, Inc., 2001-05-21
25
26  CHANGE LOG IS AT THE END OF THE FILE
27 *****************************************************************************/
28
29#ifndef SC_MUTEX_IF_H
30#define SC_MUTEX_IF_H
31
32#include "sysc/communication/sc_interface.h"
33
34namespace sc_core {
35
36// ----------------------------------------------------------------------------
37//  CLASS : sc_mutex_if
38//
39//  The sc_mutex_if interface class.
40// ----------------------------------------------------------------------------
41
42class sc_mutex_if
43: virtual public sc_interface
44{
45public:
46
47    // the classical operations: lock(), trylock(), and unlock()
48
49    // blocks until mutex could be locked
50    virtual int lock() = 0;
51
52    // returns -1 if mutex could not be locked
53    virtual int trylock() = 0;
54
55    // returns -1 if mutex was not locked by caller
56    virtual int unlock() = 0;
57
58protected:
59
60    // constructor
61
62    sc_mutex_if()
63	{}
64
65private:
66
67    // disabled
68    sc_mutex_if( const sc_mutex_if& );
69    sc_mutex_if& operator = ( const sc_mutex_if& );
70};
71
72// ----------------------------------------------------------------------------
73//  CLASS : sc_scoped_lock
74//
75//  The sc_scoped_lock class to lock (and automatically release) a mutex.
76// ----------------------------------------------------------------------------
77
78//template< typename Lockable = sc_mutex_if >
79class sc_scoped_lock
80{
81public:
82    //typedef Lockable lockable_type;
83    typedef sc_mutex_if lockable_type;
84
85    explicit
86    sc_scoped_lock( lockable_type& mtx )
87      : m_ref(mtx)
88      , m_active(true)
89    {
90        m_ref.lock();
91    }
92
93    bool release()
94    {
95        if( m_active )
96        {
97            m_ref.unlock();
98            m_active = false;
99            return true;
100        }
101        return false;
102    }
103
104    ~sc_scoped_lock()
105    {
106        release();
107    }
108
109private:
110    // disabled
111    sc_scoped_lock( const sc_scoped_lock& );
112    sc_scoped_lock& operator=( const sc_scoped_lock& );
113
114    lockable_type& m_ref;
115    bool           m_active;
116};
117
118} // namespace sc_core
119
120//$Log: sc_mutex_if.h,v $
121//Revision 1.4  2011/08/26 20:45:41  acg
122// Andy Goodrich: moved the modification log to the end of the file to
123// eliminate source line number skew when check-ins are done.
124//
125//Revision 1.3  2011/04/19 02:36:26  acg
126// Philipp A. Hartmann: new aysnc_update and mutex support.
127//
128//Revision 1.2  2011/02/18 20:23:45  acg
129// Andy Goodrich: Copyright update.
130//
131//Revision 1.1.1.1  2006/12/15 20:20:04  acg
132//SystemC 2.3
133//
134//Revision 1.2  2006/01/03 23:18:26  acg
135//Changed copyright to include 2006.
136//
137//Revision 1.1.1.1  2005/12/19 23:16:43  acg
138//First check in of SystemC 2.1 into its own archive.
139//
140//Revision 1.8  2005/06/10 22:43:55  acg
141//Added CVS change log annotation.
142//
143
144#endif
145
146// Taf!
147