PMDK C++ bindings
1.10
This is the C++ bindings documentation for PMDK's libpmemobj.
|
Go to the documentation of this file.
9 #ifndef LIBPMEMOBJ_CPP_TRANSACTION_HPP
10 #define LIBPMEMOBJ_CPP_TRANSACTION_HPP
20 #include <libpmemobj/tx_base.h>
86 template <
typename... L>
91 if (pmemobj_tx_stage() == TX_STAGE_NONE) {
92 ret = pmemobj_tx_begin(pop.
handle(),
nullptr,
95 nullptr, TX_PARAM_NONE);
97 ret = pmemobj_tx_begin(pop.
handle(),
nullptr,
103 "failed to start transaction")
104 .with_pmemobj_errormsg();
109 pmemobj_tx_abort(EINVAL);
110 (void)pmemobj_tx_end();
112 "failed to add lock")
113 .with_pmemobj_errormsg();
127 if (pmemobj_tx_stage() == TX_STAGE_WORK)
128 pmemobj_tx_abort(ECANCELED);
130 (void)pmemobj_tx_end();
158 #if __cpp_lib_uncaught_exceptions || _MSC_VER >= 1900
197 template <
typename... L>
199 : tx_worker(pop, locks...)
220 if (pmemobj_tx_stage() == TX_STAGE_WORK)
223 else if (pmemobj_tx_stage() == TX_STAGE_ONABORT ||
224 (pmemobj_tx_stage() == TX_STAGE_FINALLY &&
225 pmemobj_tx_errno() != 0))
227 "Transaction aborted");
263 :
count(std::uncaught_exceptions())
277 return std::uncaught_exceptions() > this->
count;
321 if (pmemobj_tx_stage() != TX_STAGE_WORK)
324 pmemobj_tx_abort(err);
326 std::to_string(err));
342 if (pmemobj_tx_stage() != TX_STAGE_WORK)
351 return pmemobj_tx_errno();
354 POBJ_CPP_DEPRECATED
static int
355 get_last_tx_error() noexcept
357 return transaction::error();
391 template <
typename... Locks>
397 if (pmemobj_tx_stage() == TX_STAGE_NONE) {
398 ret = pmemobj_tx_begin(
pool.handle(),
nullptr,
403 ret = pmemobj_tx_begin(
pool.handle(),
nullptr,
409 "failed to start transaction")
410 .with_pmemobj_errormsg();
415 pmemobj_tx_abort(err);
416 (void)pmemobj_tx_end();
418 "failed to add a lock to the transaction")
419 .with_pmemobj_errormsg();
425 (void)pmemobj_tx_end();
429 if (pmemobj_tx_stage() == TX_STAGE_WORK)
430 pmemobj_tx_abort(ECANCELED);
433 (void)pmemobj_tx_end();
437 auto stage = pmemobj_tx_stage();
439 if (
stage == TX_STAGE_WORK) {
441 }
else if (
stage == TX_STAGE_ONABORT) {
442 (void)pmemobj_tx_end();
444 }
else if (
stage == TX_STAGE_NONE) {
446 "transaction ended prematurely");
449 (void)pmemobj_tx_end();
452 template <
typename... Locks>
453 POBJ_CPP_DEPRECATED
static void
454 exec_tx(
pool_base &
pool, std::function<
void()> tx, Locks &... locks)
481 typename std::enable_if<LIBPMEMOBJ_CPP_IS_TRIVIALLY_COPYABLE(T),
482 T>::type * =
nullptr>
486 if (TX_STAGE_WORK != pmemobj_tx_stage())
488 "wrong stage for taking a snapshot.");
490 if (pmemobj_tx_add_range_direct(addr,
sizeof(*addr) * num)) {
493 "Could not take a snapshot of given memory range.")
494 .with_pmemobj_errormsg();
497 "Could not take a snapshot of given memory range.")
498 .with_pmemobj_errormsg();
507 work = TX_STAGE_WORK,
508 oncommit = TX_STAGE_ONCOMMIT,
509 onabort = TX_STAGE_ONABORT,
511 finally = TX_STAGE_FINALLY,
530 if (pmemobj_tx_stage() != TX_STAGE_WORK)
532 "register_callback must be called during a transaction");
534 get_tx_data()->callbacks[
static_cast<size_t>(stg)].push_back(
551 template <
typename L,
typename... Locks>
556 pmemobj_tx_lock(lock.lock_type(), lock.native_handle());
573 using callbacks_list_type = std::vector<std::function<void()>>;
574 using callbacks_map_type =
575 std::array<callbacks_list_type, MAX_TX_STAGE>;
582 c_callback(PMEMobjpool *pop,
enum pobj_tx_stage obj_stage,
void *arg)
589 if (obj_stage == TX_STAGE_NONE)
592 auto *data =
static_cast<tx_data *
>(pmemobj_tx_get_user_data());
596 for (
auto &cb : data->callbacks[obj_stage])
603 if (obj_stage == TX_STAGE_FINALLY) {
605 pmemobj_tx_set_user_data(NULL);
614 callbacks_map_type callbacks;
624 auto *data =
static_cast<tx_data *
>(pmemobj_tx_get_user_data());
625 if (data ==
nullptr) {
627 pmemobj_tx_set_user_data(data);
automatic(obj::pool_base &pop, L &... locks)
RAII constructor with pmem resident locks.
Definition: transaction.hpp:198
int count
The number of active exceptions.
Definition: transaction.hpp:284
static void register_callback(stage stg, std::function< void()> cb)
Registers callback to be called on specified stage for the transaction.
Definition: transaction.hpp:528
static void commit()
Manually commit a transaction.
Definition: transaction.hpp:340
manual & operator=(manual &&p)=delete
Deleted move assignment operator.
Custom transaction error class.
Definition: pexceptions.hpp:63
Persistent memory namespace.
Definition: allocation_flag.hpp:15
Custom out of memory error class.
Definition: pexceptions.hpp:120
automatic & operator=(const automatic &p)=delete
Deleted assignment operator.
Commonly used functionality.
static int add_lock(L &lock, Locks &... locks) noexcept
Recursively add locks to the active transaction.
Definition: transaction.hpp:553
~manual() noexcept
Destructor.
Definition: transaction.hpp:124
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:484
Resides on pmem class.
Definition: p.hpp:35
manual(obj::pool_base &pop, L &... locks)
RAII constructor with pmem resident locks.
Definition: transaction.hpp:87
stage
Possible stages of a transaction, for every stage one or more callbacks can be registered.
Definition: transaction.hpp:506
static void run(pool_base &pool, std::function< void()> tx, Locks &... locks)
Execute a closure-like transaction and lock locks.
Definition: transaction.hpp:393
static int add_lock() noexcept
Method ending the recursive algorithm.
Definition: transaction.hpp:568
automatic & operator=(automatic &&p)=delete
Deleted move assignment operator.
This data is stored along with the pmemobj transaction data using pmemobj_tx_set_data().
Definition: transaction.hpp:613
static void c_callback(PMEMobjpool *pop, enum pobj_tx_stage obj_stage, void *arg)
C-style function which is passed as callback to pmemobj_begin.
Definition: transaction.hpp:582
PMEMobjpool * handle() noexcept
Gets the C style handle to the pool.
Definition: pool.hpp:398
automatic(const automatic &p)=delete
Deleted copy constructor.
C++ automatic scope transaction class.
Definition: transaction.hpp:178
Internal class for counting active exceptions.
Definition: transaction.hpp:254
bool new_uncaught_exception()
Notifies is a new exception is being handled.
Definition: transaction.hpp:275
uncaught_exception_counter()
Default constructor.
Definition: transaction.hpp:262
PMEMobj pool class.
Definition: pool.hpp:462
manual & operator=(const manual &p)=delete
Deleted assignment operator.
~transaction() noexcept=delete
Default destructor.
~automatic() noexcept(false)
Destructor.
Definition: transaction.hpp:213
manual(const manual &p)=delete
Deleted copy constructor.
C++ transaction handler class.
Definition: transaction.hpp:51
Custom transaction error class.
Definition: pexceptions.hpp:158
Custom transaction error class.
Definition: pexceptions.hpp:168
The non-template pool base class.
Definition: pool.hpp:46
automatic(const automatic &&p)=delete
Deleted move constructor.
static void abort(int err)
Manually abort the current transaction.
Definition: transaction.hpp:319
static tx_data * get_tx_data()
Gets tx user data from pmemobj or creates it if this is a first call to this function inside a transa...
Definition: transaction.hpp:622
C++ manual scope transaction class.
Definition: transaction.hpp:71
manual(const manual &&p)=delete
Deleted move constructor.