54 #if defined( MBG_TGT_KERNEL ) // definitions used in kernel space 56 #if defined( MBG_TGT_WIN32 ) // Windows kernel space 58 typedef KSPIN_LOCK MBG_SPINLOCK;
59 #define _mbg_spin_lock_init( _spl, _n ) KeInitializeSpinLock( _spl ) 61 #define _mbg_spin_lock_acquire( _spl ) KeAcquireSpinLock( _spl, &OldIrql ) 62 #define _mbg_spin_lock_release( _spl ) KeReleaseSpinLock( _spl, OldIrql ) 64 #define _MBG_SPINLOCK_DEFINED 1 68 #define _mbg_mutex_init( _pmtx, _n ) ExInitializeFastMutex( _pmtx ) 70 #define _mbg_mutex_acquire( _pmtx ) ExAcquireFastMutex( _pmtx ) 71 #define _mbg_mutex_release( _pmtx ) ExReleaseFastMutex( _pmtx ) 73 #define _MBG_MUTEX_DEFINED 1 75 #elif defined( MBG_TGT_LINUX ) // Linux kernel space 77 #include <linux/spinlock.h> 78 #include <linux/version.h> 80 #if ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 6, 26 ) ) 81 #include <linux/semaphore.h> 83 #include <asm/semaphore.h> 86 typedef spinlock_t MBG_SPINLOCK;
87 #define _mbg_spin_lock_init( _spl, _n ) spin_lock_init( _spl ) 89 #define _mbg_spin_lock_acquire( _spl ) spin_lock( _spl ) 90 #define _mbg_spin_lock_release( _spl ) spin_unlock( _spl ) 92 #define _MBG_SPINLOCK_DEFINED 1 95 typedef struct semaphore MBG_MUTEX;
96 #define _mbg_mutex_init( _pmtx, _n ) sema_init( _pmtx, 1 ) 98 #define _mbg_mutex_acquire( _pmtx ) down_interruptible( _pmtx ) 99 #define _mbg_mutex_release( _pmtx ) up( _pmtx ) 101 #define _MBG_MUTEX_DEFINED 1 103 #elif defined( MBG_TGT_FREEBSD ) // FreeBSD kernel space 105 #include <sys/lock.h> 106 #include <sys/mutex.h> 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 ) 114 #define _MBG_SPINLOCK_DEFINED 1 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 ) 123 #define _MBG_MUTEX_DEFINED 1 125 #elif defined( MBG_TGT_NETBSD ) 127 #include <sys/mutex.h> 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 ) 138 #define _MBG_SPINLOCK_DEFINED 1 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 ) 147 #define _MBG_MUTEX_DEFINED 1 151 #else // user space applications 153 #if defined( MBG_TGT_WIN32 ) // Windows user space 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) ) 162 #define _MBG_MUTEX_DEFINED 1 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) ) 171 #define _MBG_CRIT_SECT_DEFINED 1 173 #elif defined( MBG_TGT_POSIX ) // Unix user space use pthread library 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) ) 187 #define _MBG_MUTEX_DEFINED 1 191 #elif defined( MBG_TGT_DOS ) || defined( MBG_TGT_QNX ) 195 #define _MBG_MUTEX_DEFINED 1 202 #if !defined( _MBG_SPINLOCK_DEFINED ) 204 #define _mbg_spin_lock_init( _spl, _n ) _nop_macro_fnc() 206 #define _mbg_spin_lock_acquire( _spl ) _nop_macro_fnc() 207 #define _mbg_spin_lock_release( _spl ) _nop_macro_fnc() 212 #if !defined( _MBG_MUTEX_DEFINED ) 214 #define _MBG_MUTEX_DEFINED 1 218 #define _mbg_mutex_init( _pm, _n ) _nop_macro_fnc() 220 #define _mbg_mutex_acquire( _pm ) _nop_macro_fnc() 221 #define _mbg_mutex_release( _pm ) _nop_macro_fnc() 226 #if !defined( _MBG_CRIT_SECT_DEFINED ) 230 #define _MBG_CRIT_SECT_DEFINED 1 233 #define _mbg_crit_sect_init _mbg_mutex_init 234 #if defined( _mbg_mutex_destroy ) 235 #define _mbg_crit_sect_destroy _mbg_mutex_destroy 237 #define _mbg_crit_sect_enter _mbg_mutex_acquire 238 #define _mbg_crit_sect_leave _mbg_mutex_release
pthread_mutex_t MBG_MUTEX