SystemC  Recoding Infrastructure for SystemC v0.6.2 derived from Accellera SystemC 2.3.1
Accellera SystemC proof-of-concept library
sc_host_mutex.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  The following code is derived, directly or indirectly, from the SystemC
4  source code Copyright (c) 1996-2014 by all Contributors.
5  All Rights reserved.
6 
7  The contents of this file are subject to the restrictions and limitations
8  set forth in the SystemC Open Source License (the "License");
9  You may not use this file except in compliance with such restrictions and
10  limitations. You may obtain instructions on how to receive a copy of the
11  License at http://www.accellera.org/. Software distributed by Contributors
12  under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
13  ANY KIND, either express or implied. See the License for the specific
14  language governing rights and limitations under the License.
15 
16  *****************************************************************************/
17 
18 /*****************************************************************************
19 
20  sc_host_mutex.h -- A "real" mutex for the underlying host system
21 
22  Original Author: Philipp A. Hartmann, OFFIS
23 
24  CHANGE LOG AT THE END OF THE FILE
25  *****************************************************************************/
26 
27 #ifndef SC_HOST_MUTEX_H_INCLUDED_
28 #define SC_HOST_MUTEX_H_INCLUDED_
29 
30 #ifndef SC_INCLUDE_WINDOWS_H
31 # define SC_INCLUDE_WINDOWS_H // include Windows.h, if needed
32 #endif
33 #include "sysc/kernel/sc_cmnhdr.h"
35 
36 #if defined(WIN32) || defined(_WIN32)
37 
38 #define SC_MTX_TYPE_ CRITICAL_SECTION
39 
40 #define SC_MTX_INIT_( Mutex ) \
41  InitializeCriticalSection( &(Mutex) )
42 #define SC_MTX_LOCK_( Mutex ) \
43  EnterCriticalSection( &(Mutex) )
44 #define SC_MTX_UNLOCK_( Mutex ) \
45  LeaveCriticalSection( &(Mutex) )
46 #define SC_MTX_TRYLOCK_( Mutex ) \
47  ( TryEnterCriticalSection( &(Mutex) ) != 0 )
48 #define SC_MTX_DESTROY_( Mutex ) \
49  DeleteCriticalSection( &(Mutex) )
50 
51 #else // use pthread mutex
52 
53 #include <pthread.h>
54 #define SC_MTX_TYPE_ pthread_mutex_t
55 
56 #if defined(__hpux)
57 # define SC_PTHREAD_NULL_ cma_c_null
58 #else // !defined(__hpux)
59 # define SC_PTHREAD_NULL_ NULL
60 #endif
61 
62 #define SC_MTX_INIT_( Mutex ) \
63  pthread_mutex_init( &(Mutex), SC_PTHREAD_NULL_ )
64 #define SC_MTX_LOCK_( Mutex ) \
65  pthread_mutex_lock( &(Mutex) )
66 #define SC_MTX_UNLOCK_( Mutex ) \
67  pthread_mutex_unlock( &(Mutex) )
68 
69 #ifdef _XOPEN_SOURCE
70 # define SC_MTX_TRYLOCK_( Mutex ) \
71  ( pthread_mutex_trylock( &(Mutex) ) == 0 )
72 #else // no try_lock available
73 # define SC_MTX_TRYLOCK_( Mutex ) \
74  ( false )
75 #endif
76 
77 #define SC_MTX_DESTROY_( Mutex ) \
78  pthread_mutex_destroy( &(Mutex) )
79 
80 #endif
81 
82 namespace sc_core {
83 
84 /**************************************************************************/
91 class sc_host_mutex : public sc_mutex_if
92 {
93  typedef SC_MTX_TYPE_ underlying_type;
94 public:
95 
96  // constructors and destructor
97 
99  { SC_MTX_INIT_(m_mtx); }
100  virtual ~sc_host_mutex()
101  { SC_MTX_DESTROY_(m_mtx); }
102 
103 
104  // interface methods
105 
106  // blocks until mutex could be locked
107 
115  // 08/20/2015 GL: modified for the OoO simulation
116  virtual int lock( int )
117  { assert( 0 ); SC_MTX_LOCK_(m_mtx); return 0; }
118 
119  // returns -1 if mutex could not be locked
120  virtual int trylock()
121  { return SC_MTX_TRYLOCK_(m_mtx) ? 0 : -1; }
122 
123  // should return -1 if mutex was not locked by caller,
124  // but is not yet able to check this
125  virtual int unlock()
126  { SC_MTX_UNLOCK_(m_mtx); return 0; }
127 
128 private:
129  underlying_type m_mtx;
130 };
131 
132 } // namespace sc_core
133 
134 #undef SC_MTX_TYPE_
135 #undef SC_MTX_INIT_
136 #undef SC_MTX_DESTROY_
137 #undef SC_MTX_LOCK_
138 #undef SC_MTX_TRYLOCK_
139 #undef SC_MTX_UNLOCK_
140 #undef SC_PTHREAD_NULL_
141 
142 /*****************************************************************************
143 
144  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
145  changes you are making here.
146 
147  Name, Affiliation, Date:
148  Description of Modification:
149  *****************************************************************************/
150 //$Log: sc_host_mutex.h,v $
151 //Revision 1.4 2011/08/30 21:53:23 acg
152 // Jerome Cornet: include window.h for gnu c in windows case.
153 //
154 //Revision 1.3 2011/08/07 19:08:01 acg
155 // Andy Goodrich: moved logs to end of file so line number synching works
156 // better between versions.
157 //
158 //Revision 1.2 2011/06/25 17:08:38 acg
159 // Andy Goodrich: Jerome Cornet's changes to use libtool to build the
160 // library.
161 //
162 //Revision 1.1 2011/04/18 19:04:11 acg
163 // Philipp A. Hartmann: first check in of file.
164 //
165 
166 #endif
167 
168 // Taf!
#define SC_MTX_TRYLOCK_(Mutex)
Definition: sc_host_mutex.h:73
#define SC_MTX_TYPE_
Definition: sc_host_mutex.h:54
#define SC_MTX_INIT_(Mutex)
Definition: sc_host_mutex.h:62
#define SC_MTX_LOCK_(Mutex)
Definition: sc_host_mutex.h:64
#define SC_MTX_UNLOCK_(Mutex)
Definition: sc_host_mutex.h:66
The sc_mutex_if interface class.
Definition: sc_mutex_if.h:40
virtual int lock(int)
A new parameter segment ID is added for the out-of-order simulation.
The sc_host_mutex class, wrapping an OS mutex on the simulation host.
Definition: sc_host_mutex.h:91
#define SC_MTX_DESTROY_(Mutex)
Definition: sc_host_mutex.h:77