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_semaphore.cpp -- The sc_semaphore 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_communication_ids.h"
30#include "sysc/communication/sc_semaphore.h"
31#include "sysc/kernel/sc_simcontext.h"
32#include "sysc/kernel/sc_wait.h"
33
34namespace sc_core {
35
36// ----------------------------------------------------------------------------
37//  CLASS : sc_semaphore
38//
39//  The sc_semaphore primitive channel class.
40// ----------------------------------------------------------------------------
41
42// error reporting
43
44void
45sc_semaphore::report_error( const char* id, const char* add_msg ) const
46{
47    char msg[BUFSIZ];
48    if( add_msg != 0 ) {
49	std::sprintf( msg, "%s: semaphore '%s'", add_msg, name() );
50    } else {
51	std::sprintf( msg, "semaphore '%s'", name() );
52    }
53    SC_REPORT_ERROR( id, msg );
54}
55
56
57// constructors
58
59sc_semaphore::sc_semaphore( int init_value_ )
60: sc_object( sc_gen_unique_name( "semaphore" ) ),
61  m_free( (std::string(SC_KERNEL_EVENT_PREFIX)+"_free_event").c_str() ),
62  m_value( init_value_ )
63{
64    if( m_value < 0 ) {
65	report_error( SC_ID_INVALID_SEMAPHORE_VALUE_ );
66    }
67}
68
69sc_semaphore::sc_semaphore( const char* name_, int init_value_ )
70: sc_object( name_ ),
71  m_free( (std::string(SC_KERNEL_EVENT_PREFIX)+"_free_event").c_str() ),
72  m_value( init_value_ )
73{
74    if( m_value < 0 ) {
75	report_error( SC_ID_INVALID_SEMAPHORE_VALUE_ );
76    }
77}
78
79
80// interface methods
81
82// lock (take) the semaphore, block if not available
83
84int
85sc_semaphore::wait()
86{
87    while( in_use() ) {
88	sc_core::wait( m_free, sc_get_curr_simcontext() );
89    }
90    -- m_value;
91    return 0;
92}
93
94
95// lock (take) the semaphore, return -1 if not available
96
97int
98sc_semaphore::trywait()
99{
100    if( in_use() ) {
101	return -1;
102    }
103    -- m_value;
104    return 0;
105}
106
107
108// unlock (give) the semaphore
109
110int
111sc_semaphore::post()
112{
113    ++m_value;
114    m_free.notify();
115    return 0;
116}
117
118} // namespace sc_core
119
120// $Log: sc_semaphore.cpp,v $
121// Revision 1.5  2011/08/26 20:45:42  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.4  2011/03/23 16:17:22  acg
126//  Andy Goodrich: hide the sc_events that are kernel related.
127//
128// Revision 1.3  2011/02/18 20:23:45  acg
129//  Andy Goodrich: Copyright update.
130//
131// Revision 1.2  2010/11/02 16:31:01  acg
132//  Andy Goodrich: changed object derivation to use sc_object rather than
133//  sc_prim_channel as the parent class.
134//
135// Revision 1.1.1.1  2006/12/15 20:20:04  acg
136// SystemC 2.3
137//
138// Revision 1.6  2006/11/28 20:30:49  acg
139//  Andy Goodrich: updated from 2.2 source. sc_event_queue constructors
140//  collapsed into a single constructor with an optional argument to get
141//  the sc_module_name stack done correctly. Class name prefixing added
142//  to sc_semaphore calls to wait() to keep gcc 4.x happy.
143//
144// Revision 1.3  2006/01/13 18:47:42  acg
145// Added $Log command so that CVS comments are reproduced in the source.
146//
147
148// Taf!
149