PCL
Mutex.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/Mutex.h - Released 2024-06-18T15:48:54Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2024 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_Mutex_h
53 #define __PCL_Mutex_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/Utility.h>
61 
62 #ifdef __PCL_UNIX
63 # include <pcl/Atomic.h>
64 # include <stdio.h>
65 # include <pthread.h>
66 #endif
67 
68 #ifdef __PCL_WINDOWS
69 # include <windows.h>
70 #endif
71 
72 namespace pcl
73 {
74 
75 // ----------------------------------------------------------------------------
76 
208 class PCL_CLASS Mutex
209 {
210 public:
211 
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 ) )
246  {
247 #ifdef __PCL_WINDOWS
248  (void)InitializeCriticalSectionAndSpinCount( &criticalSection, DWORD( m_spinCount ) );
249 #else
250  (void)PThreadInitMutex();
251 #endif
252  }
253 
260  virtual ~Mutex()
261  {
262 #ifdef __PCL_WINDOWS
263  DeleteCriticalSection( &criticalSection );
264 #else
265  (void)PThreadDestroyMutex();
266 #endif
267  }
268 
273  Mutex( const Mutex& ) = delete;
274 
279  Mutex& operator =( const Mutex& ) = delete;
280 
301  void Lock()
302  {
303 #ifdef __PCL_WINDOWS
304  EnterCriticalSection( &criticalSection );
305 #else
306  for ( int spin = m_spinCount;; )
307  {
308  // Is the mutex free? If so, get it now and don't look back!
309  if ( m_lockState.TestAndSet( 0, 1 ) )
310  {
311  (void)PThreadLockMutex();
312  break;
313  }
314 
315  if ( --spin < 0 )
316  {
317  // Either no spinning, or spinned to no avail.
318  // Block thread until we can get this mutex. This is expensive.
319  (void)PThreadLockMutex();
320  m_lockState.Store( 1 );
321  // If we are spinning, the spin count was too low. We can increase
322  // it to make the spinning mutex adaptive.
323  if ( m_spinCount > 0 )
324  if ( m_spinCount < m_spinCountMax )
325  m_spinCount += m_spinCountDelta;
326  break;
327  }
328  }
329 #endif
330  }
331 
337  void Unlock()
338  {
339 #ifdef __PCL_WINDOWS
340  LeaveCriticalSection( &criticalSection );
341 #else
342  m_lockState.Store( 0 );
343  (void)PThreadUnlockMutex();
344 #endif
345  }
346 
374  void operator ()( bool lock = true )
375  {
376  if ( lock )
377  Lock();
378  else
379  Unlock();
380  }
381 
389  bool TryLock()
390  {
391 #ifdef __PCL_WINDOWS
392  return TryEnterCriticalSection( &criticalSection ) != FALSE;
393 #else
394  // ### N.B.: This code is performance and stability critical. DO NOT
395  // modify it unless you are absolutely sure of what you are doing.
396  return m_lockState == 0 && m_lockState.TestAndSet( 0, 1 ) && PThreadLockMutex();
397 #endif
398  }
399 
408  int SpinCount() const
409  {
410  return m_spinCount;
411  }
412 
413 private:
414 
415 #ifdef __PCL_WINDOWS
416 
417  CRITICAL_SECTION criticalSection;
418 
419 #else // Linux/UNIX
420 
421  AtomicInt m_lockState; // 0=unlocked, 1=acquired
422  pthread_mutex_t m_mutex;
423 
424  bool PThreadInitMutex()
425  {
426  return PThreadCheckError( pthread_mutex_init( &m_mutex, 0 ), "pthread_mutex_init" );
427  }
428 
429  bool PThreadDestroyMutex()
430  {
431  return PThreadCheckError( pthread_mutex_destroy( &m_mutex ), "pthread_mutex_destroy" );
432  }
433 
434  bool PThreadLockMutex()
435  {
436  return PThreadCheckError( pthread_mutex_lock( &m_mutex ), "pthread_mutex_lock" );
437  }
438 
439  bool PThreadUnlockMutex()
440  {
441  return PThreadCheckError( pthread_mutex_unlock( &m_mutex ), "pthread_mutex_unlock" );
442  }
443 
444  static bool PThreadCheckError( int errorCode, const char* funcName )
445  {
446  if ( errorCode == 0 )
447  return true;
448  fprintf( stderr, "%s() failed. Error code: %d\n", funcName, errorCode );
449  return false;
450  }
451 
452 #endif // __PCL_WINDOWS
453 
454  int m_spinCount = 4000;
455  int m_spinCountDelta = 1000;
456  int m_spinCountMax = 12000;
457 };
458 
459 // ----------------------------------------------------------------------------
460 
461 } // pcl
462 
463 #endif // __PCL_Mutex_h
464 
465 // ----------------------------------------------------------------------------
466 // EOF pcl/Mutex.h - Released 2024-06-18T15:48:54Z
Atomic operations on integers.
Definition: Atomic.h:95
Adaptive mutual exclusion lock variable.
Definition: Mutex.h:209
void Unlock()
Definition: Mutex.h:337
int SpinCount() const
Definition: Mutex.h:408
Mutex(const Mutex &)=delete
Mutex(int spinCount=4000, int spinCountDelta=1000, int spinCountMax=12000)
Definition: Mutex.h:242
virtual ~Mutex()
Definition: Mutex.h:260
bool TryLock()
Definition: Mutex.h:389
void Lock()
Definition: Mutex.h:301
constexpr const T & Max(const T &a, const T &b) noexcept
Definition: Utility.h:119
PCL root namespace.
Definition: AbstractImage.h:77