PCL
AutoViewLock.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.6.5
6 // ----------------------------------------------------------------------------
7 // pcl/AutoViewLock.h - Released 2024-01-13T15:47:58Z
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_AutoViewLock_h
53 #define __PCL_AutoViewLock_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/Atomic.h>
61 #include <pcl/View.h>
62 
63 namespace pcl
64 {
65 
66 // ----------------------------------------------------------------------------
67 
152 class PCL_CLASS AutoViewLock
153 {
154 public:
155 
187  explicit AutoViewLock( View& view, bool lock = true )
188  : m_view( view )
189  , m_readLock( 0 )
190  , m_writeLock( 0 )
191  {
192  if ( lock )
193  Lock();
194  }
195 
203  {
204  Unlock();
205  m_view = View::Null();
206  }
207 
212  AutoViewLock( const AutoViewLock& ) = delete;
213 
218  AutoViewLock& operator =( const AutoViewLock& ) = delete;
219 
226  void Lock()
227  {
228  if ( !m_view.IsNull() )
229  {
230  int unlocked = 0;
231  if ( m_readLock.TestAndSet( 0, 1 ) )
232  ++unlocked;
233  if ( m_writeLock.TestAndSet( 0, 1 ) )
234  ++unlocked;
235  if ( unlocked )
236  m_view.Lock();
237  }
238  }
239 
246  void Unlock()
247  {
248  if ( !m_view.IsNull() )
249  {
250  int locked = 0;
251  if ( m_readLock.TestAndSet( 1, 0 ) )
252  ++locked;
253  if ( m_writeLock.TestAndSet( 1, 0 ) )
254  ++locked;
255  if ( locked )
256  m_view.Unlock();
257  }
258  }
259 
267  {
268  if ( !m_view.IsNull() )
269  if ( m_writeLock.TestAndSet( 0, 1 ) )
270  m_view.LockForWrite();
271  }
272 
280  {
281  if ( !m_view.IsNull() )
282  if ( m_readLock.TestAndSet( 1, 0 ) )
283  m_view.UnlockForRead();
284  }
285 
293  {
294  if ( !m_view.IsNull() )
295  if ( m_readLock.TestAndSet( 0, 1 ) )
296  m_view.RelockForRead();
297  }
298 
299 private:
300 
301  View m_view;
302  AtomicInt m_readLock;
303  AtomicInt m_writeLock;
304 };
305 
316 class PCL_CLASS AutoViewWriteLock : public AutoViewLock
317 {
318 public:
319 
331  explicit AutoViewWriteLock( View& view )
332  : AutoViewLock( view, false/*lock*/ )
333  {
334  LockForWrite();
335  }
336 };
337 
338 // ----------------------------------------------------------------------------
339 
340 } // pcl
341 
342 #endif // __PCL_AutoViewLock_h
343 
344 // ----------------------------------------------------------------------------
345 // EOF pcl/AutoViewLock.h - Released 2024-01-13T15:47:58Z
pcl
PCL root namespace.
Definition: AbstractImage.h:76
Atomic.h
pcl::AutoViewLock::AutoViewLock
AutoViewLock(View &view, bool lock=true)
Definition: AutoViewLock.h:187
pcl::AutoViewLock::Unlock
void Unlock()
Definition: AutoViewLock.h:246
pcl::View
High-level interface to a PixInsight view object.
Definition: View.h:212
pcl::AutoViewLock::Lock
void Lock()
Definition: AutoViewLock.h:226
pcl::AutoViewWriteLock::AutoViewWriteLock
AutoViewWriteLock(View &view)
Definition: AutoViewLock.h:331
pcl::AutoViewWriteLock
Automatic write-only view lock/unlock.
Definition: AutoViewLock.h:316
pcl::AtomicInt
Atomic operations on integers.
Definition: Atomic.h:94
pcl::AutoViewLock
Automatic view lock/unlock.
Definition: AutoViewLock.h:152
pcl::AutoViewLock::~AutoViewLock
~AutoViewLock()
Definition: AutoViewLock.h:202
pcl::AutoViewLock::RelockForRead
void RelockForRead()
Definition: AutoViewLock.h:292
pcl::AutoViewLock::LockForWrite
void LockForWrite()
Definition: AutoViewLock.h:266
pcl::AutoViewLock::UnlockForRead
void UnlockForRead()
Definition: AutoViewLock.h:279
View.h
Defs.h
pcl::View::Null
static View & Null()