1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2021 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // many_read.cfa -- Make sure that multiple concurrent reads don't mess up. |
---|
8 | // |
---|
9 | // Author : Thierry Delisle |
---|
10 | // Created On : Thu Feb 18 15:26:05 2021 |
---|
11 | // Last Modified By : |
---|
12 | // Last Modified On : |
---|
13 | // Update Count : |
---|
14 | // |
---|
15 | |
---|
16 | extern "C" { |
---|
17 | #include <sys/types.h> |
---|
18 | #include <sys/stat.h> |
---|
19 | #include <fcntl.h> |
---|
20 | #include <unistd.h> |
---|
21 | } |
---|
22 | |
---|
23 | #include <string.h> |
---|
24 | |
---|
25 | #include <fstream.hfa> |
---|
26 | #include <iofwd.hfa> |
---|
27 | |
---|
28 | #include <thread.hfa> |
---|
29 | #include <kernel.hfa> |
---|
30 | |
---|
31 | #define xstr(s) str(s) |
---|
32 | #define str(s) #s |
---|
33 | |
---|
34 | const char * file = xstr(IN_DIR) "many_read.data"; |
---|
35 | int fd; |
---|
36 | off_t size; |
---|
37 | |
---|
38 | const char * content; |
---|
39 | |
---|
40 | thread reader {}; |
---|
41 | void ?{}(reader & this) {} |
---|
42 | void ^?{}(reader & mutex this) {} |
---|
43 | void main(reader & this); |
---|
44 | |
---|
45 | int main() { |
---|
46 | fd = open(file, 0); |
---|
47 | if(fd < 0) { |
---|
48 | serr | "Failed to open file: " | file; |
---|
49 | exit(1); |
---|
50 | } |
---|
51 | |
---|
52 | struct stat sb; |
---|
53 | int ret = fstat(fd, &sb); |
---|
54 | if(ret < 0) { |
---|
55 | serr | "Failed to stat file: " | file; |
---|
56 | exit(1); |
---|
57 | } |
---|
58 | |
---|
59 | size = sb.st_size; |
---|
60 | sout | "File has size: " | size | "bytes"; |
---|
61 | |
---|
62 | char data[size+1]; |
---|
63 | { |
---|
64 | size_t left = size; |
---|
65 | char * it = data; |
---|
66 | |
---|
67 | while(left) { |
---|
68 | ret = read(fd, it, left); |
---|
69 | if(ret < 0) { |
---|
70 | serr | "Failed to read file: " | file | " (the first time)"; |
---|
71 | exit(1); |
---|
72 | } |
---|
73 | it += ret; |
---|
74 | left -= ret; |
---|
75 | } |
---|
76 | } |
---|
77 | data[size] = 0; |
---|
78 | content = data; |
---|
79 | sout | content; |
---|
80 | |
---|
81 | { |
---|
82 | processor proc[2]; |
---|
83 | { |
---|
84 | reader readers[17]; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | sout | "Yup, it all matches."; |
---|
89 | } |
---|
90 | |
---|
91 | #ifdef CFA_HAVE_PREADV2 |
---|
92 | extern "C" { |
---|
93 | #include <sys/uio.h> |
---|
94 | } |
---|
95 | #endif |
---|
96 | |
---|
97 | void main(reader & this) { |
---|
98 | for(50) { |
---|
99 | char data[size]; |
---|
100 | char * it = data; |
---|
101 | size_t left = size; |
---|
102 | off_t offset = 0; |
---|
103 | |
---|
104 | #ifdef CFA_HAVE_PREADV2 |
---|
105 | while(left) { |
---|
106 | struct iovec vec; |
---|
107 | vec.iov_base = (void*)it; |
---|
108 | vec.iov_len = left; |
---|
109 | int ret = cfa_preadv2(fd, &vec, 1, offset, 0, 0); |
---|
110 | if(ret < 0) { |
---|
111 | serr | "Failed to read file: " | file | " (NOT the first time)"; |
---|
112 | exit(1); |
---|
113 | } |
---|
114 | it += ret; |
---|
115 | left -= ret; |
---|
116 | offset += ret; |
---|
117 | } |
---|
118 | |
---|
119 | if(strncmp(content, data, size) != 0) { |
---|
120 | serr | "Subsequent read of file '" | file | "' return different content"; |
---|
121 | exit(1); |
---|
122 | } |
---|
123 | #endif |
---|
124 | } |
---|
125 | } |
---|