PMDK C++ bindings  1.5.2
This is the C++ bindings documentation for PMDK's libpmemobj.
timed_mutex.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2018, 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 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  int ret = pmemobj_mutex_unlock(pop, &this->plock);
199  if (ret)
200  throw lock_error(ret, std::system_category(),
201  "Failed to unlock a mutex.");
202  }
203 
210  native_handle() noexcept
211  {
212  return &this->plock;
213  }
214 
218  timed_mutex &operator=(const timed_mutex &) = delete;
219 
223  timed_mutex(const timed_mutex &) = delete;
224 
225 private:
229  template <typename Clock, typename Duration>
230  bool
231  timedlock_impl(const std::chrono::time_point<Clock, Duration> &abs_time)
232  {
233  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
234 
235  /* convert to my clock */
236  const typename Clock::time_point their_now = Clock::now();
237  const clock_type::time_point my_now = clock_type::now();
238  const auto delta = abs_time - their_now;
239  const auto my_abs = my_now + delta;
240 
241  struct timespec ts = detail::timepoint_to_timespec(my_abs);
242 
243  auto ret = pmemobj_mutex_timedlock(pop, &this->plock, &ts);
244 
245  if (ret == 0)
246  return true;
247  else if (ret == ETIMEDOUT)
248  return false;
249  else
250  throw lock_error(ret, std::system_category(),
251  "Failed to lock a mutex");
252  }
253 
255  PMEMmutex plock;
256 };
257 
258 } /* namespace obj */
259 
260 } /* namespace pmem */
261 
262 #endif /* LIBPMEMOBJ_CPP_TIMED_MUTEX_HPP */
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:195
pmem::obj::timed_mutex::plock
PMEMmutex plock
A POSIX style PMEM-resident timed_mutex.
Definition: timed_mutex.hpp:255
pmem::obj::timed_mutex::native_handle
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: timed_mutex.hpp:210
pmem::obj::timed_mutex::~timed_mutex
~timed_mutex()=default
Defaulted destructor.
pmem::lock_error
Custom lock error class.
Definition: pexceptions.hpp:74
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:157
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:231
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:182
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:124