NVML C++ bindings  0.1 experimental
This is the C++ bindings documentation for NVML's libpmemobj.
Classes | Public Member Functions | List of all members
nvml::obj::transaction::automatic Class Reference

C++ automatic scope transaction class. More...

#include <libpmemobj++/transaction.hpp>

Collaboration diagram for nvml::obj::transaction::automatic:
Collaboration graph
[legend]

Classes

class  uncaught_exception_counter
 Internal class for counting active exceptions. More...
 

Public Member Functions

template<typename... L>
 automatic (obj::pool_base &pop, L &...locks)
 RAII constructor with pmem resident locks. More...
 
 ~automatic () noexcept
 Destructor. More...
 
 automatic (const automatic &p)=delete
 Deleted copy constructor.
 
 automatic (const automatic &&p)=delete
 Deleted move constructor.
 
automaticoperator= (const automatic &p)=delete
 Deleted assignment operator.
 
automaticoperator= (automatic &&p)=delete
 Deleted move assignment operator.
 

Detailed Description

C++ automatic scope transaction class.

This class is one of pmemobj transaction handlers. All operations between creating and destroying the transaction object are treated as performed in a transaction block and can be rolled back. If you have a C++17 compliant compiler, the automatic transaction will commit and abort automatically depending on the context of object destruction.

The locks are held for the entire duration of the transaction. They are released at the end of the scope, so within the catch block, they are already unlocked. If the cleanup action requires access to data within a critical section, the locks have to be manually acquired once again.

The typical usage example would be:

using namespace nvml::obj;
int
automatic_tx_example()
{
// pool root structure
struct root {
mutex pmutex;
shared_mutex shared_pmutex;
p<int> count;
persistent_ptr<root> another_root;
};
// create a pmemobj pool
auto pop = pool<root>::create("poolfile", "layout", PMEMOBJ_MIN_POOL,
S_IWUSR | S_IRUSR);
auto proot = pop.get_root();
try {
transaction::automatic tx(pop, proot->pmutex,
proot->shared_pmutex);
// atomically allocate objects
proot->another_root = make_persistent<root>();
// atomically modify objects
proot->count++;
// manual transaction commit is no longer necessary
} catch (nvml::transaction_error &te) {
// an internal transaction error occurred, tx aborted
// reacquire locks if necessary
} catch (...) {
// some other exception thrown, tx aborted
// reacquire locks if necessary
}
// In complex cases with library calls, remember to check the status of
// the previous transaction.
return transaction::get_last_tx_error();
}

Constructor & Destructor Documentation

template<typename... L>
nvml::obj::transaction::automatic::automatic ( obj::pool_base pop,
L &...  locks 
)
inline

RAII constructor with pmem resident locks.

Start pmemobj transaction and add list of locks to new transaction. The list of locks may be empty.

This class is only available if the __cpp_lib_uncaught_exceptions feature macro is defined. This is a C++17 feature.

Parameters
[in,out]poppool object.
[in,out]lockslocks of obj::mutex or obj::shared_mutex type.
Exceptions
nvml::transaction_errorwhen pmemobj_tx_begin function or locks adding failed.
nvml::obj::transaction::automatic::~automatic ( )
inlinenoexcept

Destructor.

End pmemobj transaction. Depending on the context of object destruction, the transaction will automatically be either committed or aborted.


The documentation for this class was generated from the following file: