PMDK C++ bindings  1.7.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-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(
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 lock_error(ret, std::system_category(),
104  "Failed to lock a shared mutex.");
105  }
106 
122  void
124  {
125  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
126  if (int ret = pmemobj_rwlock_rdlock(pop, &this->plock))
127  throw lock_error(
128  ret, std::system_category(),
129  "Failed to shared lock a shared mutex.");
130  }
131 
146  bool
148  {
149  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
150  int ret = pmemobj_rwlock_trywrlock(pop, &this->plock);
151 
152  if (ret == 0)
153  return true;
154  else if (ret == EBUSY)
155  return false;
156  else
157  throw lock_error(ret, std::system_category(),
158  "Failed to lock a shared mutex.");
159  }
160 
177  bool
179  {
180  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
181  int ret = pmemobj_rwlock_tryrdlock(pop, &this->plock);
182 
183  if (ret == 0)
184  return true;
185  else if (ret == EBUSY)
186  return false;
187  else
188  throw lock_error(ret, std::system_category(),
189  "Failed to lock a shared mutex.");
190  }
191 
198  void
200  {
201  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
202  int ret = pmemobj_rwlock_unlock(pop, &this->plock);
203  if (ret)
204  throw lock_error(ret, std::system_category(),
205  "Failed to unlock a shared mutex.");
206  }
207 
214  void
216  {
217  this->unlock();
218  }
219 
225  native_handle_type
226  native_handle() noexcept
227  {
228  return &this->plock;
229  }
230 
236  enum pobj_tx_param
237  lock_type() const noexcept
238  {
239  return TX_PARAM_RWLOCK;
240  }
241 
245  shared_mutex &operator=(const shared_mutex &) = delete;
246 
250  shared_mutex(const shared_mutex &) = delete;
251 
252 private:
254  PMEMrwlock plock;
255 };
256 
257 } /* namespace obj */
258 
259 } /* namespace pmem */
260 
261 #endif /* LIBPMEMOBJ_CPP_SHARED_MUTEX_HPP */
void unlock()
Unlocks the mutex.
Definition: shared_mutex.hpp:199
enum pobj_tx_param lock_type() const noexcept
The type of lock needed for the transaction API.
Definition: shared_mutex.hpp:237
PMEMrwlock plock
A POSIX style PMEM-resident shared_mutex.
Definition: shared_mutex.hpp:254
~shared_mutex()=default
Defaulted destructor.
PMEMrwlock * native_handle_type
Implementation defined handle to the native type.
Definition: shared_mutex.hpp:62
void lock_shared()
Lock the mutex for shared access.
Definition: shared_mutex.hpp:123
void unlock_shared()
Unlocks the mutex.
Definition: shared_mutex.hpp:215
shared_mutex()
Default constructor.
Definition: shared_mutex.hpp:70
Custom lock error class.
Definition: pexceptions.hpp:74
Persistent memory resident shared_mutex implementation.
Definition: shared_mutex.hpp:59
native_handle_type native_handle() noexcept
Access a native handle to this shared mutex.
Definition: shared_mutex.hpp:226
bool try_lock()
Try to lock the mutex for exclusive access, returns regardless if the lock succeeds.
Definition: shared_mutex.hpp:147
void lock()
Lock the mutex for exclusive access.
Definition: shared_mutex.hpp:99
bool try_lock_shared()
Try to lock the mutex for shared access, returns regardless if the lock succeeds.
Definition: shared_mutex.hpp:178
shared_mutex & operator=(const shared_mutex &)=delete
Deleted assignment operator.