PMDK C++ bindings  1.7.1
This is the C++ bindings documentation for PMDK's libpmemobj.
transaction.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_TRANSACTION_HPP
39 #define LIBPMEMOBJ_CPP_TRANSACTION_HPP
40 
41 #include <functional>
42 #include <string>
43 
46 #include <libpmemobj++/pool.hpp>
47 #include <libpmemobj/tx_base.h>
48 
49 namespace pmem
50 {
51 
52 namespace obj
53 {
54 
72 class transaction {
73 public:
92  class manual {
93  public:
107  template <typename... L>
108  manual(obj::pool_base &pop, L &... locks)
109  {
110  if (pmemobj_tx_begin(pop.handle(), nullptr,
111  TX_PARAM_NONE) != 0)
112  throw transaction_error(
113  "failed to start transaction");
114 
115  auto err = add_lock(locks...);
116 
117  if (err) {
118  pmemobj_tx_abort(EINVAL);
119  (void)pmemobj_tx_end();
120  throw transaction_error("failed to add lock");
121  }
122  }
123 
131  ~manual() noexcept
132  {
133  /* normal exit or with an active exception */
134  if (pmemobj_tx_stage() == TX_STAGE_WORK)
135  pmemobj_tx_abort(ECANCELED);
136 
137  (void)pmemobj_tx_end();
138  }
139 
143  manual(const manual &p) = delete;
144 
148  manual(const manual &&p) = delete;
149 
153  manual &operator=(const manual &p) = delete;
154 
158  manual &operator=(manual &&p) = delete;
159  };
160 
161 /*
162  * XXX The Microsoft compiler does not follow the ISO SD-6: SG10 Feature
163  * Test Recommendations. "|| _MSC_VER >= 1900" is a workaround.
164  */
165 #if __cpp_lib_uncaught_exceptions || _MSC_VER >= 1900
166 
185  class automatic {
186  public:
204  template <typename... L>
205  automatic(obj::pool_base &pop, L &... locks)
206  : tx_worker(pop, locks...)
207  {
208  }
209 
220  ~automatic() noexcept(false)
221  {
222  /* active exception, abort handled by tx_worker */
223  if (exceptions.new_uncaught_exception())
224  return;
225 
226  /* transaction ended normally */
227  if (pmemobj_tx_stage() == TX_STAGE_WORK)
228  pmemobj_tx_commit();
229  /* transaction aborted, throw an exception */
230  else if (pmemobj_tx_stage() == TX_STAGE_ONABORT ||
231  (pmemobj_tx_stage() == TX_STAGE_FINALLY &&
232  pmemobj_tx_errno() != 0))
233  throw transaction_error("Transaction aborted");
234  }
235 
239  automatic(const automatic &p) = delete;
240 
244  automatic(const automatic &&p) = delete;
245 
249  automatic &operator=(const automatic &p) = delete;
250 
254  automatic &operator=(automatic &&p) = delete;
255 
256  private:
261  public:
269  : count(std::uncaught_exceptions())
270  {
271  }
272 
280  bool
282  {
283  return std::uncaught_exceptions() > this->count;
284  }
285 
286  private:
290  int count;
291  } exceptions;
292 
293  transaction::manual tx_worker;
294  };
295 #endif /* __cpp_lib_uncaught_exceptions */
296 
297  /*
298  * Deleted default constructor.
299  */
300  transaction() = delete;
301 
308  ~transaction() noexcept = delete;
309 
324  static void
325  abort(int err)
326  {
327  if (pmemobj_tx_stage() != TX_STAGE_WORK)
328  throw transaction_error("wrong stage for abort");
329 
330  pmemobj_tx_abort(err);
331  throw manual_tx_abort("explicit abort " + std::to_string(err));
332  }
333 
344  static void
346  {
347  if (pmemobj_tx_stage() != TX_STAGE_WORK)
348  throw transaction_error("wrong stage for commit");
349 
350  pmemobj_tx_commit();
351  }
352 
353  static int
354  error() noexcept
355  {
356  return pmemobj_tx_errno();
357  }
358 
359  POBJ_CPP_DEPRECATED static int
360  get_last_tx_error() noexcept
361  {
362  return transaction::error();
363  }
364 
396  template <typename... Locks>
397  static void
398  run(pool_base &pool, std::function<void()> tx, Locks &... locks)
399  {
400  if (pmemobj_tx_begin(pool.handle(), nullptr, TX_PARAM_NONE) !=
401  0)
402  throw transaction_error("failed to start transaction");
403 
404  auto err = add_lock(locks...);
405 
406  if (err) {
407  pmemobj_tx_abort(err);
408  (void)pmemobj_tx_end();
409  throw transaction_error(
410  "failed to add a lock to the transaction");
411  }
412 
413  try {
414  tx();
415  } catch (manual_tx_abort &) {
416  (void)pmemobj_tx_end();
417  throw;
418  } catch (...) {
419  /* first exception caught */
420  if (pmemobj_tx_stage() == TX_STAGE_WORK)
421  pmemobj_tx_abort(ECANCELED);
422 
423  /* waterfall tx_end for outer tx */
424  (void)pmemobj_tx_end();
425  throw;
426  }
427 
428  auto stage = pmemobj_tx_stage();
429 
430  if (stage == TX_STAGE_WORK) {
431  pmemobj_tx_commit();
432  } else if (stage == TX_STAGE_ONABORT) {
433  (void)pmemobj_tx_end();
434  throw transaction_error("transaction aborted");
435  } else if (stage == TX_STAGE_NONE) {
436  throw transaction_error(
437  "transaction ended prematurely");
438  }
439 
440  (void)pmemobj_tx_end();
441  }
442 
443  template <typename... Locks>
444  POBJ_CPP_DEPRECATED static void
445  exec_tx(pool_base &pool, std::function<void()> tx, Locks &... locks)
446  {
447  transaction::run(pool, tx, locks...);
448  }
449 
470  template <
471  typename T,
472  typename std::enable_if<LIBPMEMOBJ_CPP_IS_TRIVIALLY_COPYABLE(T),
473  T>::type * = nullptr>
474  static void
475  snapshot(const T *addr, size_t num = 1)
476  {
477  if (TX_STAGE_WORK != pmemobj_tx_stage())
478  throw transaction_error(
479  "wrong stage for taking a snapshot.");
480 
481  if (pmemobj_tx_add_range_direct(addr, sizeof(*addr) * num))
482  throw transaction_error(
483  "Could not take a snapshot of given memory range.");
484  }
485 
486 private:
499  template <typename L, typename... Locks>
500  static int
501  add_lock(L &lock, Locks &... locks) noexcept
502  {
503  auto err =
504  pmemobj_tx_lock(lock.lock_type(), lock.native_handle());
505 
506  if (err)
507  return err;
508 
509  return add_lock(locks...);
510  }
511 
515  static inline int
516  add_lock() noexcept
517  {
518  return 0;
519  }
520 };
521 
522 } /* namespace obj */
523 
524 } /* namespace pmem */
525 
526 #endif /* LIBPMEMOBJ_CPP_TRANSACTION_HPP */
int count
The number of active exceptions.
Definition: transaction.hpp:290
static int add_lock() noexcept
Method ending the recursive algorithm.
Definition: transaction.hpp:516
The non-template pool base class.
Definition: pool.hpp:67
Internal class for counting active exceptions.
Definition: transaction.hpp:260
~manual() noexcept
Destructor.
Definition: transaction.hpp:131
~automatic() noexcept(false)
Destructor.
Definition: transaction.hpp:220
C++ transaction handler class.
Definition: transaction.hpp:72
Custom transaction error class.
Definition: pexceptions.hpp:114
A persistent version of concurrent hash map implementation Ref: https://arxiv.org/abs/1509....
Definition: concurrent_hash_map.hpp:74
static void snapshot(const T *addr, size_t num=1)
Takes a “snapshot” of given elements of type T number (1 by default), located at the given address pt...
Definition: transaction.hpp:475
C++ manual scope transaction class.
Definition: transaction.hpp:92
PMEMobj pool class.
Definition: persistent_ptr.hpp:59
manual & operator=(const manual &p)=delete
Deleted assignment operator.
PMEMobjpool * handle() noexcept
Gets the C style handle to the pool.
Definition: pool.hpp:398
manual(obj::pool_base &pop, L &... locks)
RAII constructor with pmem resident locks.
Definition: transaction.hpp:108
Commonly used functionality.
Custom exceptions.
static void commit()
Manually commit a transaction.
Definition: transaction.hpp:345
static void abort(int err)
Manually abort the current transaction.
Definition: transaction.hpp:325
~transaction() noexcept=delete
Default destructor.
C++ automatic scope transaction class.
Definition: transaction.hpp:185
Custom transaction error class.
Definition: pexceptions.hpp:63
static int add_lock(L &lock, Locks &... locks) noexcept
Recursively add locks to the active transaction.
Definition: transaction.hpp:501
bool new_uncaught_exception()
Notifies is a new exception is being handled.
Definition: transaction.hpp:281
automatic(obj::pool_base &pop, L &... locks)
RAII constructor with pmem resident locks.
Definition: transaction.hpp:205
uncaught_exception_counter()
Default constructor.
Definition: transaction.hpp:268
Resides on pmem class.
Definition: p.hpp:64
C++ pmemobj pool.
static void run(pool_base &pool, std::function< void()> tx, Locks &... locks)
Execute a closure-like transaction and lock locks.
Definition: transaction.hpp:398
automatic & operator=(const automatic &p)=delete
Deleted assignment operator.