PMDK C++ bindings 1.2.0
This is the C++ bindings documentation for PMDK's libpmemobj.
shared_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_SHARED_MUTEX_HPP
39#define PMEMOBJ_SHARED_MUTEX_HPP
40
41#include "libpmemobj/thread.h"
42#include "libpmemobj/tx_base.h"
43
44namespace pmem
45{
46
47namespace obj
48{
49
60public:
62 typedef PMEMrwlock *native_handle_type;
63
71 {
72 PMEMobjpool *pop;
73 if ((pop = pmemobj_pool_by_ptr(&plock)) == nullptr)
74 throw lock_error(1, std::generic_category(),
75 "Persistent shared mutex not from "
76 "persistent memory.");
77
78 pmemobj_rwlock_zero(pop, &plock);
79 }
80
84 ~shared_mutex() = default;
85
98 void
100 {
101 PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
102 if (int ret = pmemobj_rwlock_wrlock(pop, &this->plock))
103 throw lock_error(ret, std::system_category(),
104 "Failed to lock a "
105 "shared mutex.");
106 }
107
123 void
125 {
126 PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
127 if (int ret = pmemobj_rwlock_rdlock(pop, &this->plock))
128 throw lock_error(ret, std::system_category(),
129 "Failed to shared lock a "
130 "shared mutex.");
131 }
132
147 bool
149 {
150 PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
151 int ret = pmemobj_rwlock_trywrlock(pop, &this->plock);
152
153 if (ret == 0)
154 return true;
155 else if (ret == EBUSY)
156 return false;
157 else
158 throw lock_error(ret, std::system_category(),
159 "Failed to lock a"
160 " shared mutex.");
161 }
162
179 bool
181 {
182 PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
183 int ret = pmemobj_rwlock_tryrdlock(pop, &this->plock);
184
185 if (ret == 0)
186 return true;
187 else if (ret == EBUSY)
188 return false;
189 else
190 throw lock_error(ret, std::system_category(),
191 "Failed to lock a"
192 " shared mutex.");
193 }
194
201 void
203 {
204 PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
205 (void)pmemobj_rwlock_unlock(pop, &this->plock);
206 }
207
214 void
216 {
217 this->unlock();
218 }
219
225 native_handle_type
226 native_handle() noexcept
227 {
228 return &this->plock;
229 }
230
236 enum pobj_tx_param
237 lock_type() const noexcept
238 {
239 return TX_PARAM_RWLOCK;
240 }
241
246
250 shared_mutex(const shared_mutex &) = delete;
251
252private:
254 PMEMrwlock plock;
255};
256
257} /* namespace obj */
258
259} /* namespace pmem */
260
261#endif /* PMEMOBJ_SHARED_MUTEX_HPP */
Custom lock error class.
Definition: pexceptions.hpp:74
Persistent memory resident shared_mutex implementation.
Definition: shared_mutex.hpp:59
void lock_shared()
Lock the mutex for shared access.
Definition: shared_mutex.hpp:124
shared_mutex()
Default constructor.
Definition: shared_mutex.hpp:70
void unlock_shared()
Unlocks the mutex.
Definition: shared_mutex.hpp:215
native_handle_type native_handle() noexcept
Access a native handle to this shared mutex.
Definition: shared_mutex.hpp:226
shared_mutex & operator=(const shared_mutex &)=delete
Deleted assignment operator.
shared_mutex(const shared_mutex &)=delete
Deleted copy constructor.
~shared_mutex()=default
Defaulted destructor.
void unlock()
Unlocks the mutex.
Definition: shared_mutex.hpp:202
enum pobj_tx_param lock_type() const noexcept
The type of lock needed for the transaction API.
Definition: shared_mutex.hpp:237
PMEMrwlock * native_handle_type
Implementation defined handle to the native type.
Definition: shared_mutex.hpp:62
PMEMrwlock plock
A POSIX style PMEM-resident shared_mutex.
Definition: shared_mutex.hpp:254
void lock()
Lock the mutex for exclusive access.
Definition: shared_mutex.hpp:99
bool try_lock()
Try to lock the mutex for exclusive access, returns regardless if the lock succeeds.
Definition: shared_mutex.hpp:148
bool try_lock_shared()
Try to lock the mutex for shared access, returns regardless if the lock succeeds.
Definition: shared_mutex.hpp:180