forked from CU-NVM/NUMATyping
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex1_persist.cpp
More file actions
53 lines (42 loc) · 1.13 KB
/
ex1_persist.cpp
File metadata and controls
53 lines (42 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
#include<libpmemobj++/make_persistent.hpp>
#include<libpmemobj++/persistent_ptr.hpp>
#include<libpmemobj++/pool.hpp>
#include <libpmemobj++/p.hpp>
#include<libpmemobj++/transaction.hpp>
#include <filesystem>
#include <sys/wait.h>
#include <unistd.h>
using namespace pmem::obj;
#define POOL_PATH "/mnt/pmem-emu/ex1.pool"
#define POOL_SIZE (1024 *1024* 8) // 8MB minimum
struct root {
persistent_ptr<int> counter;
};
int main() {
pool<root> pop;
if(std::filesystem::exists(POOL_PATH)) {
pop= pool<root>::open(POOL_PATH, "ex1");
}
else {
pop = pool<root>::create(POOL_PATH, "ex1", POOL_SIZE, 0666);
}
auto r = pop.root();
transaction::run(pop, [&] {
if(r->counter == nullptr)
r->counter = make_persistent<int>(0);
r->counter = r->counter + 1;
});
pid_t pid=fork();
if(pid==0) {
transaction::run(pop, [&] {
r->counter = r->counter + 999;
abort();
});
} else {
wait(nullptr);
}
std::cout << "Counter value: " << r->counter << std::endl;
pop.close();
return 0;
}