102
94 int main()
95 {
96
if (!(pool = pmemobj_create(POOL, " ",
97
PMEMOBJ_MIN_POOL, 0600)))
98
die("Can't create pool "%s": %m\n", POOL);
99
100
TOID(struct account) account_a, account_b;
101
TOID_ASSIGN(account_a,
102
new_account("Julius Caesar", 100));
103
TOID_ASSIGN(account_b,
104
new_account("Mark Anthony", 50));
105
106
int price = 42;
107
TX_BEGIN(pool) {
108
TX_ADD_DIRECT(&D_RW(account_a)->balance);
109
TX_ADD_DIRECT(&D_RW(account_b)->balance);
110
D_RW(account_a)->balance -= price;
111
D_RW(account_b)->balance += price;
112
} TX_END
113
114
pmemobj_close(pool);
115
return 0;
116 }
• Line 107: We start the transaction.
• Lines 108-111: Make balance modifications to multiple accounts.
• Line 112: Finish the transaction. All updates will either complete
entirely or they will be rolled back if the application or system crashes
before the transaction completes.
Each transaction has multiple stages in which an application can interact. These
transaction stages include
• TX_STAGE_NONE: No open transaction in this thread.
• TX_STAGE_WORK: Transaction in progress.
• TX_STAGE_ONCOMMIT: Successfully committed.
Chapter 7 libpmemobj: a Native traNsaCtioNal objeCt store
94 int main()
95 {
96
if (!(pool = pmemobj_create(POOL, " ",
97
PMEMOBJ_MIN_POOL, 0600)))
98
die("Can't create pool "%s": %m\n", POOL);
99
100
TOID(struct account) account_a, account_b;
101
TOID_ASSIGN(account_a,
102
new_account("Julius Caesar", 100));
103
TOID_ASSIGN(account_b,
104
new_account("Mark Anthony", 50));
105
106
int price = 42;
107
TX_BEGIN(pool) {
108
TX_ADD_DIRECT(&D_RW(account_a)->balance);
109
TX_ADD_DIRECT(&D_RW(account_b)->balance);
110
D_RW(account_a)->balance -= price;
111
D_RW(account_b)->balance += price;
112
} TX_END
113
114
pmemobj_close(pool);
115
return 0;
116 }
• Line 107: We start the transaction.
• Lines 108-111: Make balance modifications to multiple accounts.
• Line 112: Finish the transaction. All updates will either complete
entirely or they will be rolled back if the application or system crashes
before the transaction completes.
Each transaction has multiple stages in which an application can interact. These
transaction stages include
• TX_STAGE_NONE: No open transaction in this thread.
• TX_STAGE_WORK: Transaction in progress.
• TX_STAGE_ONCOMMIT: Successfully committed.
Chapter 7 libpmemobj: a Native traNsaCtioNal objeCt store
