PMDK C++ bindings  1.13.0-git107.g7e59f08f
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-2021, 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 
33 class mutex {
34 public:
36  typedef PMEMmutex *native_handle_type;
37 
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 mutex not from persistent memory.");
50 
51  pmemobj_mutex_zero(pop, &plock);
52  }
53 
57  ~mutex() = default;
58 
70  void
71  lock()
72  {
73  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
74  if (int ret = pmemobj_mutex_lock(pop, &this->plock))
75  throw detail::exception_with_errormsg<lock_error>(
76  ret, std::system_category(),
77  "Failed to lock a mutex.");
78  }
79 
94  bool
96  {
97  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
98  int ret = pmemobj_mutex_trylock(pop, &this->plock);
99 
100  if (ret == 0)
101  return true;
102  else if (ret == EBUSY)
103  return false;
104  else
105  throw detail::exception_with_errormsg<lock_error>(
106  ret, std::system_category(),
107  "Failed to lock a mutex.");
108  }
109 
117  void
119  {
120  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
121  int ret = pmemobj_mutex_unlock(pop, &this->plock);
122  if (ret)
123  throw detail::exception_with_errormsg<lock_error>(
124  ret, std::system_category(),
125  "Failed to unlock a mutex.");
126  }
127 
134  native_handle() noexcept
135  {
136  return &this->plock;
137  }
138 
144  enum pobj_tx_param
145  lock_type() const noexcept
146  {
147  return TX_PARAM_MUTEX;
148  }
149 
153  mutex &operator=(const mutex &) = delete;
154 
158  mutex(const mutex &) = delete;
159 
160 private:
162  PMEMmutex plock;
163 };
164 
165 } /* namespace obj */
166 
167 } /* namespace pmem */
168 
169 #endif /* LIBPMEMOBJ_CPP_MUTEX_HPP */
Custom lock error class.
Definition: pexceptions.hpp:121
Persistent memory resident mutex implementation.
Definition: mutex.hpp:33
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: mutex.hpp:134
mutex()
Default constructor.
Definition: mutex.hpp:43
enum pobj_tx_param lock_type() const noexcept
The type of lock needed for the transaction API.
Definition: mutex.hpp:145
PMEMmutex * native_handle_type
Implementation defined handle to the native type.
Definition: mutex.hpp:36
void unlock()
Unlocks a previously locked mutex.
Definition: mutex.hpp:118
mutex & operator=(const mutex &)=delete
Deleted assignment operator.
void lock()
Locks the mutex, blocks if already locked.
Definition: mutex.hpp:71
~mutex()=default
Defaulted destructor.
mutex(const mutex &)=delete
Deleted copy constructor.
bool try_lock()
Tries to lock the mutex, returns regardless if the lock succeeds.
Definition: mutex.hpp:95
Persistent memory namespace.
Definition: allocation_flag.hpp:15
Custom pmem exceptions.