PMDK C++ bindings
1.12.1-rc1
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>
22 #ifndef LIBPMEMOBJ_CPP_FLAT_TX_USE_FAILURE_RETURN
23 #define LIBPMEMOBJ_CPP_FLAT_TX_USE_FAILURE_RETURN true
37 static constexpr
bool value = LIBPMEMOBJ_CPP_IS_TRIVIALLY_COPYABLE(T);
43 template <
bool is_flat>
61 template <
typename... L>
66 nested = pmemobj_tx_stage() == TX_STAGE_WORK;
69 ret = pmemobj_tx_begin(pop.
handle(),
nullptr,
71 }
else if (pmemobj_tx_stage() == TX_STAGE_NONE) {
72 ret = pmemobj_tx_begin(
73 pop.
handle(),
nullptr, TX_PARAM_CB,
78 "Cannot start transaction in stage different than WORK or NONE");
83 "failed to start transaction")
84 .with_pmemobj_errormsg();
89 pmemobj_tx_abort(EINVAL);
90 (void)pmemobj_tx_end();
93 .with_pmemobj_errormsg();
96 set_failure_behavior();
109 if (pmemobj_tx_stage() == TX_STAGE_WORK) {
110 if (is_flat && nested)
113 pmemobj_tx_abort(ECANCELED);
116 (void)pmemobj_tx_end();
122 manual(
const manual &p) =
delete;
127 manual(
const manual &&p) =
delete;
132 manual &operator=(
const manual &p) =
delete;
137 manual &operator=(manual &&p) =
delete;
140 template <
bool cnd = is_flat
141 &&LIBPMEMOBJ_CPP_FLAT_TX_USE_FAILURE_RETURN>
142 typename std::enable_if<cnd>::type
143 set_failure_behavior()
145 pmemobj_tx_set_failure_behavior(POBJ_TX_FAILURE_RETURN);
148 template <
bool cnd = is_flat
149 &&LIBPMEMOBJ_CPP_FLAT_TX_USE_FAILURE_RETURN>
150 typename std::enable_if<!cnd>::type
151 set_failure_behavior()
162 #if __cpp_lib_uncaught_exceptions || _MSC_VER >= 1900
182 template <
typename... L>
184 : tx_worker(pop, locks...)
198 ~automatic() noexcept(
false)
201 if (exceptions.new_uncaught_exception())
205 if (pmemobj_tx_stage() == TX_STAGE_WORK)
208 else if (pmemobj_tx_stage() == TX_STAGE_ONABORT ||
209 (pmemobj_tx_stage() == TX_STAGE_FINALLY &&
210 pmemobj_tx_errno() != 0))
212 "Transaction aborted");
218 automatic(
const automatic &p) =
delete;
223 automatic(
const automatic &&p) =
delete;
228 automatic &operator=(
const automatic &p) =
delete;
233 automatic &operator=(automatic &&p) =
delete;
248 :
count(std::uncaught_exceptions())
262 return std::uncaught_exceptions() > this->
count;
306 if (pmemobj_tx_stage() != TX_STAGE_WORK)
309 pmemobj_tx_abort(err);
311 std::to_string(err));
327 if (pmemobj_tx_stage() != TX_STAGE_WORK)
336 return pmemobj_tx_errno();
339 POBJ_CPP_DEPRECATED
static int
340 get_last_tx_error() noexcept
376 template <
typename... Locks>
380 manual worker(pool, locks...);
384 auto stage = pmemobj_tx_stage();
386 if (
stage == TX_STAGE_WORK) {
388 }
else if (
stage == TX_STAGE_ONABORT) {
390 }
else if (
stage == TX_STAGE_NONE) {
392 "transaction ended prematurely");
396 template <
typename... Locks>
397 POBJ_CPP_DEPRECATED
static void
401 run(pool, tx, locks...);
424 template <
typename T,
425 typename std::enable_if<detail::can_do_snapshot<T>::value,
426 T>::type * =
nullptr>
430 if (TX_STAGE_WORK != pmemobj_tx_stage())
432 "wrong stage for taking a snapshot.");
434 if (pmemobj_tx_add_range_direct(addr,
sizeof(*addr) * num)) {
437 "Could not take a snapshot of given memory range.")
438 .with_pmemobj_errormsg();
441 "Could not take a snapshot of given memory range.")
442 .with_pmemobj_errormsg();
451 work = TX_STAGE_WORK,
452 oncommit = TX_STAGE_ONCOMMIT,
453 onabort = TX_STAGE_ONABORT,
455 finally = TX_STAGE_FINALLY,
474 if (pmemobj_tx_stage() != TX_STAGE_WORK)
476 "register_callback must be called during a transaction");
478 get_tx_data()->callbacks[
static_cast<size_t>(stg)].push_back(
495 template <
typename L,
typename... Locks>
500 pmemobj_tx_lock(lock.lock_type(), lock.native_handle());
517 using callbacks_list_type = std::vector<std::function<void()>>;
518 using callbacks_map_type =
519 std::array<callbacks_list_type, MAX_TX_STAGE>;
526 c_callback(PMEMobjpool *pop,
enum pobj_tx_stage obj_stage,
void *arg)
533 if (obj_stage == TX_STAGE_NONE)
536 auto *data =
static_cast<tx_data *
>(pmemobj_tx_get_user_data());
540 for (
auto &cb : data->callbacks[obj_stage])
547 if (obj_stage == TX_STAGE_FINALLY) {
549 pmemobj_tx_set_user_data(NULL);
558 callbacks_map_type callbacks;
568 auto *data =
static_cast<tx_data *
>(pmemobj_tx_get_user_data());
569 if (data ==
nullptr) {
571 pmemobj_tx_set_user_data(data);
633 #if __cpp_lib_uncaught_exceptions || _MSC_VER >= 1900
686 template <
typename... Locks>
762 #if __cpp_lib_uncaught_exceptions || _MSC_VER >= 1900
815 template <
typename... Locks>
833 #ifdef LIBPMEMOBJ_CPP_USE_FLAT_TRANSACTION
Custom transaction error class.
Definition: pexceptions.hpp:65
Persistent memory namespace.
Definition: allocation_flag.hpp:15
int count
The number of active exceptions.
Definition: transaction.hpp:269
Custom out of memory error class.
Definition: pexceptions.hpp:125
static void register_callback(stage stg, std::function< void()> cb)
Registers callback to be called on specified stage for the transaction.
Definition: transaction.hpp:472
static void run(obj::pool_base &pool, std::function< void()> tx, Locks &... locks)
Execute a closure-like transaction and lock locks.
Definition: transaction.hpp:378
Commonly used functionality.
static int add_lock() noexcept
Method ending the recursive algorithm.
Definition: transaction.hpp:512
static void abort(int err)
Manually abort the current transaction.
Definition: transaction.hpp:304
typename detail::transaction_base< true >::automatic automatic
C++ automatic scope transaction class.
Definition: transaction.hpp:782
static void run(obj::pool_base &pool, std::function< void()> tx, Locks &... locks)
Execute a closure-like transaction and lock locks.
Definition: transaction.hpp:688
This data is stored along with the pmemobj transaction data using pmemobj_tx_set_data().
Definition: transaction.hpp:557
Common functionality for basic_transaction and flat_transaction.
Definition: transaction.hpp:44
bool new_uncaught_exception()
Notifies is a new exception is being handled.
Definition: transaction.hpp:260
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:526
~transaction_base() noexcept=delete
Default destructor.
stage
Possible stages of a transaction, for every stage one or more callbacks can be registered.
Definition: transaction.hpp:450
A structure that checks if it is possible to snapshot the specified memory.
Definition: transaction.hpp:36
C++ transaction handler class.
Definition: transaction.hpp:607
Internal class for counting active exceptions.
Definition: transaction.hpp:239
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:566
PMEMobjpool * handle() noexcept
Gets the C style handle to the pool.
Definition: pool.hpp:398
uncaught_exception_counter()
Default constructor.
Definition: transaction.hpp:247
static void commit()
Manually commit a transaction.
Definition: transaction.hpp:325
PMEMobj pool class.
Definition: pool.hpp:465
~basic_transaction() noexcept=delete
Default destructor.
typename detail::transaction_base< false >::manual manual
C++ manual scope transaction class.
Definition: transaction.hpp:627
static int add_lock(L &lock, Locks &... locks) noexcept
Recursively add locks to the active transaction.
Definition: transaction.hpp:497
C++ flat transaction handler class.
Definition: transaction.hpp:735
typename detail::transaction_base< false >::automatic automatic
C++ automatic scope transaction class.
Definition: transaction.hpp:653
Custom transaction error class.
Definition: pexceptions.hpp:163
~flat_transaction() noexcept=delete
Default destructor.
Custom transaction error class.
Definition: pexceptions.hpp:173
The non-template pool base class.
Definition: pool.hpp:46
typename detail::transaction_base< true >::manual manual
C++ manual scope transaction class.
Definition: transaction.hpp:756
static void run(obj::pool_base &pool, std::function< void()> tx, Locks &... locks)
Execute a closure-like transaction and lock locks.
Definition: transaction.hpp:817
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:428