PMDK C++ bindings  1.11.1
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-2020, 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 
30 class shared_mutex {
31 public:
33  typedef PMEMrwlock *native_handle_type;
34 
42  {
43  PMEMobjpool *pop;
44  if ((pop = pmemobj_pool_by_ptr(&plock)) == nullptr)
45  throw pmem::lock_error(
46  1, std::generic_category(),
47  "Persistent shared mutex not from persistent memory.");
48 
49  pmemobj_rwlock_zero(pop, &plock);
50  }
51 
55  ~shared_mutex() = default;
56 
69  void
70  lock()
71  {
72  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
73  if (int ret = pmemobj_rwlock_wrlock(pop, &this->plock))
74  throw pmem::lock_error(ret, std::system_category(),
75  "Failed to lock a shared mutex.")
76  .with_pmemobj_errormsg();
77  }
78 
94  void
96  {
97  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
98  if (int ret = pmemobj_rwlock_rdlock(pop, &this->plock))
99  throw pmem::lock_error(
100  ret, std::system_category(),
101  "Failed to shared lock a shared mutex.");
102  }
103 
118  bool
120  {
121  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
122  int ret = pmemobj_rwlock_trywrlock(pop, &this->plock);
123 
124  if (ret == 0)
125  return true;
126  else if (ret == EBUSY)
127  return false;
128  else
129  throw pmem::lock_error(ret, std::system_category(),
130  "Failed to lock a shared mutex.")
131  .with_pmemobj_errormsg();
132  }
133 
150  bool
152  {
153  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
154  int ret = pmemobj_rwlock_tryrdlock(pop, &this->plock);
155 
156  if (ret == 0)
157  return true;
158  else if (ret == EBUSY)
159  return false;
160  else
161  throw pmem::lock_error(ret, std::system_category(),
162  "Failed to lock a shared mutex.")
163  .with_pmemobj_errormsg();
164  }
165 
172  void
174  {
175  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
176  int ret = pmemobj_rwlock_unlock(pop, &this->plock);
177  if (ret)
178  throw pmem::lock_error(
179  ret, std::system_category(),
180  "Failed to unlock a shared mutex.")
181  .with_pmemobj_errormsg();
182  }
183 
190  void
192  {
193  this->unlock();
194  }
195 
201  native_handle_type
202  native_handle() noexcept
203  {
204  return &this->plock;
205  }
206 
212  enum pobj_tx_param
213  lock_type() const noexcept
214  {
215  return TX_PARAM_RWLOCK;
216  }
217 
221  shared_mutex &operator=(const shared_mutex &) = delete;
222 
226  shared_mutex(const shared_mutex &) = delete;
227 
228 private:
230  PMEMrwlock plock;
231 };
232 
233 } /* namespace obj */
234 
235 } /* namespace pmem */
236 
237 #endif /* LIBPMEMOBJ_CPP_SHARED_MUTEX_HPP */
pmem
Persistent memory namespace.
Definition: allocation_flag.hpp:15
pmem::obj::shared_mutex::shared_mutex
shared_mutex()
Default constructor.
Definition: shared_mutex.hpp:41
pmem::obj::shared_mutex::lock
void lock()
Lock the mutex for exclusive access.
Definition: shared_mutex.hpp:70
pmem::obj::shared_mutex::operator=
shared_mutex & operator=(const shared_mutex &)=delete
Deleted assignment operator.
pmem::obj::shared_mutex::native_handle
native_handle_type native_handle() noexcept
Access a native handle to this shared mutex.
Definition: shared_mutex.hpp:202
pmem::obj::shared_mutex::try_lock
bool try_lock()
Try to lock the mutex for exclusive access, returns regardless if the lock succeeds.
Definition: shared_mutex.hpp:119
pmem::obj::shared_mutex::shared_mutex
shared_mutex(const shared_mutex &)=delete
Deleted copy constructor.
pmem::obj::shared_mutex::try_lock_shared
bool try_lock_shared()
Try to lock the mutex for shared access, returns regardless if the lock succeeds.
Definition: shared_mutex.hpp:151
pmem::obj::shared_mutex::unlock
void unlock()
Unlocks the mutex.
Definition: shared_mutex.hpp:173
pmem::lock_error
Custom lock error class.
Definition: pexceptions.hpp:84
pmem::obj::shared_mutex::lock_type
enum pobj_tx_param lock_type() const noexcept
The type of lock needed for the transaction API.
Definition: shared_mutex.hpp:213
pmem::obj::shared_mutex::plock
PMEMrwlock plock
A POSIX style PMEM-resident shared_mutex.
Definition: shared_mutex.hpp:230
pmem::obj::shared_mutex::lock_shared
void lock_shared()
Lock the mutex for shared access.
Definition: shared_mutex.hpp:95
pmem::obj::shared_mutex::~shared_mutex
~shared_mutex()=default
Defaulted destructor.
pmem::obj::shared_mutex::native_handle_type
PMEMrwlock * native_handle_type
Implementation defined handle to the native type.
Definition: shared_mutex.hpp:33
pmem::obj::shared_mutex
Persistent memory resident shared_mutex implementation.
Definition: shared_mutex.hpp:30
pmem::obj::shared_mutex::unlock_shared
void unlock_shared()
Unlocks the mutex.
Definition: shared_mutex.hpp:191