PMDK C++ bindings
1.9.1
This is the C++ bindings documentation for PMDK's libpmemobj.
|
Go to the documentation of this file.
38 #ifndef LIBPMEMOBJ_CPP_TRANSACTION_HPP
39 #define LIBPMEMOBJ_CPP_TRANSACTION_HPP
49 #include <libpmemobj/tx_base.h>
115 template <
typename... L>
120 if (pmemobj_tx_stage() == TX_STAGE_NONE) {
121 ret = pmemobj_tx_begin(pop.
handle(),
nullptr,
124 nullptr, TX_PARAM_NONE);
126 ret = pmemobj_tx_begin(pop.
handle(),
nullptr,
132 "failed to start transaction")
133 .with_pmemobj_errormsg();
138 pmemobj_tx_abort(EINVAL);
139 (void)pmemobj_tx_end();
141 "failed to add lock")
142 .with_pmemobj_errormsg();
156 if (pmemobj_tx_stage() == TX_STAGE_WORK)
157 pmemobj_tx_abort(ECANCELED);
159 (void)pmemobj_tx_end();
187 #if __cpp_lib_uncaught_exceptions || _MSC_VER >= 1900
226 template <
typename... L>
228 : tx_worker(pop, locks...)
249 if (pmemobj_tx_stage() == TX_STAGE_WORK)
252 else if (pmemobj_tx_stage() == TX_STAGE_ONABORT ||
253 (pmemobj_tx_stage() == TX_STAGE_FINALLY &&
254 pmemobj_tx_errno() != 0))
256 "Transaction aborted");
292 :
count(std::uncaught_exceptions())
306 return std::uncaught_exceptions() > this->
count;
350 if (pmemobj_tx_stage() != TX_STAGE_WORK)
353 pmemobj_tx_abort(err);
355 std::to_string(err));
371 if (pmemobj_tx_stage() != TX_STAGE_WORK)
380 return pmemobj_tx_errno();
383 POBJ_CPP_DEPRECATED
static int
384 get_last_tx_error() noexcept
386 return transaction::error();
420 template <
typename... Locks>
426 if (pmemobj_tx_stage() == TX_STAGE_NONE) {
427 ret = pmemobj_tx_begin(
pool.handle(),
nullptr,
432 ret = pmemobj_tx_begin(
pool.handle(),
nullptr,
438 "failed to start transaction")
439 .with_pmemobj_errormsg();
444 pmemobj_tx_abort(err);
445 (void)pmemobj_tx_end();
447 "failed to add a lock to the transaction")
448 .with_pmemobj_errormsg();
454 (void)pmemobj_tx_end();
458 if (pmemobj_tx_stage() == TX_STAGE_WORK)
459 pmemobj_tx_abort(ECANCELED);
462 (void)pmemobj_tx_end();
466 auto stage = pmemobj_tx_stage();
468 if (
stage == TX_STAGE_WORK) {
470 }
else if (
stage == TX_STAGE_ONABORT) {
471 (void)pmemobj_tx_end();
473 }
else if (
stage == TX_STAGE_NONE) {
475 "transaction ended prematurely");
478 (void)pmemobj_tx_end();
481 template <
typename... Locks>
482 POBJ_CPP_DEPRECATED
static void
483 exec_tx(
pool_base &
pool, std::function<
void()> tx, Locks &... locks)
510 typename std::enable_if<LIBPMEMOBJ_CPP_IS_TRIVIALLY_COPYABLE(T),
511 T>::type * =
nullptr>
515 if (TX_STAGE_WORK != pmemobj_tx_stage())
517 "wrong stage for taking a snapshot.");
519 if (pmemobj_tx_add_range_direct(addr,
sizeof(*addr) * num)) {
522 "Could not take a snapshot of given memory range.")
523 .with_pmemobj_errormsg();
526 "Could not take a snapshot of given memory range.")
527 .with_pmemobj_errormsg();
536 work = TX_STAGE_WORK,
537 oncommit = TX_STAGE_ONCOMMIT,
538 onabort = TX_STAGE_ONABORT,
540 finally = TX_STAGE_FINALLY,
559 if (pmemobj_tx_stage() != TX_STAGE_WORK)
561 "register_callback must be called during a transaction");
563 get_tx_data()->callbacks[
static_cast<size_t>(stg)].push_back(
580 template <
typename L,
typename... Locks>
585 pmemobj_tx_lock(lock.lock_type(), lock.native_handle());
602 using callbacks_list_type = std::vector<std::function<void()>>;
603 using callbacks_map_type =
604 std::array<callbacks_list_type, MAX_TX_STAGE>;
611 c_callback(PMEMobjpool *pop,
enum pobj_tx_stage obj_stage,
void *arg)
618 if (obj_stage == TX_STAGE_NONE)
621 auto *data =
static_cast<tx_data *
>(pmemobj_tx_get_user_data());
625 for (
auto &cb : data->callbacks[obj_stage])
632 if (obj_stage == TX_STAGE_FINALLY) {
634 pmemobj_tx_set_user_data(NULL);
643 callbacks_map_type callbacks;
653 auto *data =
static_cast<tx_data *
>(pmemobj_tx_get_user_data());
654 if (data ==
nullptr) {
656 pmemobj_tx_set_user_data(data);
automatic(obj::pool_base &pop, L &... locks)
RAII constructor with pmem resident locks.
Definition: transaction.hpp:227
int count
The number of active exceptions.
Definition: transaction.hpp:313
static void register_callback(stage stg, std::function< void()> cb)
Registers callback to be called on specified stage for the transaction.
Definition: transaction.hpp:557
static void commit()
Manually commit a transaction.
Definition: transaction.hpp:369
manual & operator=(manual &&p)=delete
Deleted move assignment operator.
Custom transaction error class.
Definition: pexceptions.hpp:94
Persistent memory namespace.
Definition: allocation_flag.hpp:44
Custom out of memory error class.
Definition: pexceptions.hpp:154
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:582
~manual() noexcept
Destructor.
Definition: transaction.hpp:153
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:513
Resides on pmem class.
Definition: p.hpp:64
manual(obj::pool_base &pop, L &... locks)
RAII constructor with pmem resident locks.
Definition: transaction.hpp:116
stage
Possible stages of a transaction, for every stage one or more callbacks can be registered.
Definition: transaction.hpp:535
static void run(pool_base &pool, std::function< void()> tx, Locks &... locks)
Execute a closure-like transaction and lock locks.
Definition: transaction.hpp:422
static int add_lock() noexcept
Method ending the recursive algorithm.
Definition: transaction.hpp:597
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:642
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:611
PMEMobjpool * handle() noexcept
Gets the C style handle to the pool.
Definition: pool.hpp:427
automatic(const automatic &p)=delete
Deleted copy constructor.
C++ automatic scope transaction class.
Definition: transaction.hpp:207
Internal class for counting active exceptions.
Definition: transaction.hpp:283
bool new_uncaught_exception()
Notifies is a new exception is being handled.
Definition: transaction.hpp:304
uncaught_exception_counter()
Default constructor.
Definition: transaction.hpp:291
PMEMobj pool class.
Definition: pool.hpp:491
manual & operator=(const manual &p)=delete
Deleted assignment operator.
~transaction() noexcept=delete
Default destructor.
~automatic() noexcept(false)
Destructor.
Definition: transaction.hpp:242
manual(const manual &p)=delete
Deleted copy constructor.
C++ transaction handler class.
Definition: transaction.hpp:80
Custom transaction error class.
Definition: pexceptions.hpp:192
Custom transaction error class.
Definition: pexceptions.hpp:202
The non-template pool base class.
Definition: pool.hpp:75
automatic(const automatic &&p)=delete
Deleted move constructor.
static void abort(int err)
Manually abort the current transaction.
Definition: transaction.hpp:348
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:651
C++ manual scope transaction class.
Definition: transaction.hpp:100
manual(const manual &&p)=delete
Deleted move constructor.