PMDK C++ bindings  1.5.2
This is the C++ bindings documentation for PMDK's libpmemobj.
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_MUTEX_HPP
39 #define LIBPMEMOBJ_CPP_MUTEX_HPP
40 
42 #include <libpmemobj/thread.h>
43 #include <libpmemobj/tx_base.h>
44 
45 namespace pmem
46 {
47 
48 namespace obj
49 {
50 
60 class mutex {
61 public:
63  typedef PMEMmutex *native_handle_type;
64 
71  {
72  PMEMobjpool *pop;
73  if ((pop = pmemobj_pool_by_ptr(&plock)) == nullptr)
74  throw lock_error(1, std::generic_category(),
75  "Persistent mutex not from persistent"
76  "memory.");
77 
78  pmemobj_mutex_zero(pop, &plock);
79  }
80 
84  ~mutex() = default;
85 
97  void
98  lock()
99  {
100  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
101  if (int ret = pmemobj_mutex_lock(pop, &this->plock))
102  throw lock_error(ret, std::system_category(),
103  "Failed to lock a mutex.");
104  }
105 
120  bool
122  {
123  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
124  int ret = pmemobj_mutex_trylock(pop, &this->plock);
125 
126  if (ret == 0)
127  return true;
128  else if (ret == EBUSY)
129  return false;
130  else
131  throw lock_error(ret, std::system_category(),
132  "Failed to lock a mutex.");
133  }
134 
142  void
144  {
145  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
146  int ret = pmemobj_mutex_unlock(pop, &this->plock);
147  if (ret)
148  throw lock_error(ret, std::system_category(),
149  "Failed to unlock a mutex.");
150  }
151 
158  native_handle() noexcept
159  {
160  return &this->plock;
161  }
162 
168  enum pobj_tx_param
169  lock_type() const noexcept
170  {
171  return TX_PARAM_MUTEX;
172  }
173 
177  mutex &operator=(const mutex &) = delete;
178 
182  mutex(const mutex &) = delete;
183 
184 private:
186  PMEMmutex plock;
187 };
188 
189 } /* namespace obj */
190 
191 } /* namespace pmem */
192 
193 #endif /* LIBPMEMOBJ_CPP_MUTEX_HPP */
pmem::obj::mutex::operator=
mutex & operator=(const mutex &)=delete
Deleted assignment operator.
pmem::obj::mutex
Persistent memory resident mutex implementation.
Definition: mutex.hpp:60
pmem::obj::mutex::~mutex
~mutex()=default
Defaulted destructor.
pmem::obj::mutex::native_handle
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: mutex.hpp:158
pexceptions.hpp
Custom exceptions.
pmem::obj::mutex::lock_type
enum pobj_tx_param lock_type() const noexcept
The type of lock needed for the transaction API.
Definition: mutex.hpp:169
pmem::obj::mutex::native_handle_type
PMEMmutex * native_handle_type
Implementation defined handle to the native type.
Definition: mutex.hpp:63
pmem::lock_error
Custom lock error class.
Definition: pexceptions.hpp:74
pmem::obj::mutex::plock
PMEMmutex plock
A POSIX style PMEM-resident mutex.
Definition: mutex.hpp:186
pmem::obj::mutex::try_lock
bool try_lock()
Tries to lock the mutex, returns regardless if the lock succeeds.
Definition: mutex.hpp:121
pmem::obj::mutex::unlock
void unlock()
Unlocks a previously locked mutex.
Definition: mutex.hpp:143
pmem::obj::mutex::lock
void lock()
Locks the mutex, blocks if already locked.
Definition: mutex.hpp:98
pmem::obj::mutex::mutex
mutex()
Default constructor.
Definition: mutex.hpp:70
pmem::obj::mutex::mutex
mutex(const mutex &)=delete
Deleted copy constructor.