mbgtools-lx  4.2.8
mbgmutex.h
Go to the documentation of this file.
1 
2 /**************************************************************************
3  *
4  * $Id: mbgmutex.h 1.5 2018/07/16 14:18:05 martin REL_M $
5  *
6  * Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany
7  *
8  * Description:
9  * Portable macros to deal with spinlocks, mutexes,
10  * and critical sections.
11  *
12  * -----------------------------------------------------------------------
13  * $Log: mbgmutex.h $
14  * Revision 1.5 2018/07/16 14:18:05 martin
15  * Support ID strings for spinlocks and mutexes.
16  * Revision 1.4 2017/07/05 10:05:02 martin
17  * windows.h is now included by mbg_tgt.h.
18  * Check for MBG_TGT_POSIX instead of MBG_TGT_UNIX.
19  * Revision 1.3 2013/04/11 13:46:58 martin
20  * Use non-specific spinlock function under Windows.
21  * Revision 1.2 2012/03/08 12:19:01Z martin
22  * Fixes for Linux kernel and FreeBSD.
23  * Fixed build under DOS and QNX usinc dummy defines.
24  * Don't define macros for semaphore destroy functions
25  * if not required/supported on the target OS.
26  * Revision 1.1 2011/04/15 12:26:59 martin
27  * Initial revision.
28  *
29  **************************************************************************/
30 
31 #ifndef _MBGMUTEX_H
32 #define _MBGMUTEX_H
33 
34 
35 /* Other headers to be included */
36 
37 #include <mbg_tgt.h>
38 #include <words.h>
39 
40 #ifdef _MBGMUTEX
41  #define _ext
42  #define _DO_INIT
43 #else
44  #define _ext extern
45 #endif
46 
47 
48 /* Start of header body */
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 #if defined( MBG_TGT_KERNEL ) // definitions used in kernel space
55 
56  #if defined( MBG_TGT_WIN32 ) // Windows kernel space
57 
58  typedef KSPIN_LOCK MBG_SPINLOCK;
59  #define _mbg_spin_lock_init( _spl, _n ) KeInitializeSpinLock( _spl )
60  // _mbg_spin_lock_destroy is not supported
61  #define _mbg_spin_lock_acquire( _spl ) KeAcquireSpinLock( _spl, &OldIrql )
62  #define _mbg_spin_lock_release( _spl ) KeReleaseSpinLock( _spl, OldIrql )
63 
64  #define _MBG_SPINLOCK_DEFINED 1
65 
66 
67  typedef FAST_MUTEX MBG_MUTEX;
68  #define _mbg_mutex_init( _pmtx, _n ) ExInitializeFastMutex( _pmtx )
69  // _mbg_mutex_destroy( _pmtx ) is not supported
70  #define _mbg_mutex_acquire( _pmtx ) ExAcquireFastMutex( _pmtx )
71  #define _mbg_mutex_release( _pmtx ) ExReleaseFastMutex( _pmtx )
72 
73  #define _MBG_MUTEX_DEFINED 1
74 
75  #elif defined( MBG_TGT_LINUX ) // Linux kernel space
76 
77  #include <linux/spinlock.h>
78  #include <linux/version.h>
79 
80  #if ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 6, 26 ) )
81  #include <linux/semaphore.h>
82  #else
83  #include <asm/semaphore.h>
84  #endif
85 
86  typedef spinlock_t MBG_SPINLOCK;
87  #define _mbg_spin_lock_init( _spl, _n ) spin_lock_init( _spl )
88  // _mbg_spin_lock_destroy is not supported
89  #define _mbg_spin_lock_acquire( _spl ) spin_lock( _spl )
90  #define _mbg_spin_lock_release( _spl ) spin_unlock( _spl )
91 
92  #define _MBG_SPINLOCK_DEFINED 1
93 
94 
95  typedef struct semaphore MBG_MUTEX;
96  #define _mbg_mutex_init( _pmtx, _n ) sema_init( _pmtx, 1 )
97  // _mbg_mutex_destroy( _pmtx ) is not supported
98  #define _mbg_mutex_acquire( _pmtx ) down_interruptible( _pmtx )
99  #define _mbg_mutex_release( _pmtx ) up( _pmtx )
100 
101  #define _MBG_MUTEX_DEFINED 1
102 
103  #elif defined( MBG_TGT_FREEBSD ) // FreeBSD kernel space
104 
105  #include <sys/lock.h>
106  #include <sys/mutex.h>
107 
108  typedef struct mtx MBG_SPINLOCK;
109  #define _mbg_spin_lock_init( _spl, _n ) mtx_init( _spl, _n, NULL, MTX_SPIN )
110  #define _mbg_spin_lock_destroy( _spl ) mtx_destroy( _spl )
111  #define _mbg_spin_lock_acquire( _spl ) mtx_lock_spin( _spl )
112  #define _mbg_spin_lock_release( _spl ) mtx_unlock_spin( _spl )
113 
114  #define _MBG_SPINLOCK_DEFINED 1
115 
116 
117  typedef struct mtx MBG_MUTEX;
118  #define _mbg_mutex_init( _pmtx, _n ) mtx_init( _pmtx, _n, NULL, MTX_DEF )
119  #define _mbg_mutex_destroy( _pmtx ) mtx_destroy( _pmtx )
120  #define _mbg_mutex_acquire( _pmtx ) mtx_lock( _pmtx )
121  #define _mbg_mutex_release( _pmtx ) mtx_unlock( _pmtx )
122 
123  #define _MBG_MUTEX_DEFINED 1
124 
125  #elif defined( MBG_TGT_NETBSD )
126 
127  #include <sys/mutex.h>
128 
129  // The API used below has been introduced in NetBSD 5.0
130  // For earlier NetBSD versions see 'man 9 lockinit'.
131 
132  typedef kmutex_t MBG_SPINLOCK;
133  #define _mbg_spin_lock_init( _spl, _n ) mutex_init( _spl, MUTEX_DEFAULT, IPL_HIGH )
134  #define _mbg_spin_lock_destroy( _spl ) mutex_destroy( _spl )
135  #define _mbg_spin_lock_acquire( _spl ) mutex_spin_enter( _spl )
136  #define _mbg_spin_lock_release( _spl ) mutex_spin_exit( _spl )
137 
138  #define _MBG_SPINLOCK_DEFINED 1
139 
140 
141  typedef kmutex_t MBG_MUTEX;
142  #define _mbg_mutex_init( _pmtx, _n ) mutex_init( _pmtx, MUTEX_DEFAULT, IPL_NONE )
143  #define _mbg_mutex_destroy( _spl ) mutex_destroy( _spl )
144  #define _mbg_mutex_acquire( _pmtx ) mutex_enter( _pmtx )
145  #define _mbg_mutex_release( _pmtx ) mutex_exit( _pmtx )
146 
147  #define _MBG_MUTEX_DEFINED 1
148 
149  #endif
150 
151 #else // user space applications
152 
153  #if defined( MBG_TGT_WIN32 ) // Windows user space
154 
155  // definitions used with mutexes
156  typedef HANDLE MBG_MUTEX;
157  #define _mbg_mutex_init( _pm, _n ) *(_pm) = CreateMutex( NULL, FALSE, NULL )
158  #define _mbg_mutex_destroy( _pm ) do { CloseHandle( *(_pm) ); *(_pm) = INVALID_HANDLE_VALUE; } while ( 0 )
159  #define _mbg_mutex_acquire( _pm ) WaitForSingleObject( *(_pm), INFINITE )
160  #define _mbg_mutex_release( _pm ) ReleaseMutex( *(_pm) )
161 
162  #define _MBG_MUTEX_DEFINED 1
163 
164  // definitions used with critical sections
165  typedef CRITICAL_SECTION MBG_CRIT_SECT;
166  #define _mbg_crit_sect_init( _pcs, _n ) InitializeCriticalSection( (_pcs) )
167  #define _mbg_crit_sect_destroy( _pcs ) DeleteCriticalSection( (_pcs) )
168  #define _mbg_crit_sect_enter( _pcs ) EnterCriticalSection( (_pcs) )
169  #define _mbg_crit_sect_leave( _pcs ) LeaveCriticalSection( (_pcs) )
170 
171  #define _MBG_CRIT_SECT_DEFINED 1
172 
173  #elif defined( MBG_TGT_POSIX ) // Unix user space use pthread library
174 
175  #include <pthread.h>
176 
177  // Mutex types:
178  // PTHREAD_MUTEX_INITIALIZER /* Fast */
179  // PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP /* Recursive */
180  // PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP /* Errorcheck */
181  typedef pthread_mutex_t MBG_MUTEX;
182  #define _mbg_mutex_init( _pm, _n ) pthread_mutex_init( (_pm), NULL )
183  #define _mbg_mutex_destroy( _pm ) pthread_mutex_destroy( (_pm) )
184  #define _mbg_mutex_acquire( _pm ) pthread_mutex_lock( (_pm) )
185  #define _mbg_mutex_release( _pm ) pthread_mutex_unlock( (_pm) )
186 
187  #define _MBG_MUTEX_DEFINED 1
188 
189  // For critical sections use defaults specified below
190 
191  #elif defined( MBG_TGT_DOS ) || defined( MBG_TGT_QNX )
192 
193  typedef int MBG_MUTEX; // just a dummy declaration
194 
195  #define _MBG_MUTEX_DEFINED 1
196 
197  #endif
198 
199 #endif
200 
201 
202 #if !defined( _MBG_SPINLOCK_DEFINED )
203 
204  #define _mbg_spin_lock_init( _spl, _n ) _nop_macro_fnc()
205  // _mbg_spin_lock_destroy is not supported
206  #define _mbg_spin_lock_acquire( _spl ) _nop_macro_fnc()
207  #define _mbg_spin_lock_release( _spl ) _nop_macro_fnc()
208 
209 #endif
210 
211 
212 #if !defined( _MBG_MUTEX_DEFINED )
213 
214  #define _MBG_MUTEX_DEFINED 1
215 
216  typedef MBG_CRIT_SECT MBG_MUTEX;
217 
218  #define _mbg_mutex_init( _pm, _n ) _nop_macro_fnc()
219  // _mbg_mutex_destroy( _pmtx ) is not supported
220  #define _mbg_mutex_acquire( _pm ) _nop_macro_fnc()
221  #define _mbg_mutex_release( _pm ) _nop_macro_fnc()
222 
223 #endif
224 
225 
226 #if !defined( _MBG_CRIT_SECT_DEFINED )
227 
228  // use mutex by default, e.g. with the pthread library
229 
230  #define _MBG_CRIT_SECT_DEFINED 1
231 
232  typedef MBG_MUTEX MBG_CRIT_SECT;
233  #define _mbg_crit_sect_init _mbg_mutex_init
234  #if defined( _mbg_mutex_destroy )
235  #define _mbg_crit_sect_destroy _mbg_mutex_destroy
236  #endif
237  #define _mbg_crit_sect_enter _mbg_mutex_acquire
238  #define _mbg_crit_sect_leave _mbg_mutex_release
239 
240 #endif
241 
242 
243 /* ----- function prototypes begin ----- */
244 
245 /* This section was generated automatically */
246 /* by MAKEHDR, do not remove the comments. */
247 
248 /* (no header definitions found) */
249 
250 /* ----- function prototypes end ----- */
251 
252 #ifdef __cplusplus
253 }
254 #endif
255 
256 /* End of header body */
257 
258 #undef _ext
259 #undef _DO_INIT
260 
261 #endif /* _MBGMUTEX_H */
pthread_mutex_t MBG_MUTEX
Definition: mbgmutex.h:181
MBG_MUTEX MBG_CRIT_SECT
Definition: mbgmutex.h:232