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