PMDK C++ bindings  1.5.2
This is the C++ bindings documentation for PMDK's libpmemobj.
shared_mutex.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2018, 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 lock_error(1, std::generic_category(),
75  "Persistent shared mutex not from "
76  "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 lock_error(ret, std::system_category(),
104  "Failed to lock a "
105  "shared mutex.");
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 lock_error(ret, std::system_category(),
129  "Failed to shared lock a "
130  "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 lock_error(ret, std::system_category(),
159  "Failed to lock a"
160  " shared mutex.");
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 lock_error(ret, std::system_category(),
191  "Failed to lock a"
192  " shared mutex.");
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 lock_error(ret, std::system_category(),
208  "Failed to unlock a shared mutex.");
209  }
210 
217  void
219  {
220  this->unlock();
221  }
222 
228  native_handle_type
229  native_handle() noexcept
230  {
231  return &this->plock;
232  }
233 
239  enum pobj_tx_param
240  lock_type() const noexcept
241  {
242  return TX_PARAM_RWLOCK;
243  }
244 
248  shared_mutex &operator=(const shared_mutex &) = delete;
249 
253  shared_mutex(const shared_mutex &) = delete;
254 
255 private:
257  PMEMrwlock plock;
258 };
259 
260 } /* namespace obj */
261 
262 } /* namespace pmem */
263 
264 #endif /* LIBPMEMOBJ_CPP_SHARED_MUTEX_HPP */
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:229
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:74
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:240
pmem::obj::shared_mutex::plock
PMEMrwlock plock
A POSIX style PMEM-resident shared_mutex.
Definition: shared_mutex.hpp:257
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:218