NVML C++ bindings  0.1 experimental
This is the C++ bindings documentation for NVML's libpmemobj.
shared_mutex.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016, 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 PMEMOBJ_SHARED_MUTEX_HPP
39 #define PMEMOBJ_SHARED_MUTEX_HPP
40 
41 #include "libpmemobj/thread.h"
42 #include "libpmemobj/tx_base.h"
43 
44 namespace nvml
45 {
46 
47 namespace obj
48 {
49 
59 class shared_mutex {
60 public:
62  typedef PMEMrwlock *native_handle_type;
63 
67  shared_mutex() noexcept = default;
68 
72  ~shared_mutex() = default;
73 
86  void
87  lock()
88  {
89  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
90  if (int ret = pmemobj_rwlock_wrlock(pop, &this->plock))
91  throw lock_error(ret, std::system_category(),
92  "Failed to lock a "
93  "shared mutex.");
94  }
95 
111  void
113  {
114  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
115  if (int ret = pmemobj_rwlock_rdlock(pop, &this->plock))
116  throw lock_error(ret, std::system_category(),
117  "Failed to shared lock a "
118  "shared mutex.");
119  }
120 
135  bool
137  {
138  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
139  int ret = pmemobj_rwlock_trywrlock(pop, &this->plock);
140 
141  if (ret == 0)
142  return true;
143  else if (ret == EBUSY)
144  return false;
145  else
146  throw lock_error(ret, std::system_category(),
147  "Failed to lock a"
148  " shared mutex.");
149  }
150 
167  bool
169  {
170  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
171  int ret = pmemobj_rwlock_tryrdlock(pop, &this->plock);
172 
173  if (ret == 0)
174  return true;
175  else if (ret == EBUSY)
176  return false;
177  else
178  throw lock_error(ret, std::system_category(),
179  "Failed to lock a"
180  " shared mutex.");
181  }
182 
193  void
195  {
196  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
197  if (int ret = pmemobj_rwlock_unlock(pop, &this->plock))
198  throw lock_error(ret, std::system_category(),
199  "Failed to unlock a"
200  " shared mutex.");
201  }
202 
213  void
215  {
216  this->unlock();
217  }
218 
224  native_handle_type
225  native_handle() noexcept
226  {
227  return &this->plock;
228  }
229 
235  enum pobj_tx_lock
236  lock_type() const noexcept
237  {
238  return TX_LOCK_RWLOCK;
239  }
240 
244  shared_mutex &operator=(const shared_mutex &) = delete;
245 
249  shared_mutex(const shared_mutex &) = delete;
250 
251 private:
253  PMEMrwlock plock;
254 };
255 
256 } /* namespace obj */
257 
258 } /* namespace nvml */
259 
260 #endif /* PMEMOBJ_SHARED_MUTEX_HPP */
PMEMrwlock * native_handle_type
Implementation defined handle to the native type.
Definition: shared_mutex.hpp:62
shared_mutex & operator=(const shared_mutex &)=delete
Deleted assignment operator.
native_handle_type native_handle() noexcept
Access a native handle to this shared mutex.
Definition: shared_mutex.hpp:225
bool try_lock_shared()
Try to lock the mutex for shared access, returns regardless if the lock succeeds. ...
Definition: shared_mutex.hpp:168
void unlock_shared()
Unlocks the mutex.
Definition: shared_mutex.hpp:214
void lock()
Lock the mutex for exclusive access.
Definition: shared_mutex.hpp:87
void unlock()
Unlocks the mutex.
Definition: shared_mutex.hpp:194
Custom lock error class.
Definition: pexceptions.hpp:74
enum pobj_tx_lock lock_type() const noexcept
The type of lock needed for the transaction API.
Definition: shared_mutex.hpp:236
bool try_lock()
Try to lock the mutex for exclusive access, returns regardless if the lock succeeds.
Definition: shared_mutex.hpp:136
void lock_shared()
Lock the mutex for shared access.
Definition: shared_mutex.hpp:112
Definition: condition_variable.hpp:48
Persistent memory resident shared_mutex implementation.
Definition: shared_mutex.hpp:59
shared_mutex() noexcept=default
Defaulted constructor.
PMEMrwlock plock
A POSIX style PMEM-resident shared_mutex.
Definition: shared_mutex.hpp:253