summaryrefslogtreecommitdiff
path: root/contrib/getdents-debug/getdents_c/getdents.c
diff options
context:
space:
mode:
authorJakob Unterwurzacher2020-06-21 11:59:08 +0200
committerJakob Unterwurzacher2020-06-21 12:01:34 +0200
commit77632b7554000f2e832a0e9fded1a3894b2502c3 (patch)
treeb9a45e1ca80431c884f3e87e4d6bbfef5a9c2482 /contrib/getdents-debug/getdents_c/getdents.c
parent6aa9f5636f03392b5da5fc19dc4ea908e2e55e26 (diff)
getdents_c: read from two threads
This was an attempt to make the C code more similar to Go (which also reads from multiple threads). However, I still could not repro the ENOENT problems. https://github.com/rfjakob/gocryptfs/issues/483
Diffstat (limited to 'contrib/getdents-debug/getdents_c/getdents.c')
-rw-r--r--contrib/getdents-debug/getdents_c/getdents.c65
1 files changed, 49 insertions, 16 deletions
diff --git a/contrib/getdents-debug/getdents_c/getdents.c b/contrib/getdents-debug/getdents_c/getdents.c
index 936bd0b..d504976 100644
--- a/contrib/getdents-debug/getdents_c/getdents.c
+++ b/contrib/getdents-debug/getdents_c/getdents.c
@@ -1,6 +1,7 @@
// See ../getdents/getdents.go for some info on why
// this exists.
+#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
@@ -9,6 +10,38 @@
#include <sys/stat.h>
#include <sys/syscall.h>
#include <errno.h>
+#include <pthread.h>
+
+static pthread_mutex_t mutex;
+static int sum;
+
+struct up {
+ pthread_mutex_t *my_mutex;
+ pthread_mutex_t *other_mutex;
+ int fd;
+};
+
+void *reader(void *arg) {
+ struct up* up = (struct up*)arg;
+ int fd = up->fd;
+ char tmp[10000];
+ while(1) {
+ pthread_mutex_lock(up->my_mutex);
+ int n = syscall(SYS_getdents64, fd, tmp, sizeof(tmp));
+ if (n > 0) {
+ printf("t%ld: n=%d\n", gettid(), n);
+ } else {
+ printf("t%ld: n=0 errno=%d total %d bytes\n", gettid(), errno, sum);
+ if (n < 0) {
+ exit(1);
+ }
+ pthread_mutex_unlock(up->other_mutex);
+ break;
+ }
+ sum += n;
+ pthread_mutex_unlock(up->other_mutex);
+ }
+}
int main(int argc, char *argv[])
{
@@ -21,28 +54,28 @@ int main(int argc, char *argv[])
const char *path = argv[1];
for (int i = 1 ; ; i ++ ) {
+ sum = 0;
int fd = open(path, O_RDONLY);
if (fd == -1) {
perror("open");
exit(1);
}
-
- char tmp[10000];
- int sum = 0;
- printf("%3d: getdents64: ", i);
- for ( ; ; ) {
- int n = syscall(SYS_getdents64, fd, tmp, sizeof(tmp));
- printf("n=%d; ", n);
- if (n <= 0) {
- printf("errno=%d total %d bytes\n", errno, sum);
- if (n < 0) {
- exit(1);
- }
- break;
- }
- sum += n;
- }
+ pthread_t reader1_thread, reader2_thread;
+ pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER, m2 = PTHREAD_MUTEX_INITIALIZER;
+ pthread_mutex_lock(&m2);
+ struct up up1, up2;
+ up1.fd = fd;
+ up1.my_mutex = &m1;
+ up1.other_mutex = &m2;
+ up2.fd = fd;
+ up2.my_mutex = &m2;
+ up2.other_mutex = &m1;
+ pthread_create(&reader1_thread, NULL, reader, &up1);
+ pthread_create(&reader2_thread, NULL, reader, &up2);
+ pthread_join(reader1_thread, NULL);
+ pthread_join(reader2_thread, NULL);
close(fd);
+ printf("---\n");
usleep(100000);
}
}