NVML C++ bindings  0.1 experimental
This is the C++ bindings documentation for NVML's libpmemobj.
timed_mutex.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016, 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 
46 namespace nvml
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 
71  timed_mutex() noexcept = default;
72 
76  ~timed_mutex() = default;
77 
89  void
90  lock()
91  {
92  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
93  if (int ret = pmemobj_mutex_lock(pop, &this->plock))
94  throw lock_error(ret, std::system_category(),
95  "Failed to lock a mutex.");
96  }
97 
112  bool
114  {
115  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
116  int ret = pmemobj_mutex_trylock(pop, &this->plock);
117 
118  if (ret == 0)
119  return true;
120  else if (ret == EBUSY)
121  return false;
122  else
123  throw lock_error(ret, std::system_category(),
124  "Failed to lock a mutex.");
125  }
126 
144  template <typename Clock, typename Duration>
145  bool
147  const std::chrono::time_point<Clock, Duration> &timeout_time)
148  {
149  return timedlock_impl(timeout_time);
150  }
151 
169  template <typename Rep, typename Period>
170  bool
171  try_lock_for(const std::chrono::duration<Rep, Period> &timeout_duration)
172  {
173  return timedlock_impl(clock_type::now() + timeout_duration);
174  }
175 
187  void
189  {
190  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
191  if (int ret = pmemobj_mutex_unlock(pop, &this->plock))
192  throw lock_error(ret, std::system_category(),
193  "Failed to unlock a mutex.");
194  }
195 
201  native_handle_type
202  native_handle() noexcept
203  {
204  return &this->plock;
205  }
206 
210  timed_mutex &operator=(const timed_mutex &) = delete;
211 
215  timed_mutex(const timed_mutex &) = delete;
216 
217 private:
221  template <typename Clock, typename Duration>
222  bool
223  timedlock_impl(const std::chrono::time_point<Clock, Duration> &abs_time)
224  {
225  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
226 
227  /* convert to my clock */
228  const typename Clock::time_point their_now = Clock::now();
229  const clock_type::time_point my_now = clock_type::now();
230  const auto delta = abs_time - their_now;
231  const auto my_abs = my_now + delta;
232 
233  struct timespec ts = detail::timepoint_to_timespec(my_abs);
234 
235  auto ret = pmemobj_mutex_timedlock(pop, &this->plock, &ts);
236 
237  if (ret == 0)
238  return true;
239  else if (ret == ETIMEDOUT)
240  return false;
241  else
242  throw lock_error(ret, std::system_category(),
243  "Failed to lock a mutex");
244  }
245 
247  PMEMmutex plock;
248 };
249 
250 } /* namespace obj */
251 
252 } /* namespace nvml */
253 
254 #endif /* PMEMOBJ_TIMED_MUTEX_HPP */
void lock()
Locks the mutex, blocks if already locked.
Definition: timed_mutex.hpp:90
timed_mutex & operator=(const timed_mutex &)=delete
Deleted assignment operator.
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:146
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:171
void unlock()
Unlocks a previously locked mutex.
Definition: timed_mutex.hpp:188
Commonly used conversions.
PMEMmutex * native_handle_type
Implementation defined handle to the native type.
Definition: timed_mutex.hpp:66
timed_mutex() noexcept=default
Defaulted constructor.
PMEMmutex plock
A POSIX style PMEM-resident timed_mutex.
Definition: timed_mutex.hpp:247
Custom lock error class.
Definition: pexceptions.hpp:74
bool try_lock()
Tries to lock the mutex, returns regardless if the lock succeeds.
Definition: timed_mutex.hpp:113
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: timed_mutex.hpp:202
Definition: condition_variable.hpp:48
bool timedlock_impl(const std::chrono::time_point< Clock, Duration > &abs_time)
Internal implementation of the timed lock call.
Definition: timed_mutex.hpp:223
Persistent memory resident timed_mutex implementation.
Definition: timed_mutex.hpp:61