58 #include <pcl/Diagnostics.h> 223 : m_spinCount(
Max( 0, spin ) )
226 (void)InitializeCriticalSectionAndSpinCount( &criticalSection, DWORD( m_spinCount ) );
228 (void)PThreadInitMutex();
242 DeleteCriticalSection( &criticalSection );
244 (void)PThreadDestroyMutex();
282 EnterCriticalSection( &criticalSection );
284 for (
int spin = m_spinCount; ; )
287 if ( m_lockState.TestAndSet( 0, 1 ) )
289 (void)PThreadLockMutex();
297 (void)PThreadLockMutex();
298 m_lockState.Store( 1 );
313 LeaveCriticalSection( &criticalSection );
315 m_lockState.Store( 0 );
316 (void)PThreadUnlockMutex();
347 void operator ()(
bool lock =
true )
365 return TryEnterCriticalSection( &criticalSection ) != FALSE;
369 return m_lockState == 0 && m_lockState.TestAndSet( 0, 1 ) && PThreadLockMutex();
389 CRITICAL_SECTION criticalSection;
394 pthread_mutex_t m_mutex;
396 bool PThreadInitMutex()
398 return PThreadCheckError( pthread_mutex_init( &m_mutex, 0 ),
"pthread_mutex_init" );
401 bool PThreadDestroyMutex()
403 return PThreadCheckError( pthread_mutex_destroy( &m_mutex ),
"pthread_mutex_destroy" );
406 bool PThreadLockMutex()
408 return PThreadCheckError( pthread_mutex_lock( &m_mutex ),
"pthread_mutex_lock" );
411 bool PThreadUnlockMutex()
413 return PThreadCheckError( pthread_mutex_unlock( &m_mutex ),
"pthread_mutex_unlock" );
416 static bool PThreadCheckError(
int errorCode,
const char* funcName )
418 if ( errorCode == 0 )
420 fprintf( stderr,
"%s() failed. Error code: %d\n", funcName, errorCode );
424 #endif // __PCL_WINDOWS 426 int m_spinCount = 512;
433 #endif // __PCL_Mutex_h
Adaptive mutual exclusion lock variable.
Atomic operations on integers.
constexpr const T & Max(const T &a, const T &b) noexcept