PMDK C++ bindings 1.2.0
This is the C++ bindings documentation for PMDK's libpmemobj.
mutex.hpp
Go to the documentation of this file.
1/*
2 * Copyright 2016-2017, 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
45namespace pmem
46{
47
48namespace obj
49{
50
60class mutex {
61public:
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
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 (void)pmemobj_mutex_unlock(pop, &this->plock);
147 }
148
154 native_handle_type
155 native_handle() noexcept
156 {
157 return &this->plock;
158 }
159
165 enum pobj_tx_param
166 lock_type() const noexcept
167 {
168 return TX_PARAM_MUTEX;
169 }
170
174 mutex &operator=(const mutex &) = delete;
175
179 mutex(const mutex &) = delete;
180
181private:
183 PMEMmutex plock;
184};
185
186} /* namespace obj */
187
188} /* namespace pmem */
189
190#endif /* PMEMOBJ_MUTEX_HPP */
Custom lock error class.
Definition: pexceptions.hpp:74
Persistent memory resident mutex implementation.
Definition: mutex.hpp:60
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: mutex.hpp:155
mutex()
Default constructor.
Definition: mutex.hpp:70
enum pobj_tx_param lock_type() const noexcept
The type of lock needed for the transaction API.
Definition: mutex.hpp:166
PMEMmutex * native_handle_type
Implementation defined handle to the native type.
Definition: mutex.hpp:63
void unlock()
Unlocks a previously locked mutex.
Definition: mutex.hpp:143
void lock()
Locks the mutex, blocks if already locked.
Definition: mutex.hpp:98
~mutex()=default
Defaulted destructor.
mutex(const mutex &)=delete
Deleted copy constructor.
mutex & operator=(const mutex &)=delete
Deleted assignment operator.
bool try_lock()
Tries to lock the mutex, returns regardless if the lock succeeds.
Definition: mutex.hpp:121
PMEMmutex plock
A POSIX style PMEM-resident mutex.
Definition: mutex.hpp:183
Custom exceptions.