PMDK C++ bindings  1.8.1
This is the C++ bindings documentation for PMDK's libpmemobj.
mutex.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2019, 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 pmem::lock_error(
75  1, std::generic_category(),
76  "Persistent mutex not from persistent 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 pmem::lock_error(ret, std::system_category(),
103  "Failed to lock a mutex.")
104  .with_pmemobj_errormsg();
105  }
106 
121  bool
123  {
124  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
125  int ret = pmemobj_mutex_trylock(pop, &this->plock);
126 
127  if (ret == 0)
128  return true;
129  else if (ret == EBUSY)
130  return false;
131  else
132  throw pmem::lock_error(ret, std::system_category(),
133  "Failed to lock a mutex.")
134  .with_pmemobj_errormsg();
135  }
136 
144  void
146  {
147  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
148  int ret = pmemobj_mutex_unlock(pop, &this->plock);
149  if (ret)
150  throw pmem::lock_error(ret, std::system_category(),
151  "Failed to unlock a mutex.")
152  .with_pmemobj_errormsg();
153  }
154 
161  native_handle() noexcept
162  {
163  return &this->plock;
164  }
165 
171  enum pobj_tx_param
172  lock_type() const noexcept
173  {
174  return TX_PARAM_MUTEX;
175  }
176 
180  mutex &operator=(const mutex &) = delete;
181 
185  mutex(const mutex &) = delete;
186 
187 private:
189  PMEMmutex plock;
190 };
191 
192 } /* namespace obj */
193 
194 } /* namespace pmem */
195 
196 #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
A persistent version of concurrent hash map implementation Ref: https://arxiv.org/abs/1509....
Definition: allocation_flag.hpp:44
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:161
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:172
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:109
pmem::obj::mutex::plock
PMEMmutex plock
A POSIX style PMEM-resident mutex.
Definition: mutex.hpp:189
pmem::obj::mutex::try_lock
bool try_lock()
Tries to lock the mutex, returns regardless if the lock succeeds.
Definition: mutex.hpp:122
pmem::obj::mutex::unlock
void unlock()
Unlocks a previously locked mutex.
Definition: mutex.hpp:145
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.