opennurbs_lock.h
1 /* $NoKeywords: $ */
2 /*
3 //
4 // Copyright (c) 1993-2013 Robert McNeel & Associates. All rights reserved.
5 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
6 // McNeel & Associates.
7 //
8 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
9 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
10 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
11 //
12 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
13 //
14 ////////////////////////////////////////////////////////////////
15 */
16 
17 #if !defined(OPENNURBS_LOCK_INC_)
18 #define OPENNURBS_LOCK_INC_
19 
20 /*
21 Description:
22  ON_Lock is a thread safe lock semephore. It is implemented using
23  platform specfic compare and set functions.
24 */
25 class ON_CLASS ON_Lock
26 {
27 public:
28 #if defined(ON_COMPILER_CLANG)
29  ON_Lock() ON_NOEXCEPT;
30 #else
31  ON_Lock() = default;
32 #endif
33  ~ON_Lock() = default;
34  ON_Lock(const ON_Lock&) = default;
35  ON_Lock& operator=(const ON_Lock&) = default;
36 
37  // ON_Lock::InvalidLockValue (= -1) may never be used as a lock value.
38  enum : int
39  {
40  UnlockedValue = 0,
41  DefaultLockedValue = 1,
42  InvalidLockValue = -1
43  };
44 
45  /*
46  Returns:
47  Current lock value
48  ON_Lock::UnlockedValue indicates the the resource protected by the lock is available.
49  */
50  int IsLocked();
51 
52  /*
53  Description:
54  Calls GetLock(ON_Lock::DefaultLockedValue);
55  Returns:
56  True if the lock state was unlocked
57  and the current lock value was changed from ON_Lock::UnlockedValue to ON_Lock::DefaultLockedValue.
58  False otherwise.
59  */
60  bool GetDefaultLock();
61 
62  /*
63  Description:
64  Calls ReturnLock(ON_Lock::DefaultLockedValue);
65  Returns:
66  True if the lock state was locked with a locak value = ON_Lock::DefaultLockedValue
67  and the current lock value was changed from ON_Lock::DefaultLockedValue to ON_Lock::UnlockedValue.
68  False otherwise.
69  */
70  bool ReturnDefaultLock();
71 
72  /*
73  Parameters:
74  lock_value - [in]
75  any value except ON_Lock::UnlockedValue or ON_Lock::InvalidLockValue.
76  Typically ON_Lock::DefaultLockedValue is used.
77  Returns:
78  True if the lock_value parameter was valid and the current
79  lock value was changed from ON_Lock::UnlockedValue to lock_value.
80  False otherwise.
81  */
82  bool GetLock(int lock_value);
83 
84  /*
85  Parameters:
86  lock_value - [in]
87  any value except ON_Lock::UnlockedValue or ON_Lock::InvalidLockValue.
88  Typically this is the value that was passed to GetLock().
89  Returns:
90  True if the lock_value parameter was valid and the current
91  lock value was changed from that value to zero.
92  False otherwise.
93  */
94  bool ReturnLock(int lock_value);
95 
96  /*
97  Description:
98  Unconditionally sets the lock value to ON_Lock::UnlockedValue.
99  Returns:
100  previous value of the lock.
101  ON_Lock::UnlockedValue indicates the lock was available
102  otherwise the lock passed to GetLock() is returned
103  */
104  int BreakLock();
105 
106 private:
107  // It is important that sizeof(ON_Lock) = sizeof(int)
108  // and that m_lock_value be an int.
109 #pragma ON_PRAGMA_WARNING_PUSH
110 #pragma ON_PRAGMA_WARNING_DISABLE_MSC( 4251 )
111  // C4251: 'ON_Lock::m_lock_value': struct 'std::atomic<int>'
112  // needs to have dll-interface to be used by clients of class 'ON_Lock'
113  // m_lock_value is private and all code that manages m_lock_value is explicitly implemented in the DLL.
114 private:
115 #if defined(ON_COMPILER_CLANG)
116  std::atomic<int> m_lock_value;
117 #else
118  std::atomic<int> m_lock_value = ON_Lock::UnlockedValue;
119 #endif
120 #pragma ON_PRAGMA_WARNING_POP
121 };
122 
123 #endif
Definition: opennurbs_lock.h:39
ON_Lock is a thread safe lock semephore. It is implemented using platform specfic compare and set fun...
Definition: opennurbs_lock.h:24