PMDK C++ bindings 1.2.0
This is the C++ bindings documentation for PMDK's libpmemobj.
timed_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_TIMED_MUTEX_HPP
39#define PMEMOBJ_TIMED_MUTEX_HPP
40
41#include <chrono>
42
44#include "libpmemobj/thread.h"
45
46namespace pmem
47{
48
49namespace obj
50{
51
62 typedef std::chrono::system_clock clock_type;
63
64public:
66 typedef PMEMmutex *native_handle_type;
67
74 {
75 PMEMobjpool *pop;
76 if ((pop = pmemobj_pool_by_ptr(&plock)) == nullptr)
77 throw lock_error(1, std::generic_category(),
78 "Persistent mutex not from persistent"
79 "memory.");
80
81 pmemobj_mutex_zero(pop, &plock);
82 }
83
87 ~timed_mutex() = default;
88
100 void
102 {
103 PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
104 if (int ret = pmemobj_mutex_lock(pop, &this->plock))
105 throw lock_error(ret, std::system_category(),
106 "Failed to lock a mutex.");
107 }
108
123 bool
125 {
126 PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
127 int ret = pmemobj_mutex_trylock(pop, &this->plock);
128
129 if (ret == 0)
130 return true;
131 else if (ret == EBUSY)
132 return false;
133 else
134 throw lock_error(ret, std::system_category(),
135 "Failed to lock a mutex.");
136 }
137
155 template <typename Clock, typename Duration>
156 bool
158 const std::chrono::time_point<Clock, Duration> &timeout_time)
159 {
160 return timedlock_impl(timeout_time);
161 }
162
180 template <typename Rep, typename Period>
181 bool
182 try_lock_for(const std::chrono::duration<Rep, Period> &timeout_duration)
183 {
184 return timedlock_impl(clock_type::now() + timeout_duration);
185 }
186
194 void
196 {
197 PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
198 (void)pmemobj_mutex_unlock(pop, &this->plock);
199 }
200
206 native_handle_type
207 native_handle() noexcept
208 {
209 return &this->plock;
210 }
211
216
220 timed_mutex(const timed_mutex &) = delete;
221
222private:
226 template <typename Clock, typename Duration>
227 bool
228 timedlock_impl(const std::chrono::time_point<Clock, Duration> &abs_time)
229 {
230 PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
231
232 /* convert to my clock */
233 const typename Clock::time_point their_now = Clock::now();
234 const clock_type::time_point my_now = clock_type::now();
235 const auto delta = abs_time - their_now;
236 const auto my_abs = my_now + delta;
237
238 struct timespec ts = detail::timepoint_to_timespec(my_abs);
239
240 auto ret = pmemobj_mutex_timedlock(pop, &this->plock, &ts);
241
242 if (ret == 0)
243 return true;
244 else if (ret == ETIMEDOUT)
245 return false;
246 else
247 throw lock_error(ret, std::system_category(),
248 "Failed to lock a mutex");
249 }
250
252 PMEMmutex plock;
253};
254
255} /* namespace obj */
256
257} /* namespace pmem */
258
259#endif /* PMEMOBJ_TIMED_MUTEX_HPP */
Custom lock error class.
Definition: pexceptions.hpp:74
Persistent memory resident timed_mutex implementation.
Definition: timed_mutex.hpp:61
void lock()
Locks the mutex, blocks if already locked.
Definition: timed_mutex.hpp:101
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: timed_mutex.hpp:207
void unlock()
Unlocks a previously locked mutex.
Definition: timed_mutex.hpp:195
bool try_lock_until(const std::chrono::time_point< Clock, Duration > &timeout_time)
Makes the current thread block until the lock is acquired or a specific time is reached.
Definition: timed_mutex.hpp:157
timed_mutex()
Default constructor.
Definition: timed_mutex.hpp:73
timed_mutex & operator=(const timed_mutex &)=delete
Deleted assignment operator.
timed_mutex(const timed_mutex &)=delete
Deleted copy constructor.
~timed_mutex()=default
Defaulted destructor.
bool try_lock()
Tries to lock the mutex, returns regardless if the lock succeeds.
Definition: timed_mutex.hpp:124
PMEMmutex * native_handle_type
Implementation defined handle to the native type.
Definition: timed_mutex.hpp:66
bool timedlock_impl(const std::chrono::time_point< Clock, Duration > &abs_time)
Internal implementation of the timed lock call.
Definition: timed_mutex.hpp:228
bool try_lock_for(const std::chrono::duration< Rep, Period > &timeout_duration)
Makes the current thread block until the lock is acquired or a specified amount of time passes.
Definition: timed_mutex.hpp:182
PMEMmutex plock
A POSIX style PMEM-resident timed_mutex.
Definition: timed_mutex.hpp:252
Commonly used conversions.