PMDK C++ bindings  1.10.1
This is the C++ bindings documentation for PMDK's libpmemobj.
mutex.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: BSD-3-Clause
2 /* Copyright 2016-2019, Intel Corporation */
3 
9 #ifndef LIBPMEMOBJ_CPP_MUTEX_HPP
10 #define LIBPMEMOBJ_CPP_MUTEX_HPP
11 
13 #include <libpmemobj/thread.h>
14 #include <libpmemobj/tx_base.h>
15 
16 namespace pmem
17 {
18 
19 namespace obj
20 {
21 
31 class mutex {
32 public:
34  typedef PMEMmutex *native_handle_type;
35 
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 mutex not from persistent memory.");
48 
49  pmemobj_mutex_zero(pop, &plock);
50  }
51 
55  ~mutex() = default;
56 
68  void
69  lock()
70  {
71  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
72  if (int ret = pmemobj_mutex_lock(pop, &this->plock))
73  throw pmem::lock_error(ret, std::system_category(),
74  "Failed to lock a mutex.")
75  .with_pmemobj_errormsg();
76  }
77 
92  bool
94  {
95  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
96  int ret = pmemobj_mutex_trylock(pop, &this->plock);
97 
98  if (ret == 0)
99  return true;
100  else if (ret == EBUSY)
101  return false;
102  else
103  throw pmem::lock_error(ret, std::system_category(),
104  "Failed to lock a mutex.")
105  .with_pmemobj_errormsg();
106  }
107 
115  void
117  {
118  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
119  int ret = pmemobj_mutex_unlock(pop, &this->plock);
120  if (ret)
121  throw pmem::lock_error(ret, std::system_category(),
122  "Failed to unlock a mutex.")
123  .with_pmemobj_errormsg();
124  }
125 
132  native_handle() noexcept
133  {
134  return &this->plock;
135  }
136 
142  enum pobj_tx_param
143  lock_type() const noexcept
144  {
145  return TX_PARAM_MUTEX;
146  }
147 
151  mutex &operator=(const mutex &) = delete;
152 
156  mutex(const mutex &) = delete;
157 
158 private:
160  PMEMmutex plock;
161 };
162 
163 } /* namespace obj */
164 
165 } /* namespace pmem */
166 
167 #endif /* LIBPMEMOBJ_CPP_MUTEX_HPP */
pmem::obj::mutex::operator=
mutex & operator=(const mutex &)=delete
Deleted assignment operator.
pmem::obj::mutex
Persistent memory resident mutex implementation.
Definition: mutex.hpp:31
pmem
Persistent memory namespace.
Definition: allocation_flag.hpp:15
pmem::obj::mutex::~mutex
~mutex()=default
Defaulted destructor.
pmem::obj::mutex::native_handle
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: mutex.hpp:132
pexceptions.hpp
Custom exceptions.
pmem::obj::mutex::lock_type
enum pobj_tx_param lock_type() const noexcept
The type of lock needed for the transaction API.
Definition: mutex.hpp:143
pmem::obj::mutex::native_handle_type
PMEMmutex * native_handle_type
Implementation defined handle to the native type.
Definition: mutex.hpp:34
pmem::lock_error
Custom lock error class.
Definition: pexceptions.hpp:84
pmem::obj::mutex::plock
PMEMmutex plock
A POSIX style PMEM-resident mutex.
Definition: mutex.hpp:160
pmem::obj::mutex::try_lock
bool try_lock()
Tries to lock the mutex, returns regardless if the lock succeeds.
Definition: mutex.hpp:93
pmem::obj::mutex::unlock
void unlock()
Unlocks a previously locked mutex.
Definition: mutex.hpp:116
pmem::obj::mutex::lock
void lock()
Locks the mutex, blocks if already locked.
Definition: mutex.hpp:69
pmem::obj::mutex::mutex
mutex()
Default constructor.
Definition: mutex.hpp:41
pmem::obj::mutex::mutex
mutex(const mutex &)=delete
Deleted copy constructor.