.\" Automatically generated by Pandoc 2.2.1 .\" .TH "PMEMOBJ_TX_BEGIN" "3" "2019-02-19" "PMDK - pmemobj API version 2.3" "PMDK Programmer's Manual" .hy .\" Copyright 2014-2019, Intel Corporation .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" .\" * Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" .\" * Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in .\" the documentation and/or other materials provided with the .\" distribution. .\" .\" * Neither the name of the copyright holder nor the names of its .\" contributors may be used to endorse or promote products derived .\" from this software without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS .\" "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT .\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR .\" A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT .\" OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, .\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT .\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE .\" OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .SH NAME .PP \f[B]pmemobj_tx_stage\f[](), .PP \f[B]pmemobj_tx_begin\f[](), \f[B]pmemobj_tx_lock\f[](), \f[B]pmemobj_tx_abort\f[](), \f[B]pmemobj_tx_commit\f[](), \f[B]pmemobj_tx_end\f[](), \f[B]pmemobj_tx_errno\f[](), \f[B]pmemobj_tx_process\f[](), .PP \f[B]TX_BEGIN_PARAM\f[](), \f[B]TX_BEGIN_CB\f[](), \f[B]TX_BEGIN\f[](), \f[B]TX_ONABORT\f[], \f[B]TX_ONCOMMIT\f[], \f[B]TX_FINALLY\f[], \f[B]TX_END\f[] \- transactional object manipulation .SH SYNOPSIS .IP .nf \f[C] #include\ enum\ tx_stage\ pmemobj_tx_stage(void); int\ pmemobj_tx_begin(PMEMobjpool\ *pop,\ jmp_buf\ *env,\ enum\ pobj_tx_param,\ ...); int\ pmemobj_tx_lock(enum\ tx_lock\ lock_type,\ void\ *lockp); void\ pmemobj_tx_abort(int\ errnum); void\ pmemobj_tx_commit(void); int\ pmemobj_tx_end(void); int\ pmemobj_tx_errno(void); void\ pmemobj_tx_process(void); TX_BEGIN_PARAM(PMEMobjpool\ *pop,\ ...) TX_BEGIN_CB(PMEMobjpool\ *pop,\ cb,\ arg,\ ...) TX_BEGIN(PMEMobjpool\ *pop) TX_ONABORT TX_ONCOMMIT TX_FINALLY TX_END \f[] .fi .SH DESCRIPTION .PP The non\-transactional functions and macros described in \f[B]pmemobj_alloc\f[](3), \f[B]pmemobj_list_insert\f[](3) and \f[B]POBJ_LIST_HEAD\f[](3) only guarantee the atomicity of a single operation on an object. In case of more complex changes involving multiple operations on an object, or allocation and modification of multiple objects, data consistency and fail\-safety may be provided only by using \f[I]atomic transactions\f[]. .PP A transaction is defined as series of operations on persistent memory objects that either all occur, or nothing occurs. In particular, if the execution of a transaction is interrupted by a power failure or a system crash, it is guaranteed that after system restart, all the changes made as a part of the uncompleted transaction will be rolled back, restoring the consistent state of the memory pool from the moment when the transaction was started. .PP Note that transactions do not provide atomicity with respect to other threads. All the modifications performed within the transactions are immediately visible to other threads. Therefore it is the responsibility of the application to implement a proper thread synchronization mechanism. .PP Each thread may have only one transaction open at a time, but that transaction may be nested. Nested transactions are flattened. Committing the nested transaction does not commit the outer transaction; however, errors in the nested transaction are propagated up to the outermost level, resulting in the interruption of the entire transaction. .PP Each transaction is visible only for the thread that started it. No other threads can add operations, commit or abort the transaction initiated by another thread. Multiple threads may have transactions open on a given memory pool at the same time. .PP Please see the \f[B]CAVEATS\f[] section below for known limitations of the transactional API. .PP The \f[B]pmemobj_tx_stage\f[]() function returns the current \f[I]transaction stage\f[] for a thread. Stages are changed only by the \f[B]pmemobj_tx_*\f[]() functions. Transaction stages are defined as follows: .IP \[bu] 2 \f[B]TX_STAGE_NONE\f[] \- no open transaction in this thread .IP \[bu] 2 \f[B]TX_STAGE_WORK\f[] \- transaction in progress .IP \[bu] 2 \f[B]TX_STAGE_ONCOMMIT\f[] \- successfully committed .IP \[bu] 2 \f[B]TX_STAGE_ONABORT\f[] \- starting the transaction failed or transaction aborted .IP \[bu] 2 \f[B]TX_STAGE_FINALLY\f[] \- ready for clean up .PP The \f[B]pmemobj_tx_begin\f[]() function starts a new transaction in the current thread. If called within an open transaction, it starts a nested transaction. The caller may use the \f[I]env\f[] argument to provide a pointer to a calling environment to be restored in case of transaction abort. This information must be provided by the caller using the \f[B]setjmp\f[](3) macro. .PP A new transaction may be started only if the current stage is \f[B]TX_STAGE_NONE\f[] or \f[B]TX_STAGE_WORK\f[]. If successful, the \f[I]transaction stage\f[] changes to \f[B]TX_STAGE_WORK\f[]. Otherwise, the stage is changed to \f[B]TX_STAGE_ONABORT\f[]. .PP Optionally, a list of parameters for the transaction may be provided. Each parameter consists of a type followed by a type\-specific number of values. Currently there are 4 types: .IP \[bu] 2 \f[B]TX_PARAM_NONE\f[], used as a termination marker. No following value. .IP \[bu] 2 \f[B]TX_PARAM_MUTEX\f[], followed by one value, a pmem\-resident PMEMmutex .IP \[bu] 2 \f[B]TX_PARAM_RWLOCK\f[], followed by one value, a pmem\-resident PMEMrwlock .IP \[bu] 2 \f[B]TX_PARAM_CB\f[], followed by two values: a callback function of type \f[I]pmemobj_tx_callback\f[], and a void pointer .PP Using \f[B]TX_PARAM_MUTEX\f[] or \f[B]TX_PARAM_RWLOCK\f[] causes the specified lock to be acquired at the beginning of the transaction. \f[B]TX_PARAM_RWLOCK\f[] acquires the lock for writing. It is guaranteed that \f[B]pmemobj_tx_begin\f[]() will acquire all locks prior to successful completion, and they will be held by the current thread until the outermost transaction is finished. Locks are taken in order from left to right. To avoid deadlocks, the user is responsible for proper lock ordering. .PP \f[B]TX_PARAM_CB\f[] registers the specified callback function to be executed at each transaction stage. For \f[B]TX_STAGE_WORK\f[], the callback is executed prior to commit. For all other stages, the callback is executed as the first operation after a stage change. It will also be called after each transaction; in this case the \f[I]stage\f[] parameter will be set to \f[B]TX_STAGE_NONE\f[]. \f[I]pmemobj_tx_callback\f[] must be compatible with: .PP \f[C]void\ func(PMEMobjpool\ *pop,\ enum\ pobj_tx_stage\ stage,\ void\ *arg)\f[] .PP \f[I]pop\f[] is a pool identifier used in \f[B]pmemobj_tx_begin\f[](), \f[I]stage\f[] is a current transaction stage and \f[I]arg\f[] is the second parameter of \f[B]TX_PARAM_CB\f[]. Without considering transaction nesting, this mechanism can be considered an alternative method for executing code between stages (instead of \f[B]TX_ONCOMMIT\f[], \f[B]TX_ONABORT\f[], etc). However, there are 2 significant differences when nested transactions are used: .IP \[bu] 2 The registered function is executed only in the outermost transaction, even if registered in an inner transaction. .IP \[bu] 2 There can be only one callback in the entire transaction, that is, the callback cannot be changed in an inner transaction. .PP Note that \f[B]TX_PARAM_CB\f[] does not replace the \f[B]TX_ONCOMMIT\f[], \f[B]TX_ONABORT\f[], etc. macros. They can be used together: the callback will be executed \f[I]before\f[] a \f[B]TX_ONCOMMIT\f[], \f[B]TX_ONABORT\f[], etc. section. .PP \f[B]TX_PARAM_CB\f[] can be used when the code dealing with transaction stage changes is shared between multiple users or when it must be executed only in the outer transaction. For example it can be very useful when the application must synchronize persistent and transient state. .PP The \f[B]pmemobj_tx_lock\f[]() function acquires the lock \f[I]lockp\f[] of type \f[I]lock_type\f[] and adds it to the current transaction. \f[I]lock_type\f[] may be \f[B]TX_LOCK_MUTEX\f[] or \f[B]TX_LOCK_RWLOCK\f[]; \f[I]lockp\f[] must be of type \f[I]PMEMmutex\f[] or \f[I]PMEMrwlock\f[], respectively. If \f[I]lock_type\f[] is \f[B]TX_LOCK_RWLOCK\f[] the lock is acquired for writing. If the lock is not successfully acquired, the stage is changed to \f[B]TX_STAGE_ONABORT\f[]. This function must be called during \f[B]TX_STAGE_WORK\f[]. .PP \f[B]pmemobj_tx_abort\f[]() aborts the current transaction and causes a transition to \f[B]TX_STAGE_ONABORT\f[]. If \f[I]errnum\f[] is equal to 0, the transaction error code is set to \f[B]ECANCELED\f[]; otherwise, it is set to \f[I]errnum\f[]. This function must be called during \f[B]TX_STAGE_WORK\f[]. .PP The \f[B]pmemobj_tx_commit\f[]() function commits the current open transaction and causes a transition to \f[B]TX_STAGE_ONCOMMIT\f[]. If called in the context of the outermost transaction, all the changes may be considered as durably written upon successful completion. This function must be called during \f[B]TX_STAGE_WORK\f[]. .PP The \f[B]pmemobj_tx_end\f[]() function performs a cleanup of the current transaction. If called in the context of the outermost transaction, it releases all the locks acquired by \f[B]pmemobj_tx_begin\f[]() for outer and nested transactions. If called in the context of a nested transaction, it returns to the context of the outer transaction in \f[B]TX_STAGE_WORK\f[], without releasing any locks. The \f[B]pmemobj_tx_end\f[]() function can be called during \f[B]TX_STAGE_NONE\f[] if transitioned to this stage using \f[B]pmemobj_tx_process\f[](). If not already in \f[B]TX_STAGE_NONE\f[], it causes the transition to \f[B]TX_STAGE_NONE\f[]. \f[B]pmemobj_tx_end\f[] must always be called for each \f[B]pmemobj_tx_begin\f[](), even if starting the transaction failed. This function must \f[I]not\f[] be called during \f[B]TX_STAGE_WORK\f[]. .PP The \f[B]pmemobj_tx_errno\f[]() function returns the error code of the last transaction. .PP The \f[B]pmemobj_tx_process\f[]() function performs the actions associated with the current stage of the transaction, and makes the transition to the next stage. It must be called in a transaction. The current stage must always be obtained by a call to \f[B]pmemobj_tx_stage\f[](). \f[B]pmemobj_tx_process\f[]() performs the following transitions in the transaction stage flow: .IP \[bu] 2 \f[B]TX_STAGE_WORK\f[] \-> \f[B]TX_STAGE_ONCOMMIT\f[] .IP \[bu] 2 \f[B]TX_STAGE_ONABORT\f[] \-> \f[B]TX_STAGE_FINALLY\f[] .IP \[bu] 2 \f[B]TX_STAGE_ONCOMMIT\f[] \-> \f[B]TX_STAGE_FINALLY\f[] .IP \[bu] 2 \f[B]TX_STAGE_FINALLY\f[] \-> \f[B]TX_STAGE_NONE\f[] .IP \[bu] 2 \f[B]TX_STAGE_NONE\f[] \-> \f[B]TX_STAGE_NONE\f[] .PP \f[B]pmemobj_tx_process\f[]() must not be called after calling \f[B]pmemobj_tx_end\f[]() for the outermost transaction. .PP In addition to the above API, \f[B]libpmemobj\f[](7) offers a more intuitive method of building transactions using the set of macros described below. When using these macros, the complete transaction flow looks like this: .IP .nf \f[C] TX_BEGIN(Pop)\ { \ \ \ \ /*\ the\ actual\ transaction\ code\ goes\ here...\ */ }\ TX_ONCOMMIT\ { \ \ \ \ /* \ \ \ \ \ *\ optional\ \-\ executed\ only\ if\ the\ above\ block \ \ \ \ \ *\ successfully\ completes \ \ \ \ \ */ }\ TX_ONABORT\ { \ \ \ \ /* \ \ \ \ \ *\ optional\ \-\ executed\ only\ if\ starting\ the\ transaction\ fails, \ \ \ \ \ *\ or\ if\ transaction\ is\ aborted\ by\ an\ error\ or\ a\ call\ to \ \ \ \ \ *\ pmemobj_tx_abort() \ \ \ \ \ */ }\ TX_FINALLY\ { \ \ \ \ /* \ \ \ \ \ *\ optional\ \-\ if\ exists,\ it\ is\ executed\ after \ \ \ \ \ *\ TX_ONCOMMIT\ or\ TX_ONABORT\ block \ \ \ \ \ */ }\ TX_END\ /*\ mandatory\ */ \f[] .fi .IP .nf \f[C] TX_BEGIN_PARAM(PMEMobjpool\ *pop,\ ...) TX_BEGIN_CB(PMEMobjpool\ *pop,\ cb,\ arg,\ ...) TX_BEGIN(PMEMobjpool\ *pop) \f[] .fi .PP The \f[B]TX_BEGIN_PARAM\f[](), \f[B]TX_BEGIN_CB\f[]() and \f[B]TX_BEGIN\f[]() macros start a new transaction in the same way as \f[B]pmemobj_tx_begin\f[](), except that instead of the environment buffer provided by a caller, they set up the local \f[I]jmp_buf\f[] buffer and use it to catch the transaction abort. The \f[B]TX_BEGIN\f[]() macro starts a transaction without any options. \f[B]TX_BEGIN_PARAM\f[] may be used when there is a need to acquire locks prior to starting a transaction (such as for a multi\-threaded program) or set up a transaction stage callback. \f[B]TX_BEGIN_CB\f[] is just a wrapper around \f[B]TX_BEGIN_PARAM\f[] that validates the callback signature. (For compatibility there is also a \f[B]TX_BEGIN_LOCK\f[] macro, which is an alias for \f[B]TX_BEGIN_PARAM\f[]). Each of these macros must be followed by a block of code with all the operations that are to be performed atomically. .PP The \f[B]TX_ONABORT\f[] macro starts a block of code that will be executed only if starting the transaction fails due to an error in \f[B]pmemobj_tx_begin\f[](), or if the transaction is aborted. This block is optional, but in practice it should not be omitted. If it is desirable to crash the application when a transaction aborts and there is no \f[B]TX_ONABORT\f[] section, the application can define the \f[B]POBJ_TX_CRASH_ON_NO_ONABORT\f[] macro before inclusion of \f[B]\f[]. This provides a default \f[B]TX_ONABORT\f[] section which just calls \f[B]abort\f[](3). .PP The \f[B]TX_ONCOMMIT\f[] macro starts a block of code that will be executed only if the transaction is successfully committed, which means that the execution of code in the \f[B]TX_BEGIN\f[]() block has not been interrupted by an error or by a call to \f[B]pmemobj_tx_abort\f[](). This block is optional. .PP The \f[B]TX_FINALLY\f[] macro starts a block of code that will be executed regardless of whether the transaction is committed or aborted. This block is optional. .PP The \f[B]TX_END\f[] macro cleans up and closes the transaction started by the \f[B]TX_BEGIN\f[]() / \f[B]TX_BEGIN_PARAM\f[]() / \f[B]TX_BEGIN_CB\f[]() macros. It is mandatory to terminate each transaction with this macro. If the transaction was aborted, \f[I]errno\f[] is set appropriately. .SH RETURN VALUE .PP The \f[B]pmemobj_tx_stage\f[]() function returns the stage of the current transaction stage for a thread. .PP On success, \f[B]pmemobj_tx_begin\f[]() returns 0. Otherwise, an error number is returned. .PP The \f[B]pmemobj_tx_begin\f[]() and \f[B]pmemobj_tx_lock\f[]() functions return zero if \f[I]lockp\f[] is successfully added to the transaction. Otherwise, an error number is returned. .PP The \f[B]pmemobj_tx_abort\f[]() and \f[B]pmemobj_tx_commit\f[]() functions return no value. .PP The \f[B]pmemobj_tx_end\f[]() function returns 0 if the transaction was successful. Otherwise it returns the error code set by \f[B]pmemobj_tx_abort\f[](). Note that \f[B]pmemobj_tx_abort\f[]() can be called internally by the library. .PP The \f[B]pmemobj_tx_errno\f[]() function returns the error code of the last transaction. .PP The \f[B]pmemobj_tx_process\f[]() function returns no value. .SH CAVEATS .PP Transaction flow control is governed by the \f[B]setjmp\f[](3) and \f[B]longjmp\f[](3) macros, and they are used in both the macro and function flavors of the API. The transaction will longjmp on transaction abort. This has one major drawback, which is described in the ISO C standard subsection 7.13.2.1. It says that \f[B]the values of objects of automatic storage duration that are local to the function containing the setjmp invocation that do not have volatile\-qualified type and have been changed between the setjmp invocation and longjmp call are indeterminate.\f[] .PP The following example illustrates the issue described above. .IP .nf \f[C] int\ *bad_example_1\ =\ (int\ *)0xBAADF00D; int\ *bad_example_2\ =\ (int\ *)0xBAADF00D; int\ *bad_example_3\ =\ (int\ *)0xBAADF00D; int\ *\ volatile\ good_example\ =\ (int\ *)0xBAADF00D; TX_BEGIN(pop)\ { \ \ \ \ bad_example_1\ =\ malloc(sizeof(int)); \ \ \ \ bad_example_2\ =\ malloc(sizeof(int)); \ \ \ \ bad_example_3\ =\ malloc(sizeof(int)); \ \ \ \ good_example\ =\ malloc(sizeof(int)); \ \ \ \ /*\ manual\ or\ library\ abort\ called\ here\ */ \ \ \ \ pmemobj_tx_abort(EINVAL); }\ TX_ONCOMMIT\ { \ \ \ \ /* \ \ \ \ \ *\ This\ section\ is\ longjmp\-safe \ \ \ \ \ */ }\ TX_ONABORT\ { \ \ \ \ /* \ \ \ \ \ *\ This\ section\ is\ not\ longjmp\-safe \ \ \ \ \ */ \ \ \ \ free(good_example);\ /*\ OK\ */ \ \ \ \ free(bad_example_1);\ /*\ undefined\ behavior\ */ }\ TX_FINALLY\ { \ \ \ \ /* \ \ \ \ \ *\ This\ section\ is\ not\ longjmp\-safe\ on\ transaction\ abort\ only \ \ \ \ \ */ \ \ \ \ free(bad_example_2);\ /*\ undefined\ behavior\ */ }\ TX_END free(bad_example_3);\ /*\ undefined\ behavior\ */ \f[] .fi .PP Objects which are not volatile\-qualified, are of automatic storage duration and have been changed between the invocations of \f[B]setjmp\f[](3) and \f[B]longjmp\f[](3) (that also means within the work section of the transaction after \f[B]TX_BEGIN\f[]()) should not be used after a transaction abort, or should be used with utmost care. This also includes code after the \f[B]TX_END\f[] macro. .PP \f[B]libpmemobj\f[](7) is not cancellation\-safe. The pool will never be corrupted because of a canceled thread, but other threads may stall waiting on locks taken by that thread. If the application wants to use \f[B]pthread_cancel\f[](3), it must disable cancellation before calling any \f[B]libpmemobj\f[](7) APIs (see \f[B]pthread_setcancelstate\f[](3) with \f[B]PTHREAD_CANCEL_DISABLE\f[]), and re\-enable it afterwards. Deferring cancellation (\f[B]pthread_setcanceltype\f[](3) with \f[B]PTHREAD_CANCEL_DEFERRED\f[]) is not safe enough, because \f[B]libpmemobj\f[](7) internally may call functions that are specified as cancellation points in POSIX. .PP \f[B]libpmemobj\f[](7) relies on the library destructor being called from the main thread. For this reason, all functions that might trigger destruction (e.g. \f[B]dlclose\f[](3)) should be called in the main thread. Otherwise some of the resources associated with that thread might not be cleaned up properly. .SH SEE ALSO .PP \f[B]dlclose\f[](3), \f[B]longjmp\f[](3), \f[B]pmemobj_tx_add_range\f[](3), \f[B]pmemobj_tx_alloc\f[](3), \f[B]pthread_setcancelstate\f[](3), \f[B]pthread_setcanceltype\f[](3), \f[B]setjmp\f[](3), \f[B]libpmemobj\f[](7) and \f[B]\f[]