PMDK C++ bindings 1.13.0
This is the C++ bindings documentation for PMDK's libpmemobj.
Loading...
Searching...
No Matches
timed_mutex.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2/* Copyright 2016-2020, Intel Corporation */
3
9#ifndef LIBPMEMOBJ_CPP_TIMED_MUTEX_HPP
10#define LIBPMEMOBJ_CPP_TIMED_MUTEX_HPP
11
12#include <chrono>
13
15#include <libpmemobj/thread.h>
16
17namespace pmem
18{
19
20namespace obj
21{
22
33 typedef std::chrono::system_clock clock_type;
34
35public:
37 typedef PMEMmutex *native_handle_type;
38
45 {
46 PMEMobjpool *pop;
47 if ((pop = pmemobj_pool_by_ptr(&plock)) == nullptr)
48 throw pmem::lock_error(
49 1, std::generic_category(),
50 "Persistent mutex not from persistent memory.");
51
52 pmemobj_mutex_zero(pop, &plock);
53 }
54
58 ~timed_mutex() = default;
59
71 void
73 {
74 PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
75 if (int ret = pmemobj_mutex_lock(pop, &this->plock))
76 throw pmem::lock_error(ret, std::system_category(),
77 "Failed to lock a mutex.")
78 .with_pmemobj_errormsg();
79 }
80
95 bool
97 {
98 PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
99 int ret = pmemobj_mutex_trylock(pop, &this->plock);
100
101 if (ret == 0)
102 return true;
103 else if (ret == EBUSY)
104 return false;
105 else
106 throw pmem::lock_error(ret, std::system_category(),
107 "Failed to lock a mutex.")
108 .with_pmemobj_errormsg();
109 }
110
128 template <typename Clock, typename Duration>
129 bool
131 const std::chrono::time_point<Clock, Duration> &timeout_time)
132 {
133 return timedlock_impl(timeout_time);
134 }
135
153 template <typename Rep, typename Period>
154 bool
155 try_lock_for(const std::chrono::duration<Rep, Period> &timeout_duration)
156 {
157 return timedlock_impl(clock_type::now() + timeout_duration);
158 }
159
167 void
169 {
170 PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
171 int ret = pmemobj_mutex_unlock(pop, &this->plock);
172 if (ret)
173 throw pmem::lock_error(ret, std::system_category(),
174 "Failed to unlock a mutex.")
175 .with_pmemobj_errormsg();
176 }
177
184 native_handle() noexcept
185 {
186 return &this->plock;
187 }
188
193
197 timed_mutex(const timed_mutex &) = delete;
198
199private:
203 template <typename Clock, typename Duration>
204 bool
205 timedlock_impl(const std::chrono::time_point<Clock, Duration> &abs_time)
206 {
207 PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
208
209 /* convert to my clock */
210 const typename Clock::time_point their_now = Clock::now();
211 const clock_type::time_point my_now = clock_type::now();
212 const auto delta = abs_time - their_now;
213 const auto my_abs = my_now + delta;
214
215 struct timespec ts = detail::timepoint_to_timespec(my_abs);
216
217 auto ret = pmemobj_mutex_timedlock(pop, &this->plock, &ts);
218
219 if (ret == 0)
220 return true;
221 else if (ret == ETIMEDOUT)
222 return false;
223 else
224 throw pmem::lock_error(ret, std::system_category(),
225 "Failed to lock a mutex");
226 }
227
229 PMEMmutex plock;
230};
231
232} /* namespace obj */
233
234} /* namespace pmem */
235
236#endif /* LIBPMEMOBJ_CPP_TIMED_MUTEX_HPP */
Custom lock error class.
Definition: pexceptions.hpp:100
Persistent memory resident timed_mutex implementation.
Definition: timed_mutex.hpp:32
void lock()
Locks the mutex, blocks if already locked.
Definition: timed_mutex.hpp:72
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: timed_mutex.hpp:184
void unlock()
Unlocks a previously locked mutex.
Definition: timed_mutex.hpp:168
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:130
timed_mutex()
Default constructor.
Definition: timed_mutex.hpp:44
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:96
PMEMmutex * native_handle_type
Implementation defined handle to the native type.
Definition: timed_mutex.hpp:37
bool timedlock_impl(const std::chrono::time_point< Clock, Duration > &abs_time)
Internal implementation of the timed lock call.
Definition: timed_mutex.hpp:205
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:155
PMEMmutex plock
A POSIX style PMEM-resident timed_mutex.
Definition: timed_mutex.hpp:229
Commonly used conversions.
timespec timepoint_to_timespec(const std::chrono::time_point< Clock, Duration > &timepoint)
Convert std::chrono::time_point to posix timespec.
Definition: conversions.hpp:30
Persistent memory namespace.
Definition: allocation_flag.hpp:15