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 // ----------------------------------------------------------------------------
85 // CLASS : sc_host_mutex
86 //
87 // The sc_host_mutex class, wrapping an OS mutex on the simulation host
88 // ----------------------------------------------------------------------------
89 
90 class sc_host_mutex : public sc_mutex_if
91 {
92  typedef SC_MTX_TYPE_ underlying_type;
93 public:
94 
95  // constructors and destructor
96 
98  { SC_MTX_INIT_(m_mtx); }
99  virtual ~sc_host_mutex()
100  { SC_MTX_DESTROY_(m_mtx); }
101 
102 
103  // interface methods
104 
105  // blocks until mutex could be locked
106  virtual int lock()
107  { SC_MTX_LOCK_(m_mtx); return 0; }
108 
109  // returns -1 if mutex could not be locked
110  virtual int trylock()
111  { return SC_MTX_TRYLOCK_(m_mtx) ? 0 : -1; }
112 
113  // should return -1 if mutex was not locked by caller,
114  // but is not yet able to check this
115  virtual int unlock()
116  { SC_MTX_UNLOCK_(m_mtx); return 0; }
117 
118 private:
119  underlying_type m_mtx;
120 };
121 
122 } // namespace sc_core
123 
124 #undef SC_MTX_TYPE_
125 #undef SC_MTX_INIT_
126 #undef SC_MTX_DESTROY_
127 #undef SC_MTX_LOCK_
128 #undef SC_MTX_TRYLOCK_
129 #undef SC_MTX_UNLOCK_
130 #undef SC_PTHREAD_NULL_
131 
132 /*****************************************************************************
133 
134  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
135  changes you are making here.
136 
137  Name, Affiliation, Date:
138  Description of Modification:
139  *****************************************************************************/
140 //$Log: sc_host_mutex.h,v $
141 //Revision 1.4 2011/08/30 21:53:23 acg
142 // Jerome Cornet: include window.h for gnu c in windows case.
143 //
144 //Revision 1.3 2011/08/07 19:08:01 acg
145 // Andy Goodrich: moved logs to end of file so line number synching works
146 // better between versions.
147 //
148 //Revision 1.2 2011/06/25 17:08:38 acg
149 // Andy Goodrich: Jerome Cornet's changes to use libtool to build the
150 // library.
151 //
152 //Revision 1.1 2011/04/18 19:04:11 acg
153 // Philipp A. Hartmann: first check in of file.
154 //
155 
156 #endif
157 
158 // Taf!
#define SC_MTX_DESTROY_(Mutex)
Definition: sc_host_mutex.h:77
#define SC_MTX_TYPE_
Definition: sc_host_mutex.h:54
#define SC_MTX_INIT_(Mutex)
Definition: sc_host_mutex.h:62
#define SC_MTX_UNLOCK_(Mutex)
Definition: sc_host_mutex.h:66
#define SC_MTX_LOCK_(Mutex)
Definition: sc_host_mutex.h:64
#define SC_MTX_TRYLOCK_(Mutex)
Definition: sc_host_mutex.h:73