58 #include <pcl/Diagnostics.h>
242 Mutex(
int spinCount = 4000,
int spinCountDelta = 1000,
int spinCountMax = 12000 )
243 : m_spinCount(
Max( 0, spinCount ) )
244 , m_spinCountDelta(
Max( 0, spinCountDelta ) )
245 , m_spinCountMax(
Max( 0, spinCountMax ) )
248 (void)InitializeCriticalSectionAndSpinCount( &criticalSection, DWORD( m_spinCount ) );
250 (void)PThreadInitMutex();
263 DeleteCriticalSection( &criticalSection );
265 (void)PThreadDestroyMutex();
304 EnterCriticalSection( &criticalSection );
306 for (
int spin = m_spinCount;; )
309 if ( m_lockState.TestAndSet( 0, 1 ) )
311 (void)PThreadLockMutex();
319 (void)PThreadLockMutex();
320 m_lockState.Store( 1 );
323 if ( m_spinCount > 0 )
324 if ( m_spinCount < m_spinCountMax )
325 m_spinCount += m_spinCountDelta;
340 LeaveCriticalSection( &criticalSection );
342 m_lockState.Store( 0 );
343 (void)PThreadUnlockMutex();
374 void operator ()(
bool lock =
true )
392 return TryEnterCriticalSection( &criticalSection ) != FALSE;
396 return m_lockState == 0 && m_lockState.TestAndSet( 0, 1 ) && PThreadLockMutex();
417 CRITICAL_SECTION criticalSection;
422 pthread_mutex_t m_mutex;
424 bool PThreadInitMutex()
426 return PThreadCheckError( pthread_mutex_init( &m_mutex, 0 ),
"pthread_mutex_init" );
429 bool PThreadDestroyMutex()
431 return PThreadCheckError( pthread_mutex_destroy( &m_mutex ),
"pthread_mutex_destroy" );
434 bool PThreadLockMutex()
436 return PThreadCheckError( pthread_mutex_lock( &m_mutex ),
"pthread_mutex_lock" );
439 bool PThreadUnlockMutex()
441 return PThreadCheckError( pthread_mutex_unlock( &m_mutex ),
"pthread_mutex_unlock" );
444 static bool PThreadCheckError(
int errorCode,
const char* funcName )
446 if ( errorCode == 0 )
448 fprintf( stderr,
"%s() failed. Error code: %d\n", funcName, errorCode );
454 int m_spinCount = 4000;
455 int m_spinCountDelta = 1000;
456 int m_spinCountMax = 12000;
Atomic operations on integers.
Adaptive mutual exclusion lock variable.
Mutex(const Mutex &)=delete
Mutex(int spinCount=4000, int spinCountDelta=1000, int spinCountMax=12000)
constexpr const T & Max(const T &a, const T &b) noexcept