PMDK C++ bindings  1.8.1
This is the C++ bindings documentation for PMDK's libpmemobj.
timed_mutex.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2019, 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 LIBPMEMOBJ_CPP_TIMED_MUTEX_HPP
39 #define LIBPMEMOBJ_CPP_TIMED_MUTEX_HPP
40 
41 #include <chrono>
42 
44 #include <libpmemobj/thread.h>
45 
46 namespace pmem
47 {
48 
49 namespace obj
50 {
51 
61 class timed_mutex {
62  typedef std::chrono::system_clock clock_type;
63 
64 public:
66  typedef PMEMmutex *native_handle_type;
67 
74  {
75  PMEMobjpool *pop;
76  if ((pop = pmemobj_pool_by_ptr(&plock)) == nullptr)
77  throw pmem::lock_error(
78  1, std::generic_category(),
79  "Persistent mutex not from persistent 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 pmem::lock_error(ret, std::system_category(),
106  "Failed to lock a mutex.")
107  .with_pmemobj_errormsg();
108  }
109 
124  bool
126  {
127  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
128  int ret = pmemobj_mutex_trylock(pop, &this->plock);
129 
130  if (ret == 0)
131  return true;
132  else if (ret == EBUSY)
133  return false;
134  else
135  throw pmem::lock_error(ret, std::system_category(),
136  "Failed to lock a mutex.")
137  .with_pmemobj_errormsg();
138  }
139 
157  template <typename Clock, typename Duration>
158  bool
160  const std::chrono::time_point<Clock, Duration> &timeout_time)
161  {
162  return timedlock_impl(timeout_time);
163  }
164 
182  template <typename Rep, typename Period>
183  bool
184  try_lock_for(const std::chrono::duration<Rep, Period> &timeout_duration)
185  {
186  return timedlock_impl(clock_type::now() + timeout_duration);
187  }
188 
196  void
198  {
199  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
200  int ret = pmemobj_mutex_unlock(pop, &this->plock);
201  if (ret)
202  throw pmem::lock_error(ret, std::system_category(),
203  "Failed to unlock a mutex.")
204  .with_pmemobj_errormsg();
205  }
206 
213  native_handle() noexcept
214  {
215  return &this->plock;
216  }
217 
221  timed_mutex &operator=(const timed_mutex &) = delete;
222 
226  timed_mutex(const timed_mutex &) = delete;
227 
228 private:
232  template <typename Clock, typename Duration>
233  bool
234  timedlock_impl(const std::chrono::time_point<Clock, Duration> &abs_time)
235  {
236  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
237 
238  /* convert to my clock */
239  const typename Clock::time_point their_now = Clock::now();
240  const clock_type::time_point my_now = clock_type::now();
241  const auto delta = abs_time - their_now;
242  const auto my_abs = my_now + delta;
243 
244  struct timespec ts = detail::timepoint_to_timespec(my_abs);
245 
246  auto ret = pmemobj_mutex_timedlock(pop, &this->plock, &ts);
247 
248  if (ret == 0)
249  return true;
250  else if (ret == ETIMEDOUT)
251  return false;
252  else
253  throw pmem::lock_error(ret, std::system_category(),
254  "Failed to lock a mutex");
255  }
256 
258  PMEMmutex plock;
259 };
260 
261 } /* namespace obj */
262 
263 } /* namespace pmem */
264 
265 #endif /* LIBPMEMOBJ_CPP_TIMED_MUTEX_HPP */
pmem
A persistent version of concurrent hash map implementation Ref: https://arxiv.org/abs/1509....
Definition: allocation_flag.hpp:44
conversions.hpp
Commonly used conversions.
pmem::obj::timed_mutex::timed_mutex
timed_mutex(const timed_mutex &)=delete
Deleted copy constructor.
pmem::obj::timed_mutex::lock
void lock()
Locks the mutex, blocks if already locked.
Definition: timed_mutex.hpp:101
pmem::obj::timed_mutex::unlock
void unlock()
Unlocks a previously locked mutex.
Definition: timed_mutex.hpp:197
pmem::obj::timed_mutex::plock
PMEMmutex plock
A POSIX style PMEM-resident timed_mutex.
Definition: timed_mutex.hpp:258
pmem::obj::timed_mutex::native_handle
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: timed_mutex.hpp:213
pmem::obj::timed_mutex::~timed_mutex
~timed_mutex()=default
Defaulted destructor.
pmem::lock_error
Custom lock error class.
Definition: pexceptions.hpp:109
pmem::obj::timed_mutex::try_lock_until
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:159
pmem::obj::timed_mutex::timed_mutex
timed_mutex()
Default constructor.
Definition: timed_mutex.hpp:73
pmem::obj::timed_mutex::native_handle_type
PMEMmutex * native_handle_type
Implementation defined handle to the native type.
Definition: timed_mutex.hpp:66
pmem::obj::timed_mutex::timedlock_impl
bool timedlock_impl(const std::chrono::time_point< Clock, Duration > &abs_time)
Internal implementation of the timed lock call.
Definition: timed_mutex.hpp:234
pmem::obj::timed_mutex::try_lock_for
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:184
pmem::obj::timed_mutex
Persistent memory resident timed_mutex implementation.
Definition: timed_mutex.hpp:61
pmem::obj::timed_mutex::operator=
timed_mutex & operator=(const timed_mutex &)=delete
Deleted assignment operator.
pmem::obj::timed_mutex::try_lock
bool try_lock()
Tries to lock the mutex, returns regardless if the lock succeeds.
Definition: timed_mutex.hpp:125