NVML C++ bindings  0.1 experimental
This is the C++ bindings documentation for NVML's libpmemobj.
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_MUTEX_HPP
39 #define PMEMOBJ_MUTEX_HPP
40 
42 #include "libpmemobj/thread.h"
43 #include "libpmemobj/tx_base.h"
44 
45 namespace nvml
46 {
47 
48 namespace obj
49 {
50 
60 class mutex {
61 public:
63  typedef PMEMmutex *native_handle_type;
64 
68  mutex() noexcept = default;
69 
73  ~mutex() = default;
74 
86  void
87  lock()
88  {
89  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
90  if (int ret = pmemobj_mutex_lock(pop, &this->plock))
91  throw lock_error(ret, std::system_category(),
92  "Failed to lock a mutex.");
93  }
94 
109  bool
111  {
112  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
113  int ret = pmemobj_mutex_trylock(pop, &this->plock);
114 
115  if (ret == 0)
116  return true;
117  else if (ret == EBUSY)
118  return false;
119  else
120  throw lock_error(ret, std::system_category(),
121  "Failed to lock a mutex.");
122  }
123 
135  void
137  {
138  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
139  if (int ret = pmemobj_mutex_unlock(pop, &this->plock))
140  throw lock_error(ret, std::system_category(),
141  "Failed to unlock a mutex.");
142  }
143 
149  native_handle_type
150  native_handle() noexcept
151  {
152  return &this->plock;
153  }
154 
160  enum pobj_tx_lock
161  lock_type() const noexcept
162  {
163  return TX_LOCK_MUTEX;
164  }
165 
169  mutex &operator=(const mutex &) = delete;
170 
174  mutex(const mutex &) = delete;
175 
176 private:
178  PMEMmutex plock;
179 };
180 
181 } /* namespace obj */
182 
183 } /* namespace nvml */
184 
185 #endif /* PMEMOBJ_MUTEX_HPP */
mutex & operator=(const mutex &)=delete
Deleted assignment operator.
PMEMmutex * native_handle_type
Implementation defined handle to the native type.
Definition: mutex.hpp:63
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: mutex.hpp:150
mutex() noexcept=default
Defaulted constructor.
Persistent memory resident mutex implementation.
Definition: mutex.hpp:60
bool try_lock()
Tries to lock the mutex, returns regardless if the lock succeeds.
Definition: mutex.hpp:110
Custom exceptions.
void lock()
Locks the mutex, blocks if already locked.
Definition: mutex.hpp:87
Custom lock error class.
Definition: pexceptions.hpp:74
Definition: condition_variable.hpp:48
PMEMmutex plock
A POSIX style PMEM-resident mutex.
Definition: mutex.hpp:178
enum pobj_tx_lock lock_type() const noexcept
The type of lock needed for the transaction API.
Definition: mutex.hpp:161
void unlock()
Unlocks a previously locked mutex.
Definition: mutex.hpp:136