PMDK C++ bindings  1.9.1
This is the C++ bindings documentation for PMDK's libpmemobj.
shared_mutex.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2020, Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
38 #ifndef LIBPMEMOBJ_CPP_SHARED_MUTEX_HPP
39 #define LIBPMEMOBJ_CPP_SHARED_MUTEX_HPP
40 
41 #include <libpmemobj/thread.h>
42 #include <libpmemobj/tx_base.h>
43 
44 namespace pmem
45 {
46 
47 namespace obj
48 {
49 
59 class shared_mutex {
60 public:
62  typedef PMEMrwlock *native_handle_type;
63 
71  {
72  PMEMobjpool *pop;
73  if ((pop = pmemobj_pool_by_ptr(&plock)) == nullptr)
74  throw pmem::lock_error(
75  1, std::generic_category(),
76  "Persistent shared mutex not from persistent memory.");
77 
78  pmemobj_rwlock_zero(pop, &plock);
79  }
80 
84  ~shared_mutex() = default;
85 
98  void
99  lock()
100  {
101  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
102  if (int ret = pmemobj_rwlock_wrlock(pop, &this->plock))
103  throw pmem::lock_error(ret, std::system_category(),
104  "Failed to lock a shared mutex.")
105  .with_pmemobj_errormsg();
106  }
107 
123  void
125  {
126  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
127  if (int ret = pmemobj_rwlock_rdlock(pop, &this->plock))
128  throw pmem::lock_error(
129  ret, std::system_category(),
130  "Failed to shared lock a shared mutex.");
131  }
132 
147  bool
149  {
150  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
151  int ret = pmemobj_rwlock_trywrlock(pop, &this->plock);
152 
153  if (ret == 0)
154  return true;
155  else if (ret == EBUSY)
156  return false;
157  else
158  throw pmem::lock_error(ret, std::system_category(),
159  "Failed to lock a shared mutex.")
160  .with_pmemobj_errormsg();
161  }
162 
179  bool
181  {
182  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
183  int ret = pmemobj_rwlock_tryrdlock(pop, &this->plock);
184 
185  if (ret == 0)
186  return true;
187  else if (ret == EBUSY)
188  return false;
189  else
190  throw pmem::lock_error(ret, std::system_category(),
191  "Failed to lock a shared mutex.")
192  .with_pmemobj_errormsg();
193  }
194 
201  void
203  {
204  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
205  int ret = pmemobj_rwlock_unlock(pop, &this->plock);
206  if (ret)
207  throw pmem::lock_error(
208  ret, std::system_category(),
209  "Failed to unlock a shared mutex.")
210  .with_pmemobj_errormsg();
211  }
212 
219  void
221  {
222  this->unlock();
223  }
224 
230  native_handle_type
231  native_handle() noexcept
232  {
233  return &this->plock;
234  }
235 
241  enum pobj_tx_param
242  lock_type() const noexcept
243  {
244  return TX_PARAM_RWLOCK;
245  }
246 
250  shared_mutex &operator=(const shared_mutex &) = delete;
251 
255  shared_mutex(const shared_mutex &) = delete;
256 
257 private:
259  PMEMrwlock plock;
260 };
261 
262 } /* namespace obj */
263 
264 } /* namespace pmem */
265 
266 #endif /* LIBPMEMOBJ_CPP_SHARED_MUTEX_HPP */
pmem
Persistent memory namespace.
Definition: allocation_flag.hpp:44
pmem::obj::shared_mutex::shared_mutex
shared_mutex()
Default constructor.
Definition: shared_mutex.hpp:70
pmem::obj::shared_mutex::lock
void lock()
Lock the mutex for exclusive access.
Definition: shared_mutex.hpp:99
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:231
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:148
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:180
pmem::obj::shared_mutex::unlock
void unlock()
Unlocks the mutex.
Definition: shared_mutex.hpp:202
pmem::lock_error
Custom lock error class.
Definition: pexceptions.hpp:113
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:242
pmem::obj::shared_mutex::plock
PMEMrwlock plock
A POSIX style PMEM-resident shared_mutex.
Definition: shared_mutex.hpp:259
pmem::obj::shared_mutex::lock_shared
void lock_shared()
Lock the mutex for shared access.
Definition: shared_mutex.hpp:124
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:62
pmem::obj::shared_mutex
Persistent memory resident shared_mutex implementation.
Definition: shared_mutex.hpp:59
pmem::obj::shared_mutex::unlock_shared
void unlock_shared()
Unlocks the mutex.
Definition: shared_mutex.hpp:220