PMDK C++ bindings  1.13.0-git107.g7e59f08f
This is the C++ bindings documentation for PMDK's libpmemobj.
shared_mutex.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: BSD-3-Clause
2 /* Copyright 2016-2021, Intel Corporation */
3 
9 #ifndef LIBPMEMOBJ_CPP_SHARED_MUTEX_HPP
10 #define LIBPMEMOBJ_CPP_SHARED_MUTEX_HPP
11 
12 #include <libpmemobj/thread.h>
13 #include <libpmemobj/tx_base.h>
14 
15 namespace pmem
16 {
17 
18 namespace obj
19 {
20 
32 class shared_mutex {
33 public:
35  typedef PMEMrwlock *native_handle_type;
36 
44  {
45  PMEMobjpool *pop;
46  if ((pop = pmemobj_pool_by_ptr(&plock)) == nullptr)
47  throw pmem::lock_error(
48  1, std::generic_category(),
49  "Persistent shared mutex not from persistent memory.");
50 
51  pmemobj_rwlock_zero(pop, &plock);
52  }
53 
57  ~shared_mutex() = default;
58 
71  void
72  lock()
73  {
74  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
75  if (int ret = pmemobj_rwlock_wrlock(pop, &this->plock))
76  throw detail::exception_with_errormsg<lock_error>(
77  ret, std::system_category(),
78  "Failed to lock a shared mutex.");
79  }
80 
96  void
98  {
99  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
100  if (int ret = pmemobj_rwlock_rdlock(pop, &this->plock))
101  throw detail::exception_with_errormsg<lock_error>(
102  ret, std::system_category(),
103  "Failed to shared lock a shared mutex.");
104  }
105 
120  bool
122  {
123  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
124  int ret = pmemobj_rwlock_trywrlock(pop, &this->plock);
125 
126  if (ret == 0)
127  return true;
128  else if (ret == EBUSY)
129  return false;
130  else
131  throw detail::exception_with_errormsg<lock_error>(
132  ret, std::system_category(),
133  "Failed to lock a shared mutex.");
134  }
135 
152  bool
154  {
155  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
156  int ret = pmemobj_rwlock_tryrdlock(pop, &this->plock);
157 
158  if (ret == 0)
159  return true;
160  else if (ret == EBUSY)
161  return false;
162  else
163  throw detail::exception_with_errormsg<lock_error>(
164  ret, std::system_category(),
165  "Failed to lock a shared mutex.");
166  }
167 
174  void
176  {
177  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
178  int ret = pmemobj_rwlock_unlock(pop, &this->plock);
179  if (ret)
180  throw detail::exception_with_errormsg<lock_error>(
181  ret, std::system_category(),
182  "Failed to unlock a shared mutex.");
183  }
184 
191  void
193  {
194  this->unlock();
195  }
196 
202  native_handle_type
203  native_handle() noexcept
204  {
205  return &this->plock;
206  }
207 
213  enum pobj_tx_param
214  lock_type() const noexcept
215  {
216  return TX_PARAM_RWLOCK;
217  }
218 
222  shared_mutex &operator=(const shared_mutex &) = delete;
223 
227  shared_mutex(const shared_mutex &) = delete;
228 
229 private:
231  PMEMrwlock plock;
232 };
233 
234 } /* namespace obj */
235 
236 } /* namespace pmem */
237 
238 #endif /* LIBPMEMOBJ_CPP_SHARED_MUTEX_HPP */
Custom lock error class.
Definition: pexceptions.hpp:121
Persistent memory resident shared_mutex implementation.
Definition: shared_mutex.hpp:32
void lock_shared()
Lock the mutex for shared access.
Definition: shared_mutex.hpp:97
shared_mutex()
Default constructor.
Definition: shared_mutex.hpp:43
void unlock_shared()
Unlocks the mutex.
Definition: shared_mutex.hpp:192
native_handle_type native_handle() noexcept
Access a native handle to this shared mutex.
Definition: shared_mutex.hpp:203
shared_mutex & operator=(const shared_mutex &)=delete
Deleted assignment operator.
shared_mutex(const shared_mutex &)=delete
Deleted copy constructor.
~shared_mutex()=default
Defaulted destructor.
void unlock()
Unlocks the mutex.
Definition: shared_mutex.hpp:175
enum pobj_tx_param lock_type() const noexcept
The type of lock needed for the transaction API.
Definition: shared_mutex.hpp:214
PMEMrwlock * native_handle_type
Implementation defined handle to the native type.
Definition: shared_mutex.hpp:35
void lock()
Lock the mutex for exclusive access.
Definition: shared_mutex.hpp:72
bool try_lock()
Try to lock the mutex for exclusive access, returns regardless if the lock succeeds.
Definition: shared_mutex.hpp:121
bool try_lock_shared()
Try to lock the mutex for shared access, returns regardless if the lock succeeds.
Definition: shared_mutex.hpp:153
Persistent memory namespace.
Definition: allocation_flag.hpp:15