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.cpp -- The sc_mutex primitive channel 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#include "sysc/communication/sc_mutex.h"
30#include "sysc/kernel/sc_simcontext.h"
31
32namespace sc_core {
33
34// ----------------------------------------------------------------------------
35//  CLASS : sc_mutex
36//
37//  The sc_mutex primitive channel class.
38// ----------------------------------------------------------------------------
39
40// constructors
41
42sc_mutex::sc_mutex()
43: sc_object( sc_gen_unique_name( "mutex" ) ),
44  m_owner( 0 ),
45  m_free( (std::string(SC_KERNEL_EVENT_PREFIX)+"_free_event").c_str() )
46{}
47
48sc_mutex::sc_mutex( const char* name_ )
49: sc_object( name_ ),
50  m_owner( 0 ),
51  m_free( (std::string(SC_KERNEL_EVENT_PREFIX)+"_free_event").c_str() )
52{}
53
54
55// destructor
56
57sc_mutex::~sc_mutex()
58{}
59
60// interface methods
61
62// blocks until mutex could be locked
63
64int
65sc_mutex::lock()
66{
67    if ( m_owner == sc_get_current_process_b()) return 0;
68    while( in_use() ) {
69	sc_core::wait( m_free, sc_get_curr_simcontext() );
70    }
71    m_owner = sc_get_current_process_b();
72    return 0;
73}
74
75
76// returns -1 if mutex could not be locked
77
78int
79sc_mutex::trylock()
80{
81    if ( m_owner == sc_get_current_process_b()) return 0;
82    if( in_use() ) {
83	return -1;
84    }
85    m_owner = sc_get_current_process_b();
86    return 0;
87}
88
89
90// returns -1 if mutex was not locked by caller
91
92int
93sc_mutex::unlock()
94{
95    if( m_owner != sc_get_current_process_b() ) {
96	return -1;
97    }
98    m_owner = 0;
99    m_free.notify();
100    return 0;
101}
102
103} // namespace sc_core
104
105// $Log: sc_mutex.cpp,v $
106// Revision 1.7  2011/08/26 20:45:40  acg
107//  Andy Goodrich: moved the modification log to the end of the file to
108//  eliminate source line number skew when check-ins are done.
109//
110// Revision 1.6  2011/03/28 13:02:29  acg
111//  Andy Goodrich: removed sc_event in sc_mutex class from the object
112//  hierarchy since it is considered a "kernel" event.
113//
114// Revision 1.5  2011/02/18 20:23:45  acg
115//  Andy Goodrich: Copyright update.
116//
117// Revision 1.4  2010/11/02 16:31:01  acg
118//  Andy Goodrich: changed object derivation to use sc_object rather than
119//  sc_prim_channel as the parent class.
120//
121// Revision 1.3  2008/11/13 15:29:46  acg
122//  David C. Black, ESLX, Inc: lock & trylock now allow owner to apply
123//  lock more than once without incident. Previous behavior locked up the
124//  owning process.
125//
126// Revision 1.2  2008/05/20 16:46:18  acg
127//  Andy Goodrich: added checks for multiple writers.
128//
129// Revision 1.1.1.1  2006/12/15 20:20:04  acg
130// SystemC 2.3
131//
132// Revision 1.4  2006/03/21 00:00:27  acg
133//   Andy Goodrich: changed name of sc_get_current_process_base() to be
134//   sc_get_current_process_b() since its returning an sc_process_b instance.
135//
136// Revision 1.3  2006/01/13 18:47:42  acg
137// Added $Log command so that CVS comments are reproduced in the source.
138//
139
140// Taf!
141