PCL
AutoLock.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/AutoLock.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_AutoLock_h
53 #define __PCL_AutoLock_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/Atomic.h>
61 #include <pcl/Mutex.h>
62 
63 namespace pcl
64 {
65 
66 // ----------------------------------------------------------------------------
67 
83 class PCL_CLASS AutoLock
84 {
85 public:
86 
97  explicit AutoLock( pcl::Mutex& mutex )
98  : m_mutex( &mutex )
99  {
100  Lock();
101  }
102 
107  : m_mutex( x.m_mutex )
108  , m_lock( x.m_lock )
109  {
110  x.m_mutex = nullptr;
111  }
112 
120  {
121  Unlock();
122  m_mutex = nullptr;
123  }
124 
129  AutoLock( const AutoLock& ) = delete;
130 
135  AutoLock& operator =( const AutoLock& ) = delete;
136 
141  AutoLock& operator =( AutoLock&& ) = delete;
142 
147  void Lock()
148  {
149  if ( m_mutex != nullptr )
150  if ( m_lock.TestAndSet( 0, 1 ) )
151  m_mutex->Lock();
152  }
153 
158  void Unlock()
159  {
160  if ( m_mutex != nullptr )
161  if ( m_lock.TestAndSet( 1, 0 ) )
162  m_mutex->Unlock();
163  }
164 
165 private:
166 
167  pcl::Mutex* m_mutex = nullptr;
168  AtomicInt m_lock;
169 };
170 
171 // ----------------------------------------------------------------------------
172 
207 class PCL_CLASS AutoLockCounter
208 {
209 public:
210 
243  explicit AutoLockCounter( pcl::Mutex& mutex, AtomicInt& count, int limit )
244  : m_mutex( &mutex )
245  , m_count( &count )
246  , m_lock( 0 )
247  {
248  if ( m_count->FetchAndAdd( 1 ) >= limit-1 )
249  {
250  Lock();
251  if ( m_count->Load() < limit )
252  Unlock();
253  }
254  }
255 
260  : m_mutex( x.m_mutex )
261  , m_count( x.m_count )
262  , m_lock( x.m_lock )
263  {
264  x.m_mutex = nullptr;
265  x.m_count = nullptr;
266  }
267 
283  {
284  Unlock();
285  m_mutex = nullptr;
286  if ( m_count != nullptr )
287  {
288  m_count->Decrement();
289  m_count = nullptr;
290  }
291  }
292 
297  AutoLockCounter( const AutoLockCounter& ) = delete;
298 
303  AutoLockCounter& operator =( const AutoLockCounter& ) = delete;
304 
309  AutoLockCounter& operator =( AutoLockCounter&& ) = delete;
310 
315  void Lock()
316  {
317  if ( m_mutex != nullptr )
318  if ( m_lock.TestAndSet( 0, 1 ) )
319  m_mutex->Lock();
320  }
321 
326  void Unlock()
327  {
328  if ( m_mutex != nullptr )
329  if ( m_lock.TestAndSet( 1, 0 ) )
330  m_mutex->Unlock();
331  }
332 
333 private:
334 
335  pcl::Mutex* m_mutex = nullptr;
336  AtomicInt* m_count = nullptr;
337  AtomicInt m_lock;
338 };
339 
340 // ----------------------------------------------------------------------------
341 
367 #define Synchronized( mutex, code ) \
368 { \
369  pcl::AutoLock _________( mutex ); \
370  code \
371 }
372 
373 // ----------------------------------------------------------------------------
374 
375 } // pcl
376 
377 #endif // __PCL_AutoLock_h
378 
379 // ----------------------------------------------------------------------------
380 // EOF pcl/AutoLock.h - Released 2024-06-18T15:48:54Z
Atomic operations on integers.
Definition: Atomic.h:95
Automatic mutex lock/unlock with limited concurrent access allowance.
Definition: AutoLock.h:208
AutoLockCounter(pcl::Mutex &mutex, AtomicInt &count, int limit)
Definition: AutoLock.h:243
AutoLockCounter(AutoLockCounter &&x)
Definition: AutoLock.h:259
AutoLockCounter(const AutoLockCounter &)=delete
Automatic mutex lock/unlock.
Definition: AutoLock.h:84
AutoLock(AutoLock &&x)
Definition: AutoLock.h:106
AutoLock(pcl::Mutex &mutex)
Definition: AutoLock.h:97
void Unlock()
Definition: AutoLock.h:158
AutoLock(const AutoLock &)=delete
void Lock()
Definition: AutoLock.h:147
Adaptive mutual exclusion lock variable.
Definition: Mutex.h:209
PCL root namespace.
Definition: AbstractImage.h:77