forked from ShawnZhong/MadFS
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_sync.cpp
57 lines (43 loc) · 1.21 KB
/
test_sync.cpp
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
54
55
56
57
#include <fcntl.h>
#include <iostream>
#include <thread>
#include <vector>
#include "common.h"
constexpr auto NUM_BYTES = 32;
constexpr auto BYTES_PER_THREAD = 2;
const char* filepath = get_filepath();
int main() {
[[maybe_unused]] ssize_t ret;
unlink(filepath);
int fd = open(filepath, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
char empty_buf[NUM_BYTES]{};
ret = pwrite(fd, empty_buf, NUM_BYTES, 0);
ASSERT(ret == NUM_BYTES);
std::vector<std::thread> threads;
for (int i = 0; i < NUM_BYTES; ++i) {
threads.emplace_back(std::thread([&, i]() {
char buf[BYTES_PER_THREAD]{};
fill_buff(buf, BYTES_PER_THREAD, i);
ssize_t rc = pwrite(fd, buf, BYTES_PER_THREAD, i);
ASSERT(rc == BYTES_PER_THREAD);
rc = fsync(fd);
ASSERT(rc == 0);
}));
// uncomment the line below to run sequentially
// threads.back().join();
}
for (auto& thread : threads) {
if (thread.joinable()) thread.join();
}
// check result
{
char actual[NUM_BYTES]{};
ret = pread(fd, actual, NUM_BYTES, 0);
ASSERT(ret == NUM_BYTES);
char expected[NUM_BYTES];
fill_buff(expected, NUM_BYTES);
CHECK_RESULT(expected, actual, NUM_BYTES, fd);
}
fsync(fd);
close(fd);
}