Ocean
SharedLock.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_BASE_SHARED_LOCK_H
9 #define META_OCEAN_BASE_SHARED_LOCK_H
10 
11 #include "ocean/base/Base.h"
12 #include "ocean/base/Lock.h"
13 
14 namespace Ocean
15 {
16 
17 /**
18  * This class implements a lock shared over individual processes (a system-wide lock).
19  * @see ScopedSharedLock, SharedMemory.
20  * @ingroup base
21  */
22 class OCEAN_BASE_EXPORT SharedLock
23 {
24  public:
25 
26  /**
27  * Creates an invalid shared lock object.
28  */
29  SharedLock() = default;
30 
31  /**
32  * Creates a new shared lock object by a system unique name of this lock.
33  * @param name System wide unique lock name
34  */
35  explicit SharedLock(const std::wstring& name);
36 
37  /**
38  * Disabled copy constructor.
39  * @param sharedLock Shared lock object to be copied
40  */
41  SharedLock(const SharedLock& sharedLock) = delete;
42 
43  /**
44  * Destructs a shared lock object.
45  */
47 
48  /**
49  * Returns the system wide unique name of this lock.
50  * @return Lock name
51  */
52  inline const std::wstring& name() const;
53 
54  /**
55  * Looks the mutex.
56  * Beware: This object must have a valid name before locking!<br>
57  * @see unlock(), tryLock(), name(), isValid().
58  */
59  void lock();
60 
61  /**
62  * Tries to lock the mutex.
63  * Beware: This object must have a valid name before locking!<br>
64  * @return True, if the mutex could be locked
65  * @see unlock(), lock(), name(), isValid().
66  */
67  bool tryLock();
68 
69  /**
70  * Unlocks the mutex.
71  * Beware: This object must have a valid name before locking!<br>
72  * @see lock(), name(), isVaid().
73  */
74  void unlock();
75 
76  /**
77  * Returns whether this object is valid and can be used correctly.
78  * @return True, if so
79  */
80  inline bool isValid() const;
81 
82  /**
83  * Returns whether this object is valid and can be used correctly.
84  * @return True, if so
85  */
86  explicit inline operator bool() const;
87 
88  /**
89  * Disabled assign operator.
90  * @param sharedLock Shared lock object to be assigned
91  * @return Reference to this object
92  */
93  SharedLock& operator=(const SharedLock& sharedLock) = delete;
94 
95  private:
96 
97 #if defined(__APPLE__) || (defined(__linux__) && !defined(_ANDROID))
98 
99  /**
100  * Creates the semaphore object of this lock.
101  * @return True, if succeeded
102  */
104 
105 #endif
106 
107  private:
108 
109  /// System wide unique name.
110  std::wstring name_;
111 
112  /// Local lock object.
114 
115  /// Local lock counter.
116  unsigned int localCounter_ = 0u;
117 
118  /// Handle of the mutex.
119  void* handle_ = nullptr;
120 };
121 
122 /**
123  * This class defines a scoped lock object for shared locks.
124  * @see SharedLock.
125  * @ingroup base
126  */
127 class OCEAN_BASE_EXPORT ScopedSharedLock
128 {
129  public:
130 
131  /**
132  * Creates a new shared scoped lock object.
133  * @param sharedLock Shared lock object to be used for locking
134  */
135  inline ScopedSharedLock(SharedLock& sharedLock);
136 
137  /**
138  * Releases a shared scoped lock object.
139  */
140  inline ~ScopedSharedLock();
141 
142  protected:
143 
144  /**
145  * Disabled copy constructor.
146  * @param scopedSharedLock The object which would be copied
147  */
148  ScopedSharedLock(const ScopedSharedLock& scopedSharedLock) = delete;
149 
150  /**
151  * Disabled copy operator.
152  * @param scopedSharedLock The object which would be copied
153  * @return Reference to this object
154  */
155  ScopedSharedLock& operator=(const ScopedSharedLock& scopedSharedLock) = delete;
156 
157  private:
158 
159  /// Shared lock object to be used for locking.
161 };
162 
163 inline const std::wstring& SharedLock::name() const
164 {
165  return name_;
166 }
167 
168 inline bool SharedLock::isValid() const
169 {
170  return !name_.empty();
171 }
172 
173 inline SharedLock::operator bool() const
174 {
175  return !name_.empty();
176 }
177 
179  scopedLock_(sharedLock)
180 {
181  scopedLock_.lock();
182 }
183 
185 {
187 }
188 
189 }
190 
191 #endif // META_OCEAN_BASE_LOCK_H
This class implements a recursive lock object.
Definition: Lock.h:31
This class defines a scoped lock object for shared locks.
Definition: SharedLock.h:128
~ScopedSharedLock()
Releases a shared scoped lock object.
Definition: SharedLock.h:184
ScopedSharedLock & operator=(const ScopedSharedLock &scopedSharedLock)=delete
Disabled copy operator.
SharedLock & scopedLock_
Shared lock object to be used for locking.
Definition: SharedLock.h:160
ScopedSharedLock(SharedLock &sharedLock)
Creates a new shared scoped lock object.
Definition: SharedLock.h:178
ScopedSharedLock(const ScopedSharedLock &scopedSharedLock)=delete
Disabled copy constructor.
This class implements a lock shared over individual processes (a system-wide lock).
Definition: SharedLock.h:23
SharedLock & operator=(const SharedLock &sharedLock)=delete
Disabled assign operator.
void lock()
Looks the mutex.
SharedLock(const std::wstring &name)
Creates a new shared lock object by a system unique name of this lock.
void unlock()
Unlocks the mutex.
SharedLock()=default
Creates an invalid shared lock object.
bool createSemaphore()
Creates the semaphore object of this lock.
std::wstring name_
System wide unique name.
Definition: SharedLock.h:110
~SharedLock()
Destructs a shared lock object.
bool tryLock()
Tries to lock the mutex.
Lock localLock_
Local lock object.
Definition: SharedLock.h:113
SharedLock(const SharedLock &sharedLock)=delete
Disabled copy constructor.
bool isValid() const
Returns whether this object is valid and can be used correctly.
Definition: SharedLock.h:168
const std::wstring & name() const
Returns the system wide unique name of this lock.
Definition: SharedLock.h:163
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15