SystemC  2.3.2
Accellera SystemC proof-of-concept library
sc_host_mutex.h
Go to the documentation of this file.
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_host_mutex.h -- A "real" mutex for the underlying host system
23 */
33 #ifndef SC_HOST_MUTEX_H_INCLUDED_
34 #define SC_HOST_MUTEX_H_INCLUDED_
35 
36 #ifndef SC_INCLUDE_WINDOWS_H
37 # define SC_INCLUDE_WINDOWS_H // include Windows.h, if needed
38 #endif
39 #include "sysc/kernel/sc_cmnhdr.h"
41 
42 #if !defined(WIN32) && !defined(_WIN32) // use pthread mutex
43 # include <pthread.h>
44 #endif
45 
46 namespace sc_core {
47 
55 {
56 #if defined(WIN32) || defined(_WIN32) // use CRITICAL_SECTION on Windows
57 
58  typedef CRITICAL_SECTION underlying_type;
59 
60  void do_init() { InitializeCriticalSection( &m_mtx ); }
61  void do_lock() { EnterCriticalSection( &m_mtx ); }
62  bool do_trylock() { return ( TryEnterCriticalSection( &m_mtx ) != 0 ); }
63  void do_unlock() { LeaveCriticalSection( &m_mtx ); }
64  void do_destroy() { DeleteCriticalSection( &m_mtx ); }
65 
66 #else // use pthread mutex
67 
68 # if defined(__hpux)
69 # define SC_PTHREAD_NULL_ cma_c_null
70 # else // !defined(__hpux)
71 # define SC_PTHREAD_NULL_ NULL
72 # endif
73 
74  typedef pthread_mutex_t underlying_type;
75 
76  void do_init() { pthread_mutex_init( &m_mtx, SC_PTHREAD_NULL_ ); }
77  void do_lock() { pthread_mutex_lock( &m_mtx ); }
78 # ifdef _XOPEN_SOURCE
79  bool do_trylock() { return ( pthread_mutex_trylock( &m_mtx ) == 0 ); }
80 # else
81  bool do_trylock() { return /* not supported */ false; }
82 # endif
83  void do_unlock() { pthread_mutex_unlock( &m_mtx ); }
84  void do_destroy() { pthread_mutex_destroy( &m_mtx ); }
85 
86 # undef SC_PTHREAD_NULL_
87 
88 #endif // platform-specific implementation
89 
90 public:
91 
92  // constructors and destructor
93 
94  sc_host_mutex() : m_mtx()
95  { do_init(); }
96  virtual ~sc_host_mutex()
97  { do_destroy(); }
98 
99 
100  // interface methods
101 
102  // blocks until mutex could be locked
103  virtual int lock()
104  { do_lock(); return 0; }
105 
106  // returns -1 if mutex could not be locked
107  virtual int trylock()
108  { return do_trylock() ? 0 : -1; }
109 
110  // should return -1 if mutex was not locked by caller,
111  // but is not yet able to check this
112  virtual int unlock()
113  { do_unlock(); return 0; }
114 
115 private:
116  underlying_type m_mtx;
117 };
118 
119 } // namespace sc_core
120 
121 /*****************************************************************************
122 
123  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
124  changes you are making here.
125 
126  Name, Affiliation, Date:
127  Description of Modification:
128  *****************************************************************************/
129 //$Log: sc_host_mutex.h,v $
130 //Revision 1.4 2011/08/30 21:53:23 acg
131 // Jerome Cornet: include window.h for gnu c in windows case.
132 //
133 //Revision 1.3 2011/08/07 19:08:01 acg
134 // Andy Goodrich: moved logs to end of file so line number synching works
135 // better between versions.
136 //
137 //Revision 1.2 2011/06/25 17:08:38 acg
138 // Andy Goodrich: Jerome Cornet's changes to use libtool to build the
139 // library.
140 //
141 //Revision 1.1 2011/04/18 19:04:11 acg
142 // Philipp A. Hartmann: first check in of file.
143 //
144 
145 #endif
146 
147 // Taf!
The sc_mutex_if interface class.
The sc_mutex_if interface class.
Definition: sc_mutex_if.h:46
The sc_host_mutex class, wrapping an OS mutex on the simulation host.
Definition: sc_host_mutex.h:54
#define SC_PTHREAD_NULL_
Definition: sc_host_mutex.h:71
#define SC_API
Definition: sc_cmnhdr.h:168